jQuery(document).ready (
   function(){
   	j('#center-3 ul.productList li.loading, #center-4 ul.productList li.loading').remove();
   	/* Carousel Widgets */
	
	/*j('#center-3 ul.productList').wrap("<div class='jCarouselLite3 jCarouselLite'></div>");
	j(".jCarouselLite3").wrap("<div class='carousel3'></div>");
	
	j('#center-4 ul.productList').wrap("<div class='jCarouselLite4 jCarouselLite'></div>");
	j(".jCarouselLite4").wrap("<div class='carousel4'></div>");
	
	//j('#center-5 ul.productList').wrap("<div class='jCarouselLite5 jCarouselLite'></div>");
	//j(".jCarouselLite5").wrap("<div class='carousel5'></div>");
	
	j('<a href="#" class="next">&nbsp</a>').appendTo('.carousel3, .carousel4');
	j('<a href="#" class="prev">&nbsp</a>').prependTo('.carousel3, .carousel4');
	
	
	
	j(function() {
	    j(".jCarouselLite3").jCarouselLite({
	        btnNext: ".carousel3 .next",
	        btnPrev: ".carousel3 .prev",
			scroll: 4,
			 visible: 4,
			 circular: true,
			 start: 0
	    });
		j(".jCarouselLite4").jCarouselLite({
	        btnNext: ".carousel4 .next",
	        btnPrev: ".carousel4 .prev",
			scroll: 4,
			 visible: 4,
			  circular: true,
			 start: 0
	    });
		//j(".jCarouselLite5").jCarouselLite({
	    //    btnNext: ".carousel5 .next",
	    //    btnPrev: ".carousel5 .prev",
		//	 visible: 4
	   // });
	});
	*/
	
	
   	today = new Date()
   	thisDay = today.getDay()
   	elementDay = thisDay + 1;
	
	j = jQuery.noConflict();
	
	jQuery("#top-2 a").attr("href", "/info/dotd");
	j("#top-2 .dotd_addtocart a").attr("href", "#");
	
//j('#top-2 .miniDetailTargetImage').appendTo(j('#top-2 .productDetails'));
	//if (j('#top-2 dd.price').length = 0)
		jQuery("#top-2 .productDetails dl.priceBlock").append(j("<dd class='price'></dd>"));

	j('#center-3 .productDetails, #center-4 .productDetails, #top-2 .productDetails, #center-5 .productDetails,').each(function(){
		var link = j(this).children('div.hpTitlePrice').children('div.').children("h4").children("a").attr("href");
		var dlsection = j(this).children(".attributeList");
	
		//j(dlsection).append(j("<a href='" + link + "'><button class='primaryAction'> <span>BUY NOW</span> </button></a>"));
		j(dlsection).append(j("<a href='" + link + "'><img src='/media/common/addCart.gif'  /> </a>"));
		//j(dlsection).append(j("<a href='" + link + "'><img src='/media/common/btnbuynow.png'  /> </a>"));
	});
	var href = j("#top-2 .productDetails h4 a").attr("href");
	j("#top-2 .productDetails .priceBlock").children("a").children("img").attr("src", "/media/common/dealAddCart.gif").parent().attr("href", "#");

	
	var sku = jQuery("#dotd_products #product_" + elementDay).text();
	
	/* Add to Cart Button */
	jQuery('#top-2 .dotd_addtocart a').click(function() {
		if (jQuery(this).children("img").attr("src") == '/media/common/dealAddCart.gif') {
			var c_name = 'session-id';
			var cartID = '';
			var merchantID = 'A3JV5TS6Y3V4XH';
			var xml_str = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?><c:cartItems xmlns:c='http://webstore.amazon.com/API'><c:cartItem><c:product><c:identifiers><c:merchantSKU><c:merchantID>" + merchantID + "</c:merchantID><c:sku>" + sku + "</c:sku></c:merchantSKU></c:identifiers></c:product><c:quantity>1</c:quantity></c:cartItem></c:cartItems>";
			
			if (document.cookie.length >= 0) {
				c_name = 'session-id';
				c_start = document.cookie.indexOf("session-id=");
				
				if (c_start != -1) 
				
					c_start = c_start + c_name.length + 1;
				c_end = document.cookie.indexOf(";", c_start);
				if (c_end == -1) 
					c_end = document.cookie.length;
				var cartID = unescape(document.cookie.substring(c_start, c_end));
				var domain = "www.secondipity.com";
				var path = "http://" + domain + "/api/cart/" + cartID + "/items";
				
				function processRequest(){
					if (xmlhttpreq.readyState == 4) {
						if (xmlhttpreq.status == 200) {
							var message = "invalid";
							//alert(xmlhttpreq.responseText);           
							window.location = "/cart";
						}
						else {
						//alert(xmlhttpreq.statusText);
						}
					}
				}
				
				xmlhttpreq = new XMLHttpRequest();
				xmlhttpreq.onreadystatechange = processRequest;
				xmlhttpreq.open("POST", path, true);
				xmlhttpreq.setRequestHeader("Content-Type", "text/xml");
				xmlhttpreq.send(xml_str);
				
			}
		}
		 //window.location = "/cart";
	});
				
	//AJAX JQuery Example
	function addProduct(product){
		jQuery.each(product.offers.offer, function(idz, price){
			if (price.buyingPrice) {
				//alert(price.regularPrice.value);
				//alert(price.buyingPrice.value);
				if (price.regularPrice && price.buyingPrice.value != price.regularPrice.value) {
					var regPrice = formatNumber(price.regularPrice.value);
					var buyPrice = formatNumber(price.buyingPrice.value);
					jQuery("#top-2 dd.price").html("<s style='margin-right:5px; color:#6A6A69;'>$" + regPrice + "</s> $" + buyPrice);
					jQuery("#top-2 dd.price").css("color", "#AC2E34");
				}
				else {
					if (price.buyingPrice) {
					var buyPrice = formatNumber(price.buyingPrice.value);
					jQuery("#top-2 dd.price").text("$" + buyPrice);
					jQuery("#top-2 dd.price").css("color", "#AC2E34");
					}
				}
			}
			if (price.isBuyable) {
				if (price.isBuyable != 'true') {
					//jQuery("#top-2 .priceBlock").html('');
					jQuery(".dotd_addtocart img").attr("src", "/media/common/dealAddCartOOS.jpg").parent().attr("href", "/info/dotd").parent().removeClass('dotd_addtocart');
					jQuery("#top-2 .dotdEnd").hide();
				}
					
			}
			else {
				//jQuery("#top-2 .priceBlock").html('');
				jQuery(".dotd_addtocart img").attr("src", "/media/common/dealAddCartOOS.jpg").parent().attr("href", "/info/dotd").parent();
				jQuery("#top-2 .dotdEnd").hide();
			}
		});
		jQuery.each(product.attributes, function(idy, attribute){
			if (attribute.catalogItem) {
				/*if(attribute.catalogItem.bullet_point.value)
					jQuery("#top-2 dd.price s").text("$" + attribute.catalogItem.bullet_point.value);*/
				
				//alert(attribute.catalogItem.item_name.value);
				if (attribute.catalogItem.item_name.value) {
					jQuery("#top-2 .productDetails h4 a").text(attribute.catalogItem.item_name.value);
					jQuery("#top-2 .productDetails h4 a").attr("title", attribute.catalogItem.item_name.value);
				}
				if (attribute.catalogItem.target_audience_keyword) {
					jQuery("#top-2 .dotdHpCondition").text("Condition: " + attribute.catalogItem.target_audience_keyword.value);
				}
					
				//jQuery("#top-2 dd.price").prepend("<s style='margin-right:5px; color:#6A6A69;'>$" + price.regularPrice.value + "</s>");
			}
			/*if (attribute.primaryOfferContribution) {
				if (attribute.primaryOfferContribution.list_price) {
					var msrp = formatNumber(attribute.primaryOfferContribution.list_price.value);
					jQuery("#top-2 dd.price").prepend("<s style='margin-right:5px; color:#6A6A69;'>$" + msrp + "</s>");
				}
			}*/
		});
		
		getImage(product);
	}
	function getImage(product){
		jQuery.each(product.assets.link, function(idx, link){
			/*if (link.rel == "MAIN") {
				image = jQuery(document.createElement("img"));
				image.attr("src", link.href);
				image.attr("alt", "deal of the day");
				image.css({
					"maxHeight": "370px",
					"maxWidth":"370px"
				});
				console.log("Built image", image);
				return false;
			}*/
			if (link.rel == "MAIN") 
				jQuery("#top-2 .productImage").attr("src", link.href);
		});
		
	}
	function getProduct(domain, merchID, sku, callback){
		var path = "http://" + domain + "/api/product/msku/" + merchID + "/" + sku;
		jQuery.getJSON(path, callback);
	}
	function loadProducts(){
		//var skus = ["<SKU1>", "<SKU2>", "<SKU3>"];
		var skus = [sku];
		var merchID = "A3JV5TS6Y3V4XH";
		var domain = "www.secondipity.com";
		for (i in skus) {
			//console.log("adding " + skus[i]);
			getProduct(domain, merchID, skus[i], addProduct);
		}
	}
	loadProducts();
	
	jQuery("body#pageId0 #top-2 ul.productList li.product").css("display", "block");
	
	jQuery("body#pageId0 #top-2 dl.priceBlock").children("a").remove();
}
			
);
