var agt			= navigator.userAgent.toLowerCase();
var is_ie 		= (agt.indexOf("msie") != -1) && (agt.indexOf("opera")== -1);
var version_major 	= parseInt(navigator.appVersion);
var is_ie6down =  version_major < 5 && is_ie;

var timeoutVal = null;

$(document).ready(function(){
	//Onchange functions to redirect to pages
	$('.selectRedirect').change(function() {						 	
		var redirectURL = $(this).val();	
		//alert(redirectURL);
		if ((redirectURL != null) && (redirectURL != "")) {
			window.top.location.href = redirectURL;
		}

	});
						   
									
	// Removes SEO text in navItems
	$(".clearText").html("");
	$(".clearText").removeClass("clearText");
	
	// Clears input fields on Focus
	$(".clearField").bind("blur", function() {
		if ($(this).val()=="") {
			$(this).val($(this).attr("title"));
		};
	});
	$(".clearField").bind("focus", function() {
		if ($(this).val()==$(this).attr("title")) {
			$(this).val("");
		};
	});
	
	// -------------------------------------------------
	$("#tooltip").css({"left":"0","top":"0","display":"block","opacity":0});
	$("a.tooltip").hover(function(){
		var tipOffset 	= $(this).offset();
		var tipHeight	= $("#tooltip").height()+5;
		var tipWidth	= $(this).width();
		var tipLeft 	= tipOffset.left+(tipWidth/2)-59;
		var tipTop 		= tipOffset.top-tipHeight;
		var tipText 	= $(this).attr("title");
		
		$("#tooltip div").html(tipText);
		$("#tooltip").css({
				"left" : tipLeft+"px",
				"top" : (tipTop+3)+"px",
				"opacity" : 0,
				"display" : "block"
			}).animate({
				"opacity":1,
				"top":tipTop+"px"
			},200,function(){ $(this).attr("title","") });
	},function(){
		$("#tooltip").css({"left":"0","top":"0","display":"none"});
	});
	
	
	// -------------------------------------------------
	$("a.controlOpen").bind("click", function() {
		var rel = $(this).attr("rel");
		var relObj = $("#"+rel);
		relObj.css({"display":"block"});
	});
	
	$("a.controlClose").bind("click", function() {
		var rel = $(this).attr("rel");
		var relObj = $("#"+rel);
		relObj.css({"display":"none"});
	});
	
	$("a.controlToggle").live("click", function() {
		scriptActionEffects('','',$(this));
		//alert('a');
	});
	
	$("a.controlToggle").removeAttr("href");
	
	
	
	// PRODUCT DETAILS ---------------------------------
	$("a.controlTab").bind("click", function() {
							 
		if(timeoutVal != null)
		{
			clearTimeout(timeoutVal);
			timeoutVal = null;
		}
		
		var btn = $(this);
		
		timeoutVal = setTimeout(function()
		{
			var obj, objIx, grp, grpId, rel, relSplit, animSpeed, objPrev, btnPrev;
			animSpeed 		= 200;
			
			rel 			= btn.attr("rel");
			relSplit 	= rel.indexOf(",");
	
			if (relSplit > 0) {
				grpId 	= rel.substr(0,(relSplit));
				objIx		= rel.substr(relSplit+1);
			} else {
				objIx 	= rel;
			}
			grp 	= $("#"+grpId);
			obj 	= grp.find("div:nth-child("+objIx+")");
			
			if (obj != $(":animated")) {
				// item
				objPrev = grp.find("div.active");
				
				if (is_ie6down) {
					
					objPrev.css({ "z-index":"1", "display":"none" }).removeClass("active");
					obj.css({ "z-index":"2" , "display":"block" }).addClass("active");
					
				} else {
					
					objPrev.animate({
						opacity : 0
					},animSpeed,function(){
						obj.animate({
							opacity : 1
						},function(){
							obj.addClass("active").css({"z-index":"2"});
							objPrev.removeClass("active").css({"z-index":"1"});
						});
					});
					
				}
	
				btnPrev = btn.parent().parent().find(".active").css({"background-position":"center bottom"}).removeClass("active");
				btn.css({"background-position":"center top"}).addClass("active");
			}
		}, 300);
	});



	$(".hoverButton").css({cursor:"pointer"}).hover(function() {
		var src = $(this).attr("src");
		var srcStop = src.lastIndexOf(".");
		var srcExt	= src.substr(srcStop+1);
		var srcNew 	= src.substr(0,(srcStop-2));
		$(this).attr("src",srcNew+"_h."+srcExt).addClass("active");
	},function() {
		var src = $(this).attr("src");
		var srcStop = src.lastIndexOf(".");
		var srcExt	= src.substr(srcStop+1);
		var srcNew 	= src.substr(0,(srcStop-2));
		$(this).attr("src",srcNew+"_n."+srcExt).removeClass("active");
	});

	// -------------------------------------------------
	$(".controlHover").hover(function(){
		var rel = $(this).attr("rel");
		var obj;
		if (rel.length > 0) {
			obj = $("#"+rel);
		} else {
			obj = $(this).parent().find(".image");
		}
		
		if (obj.length > 0) {
			obj.stop().animate({top:"0px"},100,function(){});
		}
	},function(){
		var rel = $(this).attr("rel");
		var obj;
		if (rel.length > 0) {
			obj = $("#"+rel);
		} else {
			obj = $(this).parent().find(".image");
		}
		
		if (obj.length > 0) {
			obj.stop().animate({top:"10px"},100,function(){});
		}
	});

	// FORM
	// -------------------------------------------------
	//$(".textSelect .buttonInput").css("display","block");
	/*$(".textSelect .buttonInput").bind("click",function(){
		$(this).next("input");
	});*/


	// HOME ---------------------------------
	$("div.homeNews ul").css({
		height	:	"25px",
		overflow	:	"hidden"
	});
	$("div.homeNews ul li").css({
		position	:	"absolute",
		top	:	"0px",
		display	:	"none"
	});
	$("div.homeNews ul li:first").addClass("first active").css({"display":"block"});
	$("div.homeNews ul li:last").addClass("last");
	
	var homeNewsPause = 3000;
	var homeNewsTimer;
	var homeNewsSpeed = 800;
	var homeNewsActive, homeNewsNext;
	
	homeNewsTimer = setInterval(function() {
		if ($("div.homeNews ul li:animated").length == 0) {
			
			homeNewsActive = $("div.homeNews ul li.active");
			if (homeNewsActive.hasClass("last")) {
				homeNewsNext = $("div.homeNews ul li:first");
			} else {
				homeNewsNext = homeNewsActive.next();
			}
			
			homeNewsActive.animate({
				top: "-25px",
				opacity : 0
			},homeNewsSpeed,function(){ homeNewsActive.removeClass("active")} );
			homeNewsNext.css({"display":"block","top":"25px",opacity:0}).animate({
				top: "0px",
				opacity : 1
			},homeNewsSpeed,function(){ homeNewsNext.addClass("active")} );
			
		}
	},homeNewsPause);
	
	$("div.homeNews ul li").hover(function(){
		clearInterval(homeNewsTimer);	
	},function(){
		homeNewsTimer = setInterval(function() {
			if ($("div.homeNews ul li:animated").length == 0) {
				
				homeNewsActive = $("div.homeNews ul li.active");
				if (homeNewsActive.hasClass("last")) {
					homeNewsNext = $("div.homeNews ul li:first");
				} else {
					homeNewsNext = homeNewsActive.next();
				}
				
				homeNewsActive.animate({
					top: "-25px",
					opacity : 0
				},homeNewsSpeed,function(){ homeNewsActive.removeClass("active")} );
				homeNewsNext.css({"display":"block","top":"25px",opacity:0}).animate({
					top: "0px",
					opacity : 1
				},homeNewsSpeed,function(){ homeNewsNext.addClass("active")} );
				
			}
		},homeNewsPause);
	});
	
	/* athletes page box hover */
	$("#athletes li").hover(
		function() {
			$(this).find('.athlete_desc').stop().animate({
					top: -60
				},300);
		}, 
		function() {
		  $(this).find('.athlete_desc').stop().animate({
					top: 0
				},300);
		}
	 );
	
	/* REMOVED 20110928, desc slide up on hover
	
	$("#alliances li").hover(
		function() {
			$(this).find('.alliances_desc').stop().animate({
					top: -145
				},300);
		}, 
		function() {
		  $(this).find('.alliances_desc').stop().animate({
					top: 0
				},300);
		}
	 );*/
	
	/* alliances page box hover */
	$("#alliances li").hover(
		function(){
			$(this).find(".alliances_descOverlay").stop().fadeTo(300,0);//first param = speed, 2nd param = opacity max being 1
		},
		function(){
			$(this).find(".alliances_descOverlay").stop().fadeTo(300,0.5);
		}
	);
	
	/* alliances page banner rotator */
	$('#alliancesBanners').cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 0, 
		next:   '#nextAlliances', 
		prev:   '#prevAlliances' 
	});
	
	$('#athletesBanners').cycle({ 
		fx:     'fade', 
		speed:  'slow', 
		timeout: 0, 
		next:   '#nextAthletes', 
		prev:   '#prevAthletes' 
	});
	
	init();

});

