

	$().ready(function(){
		setupRangeForm();
		setInterval("checkAnchor()", 300);
		$('body').append('<div id="jquery-blank-anchor"><a name="_"></a></div>');
		if ($.browser.msie && $.browser.version <= 6) {
			$('#rangeContent td.imgCell').hover(
				function(){
					$(this).addClass("sfhover");
				},
				function(){
					$(this).removeClass("sfhover");
				}
			);
		}
		if (siteVer >= 3) {
			initProductSort();
		}
	});
	
	function setupRangeForm(){
		$("#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("-");
				updateProductRange();
			});
	}
	
	function setSortBy(wSortBy){
		if (wSortBy.indexOf('country') > -1) {
			selCountry = wSortBy.substr(8, 2);
		} else {
			selSortBy = wSortBy;
		}
		updateProductRange();
	}
	function updateProductRange(){
		var query = "cid=" + selCatId + "&sid=" + seasonId + "&sortby=" + selSortBy + '&styles=' + selStyles + '&country=' + selCountry + '&basePage=' + basePage;
		
		$("#productRange .loading").show();
		$.get("/common_gl/inc_product_range.php", query, function(data){
			$("#rangeContent").html(data);
			$("#productRange .loading").hide();
			
			$("#sortOptions a").removeClass('sel');
			$("#sortOptions a."+selSortBy).addClass('sel');
			$("#sortOptions a.country-"+selCountry).addClass('sel');
			
			updateBackground();
		});
	}


	// ver 3 stuff
	function initProductSort(){
		$("#sortOptions li a").each(
			function(){
				if ($(this).hasClass('sel')) {
					$(this).removeClass('sel');
					$(this).parent().addClass('sel');
				}
				$(this).parent().addClass($(this).attr('class'));
			}
		);
		$("#sortOptions li, #styleOptions li").hover(
			function(){
				$(this).addClass('sfhover');
			},
			function(){
				$(this).removeClass('sfhover');
			}
		);
		fixLiWidthsSort('sortOptions');
		fixLiWidthsSort('styleOptions');
	}
	function fixLiWidthsSort(wId){
		var maxWidth = 0;
		$("#"+wId+" li, #"+wId+" h2").css({ 'width':'auto' })
		maxWidth = Math.max(maxWidth, $("#"+wId+" ul:first").innerWidth());
		$("#"+wId+" li ul li").each(
			function(){
				maxWidth = Math.max(maxWidth, $(this).innerWidth());
			}
		);
		$("#"+wId+" li ul li").css({
			'width':maxWidth
		});
		$("#"+wId+" h2").css({
			'width':maxWidth-38
		});
		if (wId == 'styleOptions') {
			var sortWidth = $("#sortOptions").outerWidth();
			$("#styleOptions").css({ 'right':sortWidth + 18 });
		}
	}
	function setSortByV3(wSortBy){
		if (wSortBy.indexOf('country') > -1) {
			selCountry = wSortBy.substr(8, 2);
		} else {
			selSortBy = wSortBy;
		}
		updateProductRangeV3();
	}
	function updateProductRangeV3(){
		var query = "cid=" + selCatId + "&sid=" + seasonId + "&sortby=" + selSortBy + '&styles=' + selStyles + '&country=' + selCountry + '&basePage=' + basePage;
		
		$("#productRange .loading").show();
		$.get("/common_gl/inc_product_range.php", query, function(data){
			$("#rangeContent").html(data);
			$("#productRange .loading").hide();
			
			$("#sortOptions h2").html($("#sortOptions li."+selSortBy+" a").html());
			$("#sortOptions li, #sortOptions a").removeClass('sel');
			$("#sortOptions li."+selSortBy).addClass('sel');
			$("#sortOptions li.country-"+selCountry+", #sortOptions a.country-"+selCountry).addClass('sel');
			
			updateBackground();
			fixLiWidthsSort('sortOptions');
			fixLiWidthsSort('styleOptions');
		});
	}
	
	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);
	}
	
	
	
	function setSortByMyStyles(wSortBy){
		if (wSortBy.indexOf('country') > -1) {
			selCountry = wSortBy.substr(8, 2);
		} else {
			selSortBy = wSortBy;
		}
		updateProductMyStylesRange();
	}
	function updateProductMyStylesRange(){
		var query = "sortby=" + selSortBy + '&basePage=' + basePage;
		
		$("#productRange .loading").show();
		$.get("/common_gl/inc_product_my-styles_range.php", query, function(data){
			$("#rangeContent").html(data);
			$("#productRange .loading").hide();
			
			$("#sortOptions a").removeClass('sel');
			$("#sortOptions a."+selSortBy).addClass('sel');
			//$("#sortOptions a.country-"+selCountry).addClass('sel');
			
			updateBackground();
		});
	}
	
	
	function setSortByMyStylesV3(wSortBy){
		if (wSortBy.indexOf('country') > -1) {
			selCountry = wSortBy.substr(8, 2);
		} else {
			selSortBy = wSortBy;
		}
		updateProductMyStylesRangeV3();
	}
	function updateProductMyStylesRangeV3(){
		var query = "sortby=" + selSortBy + '&basePage=' + basePage;
		
		$("#productRange .loading").show();
		$.get("/common_gl/inc_product_my-styles_range.php", query, function(data){
			$("#rangeContent").html(data);
			$("#productRange .loading").hide();
			
			$("#sortOptions h2").html($("#sortOptions li."+selSortBy+" a").html());
			$("#sortOptions li").removeClass('sel');
			$("#sortOptions li."+selSortBy).addClass('sel');
			
			updateBackground();
			fixLiWidthsSort('sortOptions');
		});
	}
	
	
	
	
	
	var currentAnchor = null;
	var showingProdLightBox = 0;
	
	function checkAnchor(){
		if(currentAnchor != document.location.hash){
			currentAnchor = document.location.hash;
			
			if (!currentAnchor || currentAnchor == "" || currentAnchor == "#_") {
				if (showingProdLightBox > 0) {
					destroyProdLightBox(1);
				}
				if (showingPreviewLightBox > 0) {
					destroyPreviewLightBox(1);
				}
			} 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 (showingProdLightBox != id) {
						
						if (showingProdLightBox > 0) {
							destroyProdLightBox(0);
						}
						
						showProdLightBox(id);
						
						baseLoc = document.location.href;
						if (baseLoc.indexOf("#") > -1) {
							baseLoc = baseLoc.substring(baseLoc.indexOf('/'), baseLoc.indexOf("#"));
						}
						urchinTracker(baseLoc + "/item/" + id + "/" + title);
						
						$.get("/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) {
						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('/'), 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 = "#_";
		}
		//destroyLightBox();
	}
	
	function destroyProdLightBox(doFade){
		showingProdLightBox = 0;
		
		$('#prodItem-lightbox').remove();
		if (doFade == 1) { 
			$('#prodItem-overlay').fadeOut(function() { $('#prodItem-overlay').remove(); });
		} else {
			$('#prodItem-overlay').remove();
		}
		//$('embed, object, select').css({ 'visibility' : 'visible' });
		$('#videoPanel').css({ 'visibility' : 'visible' }); // for Design a Bikini
		$('select').css({ 'visibility' : 'visible' });
	}
	
	function showProdLightBox(wId){
		showingProdLightBox = wId;
		setupProdLightBox();
		
		$('#prodItem-overlay').fadeIn();
		$('#videoPanel').css({ 'visibility' : 'hidden' }); // for Design a Bikini
		//$('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();
	}
	
	
	
	
	
	
	
	var showingPreviewLightBox = 0;
	
	function openPreview(wId){
		baseLoc = document.location.href;
		if (baseLoc.indexOf("#") > -1) {
			baseLoc = baseLoc.substring(baseLoc.indexOf('/'), 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('/'), 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("/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: "/" };
			swfobject.embedSWF("/swf_gl/swf_loader.swf?lang=en&file=files_prod/preview_"+wId+".swf", "productPreview", width, height, "9.0.115", "/swf_gl/expressInstall.swf", flashvars, params);
		});
	}
	
	function hidePreviewLightBox(){
		baseLoc = document.location.href;
		if (baseLoc.indexOf("#") > -1) {
			baseLoc = baseLoc.substring(0, baseLoc.indexOf("#"));
		}
		document.location = "#_";
	}
	
	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').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]
				});
				$('#jquery-blank-anchor').css({
						top: arrPageScroll[1]
					})
			});
		
		var arrPageScroll = ___getPageScroll();
		$('#jquery-blank-anchor').css({
				top: arrPageScroll[1]
			})
	}

