//init variables
var $j = $;
var count = 1;
var success = false;
var arrowTotal = 0;
var currArrow = 0;
var leftArrow = false;
var rightArrow = true;
var shoeCount = 0;
/*var large = true;
var grid = false;*/
var large = false;
var grid = true;
var hash = '';
var imgCount = 1;
var largeDisp = 3;
var smDisp = 5;
var qOpen = false;
var thWidth = 451;
var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
var noFixed = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
var IE = (/MSIE ((5\.5)|6|7|8)/.test(navigator.userAgent) && navigator.platform == "Win32");
var flag = false;
var zoomImg = '';
var slideStarted = false;
var imgLoadCount = smDisp * 5;
var sorted = false;
if (large == true) { 
	if (grid) { var imgLoadCount = largeDisp * 5; }
	else { var imgLoadCount = largeDisp * 2; }
} else {
	if (grid) { var imgLoadCount = smDisp * 5; }
	else { var imgLoadCount = smDisp * 2; }
}

function URLDecode(psEncodeString) 
{
	var lsRegExp = /\+/g;
  	return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
}

function IEminWidth() {
	var winwidth = $j(window).width();
	if (winwidth < 1060) {
		$j('body').css('width', '1060px');
	} else {
		$j('body').css('width', '100%');	
	}
}

function loadImage(image_loc, imgHolder) {
	if (imgHolder == '#ql-image') { var holderTst = false; } else { var holderTst = true; }
	if (badBrowser) { $j('#loading').hide(); }
	if (badBrowser && holderTst) {
		if (large) {
			var img = '<img src="/App_Themes/Default/2010/images/pixel.gif" style="width: 283px; height: 191px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + image_loc + '\', sizingMethod=\'scale\')" />';
		} else {
			var img = '<img src="/App_Themes/Default/2010/images/pixel.gif" style="width: 178px; height: 120px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + image_loc + '\', sizingMethod=\'scale\')" />';
		}
		$j(imgHolder).children('img').remove();
		$j(imgHolder).children('div.loading-shoe').remove();
		$j(imgHolder).prepend(img);		
	} else {
		if (image_loc.indexOf('1th') > -1) { checkSize = false; } else { checkSize = true; }
		var img = new Image();
		$j(img).load(function() {
			if (imgHolder == '#ql-image') {
				zoomImg = image_loc;
				var width = img.width;
				var height = img.height;
				var ratio = thWidth / height;
				width = Math.floor(width * ratio) + 'px';
				height = Math.floor(height * ratio) + 'px';
				$j(img).addClass('ql-main');
			}
			$j(img).hide();
			$j(imgHolder).removeClass('loading').children('img').remove();
			$j(imgHolder).prepend(img);
			if (IE) {
				$j(img).css({'width': width, 'height':height}).show();
			} else {
				$j(img).css({'width': width, 'height':height}).fadeIn('slow');
			}
			if (slideStarted) {
				var tstLoc = $j('#ql-views a:first').attr('alt');
				var tstPos = image_loc.indexOf(tstLoc);
				if (tstPos < 0) {
					$j('#ql-views a.active').removeClass('active').next().addClass('active');
				} else {
					$j('#ql-views a.active').removeClass('active');
					$j('#ql-views a:first').addClass('active');
				}
			}
		}).error(function() {
			if (slideStarted) {
				var tstLoc = $j('#ql-views a:first').attr('alt');
				var tstPos = image_loc.indexOf(tstLoc);
				if (tstPos < 0) {
					$j('#ql-views a.active').next().remove(); // image does not exist - remove element
				} else {
					$j('#ql-views a:first').remove(); // img does not exist - remove element
				}
			} else {
				if ($j(imgHolder).attr('class') == 'shoe-holder') {
					$j(imgHolder).remove();	
				}
			}
		}).attr('src', image_loc);
	}
}

function sizeScroll() {
	$j('#pages').fadeOut();
	$j('.shoe-holder').show();
	$j('#left-arrow').fadeOut('slow');
	$j('#content-main').removeAttr('style');
	if (large) { if (imgCount > largeDisp) { $j('#right-arrow').fadeIn('slow'); } }
	else { if (imgCount > smDisp) { $j('#right-arrow').fadeIn('slow'); /* IE ONLY */ } }
	
	$j('#view-holder').css({"width" : "920px"});
	shoeCount = parseInt($j('.shoe-holder').size());
	if (large == true) {
		arrowTotal = parseInt(shoeCount / largeDisp);
		var numTst = largeDisp * arrowTotal;
		numTst = shoeCount - numTst;
		if (numTst < 1) { arrowTotal = arrowTotal - 1;	}
		var viewWidth = shoeCount * 322 + 10;
		viewWidth = viewWidth + 'px';	
	} else {
		arrowTotal = parseInt(shoeCount / smDisp);
		var numTst = smDisp * arrowTotal;
		numTst = shoeCount - numTst;
		if (numTst < 1) { arrowTotal = arrowTotal - 1;	}
		var viewWidth = shoeCount * 193 + 10;
		viewWidth = viewWidth + 'px';
	}
	$j('#view-holder').width(viewWidth);
	leftArrow = false;
	currArrow = 0;
	if (large) { imgLoadCount = largeDisp * 2; } else { imgLoadcount = smDisp * 2; }
}

