
	$().ready(function(){
		setupRangeForm();
		setInterval("checkAnchor()", 300);
		$('body').append('<div id="jquery-blank-anchor"><a name="_"></a></div>');
	});
	
	function setupRangeForm(){
		if (seasonType == 1) {
			$("#styleForm :checkbox")
				.click(function(){
					if (this.checked) {
						if (this.value == '0') {
							for (i=1; i<=numStyles; i++) {
								$("#styleForm #style"+i).attr({checked: ''});
							}
						} else {
							$("#styleForm #style0").attr({checked: ''});
						}
					} else {
						if (this.value == '0') {
							for (i=1; i<=numStyles; i++) {
								$("#styleForm #style"+i).attr({checked: 'checked'});
							}
						} else {
							oneChecked = 0;
							for (i=1; i<=numStyles; i++) {
								if ($("#styleForm #style"+i).is(':checked')) {
									oneChecked = 1;
								}
							}
							if (oneChecked == 0) {
								$("#styleForm #style0").attr({checked: 'checked'});
							}
						}
					}
					tmpArr = [];			
					for (i=0; i<=numStyles; i++) {
						if ($("#styleForm #style"+i).is(':checked')) {
							tmpArr.push($("#styleForm #style"+i).attr("value"));
						}
					}
					selStyles = tmpArr.join("-");
					
					// added for styles URL
					selStylesString = "";
					if (selStyles != baseSelStyles) {
						selStylesString = '_s_'+selStyles;
					}
					updateProductRange();
					document.location = "#_" + selStylesString;
				});
		} else {
			$("#suitStyles a, #thickness a")
				.addClass('sel')
				.click(function(){
					
					if ($(this).attr('id').indexOf('ss') > -1) {
						missingAtLeastOne = 0;
						for (i=1; i<9; i++) {
							if ($('#suitStyles #ss'+i).hasClass('sel') == false) {
								missingAtLeastOne = 1;
							}
						}
						if (missingAtLeastOne == 0) {
							$("#suitStyles a").removeClass('sel');
							$(this).addClass('sel');
						} else {
							$(this).toggleClass('sel');
						}
					} else if ($(this).attr('id').indexOf('st') > -1) {
						missingAtLeastOne = 0;
						for (i=2; i<7; i++) {
							if ($('#thickness #st'+i).hasClass('sel') == false) {
								missingAtLeastOne = 1;
							}
						}
						if (missingAtLeastOne == 0) {
							$("#thickness a").removeClass('sel');
							$(this).addClass('sel');
						} else {
							$(this).toggleClass('sel');
						}
					}
					
					tmpArr = [];
					for (i=1; i<9; i++) {
						if ($('#suitStyles #ss'+i).hasClass('sel')) {
							tmpArr.push('s'+i);
						}
					}
					for (i=2; i<7; i++) {
						if ($('#thickness #st'+i).hasClass('sel')) {
							tmpArr.push('t'+i);
						}
					}
					selStyles = tmpArr.join("-");
					
					// added for styles URL
					selStylesString = "";
					if (selStyles != baseSelStyles) {
						selStylesString = '_s_'+selStyles;
					}
					updateProductRange();
					document.location = "#_" + selStylesString;
					return false;
				});
		}
	}
	function updateStylesCheckboxes(wSelStyles){
		if (currSelStyles != wSelStyles) {
			if (seasonType == 1) {
				for (i=0; i<=numStyles; i++) {
					$("#styleForm #style"+i).attr({checked: ''});
				}
				tmpArr = wSelStyles.split('-');
				for (t=0; t<tmpArr.length; t++) {
					for (i=0; i<=numStyles; i++) {
						if ($("#styleForm #style"+i).attr("value") == tmpArr[t]) {
							$("#styleForm #style"+i).attr({checked: 'checked'});
						}
					}
				}
			} else {
				$("#suitStyles a, #thickness a").removeClass('sel');
				tmpArr = wSelStyles.split('-');
				for (t=0; t<tmpArr.length; t++) {
					tmpLetter = tmpArr[t].substr(0, 1);
					tmpNumber = tmpArr[t].substr(1);
					$("#viewingOptions #s"+tmpLetter+tmpNumber).addClass('sel');
				}
			}
			selStyles = wSelStyles;
			selStylesString = "";
			if (selStyles != baseSelStyles) {
				selStylesString = '_s_'+selStyles;
			}
			updateProductRange();
		}
	}
		
	function setSortBy(wSortBy){
		if (wSortBy.indexOf('country') > -1) {
			selCountry = wSortBy.substr(8, 2);
		} else {
			selSortBy = wSortBy;
		}
		updateProductRange();
	}
	
	var currSelStyles = selStyles;
	var selStylesString = '';
	if (selStyles != baseSelStyles) {
		selStylesString = '_s_'+selStyles;
	}
	function updateProductRange(){
		currSelStyles = selStyles;
		
		var query = "cid=" + selCatId + "&sortby=" + selSortBy + '&styles=' + selStyles + '&country=' + selCountry + '&sid=' + seasonId + '&stype=' + seasonType + extraQuery;
		
		$("#productRange .loading").show();
		$.get("/sa/common_gl/inc_product_range"+(seasonType == 4 ? '_4' : '')+".php", query, function(data){
			$("#rangeContent").html(data);
			$("#productRange .loading").hide();
			
			$("#sortOptions a").removeClass('sel');
			/*
			if (selSortBy.indexOf('country') > -1) {
				tmpSelSortBy = 'country';
				$("#sortOptions a."+tmpSelSortBy).addClass('sel');
			}
			*/
			$("#sortOptions a."+selSortBy).addClass('sel');
			$("#sortOptions a.country-"+selCountry).addClass('sel');
		});
	}
	
	function trackSubView(wSection){
		baseLoc = document.location.href;
		if (baseLoc.indexOf("#") > -1) {
			baseLoc = baseLoc.substring(0, baseLoc.indexOf("#"))
		}
		wSection = wSection.replace("#","/");
		if (baseLoc.indexOf(wSection) < 0) {
			baseLoc = baseLoc + wSection;
		}
		urchinTracker(baseLoc);
	}
	
	
	var currentAnchor = null;
	var showingProdLightBox = 0;
	var prodLightBoxURL = '';
	
	function checkAnchor(){
		if(currentAnchor != document.location.hash){
			currentAnchor = document.location.hash;
			
			if (!currentAnchor || currentAnchor == "" || currentAnchor == "#_" || currentAnchor.indexOf('_s_') > -1) {
				if (showingProdLightBox > 0) {
					destroyProdLightBox(1);
				}
				if (showingPreviewLightBox != 0) {
					destroyPreviewLightBox(1);
				}
				if (currentAnchor.indexOf('_s_') > -1) {
					tmpStartIndex = currentAnchor.indexOf('_s_') + 3;
					tmpString = currentAnchor.substr(tmpStartIndex);
					updateStylesCheckboxes(tmpString);
				} else {
					updateStylesCheckboxes(baseSelStyles);
				}
			} else {
				var splits = currentAnchor.substring(1).split('_');
				
				if (splits[0] == "item") {
					
					var id = splits[1];
					var title = splits[2];
					var query = "id=" + id + extraQuery;
					
					if (showingPreviewLightBox != 0) {
						var prodLightBoxZ = $('#prodItem-lightbox').css( 'z-index' );
						var prevLightBoxZ = $('#jquery-lightbox').css( 'z-index' );
						if (prodLightBoxZ < prevLightBoxZ) {
							destroyPreviewLightBox(1);
						}
					}
					if (showingProdLightBox != id) {
						
						if (showingProdLightBox > 0) {
							destroyProdLightBox(0);
						}
						
						showProdLightBox(id);
						
						baseLoc = document.location.href;
						prodLightBoxURL = baseLoc;
						if (baseLoc.indexOf("#") > -1) {
							baseLoc = baseLoc.substring(baseLoc.indexOf('/sa/'), baseLoc.indexOf("#"));
						}
						urchinTracker(baseLoc + "/item/" + id + "/" + title);
						
						$.get("/sa/common_gl/inc_product_item.php", query, function(data){
							$("#prodItem-lightbox .lightbox-loading").hide();
							$("#prodItem-lightbox .lightbox-content").html(data);
						});
						
					} else if (currentAnchor.indexOf("_buy-online") > -1) {
						
						showOnlineStores();
					
					} else {
						
						hideOnlineStores();
						
					}
					
				} else if (splits[0] == "preview") {
					
					if (showingProdLightBox > 0) {
						var prodLightBoxZ = $('#prodItem-lightbox').css( 'z-index' );
						var prevLightBoxZ = $('#jquery-lightbox').css( 'z-index' );
						if (prodLightBoxZ > prevLightBoxZ) {
							destroyProdLightBox(0);
						}
					}
					var id = splits[1];
					
					if (showingPreviewLightBox != id) {
						if (showingPreviewLightBox != 0) {
							destroyPreviewLightBox(1);
						}
						showPreview(id);
					}
					
				}
			}
		}
	}
	
	function openProdItem(wItemTxt){
		baseLoc = document.location.href;
		if (baseLoc.indexOf("#") > -1) {
			baseLoc = baseLoc.substring(baseLoc.indexOf('/sa/'), baseLoc.indexOf("#"));
		}
		document.location.href = baseLoc + "#" + wItemTxt;
	}
	
	function hideProdLightBox(){
		baseLoc = document.location.href;
		if (baseLoc.indexOf("#") > -1) {
			baseLoc = baseLoc.substring(0, baseLoc.indexOf("#"));
		}
		if (showingPreviewLightBox != 0) {
			document.location = "#preview_"+showingPreviewLightBox;
		} else {
			document.location = "#_" + selStylesString;
		}
		//destroyLightBox();
	}
	
	function destroyProdLightBox(doFade){
		showingProdLightBox = 0;
		prodLightBoxURL = '';
		
		$('#prodItem-lightbox').remove();
		if (doFade == 1) { 
			$('#prodItem-overlay').fadeOut(function() { $('#prodItem-overlay').remove(); });
		} else {
			$('#prodItem-overlay').remove();
		}
		//$('embed, object, select').css({ 'visibility' : 'visible' });
		$('select').css({ 'visibility' : 'visible' });
	}
	
	function showProdLightBox(wId){
		showingProdLightBox = wId;
		setupProdLightBox();
		
		$('#prodItem-overlay').fadeIn();
		//$('embed, object, select').css({ 'visibility' : 'hidden' });
		$('select').css({ 'visibility' : 'hidden' });
	}
	
	function setupProdLightBox(){
		$('body').append('<div id="prodItem-overlay"></div><div id="prodItem-itemNav"></div><div id="prodItem-lightbox"><div id="prodItem-lightbox-back"></div><div class="lightbox-secNav"><a href="#_" class="lightbox-secNav-btnClose"></a></div><div class="lightbox-container-image-box"><a href="#_" class="prevBtn"></a><a href="#_" class="nextBtn"></a><div class="lightbox-container-image"><div class="lightbox-content"></div><div class="lightbox-loading"><a href="#" class="lightbox-loading-link"></a></div></div></div></div>');
		
		var arrPageSizes = ___getPageSize();
		$('#prodItem-overlay').css({
				backgroundColor:	"#000",
				opacity:			flashWithOpacity == false ? 1 : showingPreviewLightBox == 0 ? 0.8 : 0,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();
		var arrPageScroll = ___getPageScroll();
		$('#prodItem-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			}).show();
		$('#prodItem-overlay, #prodItem-lightbox-back').click(function() {
				hideProdLightBox();
				return false;
			});
		$('#prodItem-lightbox .lightbox-loading-link, #prodItem-lightbox .lightbox-secNav-btnClose').click(function() {
				hideProdLightBox();
				return false;
			});
		setupWindowResize();
	}
	
	
	
	
	
	
	
	function showBasic(wId){
		$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="jquery-lightbox-back"></div><div class="lightbox-secNav"><a href="#" onClick="closeBasic(); return false;" class="lightbox-secNav-btnClose"></a></div><div class="lightbox-container-image-box"><div class="lightbox-container-image"><div class="lightbox-content"></div><div class="lightbox-loading"><a href="#" onClick="closeBasic(); return false;" class="lightbox-loading-link"></a></div></div></div></div>');
		
		var arrPageSizes = ___getPageSize();
		$('#jquery-overlay').css({
				backgroundColor:	"#000",
				opacity:			flashWithOpacity == false ? 1 : 0.8,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();
		var arrPageScroll = ___getPageScroll();
		$('#jquery-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			}).show();
		$('#jquery-overlay, #jquery-lightbox-back').click(function() {
				hidePreviewLightBox(0);
				return false;
			});
		$('#jquery-lightbox .lightbox-loading-link, #jquery-lightbox .lightbox-secNav-btnClose').click(function() {
				hidePreviewLightBox(0);
				return false;
			});
		setupWindowResize();
		
		$('#jquery-overlay').fadeIn();
		$('select').css({ 'visibility' : 'hidden' });

		baseLoc = document.location.href;
		if (baseLoc.indexOf("#") > -1) {
			baseLoc = baseLoc.substring(baseLoc.indexOf('/sa/'), baseLoc.indexOf("#"));
		}
		urchinTracker(baseLoc + "/tech_spec/" + wId);
		
		theData = '<div id="productPreview"></div>';
		$("#jquery-lightbox .lightbox-content").html(theData);
		
		$("#jquery-lightbox").show();
		$(".lightbox-loading").hide();
		
		width = 888;
		height = 507;
				$("#jquery-lightbox .lightbox-container-image-box").css({width: (width+16)+'px', height: (height+16)+'px'});
				$("#jquery-lightbox .lightbox-content").css({width: width+"px", height: height+"px"});
				$("#jquery-lightbox .lightbox-secNav").css({width: ((width/2)+26)+"px"});
		
		var flashvars = {};
		var params = { wmode: "transparent", base: "/sa/" };
		swfobject.embedSWF("/sa/swf_gl/swf_loader.swf?lang=en&file="+wId+"", "productPreview", 888, 507, "9.0.115", "/sa/swf_gl/expressInstall.swf", flashvars, params);
	}
	
	function closeBasic(){
		$('#jquery-lightbox').remove();
		$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
	}
	
	
	
	
	var showingPreviewLightBox = 0;
	var previewLightBoxOnTop = 0;
	
	function openPreview(wId){
		baseLoc = document.location.href;
		if (baseLoc.indexOf("#") > -1) {
			baseLoc = baseLoc.substring(baseLoc.indexOf('/sa/'), baseLoc.indexOf("#"));
		}
		document.location.href = baseLoc + "#preview_" + wId;
	}
	
	function showPreview(wId){
		showingPreviewLightBox = wId;
		
		setupPreviewLightBox();
		
		$('#jquery-overlay').fadeIn();
		//$('embed, object, select').css({ 'visibility' : 'hidden' });
		$('select').css({ 'visibility' : 'hidden' });

		baseLoc = document.location.href;
		if (baseLoc.indexOf("#") > -1) {
			baseLoc = baseLoc.substring(baseLoc.indexOf('/sa/'), baseLoc.indexOf("#"));
		}
		urchinTracker(baseLoc + "/preview/" + wId);
		
		theData = '<div id="productPreview"></div>';
		
		$("#jquery-lightbox .lightbox-content").html(theData);
		
		var query = "id=" + wId;
		$("#jquery-lightbox .lightbox-loading").show();
		$.get("/sa/common_gl/inc_product_preview_info.php", query, function(data){
			$("#jquery-lightbox .lightbox-loading").hide();
		
			var width = 888;
			var height = 534;
			var doResize = 0;
			if (data == "1") {
				width = 888;
				height = 599;
				doResize = 1;
			}
			if (doResize == 1) {
				$("#jquery-lightbox .lightbox-container-image-box").css({width: (width+16)+'px', height: (height+16)+'px'});
				$("#jquery-lightbox .lightbox-content").css({width: width+"px", height: height+"px"});
				$("#jquery-lightbox .lightbox-secNav").css({width: ((width/2)+26)+"px"});
			}
			var flashvars = {};
			var params = { wmode: "transparent", base: "/sa/" };
			swfobject.embedSWF("/sa/swf_gl/swf_loader.swf?lang=en&file=files_prod/preview_"+(wId == 'ai' && currLang == 'ja' ? 'ai_'+currLang : wId)+".swf", "productPreview", width, height, "9.0.115", "/sa/swf_gl/expressInstall.swf", flashvars, params);
		});
		
		if (prodLightBoxURL != "") {
			$('#jquery-overlay').css({ 'z-index' : 250 });
			$('#jquery-lightbox').css({ 'z-index' : 300 });
		}
	}
	
	function hidePreviewLightBox(){
		if (prodLightBoxURL != "") {
			document.location = prodLightBoxURL;
		} else {
			baseLoc = document.location.href;
			if (baseLoc.indexOf("#") > -1) {
				baseLoc = baseLoc.substring(0, baseLoc.indexOf("#"));
			}
			document.location = "#_" + selStylesString;
		}
	}
	
	function destroyPreviewLightBox(doFade){
		showingPreviewLightBox = 0;
		
		$('#jquery-lightbox').remove();
		if (doFade == 1) { 
			$('#jquery-overlay').fadeOut(function() { $('#jquery-overlay').remove(); });
		} else {
			$('#jquery-overlay').remove();
		}
		//$('embed, object, select').css({ 'visibility' : 'visible' });
		$('select').css({ 'visibility' : 'visible' });
	}
	
	function setupPreviewLightBox(){
		$('body').append('<div id="jquery-overlay"></div><div id="jquery-lightbox"><div id="jquery-lightbox-back"></div><div class="lightbox-secNav"><a href="#" class="lightbox-secNav-btnClose"></a></div><div class="lightbox-container-image-box"><div class="lightbox-container-image"><div class="lightbox-content"></div><div class="lightbox-loading"><a href="#" class="lightbox-loading-link"></a></div></div></div></div>');
		
		var arrPageSizes = ___getPageSize();
		$('#jquery-overlay').css({
				backgroundColor:	"#000",
				opacity:			flashWithOpacity == false ? 1 : 0.8,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();
		var arrPageScroll = ___getPageScroll();
		$('#jquery-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
			}).show();
		$('#jquery-overlay, #jquery-lightbox-back').click(function() {
				hidePreviewLightBox(0);
				return false;
			});
		$('#jquery-lightbox .lightbox-loading-link, #jquery-lightbox .lightbox-secNav-btnClose').click(function() {
				hidePreviewLightBox(0);
				return false;
			});
		setupWindowResize();
	}
	
	function setupWindowResize(){
		$(window).resize(function() {
				// Get page sizes
				var arrPageSizes = ___getPageSize();
				// Style overlay and show it
				$('#prodItem-overlay, #jquery-overlay, #pSpecBig-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				// Get page scroll
				var arrPageScroll = ___getPageScroll();
				// Calculate top and left offset for the jquery-lightbox div object and show it
				$('#prodItem-lightbox, #jquery-lightbox').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
				$('#pSpecBig-lightbox').css({
						top:	arrPageScroll[1] + Math.round(arrPageSizes[3] / 30),
						left:	arrPageScroll[0]
					});
				$('#jquery-blank-anchor').css({
						top: arrPageScroll[1]
					})
			});
		
		var arrPageScroll = ___getPageScroll();
		$('#jquery-blank-anchor').css({
				top: arrPageScroll[1]
			})
	}

