$(document).ready(function(){
	
	$("#hero").append("<span id=\"closefeature\" onclick=\"closeFeature();\">Close feature</span>");
	$(".feature").each(function() {
	
		$("#hero").append("<img id=\"" + $(this).attr("id") + "-show\" style=\"position: absolute; top: 0; left: 0; z-index: -1;\" src=\"/content/products/features/" + $(this).attr("id") + ".jpg\" width=\"620\" height=\"620\" />");
		
	});

	$(".feature").click(function() {
		
		featureID = "#" + $(this).attr("id") + "-show";
		
		if ($(featureID).hasClass("on")) {
			closeFeature();
		}
		else {		
			closeFeature();
			$("#closefeature").show();
			$(featureID).css("z-index", "10");
			$(featureID).addClass("on");
		}
	
	});
	
	// Buy online
	$("#buyonline > a").click(function() {
		$(this).parent().toggleClass("activated");
		
		if ($(this).parent().hasClass("activated")) $(this).parent().siblings().addClass("deactivated");
		else $(".shopping li").removeClass("deactivated");
		
		return false;
	});
	
	// Buy online go away
	$(document).click(function(event) {
		
		target = (event.target) ? event.target : event.srcElement;
	
		if ($(target).parents("#buyonline").length == 0 && $(target).attr("id") != "buyonline") {
			$("#buyonline").removeClass("activated");
			$(".shopping li").removeClass("deactivated");
		}
		
	});
	
	$("#product_size").change(function() {
		
		$("#product_submit").removeAttr("disabled").attr("value", "Go");
		$("#onlineresults").slideUp("fast", function() {
			$("#onlineresults").empty();
			$("#product_submit").removeClass("engaged")
		});
		
	});
	
	// Online Shop pull
	$("#product_submit").click(function() {
		
		if ($(this).attr("disabled")) return false;
	
		// Check for valid query
		if ($("#product_size").val() == "no") {
			alert("Select a size!");
			return false;
		}
		
		$("#product_size").attr("disabled", "disabled");
		$(this).attr("disabled", "disabled").attr("value", "One Sec");
		$("#shopresults").slideUp("fast");
		findOnlineShops();
		
		return false;
		
	});

});

// Close features
function closeFeature() {
	
	$("#hero .on").removeClass("on").css("z-index", "-1");
	$("#hero #closefeature").hide();
	
}

// Find online shops
function findOnlineShops() {
	
	// Do AJAX request
	shopsURL = "/api/inventorysearch/?format=json&sku=" + $("#product_sku").val() + "&code=" + $("#product_code").val() + "&size=" + $("#product_size").val();
	//document.write(shopsURL);
	$.getJSON(shopsURL, function(results){
	
		if (results.shops && results.shops.length) {
			
			$("#onlineresults").append("<ul />");
			for (var i = 0; i < results.shops.length; i++) {
								
				thisShop = results.shops[i];
				
				$("#onlineresults ul").append("<li><a href=\"" + thisShop.url + "\" onclick=\"pageTracker._trackPageview('/outbound/" + thisShop.token + "/" + $("#product_token").val() + "-" + $("#product_sku").val() + "/" + $("#colorway_token").val() + "-" + $("#product_code").val() + "/'); pageTracker._trackPageview('/productlinks/" + $("#product_token").val() + "-" + $("#product_sku").val() + "/" + $("#colorway_token").val() + "-" + $("#product_code").val() + "/');\"><strong>" + thisShop.name + "</strong><img src=\"" + thisShop.image + "\" width=\"90\" height=\"68\" alt=\"" + thisShop.name + "\" /><br/><span class=\"shopinfo\">Buy now <span class=\"shopname\">from " + thisShop.name + "</span></span></a></li>");
			}
			
		}
		else $("#onlineresults").append("<p class=\"statusmessage\">Sorry, no shops were found with that size in stock!</p>");
		
		// Show results
		$("#onlineresults").slideDown("slow");
		
		$("#product_size").removeAttr("disabled");
		$("#product_submit").addClass("engaged").attr("value", "Done");
		
	});
	
}