function showImages(nValue, holder) {
	sorted = false;
	imgCount = 0;
	if (imgCount == 0) {
		$j('#no-items').fadeOut(0);	
	}
	$j.ajax({
		 url: "/xml/results.aspx",
  		 type: "GET",
 		 data: nValue,
//		 dataType: "xml",
		success: function(xml) {
			if (!badBrowser) { $j('#loading').fadeOut(0); }
//			$j('#views').removeClass('loading');
			$j(holder).html('');
			var resHtml = '';
			$j(xml).find('product').each(function(){
				var prodId = $j(this).find('id').text();
				var prodName = $j(this).find('title').text();
				var prodPrice = parseInt($j(this).find('displayPrice').text());
				var salePrice = parseInt($j(this).find('salePrice').text());
				if ((salePrice > 0) && (salePrice < prodPrice)) { prodPrice = '<s><span class="orig">$' + prodPrice + '</span></s>&nbsp;$' + salePrice; }
				else { prodPrice = '$' + prodPrice; }
				$j(this).find('thumbnail').each(function(){
					imgCount += 1;
					var imgLoc = $j(this).text();
					var shoeClass = 'shoe-holder';
					if (large == false) { shoeClass += ' s-thumbs'; }
					if (imgCount > imgLoadCount) {
						shoeClass += ' loading'
					} else {
						shoeClass += ' ready'
					}
					if (imgCount <= imgLoadCount) {
						resHtml += '<div id="' + prodId + '" class="' + shoeClass + '"><div class="loading-shoe" title="" alt="' + imgLoc + '"></div><div class="text-holder" align="center"><p><a href="#">' + prodName + '</a> <span class="price">| ' + prodPrice + '</span></p></div></div>';
					} else {
						resHtml += '<div id="' + prodId + '" class="' + shoeClass + '"><div class="loading-shoe" title="" alt="' + imgLoc + '"></div><div class="text-holder" align="center"><p><a href="#">' + prodName + '</a> <span class="price">| ' + prodPrice + '</span></p></div></div>';
					}
				});
			});
			if (imgCount == 0) {
				$j('#no-results').fadeIn(400);
				$j('#loading').fadeOut(0);
			}
			$j('#views').scrollTo( '0px', 0 );
			$j('.text-holder p, .text-holder img').fadeTo(0,0);
			/* ADD ITEMS TO DOM */
			$j(holder).html(resHtml).fadeIn(0, function() {
				if (grid == true) {
					sizeGrid();	
				} else {
					sizeScroll();
				}
				var started = false;
				$j(this).find('div.ready').each(function() {
					var prodId = $j(this).attr('id');
					var imgLoc= '/flashproductimages/' + prodId + '/thumbnail_large.png';
					var imgHolder = '#' + prodId;
					$j(this).children('.loading-shoe').each(function() {
						if (badBrowser) {
							var img = new Image();
							$j(img).load(function() {
								$j('#loading').hide();
								if (large) {
									var imgHtml = '<img src="App_Themes/Default/2010/images/pixel.gif" style="width: 283px; height: 191px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgLoc + '\', sizingMethod=\'scale\')" />';
								} else {
									var imgHtml = '<img src="App_Themes/Default/2010/images/pixel.gif" style="width: 178px; height: 120px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgLoc + '\', sizingMethod=\'scale\')" />';
								}
								$j(imgHolder).children('.loading-shoe').remove();	
								$j(imgHolder).removeClass('loading').prepend(imgHtml);
							}).attr('src', imgLoc);
						} else {
							var img = new Image();
							$j(img).load(function(esponse, status, xhr) {
								$j(imgHolder).children('.loading-shoe').remove();	
								$j(imgHolder).removeClass('loading').prepend(img);
							}).attr('src', imgLoc);
						}		   
					});
				});
			});
		},
		error:function (xhr, ajaxOptions, thrownError, xml){
			alert(xhr.status);
			alert(thrownError);
		}
	});
}

