var zI=2,autoPlay,$Pp,$Ppl,$Ppw,step1=[],step2=[],dist;
$.fn.buildGWPs=function(){
	// Build out images and copy.
	this.each(function(i){
		var fullPath=(this.linkPath.substr(0,4)=='http'||this.linkPath.substr(0,4)=='java')?this.linkPath:(this.linkPath.search('/')==-1)?getStoreURL()+'c/'+this.linkPath.substr(this.linkPath.length-7)+'/'+this.linkPath+'?origin=beauty_gwp':getStoreURL()+'s/'+this.linkPath+'?origin=beauty_gwp';
		$('#displayArea').append('<div><p>'+this.descCopy+'<br /><a href="'+fullPath+'" name="'+this.altText+'">'+this.linkCopy+'</a></p><p style="margin-top:15px; color:#666666;">'+this.discCopy+'</p><a href="'+fullPath+'" name="'+this.altText+'" class="prodLink">'+this.altText+'</a></div>')
		.children(':last').css({backgroundImage:'url('+getImageURL()+'store/boutique/beauty_gwp/'+this.folderDate+'/images/'+this.imageName+'.jpg)',opacity:0,visibility:'visible'});
		$('#products').append('<p><img src="'+getImageURL()+'store/boutique/beauty_gwp/'+this.folderDate+'/images/'+this.imageName+'.gif" /><br />'+this.altText+'</p>')
		.children(':last').css({opacity:0.7,left:i*($('#products p').width()+1)});
	});
	$Pp=$('#products p'); $Ppl=$Pp.length; $Ppw=$Pp.width()+1;
	
	// Setup navigation
	if($Ppl>1){
		$('#displayArea div p a').after(' &#124; <a href="javascript:void(0)" name="Next Gift">NEXT GIFT</a>');
	}
	if($Ppl>7){
		$Pp.filter(':last').css({left:-$Ppw});
		$('#productNav')
		.prepend('<a href="javascript:void(0)" id="back" name="Previous">Previous</a>')
		.append('<a href="javascript:void(0)" id="next" name="Next">Next</a>')
		.children('a').css({opacity:0.5});
		$('#botNav').append('<p>Viewing <span></span>-<span></span> of '+$Ppl+'</p>');
	}else if($Ppl<7&&$Ppl>1){
		$Pp.each(function(){
			$(this).css({left:parseFloat($(this).css('left'))+($('#products').width()-($Ppw*$Ppl))/2});
		});
	}
	
	// Setup events
	$('#displayArea div').bind('displayProduct',function(){
		$('#displayArea div').stop(true);
		cmCreatePageviewTag('Beauty GWP > '+$('> a',this).attr('name'), null,'0~2377897~2377898~6010097');
		$(this).css({zIndex:zI}).animate({opacity:1},750); zI++;
		$('#displayArea div').not(this).animate({opacity:0},500);
	});
	$('#displayArea div p a:odd').bind('click',function(){
		nextProduct();
		clearTimeout(autoPlay);
	});
	$('#productNav a').bind('mouseover',function(){
		$(this).stop(true).animate({opacity:1},500);
	}).bind('mouseout',function(){
		$(this).stop(true).animate({opacity:0.5},500);
	}).filter(':first').bind('click',function(){ // Left arrow, items move right.
		if(!$Pp.is(':animated')){
			clearTimeout(autoPlay);
			var amt=-((parseFloat($Pp.eq($Ppl-1).css('left'))-(($Ppl-1)*$Ppw)))/$Ppw;
			amt=(amt==0||amt>7)?7:amt;
			$Pp.trigger({
				type:'shift',
				amt:amt
			});
		}
	}).end()
	.filter(':last').bind('click',function(){ // Right arrow, items move left.
		if(!$Pp.is(':animated')){
			clearTimeout(autoPlay);
			var amt=-parseFloat($Pp.eq(0).css('left'))/$Ppw;
			amt=(amt>=0||amt<-7)?-7:amt;
			var end=-((parseFloat($Pp.eq($Ppl-1).css('left'))/$Ppw)-6);
			end=(end==7)?-($Ppl-7):end;
			amt=(end<0&&end>amt)?end:amt;
			$Pp.trigger({
				type:'shift',
				amt:amt
			});
		}
	});
	$Pp.bind('hoverProduct',function(){
		$Pp.filter('.active').removeClass('active').end().not($Pp.filter('.selected')).css({opacity:0.7});
		$(this).addClass('active').css({opacity:1});
	}).bind('mouseover',function(){
		$(this).trigger('hoverProduct');
	}).bind('selectProduct',function(){
		$Pp.not(this).removeClass('selected');
		$(this).trigger('hoverProduct').addClass('selected');
		$('#displayArea div').eq($Pp.index(this)).trigger('displayProduct');
	}).bind('click',function(){
		clearTimeout(autoPlay);
		$(this).trigger('selectProduct');
		cmCreateManualLinkClickTag(getStoreURL()+'c/6010097/0~2377897~2377898~6010097',$(this).text());
	}).bind('shift',function(event){
		var steps,i=$Pp.index(this);
		dist=$Ppw*event.amt;
		if(event.amt>0){ // Moving to the right
			if(dist<=($Ppw*($Ppl-1))-parseFloat($(this).css('left'))){
				// Doesn't need to wrap
				if(parseFloat($(this).css('left'))>=$('#products').width()){
					steps=function(){
						$(this).animate({left:'+='+dist},1);
					}
				}else{
					steps=function(){
						$(this).animate({left:'+='+dist},1000);
					}
				}
			}else{
				// Does need to wrap
				step1[i]=($Ppw*($Ppl-1))-parseFloat($(this).css('left'));
				step2[i]=dist-step1[i];
				if(step2[i]==0&&parseFloat($(this).css('left'))>=$('#products').width()){
					steps=function(){
						$(this).css({left:-$Ppw});
					}
				}else if(step1[i]==dist){
					steps=function(){
						$(this).css({left:-$Ppw})
						.animate({left:'+='+step1[$Pp.index(this)]},1000);
					}
				}else if(step2[i]!=0){
					steps=function(){
						$(this).animate({left:'+='+step1[$Pp.index(this)]},(step1[$Pp.index(this)]/dist)*1000,function(){
							$(this).css({left:-$Ppw})
							.animate({left:'+='+step2[$Pp.index(this)]},(step2[$Pp.index(this)]/dist)*1000);
						});
					}
				}else{
					steps=function(){
						$(this).animate({left:'+='+step1[$Pp.index(this)]},(step1[$Pp.index(this)]/dist)*1000,function(){
							$(this).css({left:-$Ppw});
						});
					}
				}
			}
		}else{ // Moving to the left
			dist*=-1;
			if(dist<=parseFloat($(this).css('left'))+$Ppw){
				// Doesn't need to wrap
				if(parseFloat($(this).css('left'))-dist>=$('#products').width()){
					steps=function(){
						$(this).animate({left:'-='+dist},1);
					}
				}else{
					steps=function(){
						$(this).animate({left:'-='+dist},1000);
					}
				}
			}else{
				// Does need to wrap
				step1[i]=parseFloat($(this).css('left'))+$Ppw;
				step2[i]=dist-step1[i];
				if(step1[i]==0&&($Ppw*($Ppl-1))-dist>=$('#products').width()){
					steps=function(){
						$(this).css({left:($Ppw*($Ppl-1))-dist});
					}
				}else if(step1[i]==dist){ // Wrap before animating
					steps=function(){
						$(this).css({left:$Ppw*($Ppl-1)})
						.animate({left:'-='+step1[$Pp.index(this)]},1000);
					}
				}else if(step2[i]!=0){ // Wrap mid-animation
					steps=function(){
						$(this).animate({left:'-='+step1[$Pp.index(this)]},(step1[$Pp.index(this)]/dist)*1000,function(){
							$(this).css({left:$Ppw*($Ppl-1)})
							.animate({left:'-='+step2[$Pp.index(this)]},(step2[$Pp.index(this)]/dist)*1000);
						});
					}
				}else{
					steps=function(){
						$(this).animate({left:'-='+step1[$Pp.index(this)]},(step1[$Pp.index(this)]/dist)*1000,function(){
							$(this).css({left:$Ppw*($Ppl-1)});
						});
					}
				}
			}
		}
		$(this).bind('orders',steps);
		if($Pp.index(this)==$Ppl-1){
			// Initiate move commands
			var n=Math.ceil(($Ppl-1)/2);
			$($Pp.filter(':gt('+(n-1)+')').get().reverse()).trigger('orders').unbind('orders');
			$Pp.filter(':lt('+n+')').trigger('orders').unbind('orders');
			// Update page navigation
			setTimeout(updatePageNav,1000);
		}
	});
	$('#products').bind('deActivate',function(){
		$Pp.filter('.selected').trigger('hoverProduct');
	}).bind('mouseout',function(){
		$(this).trigger('deActivate');
	});
	
	// Load product images
	var $imageToLoad=$('#products img');
	var loadedImage=[];
	$imageToLoad.each(function(i, val){
		$(this).bind('load readystatechange',function(){
			if($.inArray(i, loadedImage)==-1){
				loadedImage.push(val);
				$imageToLoad=$.grep($imageToLoad, function(n,i){
					return n!=val;
				});
				if($imageToLoad.length==0){
					updatePageNav();
					$('#displayArea').animate({opacity:0},1000,function(){
						$('#displayArea').css({background:'none',opacity:1});
					});
					$('#topNav,#botNav p,#productNav,#productNav a').css({opacity:0,visibility:'visible'}).animate({opacity:1},1000);
					autoPlay=setTimeout(function(){
						$('#products').css({opacity:0,visibility:'visible'}).animate({opacity:1},1000,function(){
							if($('.selected').length==0){
								autoRun();
							}
						});
					},500);
				}
			}
		}).each(function(i, val){
			if(this.complete||this.complete===undefined){ 
				this.src=this.src; 
			} 
		});
	});
}
function isInteger(s){
	return parseInt(s,10)===s;
}
function updatePageNav(){
	if(!$Pp.is(':animated')){
		$Pp.each(function(){
			if(parseFloat($(this).css('left'))==0){
				$('#botNav p span:first').text($Pp.index(this)+1);
			}
			if(parseFloat($(this).css('left'))==$Ppw*6){
				$('#botNav p span:last').text($Pp.index(this)+1);
			}
		});
	}else{
		setTimeout(updatePageNav,10);
	}
}
function nextProduct(){
	if($Ppl>7){
		if($Pp.eq($Ppl-1).is('.selected')){
			$Pp.trigger({
				type:'shift',
				amt:'-7'
			});
		}else if(parseFloat($Pp.filter('.selected').css('left'))==$Ppw*6){
			$Pp.trigger({
				type:'shift',
				amt:'-1'
			});
		}
	}
	if($Pp.eq($Ppl-1).is('.selected')){
		$Pp.filter(':first').trigger('selectProduct');
	}else{
		$Pp.filter('.selected').next().trigger('selectProduct');
	}
}
function autoNext(){
	clearTimeout(autoPlay);
	if(parseFloat($Pp.eq($Ppl-1).css('left'))!=$Ppw*6&&$Ppl>7){
		if(parseFloat($Pp.filter('.selected').css('left'))==$Ppw*3){
			$Pp.trigger({
				type:'shift',
				amt:'-1'
			});
			nextProduct();
			autoPlay=setTimeout(autoNext,5000);
		}else{
			nextProduct();
			autoPlay=setTimeout(autoNext,4000);
		}
	}else{
		nextProduct();
		autoPlay=setTimeout(autoNext,4000);
	}
}
function autoRun(){
	clearTimeout(autoPlay);
	$Pp.filter(':first').trigger('selectProduct');
	autoPlay=setTimeout(autoNext,4000);
}
$(document).ready(function(){
	$('#displayArea').css({opacity:0,visibility:'visible'}).animate({opacity:1},1000);
	$(beautyGWPs).buildGWPs();
});
