var swWtStartTime = new Date();
var swWtMax,swWtPadMax;
var swWtFirstItem=0;
var swWtScrollTimeStart;
var swWtScrollTimeSpeed=4000;
var swWtBoolMoving=false;
var swWtPauseElapse=0;
var animateLeft;

$(document).ready(function(){
	if($.browser.msie && (parseFloat($.browser.version)<7)) $("body").addClass("ie6");

//inject ticker
	$("body").append('<div id="swWinnersTicker"><img src="/shared/winnersTicker/images/swWt_arrow.png" width="124" height="56" id="swWtArrow" /><div id="swWtSubWrap"><div id="swWt_winners"><img src="/shared/winnersTicker/images/swWt_gradient.png" width="49" height="46" id="swWtGradient" /><ul></ul></div></div><a href="#" class="collapse"></a></div>')

//add padding to bottom so we dont obscure copy/legal/etc
	$("body").css("margin-bottom",(parseInt($("body").css("margin-bottom").replace("px",""))+ $('#swWinnersTicker').height())+ "px");

	swWtResize()
	$(window).resize(function(){
		swWtResize();
	});

	for(iCount=0;iCount<swWtArrWinners.length;iCount++) fBuildWinner(iCount);

//make the container as big as the total number of LIs
	$("#swWt_winners ul").css("width", (swWtArrWinners.length*(getMyWidth("#swWt_winners ul li")))+"px")

//bind click
	$('#swWinnersTicker a').click(function(e) {
		e.preventDefault();

		swWtSize=swWtMax										   
		swWtPad=swWtPadMax;
		if($('#swWtSubWrap').width()==swWtMax){
			swWtSize=0;
			swWtPad=0;
		}
				
		$('#swWtSubWrap').animate({
				width: swWtSize
				
			}, "slow", function() {

				if(!swWtSize){
					swWtPause()
					$('#swWinnersTicker a').removeClass("expand").addClass("collapse");
				}else{
					swWtUnpause();
					$('#swWinnersTicker a').removeClass("collapse").addClass("expand");
				}
		});
		
		return false;
	});
	
//pause on hover, resume on leave
	$("#swWinnersTicker").hover(function(){  
			swWtPause();
		},function() {
			swWtUnpause();
	});

	//animateLeft=-getMyWidth("#winner"+swWtFirstItem)
	animateLeft=-getMyWidth("#swWt_winners ul li")
	//start it up!
	swWtScrollIt();
});

function fBuildWinner(inNum){
	//CALC TIME SINCE WIN
	var now = new Date();
	var timeElapsed = Math.floor((now-swWtStartTime)/1000);			
	var t = parseInt(swWtArrWinners[inNum].prizeAge)+parseInt(timeElapsed);
	var h = Math.floor((t/60)/60);
	var m = Math.floor((t/60))-(h*60);
	var s = t%60;
	
	if (h == 0) {
		if (m == 0) {
			  strPrizeAge = s+" Seconds Ago";
		} else if (m == 1) {
			  strPrizeAge = m+" Minute Ago";
		} else {
			  strPrizeAge = m+" Minutes Ago";
		}
	} else if (h == 1) {
		strPrizeAge = h+" Hour Ago";
	} else if (h>1 && h<24) {
		strPrizeAge = h+" Hours Ago";
	} else if (h>=24 && h<48) {
		strPrizeAge = "Yesterday";
	} else if (h>=48) {
		strPrizeAge = Math.floor(h/24)+" Days Ago";
	}

	$("#swWt_winners ul").append("<li id='winner"+inNum+"'><img src="+swWtArrWinners[inNum].imgSrc+" /><h6><strong>"+swWtArrWinners[inNum].name+", </strong>"+swWtArrWinners[inNum].location+"</h6><p>"+strPrizeAge+" | "+swWtArrWinners[inNum].prize+"</p></li>");
}

function swWtPause(){
//stop the animation
	$("#winner"+swWtFirstItem).stop();
//calculate (and hold onto) how long the current animation has played.
	var Now = new Date()	
	     swWtPauseElapse = Now.getTime() - swWtScrollTimeStart.getTime();
//make sure that we know its not moving.
	swWtBoolMoving=false;	
}

function swWtUnpause(){
//caculate new start time off of "NOW" and time past since orginal animation start
	swWtScrollTimeStart = new Date(new Date().getTime() - swWtPauseElapse);
	swWtScrollIt();
}

function swWtScrollIt(){
	if(!swWtMax) return; //if its collapsed stop animating
	if(swWtBoolMoving) return; //failsafe for mulitple calls while animating
	swWtBoolMoving=true;
	
//calc speed	
	//capture the time when anim started so that pause/resume doesnt look wierd
	//bNew tells us this is NOT a pause/resume request
	var swWtLocalSpeed=swWtScrollTimeSpeed
	if(!swWtPauseElapse){ //if its not coming off a pause...get new time
		swWtScrollTimeStart = new Date();		
	}else{ //calculate speed when coming off a pause
		swWtLocalSpeed = swWtScrollTimeSpeed - swWtPauseElapse
	}
	swWtPauseElapse=0;

//animate
	$("#winner"+swWtFirstItem).animate({marginLeft: animateLeft},swWtLocalSpeed,"linear", function(){
		swWtBoolMoving=false;
		$("#winner"+swWtFirstItem).remove();
		//rebuild it so we recalculate the "time since win"
		fBuildWinner(swWtFirstItem);
		swWtFirstItem++;
		if(swWtFirstItem >swWtArrWinners.length-1)swWtFirstItem=0;
		swWtScrollIt();
	})

}

function getMyWidth(inThis){
	return $(inThis).width()+parseInt($(inThis).css("padding-right").replace("px",""))+parseInt($(inThis).css("padding-left").replace("px",""))	
}

function swWtResize(){
	//calculate the width (100%);
	var liquidWidth = $(window).width()-$("#swWtArrow").width()-$("#swWinnersTicker a").width()-25;	
	if($('#swWtSubWrap').width()) $("#swWtSubWrap").css({width:liquidWidth+"px"});	
	swWtMax=liquidWidth;	
}