function scriptActionEffects(id,fx,trigger) {
	var btn, obj, rel, relSplit, relObj, relFx, animSpeed;
	animSpeed 		= 200;
	animSpeedFast 	= 100;
	animSpeedMed 	= 400;
	btn				= trigger;

	if ((id != '') && (fx != '')) {
		relFx 	= fx;
		obj 	= $("#"+id);
		
	} else {

		rel 		= btn.attr("rel");
		relSplit 	= rel.indexOf(",");
		
		if (relSplit > 0) {
			relObj 	= rel.substr(0,(relSplit));
			relFx		= rel.substr(relSplit+1);
		} else {
			relObj 	= rel;
		}
		obj 	= $("#"+relObj);
	}
	
	if (obj != $(":animated")) {
	
		switch (relFx) {
			case 'fade' : 
					if (obj.hasClass("active")) {
						obj.animate({
								opacity:0
							},animSpeed,function(){
								obj.removeClass("active").css({display:"none"});
								btn.removeClass("active");
						});
					} else {
						obj.css({display:"block",opacity:0})
							.animate({
								opacity:1
							},animSpeed,function(){
								obj.addClass("active");
								btn.addClass("active");
						});
					}
				break;
			case 'slide' : 
					if (obj.hasClass("active")) {
						obj.slideUp(animSpeed,function(){
								obj.removeClass("active").css({display:"none"});
								btn.removeClass("active");
						});
					} else {
						btn.addClass("active");
						obj.slideDown(animSpeed,function(){
								obj.addClass("active").css({display:"block"});
						});
					}
				break;
			case 'move' :
					var moveHeight = obj.height();
					moveHeight = moveHeight-30;
					if (obj.hasClass("active")) {
						obj.css({display:"block",top:"0px"})
							.animate({
									// move up
									top:"-"+moveHeight+"px"
								},animSpeedMed,function(){
									obj.removeClass("active").css({display:"none"});
									btn.removeClass("active");
							});
					} else {
						obj.css({display:"block",top:"-"+moveHeight+"px"})
							.animate({
									// move down
									top:"0px"
								},animSpeedMed,function(){
									obj.addClass("active");
									btn.addClass("active");
							});
					}
				break;
			default : 
					if (obj.hasClass("active")) {
						obj.css({"display":"none"}).removeClass("active");
						btn.removeClass("active");
					} else {
						obj.css({"display":"block"}).addClass("active");
						btn.addClass("active");
					}
				break;
		}
	}	
}


