/**

Classic Charms Cart Alert
Last Modified: January 16, 2012

**/

/* messages defined at bottom */

if (jQuery) (function($) {
	
	function itemNum() { return $("#product-info-table tr.code td").text(); }
	
	function match(item, criteria) {
	
		if (item==criteria) return true;
		
		if (criteria.lastIndexOf("*")==(criteria.length-1)) {
			if (item.indexOf(criteria.substring(0, criteria.length-1))==0) {
				return true;
			}
		}
		
		return false;
	
	}
	
	function click() {
	
		var id = window.CCM.id;
		if (id==null) {
			window.CCM.orderButton.click();
			return;
		}
		
		if ($("#cc_cart_alert").length==0) {
		
			var msg = window.CCM.messages[id];
			
			var $outerDiv = $("<div>")
				.css("display", "none")
				.appendTo("body");
			
			var $content = $("<div>")
				.attr("id", "cc_cart_alert")
				.appendTo($outerDiv);
			
			var $h3 = $("<h3>")
				.html(msg[0])
				.appendTo($content);
			
			var $p = $("<p>")
				.html(msg[1])
				.appendTo($content);
			
			var $button = $("<img>")
				.attr("src", window.CCM.orderButton.attr("src"))
				.attr("id", "cc_cart_alert_checkout")
				.attr("title", "Add to Cart")
				.click(function() { window.CCM.checkout(); })
				.appendTo($content);
			
			var $cancel = $("<a>")
				.attr("id", "cc_cart_alert_cancel")
				.attr("href", "javascript:void(0);")
				.click(function() { window.CCM.cancel(); })
				.text("No Thanks")
				.appendTo($content);
			
		}
		
		$.fn.colorbox({ inline:true, href:"#cc_cart_alert" });
	
	}
	
	function checkout() {
	
		$.fn.colorbox.close();
		window.CCM.orderButton.click();
	
	}
	
	function cancel() { $.fn.colorbox.close(); }
	
	function init2() {
		
		if (window.CCM.inited==true) return;
		window.CCM.inited = true;
		
		if (window.CCM.id==null) {
		
			var item = itemNum();
			
			outer: for (var i in window.CCM.messages) {
				if (window.CCM.messages[i].length<3) continue;
				var m = window.CCM.messages[i][2];
				for (var j in m) {
					if (match(item, m[j])) {
						window.CCM.id = i;
						break outer;
					}
				}
			}
		
		}
		
		window.CCM.orderButton = $("input.order-button");
		if (window.CCM.orderButton.length<1) return;
		
		var $img = $("<img>")
			.attr("src", window.CCM.orderButton.attr("src"))
			.attr("title", "Add to Cart")
			.css("cursor", "pointer")
			.insertAfter(window.CCM.orderButton)
			.click(function() { click(); });
		
		window.CCM.orderButton.hide();
	
	}
	
	function init(id) {
	
		if (window.CCM.messages[id]) window.CCM.id = id;
		else window.CCM.id = null;
		
		if (window.CCM.inited==true) {
			window.CCM.inited = false;
			init2();
		}
	
	}
	
	window.CCM = {
		messages:{
			//"xmas11backorder" : [ "Important Shipping Information", "Due to short supply and heavy demand, this item will not ship by Christmas.<br/><br/>By clicking Add to Cart, you agree that you understand that you will not receive this item by Christmas. Your order will be processed as soon as this item becomes available." ],
			//"xmas11custom" : [ "Important Shipping Information", "This item is custom-made and takes 2-3 weeks to produce. Therefore, it will not be available for delivery before Christmas.<br/><br/>By clicking Add to Cart, you agree that you understand that this item will be processed and shipped to you as soon as it is available." ]
			"xmas11custom" : [ //not really Christmas, but reusing code because it's already there
				"Important Shipping Information",
				"Please note: This item is custom-made and takes 2-3 weeks to produce. It is non-returnable and non-cancelable once ordered.<br/><br/>By clicking Add to Cart, you agree that you understand that this item will be processed and shipped to you as soon as it is available."
			],
			"outofstock" : [
				"Out of Stock",
				"This item is temporarily out of stock and on backorder with our vendor.<br/><br/>By clicking Add to Cart, you agree that you understand that you will not receive this item until it becomes available again. We will ship this item to you as soon as possible.",
				["LC305","LC171","LC294","BZ-1745","BZ-1778","BZ-1710","BZ-1705","BZ-1716", "QCC449","QCC702","QCC699","QCC283","QCC301","QCC693","QCC169","QCC437","A4268","QRS1271","QRS1946","QRS2410","QRS1788","QRS1233","QRS1267","QRS1276","QRS1385","QRS1400N3","QRS1608","QRS1627GP","QRS1648","QRS1651","QRS1776","QRS1820GP","QRS1825","QRS1902","QRS1961","QRS1962","QRS1973","QRS2193","QRS2376","QRS2379","QRS2395","QRS2409","QRS2432","QRS2437","QRS274","QRS343GP","QRS576","QRS712H","QRS713","QRS715","QRS717","QRS805","QRS984-9","QTA193","QRS1601","QRS1378","QRS1400S","QRS285","QG-M527","QRS518GP","QRS987","QCC664","QRS1627GP","QRS1611","QRS1400L","QRS1948","QRS2105","QRS1781","AHH003","MS259","MS113","MS366","MAU015","MUS031","MS175","SCHA1105","SCHA1569","SCHA1562","SCHA0844","WHD101", "AHS001", "AHS004", "WHD10210", "AHS006", "WHB127", "WHF016", "WHI044", "SCHA1268", "SCHA1553", "SCHA1491"]
			]
		},
		inited:false,
		id:null,
		checkout:checkout,
		cancel:cancel,
		init:init
	};
	
	$(function() { init2(); });
	
	//debug
	//window.CCM.init("xmas11backorder");

})(jQuery);
