var kukurbita = {

	swapImg: function(evt, alt) {
		if (!$(evt.parentNode).hasClass("selected"))
			evt.src='styleImages/'+alt+'5.gif';
	},
	
	swapImgBack: function(evt, alt) {
		if (!$(evt.parentNode).hasClass("selected"))
			evt.src='styleImages/'+alt+'4.gif';
	},	
	
	toggleOverlay: function() {
		$(".fade").show();
		$(".international-info-overlay").fadeIn("slow");
	},
	
	closeOverlay: function() {
	
		$(".international-info-overlay").fadeOut("fast");
		$(".fade").hide();
		
	},
	
	toggleSami: function() {
		
		if($('#samiOnly').is(':checked')) {
			 $('#productstable tbody tr').each(function() {
				if($(this).find("td:eq(1)").text()!="sami") {
					$(this).hide();
				}
			});
		}	
		else {
			$('#productstable tr').show();
		}
	
	},
	
	swapProductImg: function(evt) {
		var newImg=evt.src.replace(/_s/, "_m");
		newImg=newImg.replace(/imagesSmall/, "images");
		var hrefNewImgnewImg=newImg.replace(/images/, "imagesLarge");
		hrefNewImgnewImg=hrefNewImgnewImg.replace(/_m/, "_l");
		$("#largeImage").attr("src", newImg);
		$("#largerImgLink").attr("href",hrefNewImgnewImg);
	},	
	
	changeLanguage: function(evt) {

		var uri = document.location.href;
		var mySplitResult = uri.split("/");
		var subSection = mySplitResult[3].substring(2,0);
	
		if(subSection=="p-")
		{
			uri = uri.replace(/-se.html/,"");
			uri = uri.replace(/-jp.html/,"");
			
			if ($(evt).hasClass("flag1")) {
				window.location = uri+"-se.html";
			}else{
				window.location = uri+"-jp.html";
			}
		}
		else
		{
			if ($(evt).hasClass("flag1")) {
				window.location = uri+"p-home-se.html";
			}else{
				window.location = uri+"p-home-jp.html";
			}		
		}

	},		
	
	
	onReady: function() {
		
		$("#menu a img").hover(
		  function() {
			kukurbita.swapImg(this, this.alt);
		  },
		  function () {
			 kukurbita.swapImgBack(this, this.alt);      	
        });
		
		$(".extra-img-row img").click(function() {
			kukurbita.swapProductImg(this);
			
		});
		
		$(".flag").click(function() {
			kukurbita.changeLanguage(this);
			return false;
		});
		
		$(".int-flag").click(function() {
			kukurbita.toggleOverlay();
			return false;
		});
		
		$(".close-overlay").click(function() {
			kukurbita.closeOverlay();
			return false;
		});
		
		$("#samiOnly").change(function() {
			kukurbita.toggleSami();
			return false;
		});
		
		
		$("#productstable").tablesorter(); 
		
	}
	
}

$(document).ready(function()
{
	kukurbita.onReady();
});  