function init() {
	closeAllItems();
	openActiveItems();
	initTabs();
	randBackground();
}

function initTabs() {
	var compHeight = 0;
	$(".tabContent").find("div").each(function(i) {
		compHeight = comp($(this).height(),compHeight);
	}).css("height",compHeight).parent().css("height",compHeight);
	$("#prodInfoContainer").css("height",(compHeight+140)+"px");
	$("#prodInfoContainer .container").css("height",(compHeight+140)+"px");

	$(".tabContent").find("div").css({"position":"absolute","z-index":"1"});
	$(".tabContent").find("div:first").css({"z-index":"2"}).addClass("active");
	$(".tabHeader").find("a").css({"background-position":"center bottom"});
	$(".tabHeader").find("a:first").css({"background-position":"center top"}).addClass("active");
	
	if (is_ie6down) {
		$(".tabContent").find("div").css({"display":"none"});
		$(".tabContent").find("div:first").css({"display":"block"});
	} else {
		$(".tabContent").find("div").css({"opacity":"0"});
		$(".tabContent").find("div:first").css({"opacity":"1"});
	}
}

function comp(next,prev) {
	if (prev < next) {
		return next;
	} else {
		return prev;
	}
}

function openActiveItems() {
	var obj, href, hrefPos, hrefHash, btn, objID, btnRel, btnID;
	href 			= window.location.href;
	hrefPos 	= href.indexOf("#");
	
	if (hrefPos > 0) {
		hrefHash	 = href.substr(hrefPos+1);
		
		if (hrefHash.length > 0 ) {
			btn = $("a.controlToggle[name='"+hrefHash+"']");
	
			if (btn.length > 0) {
				btnRel 	= btn.attr("rel");
				btnID		= btnRel.indexOf(",");
				if (btnID > 0) {
					objID 	= btnRel.substr(0,btnID);
				} else {
					objID 	= btnRel;
				}
				
				obj 	= $("#"+objID);
				
				if (obj.length > 0) {
					setTimeout(function() {
						obj.slideDown(500,function(){
							obj.addClass("active");
							btn.addClass("active");
						});
					},500);
				}
			}
		}
	}
	
	// MENU
	// -------------------------------------------------
	$("#navContainer a").hover(function(){
		var navCur = $(this);
		var subNav = navCur.next(".menuContainer");

		if (subNav.length > 0) { // has sub nav
			subNav.addClass("active").css("display","block");
			if (is_ie6down) {  // for ie lt 6
				$("#flashContainer select").css("visibility","hidden");
				$("#bodyContainer select").css("visibility","hidden");
			};
		}
	},function(){
		var navCur = $(this);
		var subNav = navCur.next(".menuContainer");
		
		if (subNav.length > 0) { // has sub nav
			subNav.css("display","none");
			if (is_ie6down) {  // for ie lt 6
				$("#flashContainer select").css("visibility","visible");
				$("#bodyContainer select").css("visibility","visible");
			};
		}
	});
	
	$(".menuContainer").hover(function(){
		var subNav = $(this);

		if (subNav.length > 0) { // has sub nav
			subNav.addClass("active").css("display","block");
			if (is_ie6down) {  // for ie lt 6
				$("#flashContainer select").css("visibility","hidden");
				$("#bodyContainer select").css("visibility","hidden");
			};
		}
	},function(){
		var subNav = $(this);
		
		if (subNav.length > 0) { // has sub nav
			subNav.css("display","none");
			if (is_ie6down) {  // for ie lt 6
				$("#flashContainer select").css("visibility","visible");
				$("#bodyContainer select").css("visibility","visible");
			};
		}
	});

	$(".menuContainer .menuList a").hover(function(){
																  
		var relId = $(this).attr("rel");
		var imgObj = $(this).parent().parent().parent().siblings(".menuImage");

		if ( relId.length > 0 ) {
			imgObj.css("visibility","visible").attr("src",relId);
		} else {
			imgObj.css("visibility","hidden");
		}
		
	},function(){
		//var imgObj = $(this).parent().parent().parent().siblings(".menuImage");
		//imgObj.css("visibility","hidden");
	});
	
	

} // end function

