$(function() {

	




	// Ajax messages which are fixed on top of page via CSS

	$.ajaxSetup({ cache: false });

	$('<p id="AjaxMsg"></p>').insertAfter("#master_wrapper");

	$('#AjaxMsg').ajaxStart(function() { $(this).slideDown(250); });

	$('#AjaxMsg').ajaxStop(function() { $(this).slideUp(250); });



	// Adding products to cart with ajax and refreshing of the minicart-tag (some things IE6-disabled)

	if ($(".ProductBuy").length) {

		$(".BuyForm").submit(function(event) {

			$("#AjaxMsg").text("Lisätään tuote ostoskoriin"); 

			$(".AddToCart",".BuyForm").attr("disabled","disabled").children("span").text("Lisätään…");

			$(this).ajaxSubmit(function() {

				$("#ProductAddedNotification").slideUp(250, function() { $(this).remove(); });

				if (!($.browser.msie && parseInt($.browser.version)==6)) {

					$.get("/interface/MiniCart", function(minicart) {

						$(".AddToCart",".BuyForm").attr("disabled","").children("span").text("Lisää ostoskoriin");

						$("#AjaxNotification").after('<div class="Notification Success" id="ProductAddedNotification"><p>Tuote ' + ProductName + ' lisättiin ostoskoriin onnistuneesti.</p><p><a href="/">Palaa etusivulle</a>, <a href="/cart/">näytä ostoskori</a> tai <a href="/checkout/">siirry kassalle</a>.</div>');

						$("#ProductAddedNotification").slideDown(250);

						$("#MiniCart").html(minicart).fadeIn(250);

					});

					$("#AjaxNotification").fadeOut(250);

				} else {

					$(".AddToCart",".BuyForm").attr("disabled","").children("span").text("Lisää ostoskoriin");

					$("#PageHeader").after('<div class="Notification Success" id="ProductAddedNotification"><p>Tuote ' + ProductName + ' lisättiin ostoskoriin onnistuneesti.</p><p><a href="/">Palaa etusivulle</a>, <a href="/cart/">näytä ostoskori</a> tai <a href="/checkout/">siirry kassalle</a>.</div>');

					$("#ProductAddedNotification").slideDown(250);

				}

			});

			event.preventDefault();

		});

	}

	

	// Removing products from minicart with ajax (IE6-disabled)

	if (!($.browser.msie && parseInt($.browser.version)==6)) {

		$("#MiniCartWrapper").delegate(".CartProductRemove a","click", function(event) {

			$("#AjaxMsg").text("Poistetaan tuote ostoskorista");

			var $parent = $(this).closest(".CartProduct");

			var rm = $(this).attr("href");

			$.get(rm, function() {

				$parent.slideUp(250, function() { $(this).remove(); });

				$.get("/interface/MiniCart", function(data) {

					var $subtotal = $("dd.SubTotal", data).html();

					$("#MiniCartWrapper dd.SubTotal").fadeOut(250, function() {

						if ($subtotal==null) {

							$("#MiniCartWrapper dd.SubTotal").html('0,<span class="PriceDecimals">00</span><span class="Currency">€</span>');

						} else {

							$("#MiniCartWrapper dd.SubTotal").html($subtotal);

						}

						$("#MiniCartWrapper dd.SubTotal").fadeIn(250);

					});

				});

			});

			event.preventDefault();

		});

	}






	// Productimageswapper via clik on thumbnail or changing the variation and the other way aroung

	if ($("#ProductImages").length) {

		$("#CurrentProductImage").prepend($('<span id="ImgLoader">Ladataan...</span>').hide());

		if ($("#ProductBuy .BuyFormVariationSelect").length) {

			$(".BuyFormVariationSelect select").change(function(event, triggered) {

				if (triggered==undefined) {

					var vName = $("option:selected", this).text();

					var vSplit = vName.split("(");

					var vTrim = vSplit[0].replace(/^[ \t]+|[ \t]+$/,"").toLowerCase();

					$("#ProductThumbnails li a").each(function() {

						var title = $(this).attr("title");

						var href = $(this).attr("href");

						var iTrim = title.replace(/^[ \t]+|[ \t]+$/,"").toLowerCase();

						var iSubstr = iTrim.substr(0,vTrim.length);

						var vSubstr = vTrim.substr(0,iTrim.length);

						if (iSubstr==vSubstr) {

							var triggered = true;

							$(this).trigger("click", triggered);

							return false;

						}

					});

				}

			});

			$("#ProductThumbnails li a").click(function(event, triggered) {

				event.preventDefault();

				var title = $(this).attr("title");

				var href = $(this).attr("href");

				var src = $("#CurrentProductImage img").attr("src");

				var sizeArr = src.split("/");

				var targetSize = sizeArr[2];

				var tnSrc = href.split("/");

				var targetImg = tnSrc[3];

				var newImg = "/tuotekuvat/"+targetSize+"/"+targetImg;

				SwapImg(newImg,href,title);

				if (triggered==undefined) {

					var iTrim = title.replace(/^[ \t]+|[ \t]+$/,"").toLowerCase();

					$(".BuyFormVariationSelect select option").each(function() {

						var vName = $(this).text();

						var vSplit = vName.split("(");

						var vTrim = vSplit[0].replace(/^[ \t]+|[ \t]+$/,"").toLowerCase();

						var iSubstr = iTrim.substr(0,vTrim.length);

						var vSubstr = vTrim.substr(0,iTrim.length);

						if (vSubstr==iSubstr) {

							var triggered = true;

							$(this).attr("selected","selected");

							return false;

						}

					});

				}

			});

			$("#ProductBuy .BuyFormVariationSelect select").trigger("change");

		} else {

			$("#ProductBuy .BuyFormVariationRadio input").click(function(event, triggered) {

				if (triggered==undefined) {

					var vName = $(this).parent("label").text();

					var vTrim = vName.replace(/^[ \t]+|[ \t]+$/,"").toLowerCase();

					$("#ProductThumbnails li a").each(function() {

						var iTrim = $(this).attr("title").replace(/^[ \t]+|[ \t]+$/,"").toLowerCase();

						var iSubstr = iTrim.substr(0,vTrim.length);

						var vSubstr = vTrim.substr(0,iTrim.length);

						if (iSubstr==vSubstr) {

							var triggered = true;

							$(this).trigger("click", triggered);

							return false;

						}

					});

				}

			});

			$("#ProductThumbnails li a").click(function(event, triggered) {

				event.preventDefault();

				var title = $(this).attr("title");

				var href = $(this).attr("href");

				var src = $("#CurrentProductImage img").attr("src");

				var sizeArr = src.split("/");

				var targetSize = sizeArr[2];

				var tnSrc = href.split("/");

				var targetImg = tnSrc[3];

				var newImg = "/tuotekuvat/"+targetSize+"/"+targetImg;

				SwapImg(newImg,href,title);

				if (triggered==undefined) {

					var iTrim = title.replace(/^[ \t]+|[ \t]+$/,"").toLowerCase();

					$("#ProductBuy .BuyFormVariationRadio input").each(function() {

						var vName = $(this).parent("label").text();

						var vTrim = vName.replace(/^[ \t]+|[ \t]+$/,"").toLowerCase();

						var iSubstr = iTrim.substr(0,vTrim.length);

						var vSubstr = vTrim.substr(0,iTrim.length);

						if (vSubstr==iSubstr) {

							var triggered = true;

							$(this).trigger("click", triggered);

							return false;

						}

					});

				}

			});

			if ($("#ProductBuy .BuyFormVariationRadio").length) { $("#ProductBuy .BuyFormVariationRadio input:checked").trigger("click"); }

		}

	}



});



