$(window).load(function() {$('#slider').nivoSlider();});


$(document).ready(function() {

	initMenu();

	$('ul.brand-size-guide a').click(function() {
		$(this).css('outline','none');
		$('ul.brand-size-guide .current').removeClass('current');
		$(this).parent().addClass('current');
		
		var filterVal = $(this).text().toLowerCase().replace(' ','-');
				
		if(filterVal == 'all') {
			$('ul.size-guide li.hidden').fadeIn('slow').removeClass('hidden');
		} else {
			
			$('ul.size-guide li').each(function() {
				if(!$(this).hasClass(filterVal)) {$(this).fadeOut('normal').addClass('hidden');} else {$(this).fadeIn('slow').removeClass('hidden');}
			});
		}
		
		return false;
	});

	//Hide (Collapse) the toggle containers on load
	$(".size-guide-toggle").hide(); 

	//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
	$("h2.trigger").click(function(){
		$(this).toggleClass("active").next().slideToggle("slow");
		return false; //Prevent the browser jump to the link anchor
	});

	//###############   PRETTYPHOTO, Zoom functionality throughout content   ###############
	var w = $("#innerWidth-table").innerWidth();
	if (w == null) {
		w = 540;
	}
	$("#size-info").hide();
	
	$("a[rel^='prettyPhoto']")
		.each(function() {
			$(this).hover(function() {$(this).children('.zoom').fadeIn(250);}, function() {$(this).children('.zoom').fadeOut(250);});
		})
		.append('<img src="/themes/site_themes/running-outlet/img/build/zoom.png" alt="zoom" class="zoom" />')
		.prettyPhoto({
			showTitle: true,
			counter_separator_label: " of ",
			theme: 'light_rounded',
			ie6_fallback: false,
			default_width: w
		});
	$(".product-image-zoom").click(function() {
		$.prettyPhoto.open(
			$(".product-image-zoom .cloud-zoom").attr("href"),
			$(".product-image-zoom .cloud-zoom img").attr("title"),
			$(".product-image-zoom .cloud-zoom img").attr("alt")
		);
	});

	//###   Purchase History Accordion/Toggler   ###
	if ($('#purchase-history .summary').length) {
		$("#purchase-history .order-details").hide();
		$("#purchase-history .summary").click(function() {
			if ($(this).next().is(':visible')) {
				$("#purchase-history .order-details").slideUp();
			} else {
				$("#purchase-history .order-details").slideUp();
				$(this).next().slideDown();
			}
			return false;
		});
	}

	if ($('.product-container form').length) {
		if ($('.product-container select[name="size"]').val() == "")
			$('.product-container').find("input[type=image]").attr("disabled","true").fadeTo("slow", 0.20).addClass("disabled");
		if ($('.product-container select[name="colour"]').val() == "")
			$('.product-container').find("input[type=image]").attr("disabled","true").fadeTo("slow", 0.20).addClass("disabled");

		//###############   Form Updates relating to Price change   ###############
		$('.product-container select[name="size"], .product-container select[name="colour"]').change(function () {
			if ($('.product-container select[name="size"]').val() != "" && $('.product-container select[name="colour"]').val() != "") {
				$('.product-container').find("input[type=image]").removeAttr("disabled").fadeTo("slow", 1).removeClass("disabled");
/*NEW*/				var postData = new Object;
/*NEW*/				postData["size"] = $('.product-container select[name="size"]').val();
/*NEW*/				if ( $('.product-container select[name="colour"]').val() )
/*NEW*/					postData["colour"] = $('.product-container select[name="colour"]').val();
/*CHANGED*/			getVariantPrice( $(this).parentsUntil("form"), $(this).parent().parent().find('input[name="add"]').val(), postData);
			} else {
				$('.product-container').find("input[type=image]").attr("disabled","true").fadeTo("slow", 0.20).addClass("disabled");
			}
		});
	}

	//###   Basket Delivery Form   ###
	if ($('#basket .totals').length) {
		$('#basket .totals select#delivery-option').change(function () {
			if ( $("form#address #delivery-same-address").attr("checked") )
				getDeliveryPrice( $(this).parents("form"), $('#address #billing-country').val(), $(this).val() );
			else
				getDeliveryPrice( $(this).parents("form"), "", $(this).val() );
		});
	}
	if ($('#basket .totals').length) {
		$('#basket .totals #delivery-option').change(function () {
			getDeliveryPrice( "", "", $(this).val() );
		});
	}

	if ($('#basket .totals').length && $('#address').length) {
		//###   Address-details page   ###
		$('form#address #billing-country').change(function () {
			if ( $("form#address #delivery-same-address").attr("checked") )
				getDeliveryPrice( $(this).parents("form"), $('#address #billing-country').val() );
		});
		$('form#address #delivery-country').change(function () {
			getDeliveryPrice( $(this).parents("form"), $('#address #delivery-country').val() );
		});

	}
	
	$(".link-description a").attr("target","_blank");
	
	
	
	
	
	
	
	
	
	if($('form[name="search-form"]').length && $('form[name="search-form"] ul.search').attr('rel')) {
	
		var rawCategories = $('form[name="search-form"] ul.search').attr("rel");
		var explodedCategories = rawCategories.split(' ');
		
		$('select[name="category[]"] option').each(function() {
		
			var value = $(this).attr('id');
			var valueClean = value.replace('option-','');
			
			if($.inArray(valueClean, explodedCategories) > -1 ) {$(this).attr("selected","selected");};

		});
		
	}
	
	
	
	
	
	
	
	
	
});