function closeAllItems() {
	
	// Store listing
	$(".infoMore").css({display:"none"});
	
	// Product
	$("#featureFilter").css({display:"none"}).removeClass("active").prev("a").removeClass("active");
	$("#shapeFilter").css({display:"none"}).removeClass("active").prev("a").removeClass("active");
	$("#colourFilter").css({display:"none"}).removeClass("active").prev("a").removeClass("active");
	
} // end function

function randBackground() {

	// Corporate
	var imgFile = rootFolder+"/images/files/bg_corporate_01.jpg";
	$(".Corporate #bodyContainer").css({"background-image":"url("+imgFile+")"});
	
} // end function



//Ajax Product Listing Functions
function pageload(hash) {
	// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser

	//alert(hash);
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			// jquery's $.load() function does't work when hash include special characters like едц.
			hash = encodeURIComponent(hash);
		}
		var checkHash = hash.indexOf("-");
		if (checkHash >= 0) {
			var arrCallbackInfo
			arrCallbackInfo = hash.split("-");
			
			var rParCateID 	= arrCallbackInfo[0]
			var rCateID 	= arrCallbackInfo[1]
			var rPageNum 	= arrCallbackInfo[2]
			
			if (rCateID.length == 0) {
				rCateID = ''	
			}
			
			if (rPageNum.length == 0) {
				rPageNum = ''	
			}
			
			ajaxLoadProductList(rParCateID,"" + rCateID + "","" + rPageNum + "")
		}
	} else {
		// start page
		//$("#load").empty();
	}
}