function gallUpdate(nVal) {
	$j('#right-arrow, #left-arrow').fadeOut();
	$j('#no-results').hide();
	if (nVal.length > 3) {
		if (nVal == '#/women') {
			$j('#sort-options, #shoes, #right-arrow, #left-arrow').fadeOut(0);
			$j('#view-holder').html('');
			$j('#lp-content').fadeIn(400);
			var hashPos = nVal.indexOf('#') + 1;
			hash = location.hash;
		} else {
			if (badBrowser) { $j('#loading').show(); } else { $j('#loading').fadeIn(100); }
			$j('#sort-options, #shoes').fadeIn(75);
			$j('#lp-content').fadeOut(200, function() {
				$j('#content-main').removeClass('landing').removeClass('lp-women');
	//			$j('#views').addClass('loading');
				$j('#view-holder').html('');
				nVal = nVal.replace('/sale/women', '4294965982+6207');
				nVal = nVal.replace('/sale/dress', '4294965982+6207+4294961804');
				nVal = nVal.replace('/sale/casual', '4294965982+6207+4294961803');
				nVal = nVal.replace('/sale/sandals', '4294965982+6207+4294967138');
				nVal = nVal.replace('/sale/boots', '4294965982+6207+4294967251');
				nVal = nVal.replace('/sale/men', '4294965982+6208');
				nVal = nVal.replace('/sale/outerwear', '4294965982+6211+4294962087');
				nVal = nVal.replace('/sale/handbags', '4294965982+6270');
				nVal = nVal.replace('/sale/belts', '4294965982+6211+4294962473');
				nVal = nVal.replace('/sale/eyewear', '4294965982+6211+4294962088');
				nVal = nVal.replace('/women/dress', '6207+4294961804');
				nVal = nVal.replace('/women/casual', '6207+4294961803');
				nVal = nVal.replace('/women/sandals', '6207+4294967138');
				nVal = nVal.replace('/women/boots', '6207+4294967251');
				nVal = nVal.replace('/women/all', '6207');
				nVal = nVal.replace('/women', '6207');
				nVal = nVal.replace('/men', '6208');
				nVal = nVal.replace('/eyewear', '6211+4294962088');
				nVal = nVal.replace('/cold', '6211+4294966178');
				nVal = nVal.replace('/belts', '6211+4294962473');
				nVal = nVal.replace('/outerwear', '6211+4294962087');
				nVal = nVal.replace('/handbags', '6270');
				var hashPos = nVal.indexOf('#') + 1;
				nVal = nVal.substring(hashPos);
				if (nVal.indexOf('key=') > -1) {
					nVal = nVal.replace('key', 'Ntt');
					nVal = nVal.replace('#', '');
					ampPos = nVal.indexOf('&');
					if (ampPos > -1) { nVal = nVal.substring(0, ampPos); }
					showImages('N=0&Ns=pName|0&Ntk=SearchInterface&' + nVal + '&Ntx=mode%2bmatchallany&nodefault=1&Ns=pName|0', '#view-holder');
					var keyTst = $j('#search-key').html();
					if (keyTst == '') {
						var kw = nVal.replace('Ntt=', '');
						kw = URLDecode(kw);
						$j('#search-key').show().html('<span class="search-title">Keyword: </span>' + kw);
					}
				}
				else if (nVal.indexOf('N=') < 0) {
					showImages('N=' + nVal + '&nodefault=1&Ns=pName|0', '#view-holder');
				} else if (nVal != '#/women') {
					showImages(nVal + '&nodefault=1&Ns=pName|0', '#view-holder');	
				}
				hash = location.hash;			
			});
		}

	}


if (nVal == '/women') {
		$j('#sort-options, #shoes').fadeOut(300);
	}

	
}
$j('#pageContainer').html('<h1>this is new content');
$j(document).ready(function(){
	// IE MIN WIDTH FIX
	if(badBrowser) {
		IEminWidth();
		$j(window).resize(function() {
		  IEminWidth();
		}); 
	}
	
	var winLoc = window.location.href;
	if (winLoc.indexOf('shop') > -1) {
		/* $j('#sitewide').fadeIn(); */
		if ( location.hash == '' ) {
			showImages('N=6207&nodefault=1&Ns=pName|0', '#view-holder');
			$j('.category').text('Women');
			$j('.sub-cat').text('');
		}
	}
	$j('#content-nav').hide(); //IE Fix
	$j('#close-sitewide').click(function () { //close sitewide banner
		$j('#sitewide').fadeOut(300);
		return false;
	});
	$j('a.arrow').click(function() {
		var thisId = $j(this).parent().attr('id');
		var scrollVal = parseInt($j('.shoe-holder:lt(1)').width());
		scrollVal = scrollVal + parseInt($j('.shoe-holder:lt(1)').css('marginLeft'));
		if (large == true) { scrollVal = scrollVal * largeDisp; } else { scrollVal = scrollVal * smDisp; }
		if (thisId == 'right-arrow') {
			scrollVal = '+=' + scrollVal + 'px';
			$j('#views').scrollTo(scrollVal, 600, function() { 
				if (large == true) { var updateNum = largeDisp; } else { var updateNum = smDisp; }
				var updateSel = '#view-holder .loading:lt(' + updateNum + ')';
				$j(updateSel).each(function() {
					var prodId = $j(this).attr('id');
					var imgLoc= '/flashproductimages/' + prodId + '/thumbnail_large.png';
					var imgHolder = '#' + prodId;
					if (badBrowser) {
						$j(imgHolder).children('.loading-shoe').remove();
						if (large) {
							var img = '<img src="App_Themes/Default/2010/images/pixel.gif" style="width: 283px; height: 191px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgLoc + '\', sizingMethod=\'scale\')" />';
						} else {
							var img = '<img src="App_Themes/Default/2010/images/pixel.gif" style="width: 178px; height: 120px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + imgLoc + '\', sizingMethod=\'scale\')" />';
						}
						$j(imgHolder).removeClass('loading').prepend(img);
					} else {
						$j(this).children('.loading-shoe').remove();
						$j(this).removeClass('loading').prepend('<img src="' + imgLoc + '" />');
					}
				});
			});
			currArrow = currArrow + 1;
			if (currArrow >= arrowTotal) { $j('#right-arrow').fadeOut(); rightArrow = false; }
			if (leftArrow == false) { $j('#left-arrow').fadeIn(); leftArrow = true; }
		} else if (thisId == 'left-arrow') {
			scrollVal = '-=' + scrollVal + 'px';
			$j('#views').scrollTo(scrollVal, 600);
			currArrow = currArrow - 1;
			if (currArrow <= 0) { $j('#left-arrow').fadeOut(); leftArrow = false; }
			if (rightArrow == false) { $j('#right-arrow').fadeIn(); rightArrow = true; }
		}
		return false;
	});
	
	$j('.shoe-holder') 
    .livequery(function(){ 
		if(jQuery.browser.msie) {
			var thisTxt = $j('p', this).text();
			$j('p', this).animate({ opacity : 1 }, 1, function() {
				this.style.removeAttribute("filter");
			});
			$j(this)
			.hoverIntent(function() { 
				document.body.style.cursor = 'hand';
				$j('p', this).animate({
					marginTop: '12px'
				}, 400 );				
			}, function() { 
				document.body.style.cursor = 'default';
				$j('p', this).animate({ marginTop: 12 }, 400 ).animate({ 
					marginTop: '-28px'
				}, 400 );
			});
		}
		else {
			$j(this)
				.hoverIntent(function() { 
					$j('p', this).animate({
						opacity: 1.0,
						marginTop: '12px'
					}, 400 );				
				}, function() { 
					$j('p', this).animate({ opacity: 1 }, 400 ).animate({ 
						opacity: 0,
						marginTop: '-28px'
					}, 400 );
			});
		}
	}, function() { 
        // unbind the mouseover and mouseout events 
        $j(this) 
            .unbind('mouseover') 
            .unbind('mouseout'); 
    });
	$j(function(){
	 	$j(window).bind( 'hashchange', function(){
			if (location.hash == '#') {
				return false;
			}
			else if (hash != location.hash) {
				$j('#navmain').animate({opacity: 0, height: '0px'}, 50, function() {
					$j(this).hide();	
				});
				$j('#pages').fadeOut();
				if (location.hash.indexOf('key=') < 0) {
					$j('#search-key').hide().html('<span class="search-title">Keyword: </span>');
				}
				/* ADD CATEGORY TITLE */
				
				/* INIT CAT VARIABLES */
				var category = '';
				var subCat = '';
				
				var tempHash = location.hash;
				var tempHash = tempHash.replace('#/', '');
				if (tempHash.indexOf('key') < 0) {
					var pos = tempHash.indexOf('/');
					category = tempHash.substring(0, pos);
					pos = pos + 1;
					subCat = tempHash.substring(pos);
					subCat = subCat.replace('#', '');
					ampPos = subCat.indexOf('&');
					if (ampPos > -1) { subCat = subCat.substring(0, ampPos); }
					if ((category != '') && (category != '#')) {
						if (category.indexOf('N=') < 0) {
							$j('.category').text(category);
						}
						if (subCat.indexOf('N=') < 0) {
							$j('.sub-cat').text(' | ' + subCat);
						}
					} else if ((category != '#') && (subCat != '#')) {
						if (subCat.indexOf('N=') < 0) {
							$j('.category').text(subCat);
						}
						$j('.sub-cat').text('');	
					}
				} else {
					$j('.category, .sub-cat').text('');	
				}
				gallUpdate( location.hash );
			}
		});
	});

});