﻿// Weekly promo homepage
var WHHS=null, totalWHSteps=0;
function WeeklyHilightsHomeScroller() {
	this.totalSteps	= totalWHSteps;
	this.timerLeft		= null;
	this.timerRight	= null;
	this.cntWidth		= 0;
	this.scrollStep	= 0;
	this.curStep		= 0;
	this.isScrolling	= false;
	this.UI=
	{
		Scroller:	$('promoHomeScroller'),
		Container:	$('promoHomeContainer'),
		Nav:			$('phNavTop')
	};
}
WeeklyHilightsHomeScroller.prototype=
{
	init:function() {
		var phc = $$('div.promoHomeContent');
		this.cntWidth = Element.getWidth(phc[0]);
		this.UI.Scroller.style.width = (this.cntWidth*(this.totalSteps+2)) + 'px';
		this.scrollStep = this.cntWidth / 10;
		var i,I;
		for(i=0; i<this.totalSteps+1; i++) {
			I=document.createElement('img');
			I.src = (i==this.curStep ? 'img/ph_nav_on.gif' : 'img/ph_nav.gif');
			I.step=i;
			I.id='iPHNavTop'+i;
			I.onclick = function() { WHHS.scrollToStep(this.step); }
			this.UI.Nav.appendChild(I);
		}
		var pdisc = $$('div.layDiscount');
		for(i=0; i<pdisc.length; i++)
			$(pdisc[i]).setStyle({ opacity: 0.8 });

		Event.observe(this.UI.Container, 'mousewheel', WHHS.scroll);
		Event.observe(this.UI.Container, 'DOMMouseScroll', WHHS.scroll);
	},
	scrollLeft:function() {
		if(this.isScrolling) return;
		if(this.curStep==this.totalSteps) return;
		clearTimeout(this.timerRight);
		this.UI.Container.setStyle({ opacity: 0.5 });
		this.timerRight=setTimeout("WHHS.loopLeft()",10);
	},
	scrollRight:function() {
		if(this.isScrolling) return;
		if(this.curStep==0) return;
		clearTimeout(this.timerLeft);
		this.UI.Container.setStyle({ opacity: 0.5 });
		this.timerLeft=setTimeout("WHHS.loopRight()",10);	
	},
	loopLeft:function() {
		this.isScrolling = true;
		var from = (this.curStep==0 ? 0 : (this.cntWidth * this.curStep));
		var to = from + this.cntWidth;
		if(to >= this.UI.Container.scrollWidth) { this.stopall(); return; }

		if(this.UI.Container.scrollLeft < to) {
			this.UI.Container.scrollLeft+=this.scrollStep;
		}
		else {
			if(this.UI.Container.scrollLeft > to)
				this.UI.Container.scrollLeft = to;
			this.stop(true);
			return;
		}
		this.timerRight=setTimeout("WHHS.loopLeft()",10);
	},
	loopRight:function() {
		this.isScrolling = true;
		var from = (this.curStep==0 ? this.cntWidth : (this.cntWidth * this.curStep));
		var to = from - this.cntWidth;
		if(to < 0) { this.stopall(); return; }

		if(this.UI.Container.scrollLeft > to) {
			this.UI.Container.scrollLeft-=this.scrollStep;
		}
		else {
			if(this.UI.Container.scrollLeft < to)
				this.UI.Container.scrollLeft = to;
			this.stop(false);
			return;
		}
		this.timerLeft=setTimeout("WHHS.loopRight()",10)	
	},
	scrollToStep:function(step) {
		if(step == this.curStep) 	return;
		this.UI.Container.setStyle({ opacity: 0.5 });
		if(step>this.curStep) {
			clearTimeout(this.timerRight);
			this.timerRight=setTimeout("WHHS.loopStepLeft('"+step+"')",10);
		}
		else {
			clearTimeout(this.timerLeft);
			this.timerLeft=setTimeout("WHHS.loopStepRight('"+step+"')",10);
		}
	},
	loopStepLeft:function(step) {
		this.isScrolling = true;
		var from = (this.curStep==0 ? 0 : (this.cntWidth * this.curStep));
		var to = (step == 0 ? 0 : (this.cntWidth * step));
		if(to >= this.UI.Container.scrollWidth) { this.stopall(); return; }

		if(this.UI.Container.scrollLeft < to) {
			this.UI.Container.scrollLeft+=(this.scrollStep*2);
		}
		else {
			if(this.UI.Container.scrollLeft > to)
				this.UI.Container.scrollLeft = to;

			this.stop(true, step);
			return;
		}
		this.timerRight=setTimeout("WHHS.loopStepLeft('"+step+"')",10);	
	},
	loopStepRight:function(step) {
		this.isScrolling = true;
		var from = (this.curStep==0 ? this.cntWidth : (this.cntWidth * this.curStep));
		var to = (step == 0 ? 0 : (this.cntWidth * step));
		if(to < 0) { this.stopall(); return; }

		if(this.UI.Container.scrollLeft > to) {
			this.UI.Container.scrollLeft-=(this.scrollStep*2);
		}
		else {
			if(this.UI.Container.scrollLeft < to)
				this.UI.Container.scrollLeft = to;

			this.stop(false, step);
			return;
		}
		this.timerLeft=setTimeout("WHHS.loopStepRight('"+step+"')",10);	
	},
	stop:function(dir, step) {
		this.UI.Container.setStyle({ opacity: 1.0 });
		$('iPHNavTop'+this.curStep).src='img/ph_nav.gif';
		if(typeof(step)=='undefined')
			if(dir) this.curStep++; else  this.curStep--;
		else
			this.curStep = step;
		$('iPHNavTop'+this.curStep).src='img/ph_nav_on.gif';
		clearTimeout(this.timerRight);
		clearTimeout(this.timerLeft);
		this.isScrolling = false;
	},
	stopall:function() {
		this.UI.Container.setStyle({ opacity: 1.0 });
		clearTimeout(this.timerRight);
		clearTimeout(this.timerLeft);
		this.isScrolling = false;
	},
	scroll:function(evt) {
		Event.stop(evt);
		var d=0;
		if(!Object.isUndefined(evt))
			d = Event.wheel(evt);
		if(!d) return true;

		WHHS.curStep = parseInt(WHHS.curStep);
		WHHS.totalSteps = parseInt(WHHS.totalSteps);
		if(d > 0) {
			if(WHHS.curStep==0) return false;
			WHHS.scrollToStep(WHHS.curStep-1);
		}
		else {
			if(WHHS.curStep==WHHS.totalSteps) return false;
			WHHS.scrollToStep(WHHS.curStep+1);
		}
		return false;
	}
};