$(document).ready(function() {
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "");

	$("a.pagingLink").live("click",function(e){
		e.preventDefault();							 
		 
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);
	});
	
	var tO = null;
	var hoverSpeed = 200;
	
	
	
	$("input.compare").live("click",function () {						   
		var compareID = $(this).val();
		var compareAction = true;
		if ( $(this).is(':checked') ) {
			compareAction = true;
		} else {
			compareAction = false;
		}								   
		 
		$("#compareCookieAjaxArea").load(rootFolder + "/ajax/setCompareCookie.asp",{'id':compareID,'action':compareAction});	
    });
	
	$("#shoppingCartEmail .remove a").live("click",function (e) {	
		e.preventDefault();
		
		var delNum = $(this).attr("href").match(/delID=([0-9]+)/)[1];

		//alert(delNum);
		deleteProductFromCart(delNum);
    });
	
	
	
});

function comparePopup(parentCateID) {
	top.GB_showCenter('Compare Products', rootFolder + '/compare.asp?pcid=' + parentCateID, 780, 950);
}

function updateSortByCookie(currentID) {
	var sortByAction = $(currentID).val();	
	$("#compareCookieAjaxArea").load(rootFolder + "/ajax/setOrderByCookie.asp",{'action':sortByAction});
}

function addProductToCart(prodID,prodQty,prodColour,prodSize) {
	//alert(prodID + "," + prodQty + "," + prodColour + "," + prodSize);
	$("div.miniCartButton span a").load(rootFolder + "/ajax/addProductToCart.asp",{productID:prodID,productQty:prodQty,productColour:prodColour,productSize:prodSize}, function(data) {
		
		$("#shoppingCartForm").load(rootFolder + "/ajax/getCartSummary.asp");	
	});
}

function deleteProductFromCart(delCartID) {
	//alert(prodID + "," + prodQty + "," + prodColour + "," + prodSize);
	$("div.miniCartButton span a").load(rootFolder + "/ajax/deleteProductFromCart.asp",{delCartID:delCartID,action:"delete"}, function(data) {
		
		$("#shoppingCartForm").load(rootFolder + "/ajax/getCartSummary.asp");	
	});
}

function addVoucherToCart(prodID,prodQty,rVoucherTo,rVoucherFrom,rVoucherAddress,rVoucherMessage)  {
	//alert(prodID + "," + prodQty + "," + prodColour + "," + prodSize);
	$("div.miniCartButton span a").load(rootFolder + "/ajax/addVoucherToCart.asp",{productID:prodID,productQty:prodQty,voucherTo:rVoucherTo,voucherFrom:rVoucherFrom,voucherAddress:rVoucherAddress,voucherMessage:rVoucherMessage}, function(data) {
		
		$("#shoppingCartForm").load(rootFolder + "/ajax/getCartSummary.asp");	
	});
}

var tO = null;
function ajaxLoadProductList(parentCID,cID,pageNum) {
	//var tO = null;

	$("div#ajaxReplaceList").fadeOut(300);
	
	if (cID.length == 0) {
		cID = ""	
	}
	
	// Cancel previous animations.
	if(tO != null)
	{
		clearTimeout(tO);
		tO = null;
	}
	
	//tO = setTimeout("ajaxLoadProductListCode(" + parentCID + "," + cID + "," + pageNum + ");tO=null;", 300);
	tO = setTimeout(function()
	{
		ajaxLoadProductListCode(parentCID, cID, pageNum);
		tO = null;
	}, 300);
}

function ajaxLoadProductListCode(parentCID,cID,pageNum) {
	//var tO = null;
	
	var minPrice = $("#amountFr").val();
	var maxPrice = $("#amountTo").val();
	
	var searchItem = $("#productSearchItem").val();
	
	var filterAdvice = new Array();
	
	$refineData = $('input[name=Refine]:checked');
	$refineData.each(function(index) {
		filterAdvice.push($(this).val());
	});
	
	$("div#ajaxReplaceList").load(rootFolder + "/ajax/load_prod_list.asp",{'cid':cID,'pcid':parentCID,'search':searchItem,'minPrice':minPrice,'maxPrice':maxPrice,'refineFilters':filterAdvice,'p':pageNum},
	function(data)
	{
		$('div#ajaxReplaceList').fadeIn(500);
			
	});
}

function setCountryStatus(country,redirectURL,idAjax) {
	//alert(country);
	$("#" + idAjax).load(rootFolder + "/ajax/setCountryCookie.asp",{"countryCode":country},
	function(data)
	{
		redirectURL = redirectURL.replace("#", "");
	
		window.location=redirectURL;
	});	
	
}
	
// END OF JAVSCRIPT