// Imageswapperscript

function SwapImg(i,href,title) {

	if ($("#CurrentProductImage img").attr("src") != i) {

		$("#CurrentProductImage img").fadeOut(200, function() {

			$("#ImgLoader").fadeIn(100, function() {

				$("#CurrentProductImage img").attr("src",i).load(function() {

					$("#ImgLoader").fadeOut(100, function() {

						$("#CurrentProductImage img").fadeIn(200);

					});

				}).each(function() {

					if (this.complete && $.browser.msie) {

						$(this).trigger("load");

					}

				});

			});

		});

		$("#ProductImageCaption").html(title);

		$("#CurrentProductImage img").attr("alt",title);

		$("#CurrentProductImage").attr("title",title);

		$("#CurrentProductImage").attr("href",href); 

	}

}
$(function() {
	$("ul.Categories li:has(ul li.Current)").each(function() {


			$(this).removeClass('Current').addClass('CurrentAncestor');

	
	});

$('.AdditionalProductsItem:not(:has(div.Product))').css('display','none');

});
$(document).ready(function(){
$('.TermsLink').replaceWith('<a href="#ehdot" class="TermsLink">Lue toimitusehdot</a>');
$(".TermsLink").click(function () { 
$.get('/interface/TermsAndConditions', function(data) {
$('#ehdot').html(data);
});
});
});

