var brandScrollerEventMan = new N2EventManager();
 
if(!Control) var Control = {};
Control.CBrandScroller = Class.create();

Control.CBrandScroller.prototype = {
	initialize: function(sHome, brandSlider, content) {
		this.asins = new Array();
		this.slider=brandSlider;
		this.homeBase = sHome;
		this.active = 1;
		this.curValue=0;
		this.errorCodes = 0;
		this.step=1;
		this.timout=16;
		this.direction="left";
		var myMarkup = "";		
		myMarkup += "<div style=\"position:absolute; width:100%;display:block; overflow: hidden; height=100%; border:0\" id=\"brandScrollerMain\" >";
		myMarkup += "<table  id='brandimages' onmouseout=\"brandScrollerEventMan.publish(null, 'start', null);\" onmouseover=\"brandScrollerEventMan.publish(null, 'stop', null);\"><tbody>";
		myMarkup += "<tr id=\"brandContent\">";	
		if (typeof content != "undefined"){
			for(var i=0;i<content.length;i++){
				myMarkup += "<td><a href=\""+content[i].link+"\"><img onerror=\"this.parentNode.removeChild(this);\" src=\""+content[i].image+"\" alt=''></a></td>";
			}
		}
		myMarkup += "</tr>";
		myMarkup += "</tbody></table>";
		myMarkup += "</div>";
		
		$(sHome).innerHTML = myMarkup;
		brandScrollerEventMan.subscribe(this, ["start","stop","scroll","scrollToByRatio"]);
		setInterval("brandScrollerEventMan.publish(null, \"scroll\", null);", this.timout);
	},
	
	scroll: function() {
		if( this.active == 1 ) {
			var beforeInc = $("brandScrollerMain").scrollLeft;
			if (this.direction=="right"){
				$("brandScrollerMain").scrollLeft-=this.step;
				var afterInc = $("brandScrollerMain").scrollLeft;
				//$("debuga").innerHTML="right   beforeInc : afterInc="+beforeInc +";"+ afterInc+";"+$("brandScrollerMain").clientWidth+";"+$("brandScrollerMain").offsetWidth;
				newvalue=$("brandScrollerMain").scrollLeft/($("brandScrollerMain").scrollWidth-$("brandScrollerMain").clientWidth);
				if (this.curValue!=newvalue) {
					this.curValue=newvalue;
					//this.slider.setValue(this.curValue);
				}
				if( $("brandScrollerMain").scrollLeft == 0 ) {
					this.direction="left";
				}
			}
			else{
				var beforeInc = $("brandScrollerMain").scrollLeft;
				$("brandScrollerMain").scrollLeft+=this.step;
				var afterInc = $("brandScrollerMain").scrollLeft;
				//$("debuga").innerHTML="left  beforeInc : afterInc="+beforeInc +";"+ afterInc+";"+$("brandScrollerMain").scrollLeft/($("brandScrollerMain").scrollWidth-$("brandScrollerMain").clientWidth);
				newvalue=$("brandScrollerMain").scrollLeft/($("brandScrollerMain").scrollWidth-$("brandScrollerMain").clientWidth);
				if (this.curValue!=newvalue) {
					this.curValue=newvalue;
					//this.slider.setValue(this.curValue);
				}
				if( beforeInc == afterInc ) {
					this.direction="right";
				}
			}
		}
	},
		
	scrollToByRatio: function(ratio) {
		$("brandScrollerMain").scrollLeft=($("brandScrollerMain").scrollWidth-$("brandScrollerMain").clientWidth)*ratio;
	},
		
	onEvent: function(oSrcWidget, sEvent, oData){
		switch(sEvent){
				case "start": 
					this.active = 1;
					break;
				case "stop": 
					this.active = 0;
					break;
				case "scroll": 
					this.scroll();
					break;
				case "scrollToByRatio":
					this.scrollToByRatio(oData);
					break;
				
		}
	}
}
