/* Dickies Template JS */

var Template = {
		
	start: function() {

		/* Add os to body class */
		$(document.body).addClass($.os.name);

		/* Add row end for product list */
		$("ul.ProductList li:nth-child(4n)").addClass("RowEnd");

		/* Add anchor links on subcategory product listing */
		$("div.SubCategoryProductListing #CategoryHeading div.SubCategoryList li a").map(function(){
			$(this).attr("href", "#section-" + $(this).html().toLowerCase());
	    });
		$("div.SubCategoryProductListing h2").map(function(){
			$(this).attr("id", "section-" + $(this).html().toLowerCase());
	    });

		/* Add to search query */
		$('#search_query').toggleVal({
	 	    populateFrom: 'label',
	 	    removeLabels: true
	     });

		/* Cart content auto-update shipping */
		$("#CartContent div.EstimateShipping select[name='shippingZoneCountry']").change(function() {
			Cart.EstimateShipping();
		});
	
		/* Init product images */
		if ($('#ProductDetails').length != 0) {
			Template.initProductImages();

			/* Add row end for product images */
			$("#ProductDetails div.ProductImages div.Variation").map(function(){
				var i = 1;
				$(this).find("img[class!=VariationThumb]").map(function(){
					if (i++ % 6 == 0) $(this).addClass("RowEnd");
				});
		    });

			/* update (R) symbol */
			var title = $("#ProductDetails h2:first").html().replace(/®/i, '<span class="copy">®</span>');
			$("#ProductDetails h2:first").html(title);

			/* load Cufon text replacement to display price in Komika font */
			Cufon.replace($("#ProductDetails h3.ProductPrice:first"));
		}

	},

	initProductImages: function() {

		/* Trigger update function to init base product */
		updateSelectedVariation();

		/* Set default variation */
		$('body').attr('currentVariationImages', 'VariationDefault');
		Template.baseThumb = $('#ProductDetails .ProductThumb img').attr('src');

		/* Add select color event */
		$("#ProductDetails div.ProductMain select[name='variation[1]']").change(function() {
			Template.showProductImages($(this).val());
		});

		/* Add product images events */
		$('#ProductDetails div.ProductImages img').click(function() {
			var src = $(this).attr('src').replace(/^(.+)_small.(\S+)/, "$1_large.$2");
			$('#ProductDetails div.ProductThumb img:first').attr('src', src);
		});

	},

	showProductImages: function(value) {
		var variation = '#ProductDetails div.ProductImages div.Variation' + value;

		// hide all color variations
		$('#ProductDetails div.ProductImages div').hide();

		// show select color variation
		if (value != '' && $(variation).length != 0) {
			$(variation).show();
			baseProduct.thumb = $(variation + ' img.VariationThumb:first').attr('src');
			$('body').attr('currentVariationImages', 'Variation' + value);
		} else {
			$('#ProductDetails div.ProductImages div.VariationDefault').show();
			baseProduct.thumb = Template.baseThumb;
			$('body').attr('currentVariationImages', 'VariationDefault');
		}
	}

}

$(document).ready(Template.start);