function getVariantPrice( $form, entryID, searchArray ) {

	if (entryID.length > 0) {
		var dataStr = "&entry_id=" + entryID;
/*UPDATED*/	//dataStr += "&search=" + escape(searchStr); //encodeURI(searchStr);
/*NEW*/		var searchStr = "";
/*NEW*/		for (var key in searchArray)
/*NEW*/			searchStr += "&"+key+"="+escape(searchArray[key]);
/*NEW*/		dataStr += searchStr;

		//###   Send form via AJAX   ###
		$.ajax({
			url: "/products/ajax-price-update",
			type: "POST",
			data: dataStr,
			dataType: "html",
			success: function (html) {
				if ($(".product-container").length) {
					$(".product-container .price-code").html(html);
					//newSKU = $(".product-container .price-code span").html();
					//newSKU = newSKU.substr(6);
					//if (newSKU != "")
						//$('.product-container input[name="sku"]').val(newSKU);
				}
				return html;
			}
		});
	}
}


function getDeliveryPrice( $form, destination, option ) {
	//console.log("destination="+destination);
	//console.log("option="+option);
	if (destination == undefined)
		destination = "";
	if (destination.length > 0 || option.length > 0 || $(".my-basket .totals").length) {
		dataVar = "";
		if (destination.length > 0)
			dataVar += "&country=" + destination;
		if (option)
			dataVar += "&option=" + option;

		//###   Send form via AJAX   ###
		$.ajax({
			url: "/checkout/ajax-delivery-update",
			type: "POST",
			data: dataVar + "&country_file=" + "sagepay",
			dataType: "html",
			success: function (html) {
				if ($(".my-basket .totals").length) {
					$(".my-basket .totals").html(html);
					$('.my-basket .totals #delivery-option').change(function () {
						getDeliveryPrice( $(this).parents("form"), '', $(this).val() );
					});
				}
				if ($("#basket .totals").length) {
					$("#basket .totals").html(html);
					$('#basket .totals #delivery-option').change(function () {
						if ( $("form#address #delivery-same-address").attr("checked") )
							getDeliveryPrice( $(this).parents("form"), $('#address #billing-country').val(), $(this).val() );
						else
							getDeliveryPrice( $(this).parents("form"), $('#address #delivery-country').val(), $(this).val() );

					});
				}
				//console.log(html);
				return html;
			}
		});
	}
}
