
var Rotator = {
	
	swf_path: "/bin/include/widgets/PromoSlider/assets/rotator.swf",
	
	target_div: 0,
	
	overlayAlpha: 100,
	fadeInterval:	20,
	fadeIncrement:	20,

	Slides: 0,
	swf: 0,
	component: 0,
	
	rolloverTitleColor: "0xffcc00",
	rolloutTitleColor:  "0xffffff",
	
	div: {
		"rotator":	{
			"domID":	"rotator_swf_Container",
			"width":	480,
			"height":	300,
			"div": 0
		},
		
		"btns":	{
			"domID":	"rotator_btn_Container",
			"width":	138,
			"height":	300,
			"div": 0
		},
		
		"btn_slot": {
			"className": "rotator_btn_slot",
			"width": 138,
			"height": 60
		}
	},

	
	init: function() {
		
		
		this.div.rotator.div = document.createElement("div");
		this.div.rotator.div.id = this.div.rotator.domID;
		this.div.rotator.div.style.width = this.div.rotator.width;
		this.div.rotator.div.style.height = this.div.rotator.height;
		
		this.div.btns.div = document.createElement("div");
		this.div.btns.div.id = this.div.btns.domID;
		this.div.btns.div.style.width = this.div.btns.width;
		this.div.btns.div.style.height = this.div.btns.height;
		
		var container = document.getElementById(this.target_div);
		container.appendChild(this.div.btns.div);
		container.appendChild(this.div.rotator.div);
		
		
		this.swf = new SWFObject(
			this.swf_path,
			"swf_rotator",
			480,300,"8");
		this.swf.addParam("wmode","transparent");
		this.swf.addParam("allowScriptAccess","always");
		this.swf.write(this.div.rotator.domID);
	},
	
	cbk_SWF_State: function(state) {
		this.debug("cbk_SWF_state: " + state);
		switch(state) {
			case "INIT":
				this.component = document.getElementById("swf_rotator");
				this.component.setColors(this.rolloverTitleColor,this.rolloutTitleColor);
				this.loadSlides();
				break;
			case "ALL_SLIDES_LOADED":
				this.rotate();
				break;
		}
	},
	
	cbk_SWF_click: function(index) {
		var url = this.Slides[index].url;
		if (typeof(url) != "undefined") {
			window.location = url;
		}
	},
	
	
	loadSlides: function() {
		this.component.overlayAlpha(this.overlayAlpha);
		this.component.setTransition(this.fadeIncrement,this.fadeInterval);
		for(var i=0;i<this.Slides.length;i++) {
			var s = this.Slides[i];
			this.debug("Adding slide: ");
			this.debug(s);
			this.addButton(s,i);
			this.component.addSlide(s);
		}
		this.component.loadMedia();
	},
	
	addButton: function(s,index) {
		var title = document.createElement("span");
		title.innerHTML = "<h2><a href='javascript:Rotator.component.forceSlide(" + index + ")'>" + s.shortTitle + "</a></h2>";
		
		var tease = document.createElement("span");
		tease.className = "teaser";
		tease.innerHTML = "<a href='javascript:Rotator.component.forceSlide(" + index + ")'>" + s.shortTease + "</a>";
		
		
		
		var box = document.createElement("div");
		box.className = "rotator_btn_slot_content";
		box.appendChild(title);
		box.appendChild(tease);

		
		var btn = document.createElement("div");
		btn.className = this.div.btn_slot.className;
		btn.appendChild(box);
		
		btn.id = "rotator_btn_" + index;
		btn.onclick = "Rotator.component.forceSlide(" + index + ")";
		btn.setAttribute("onclick","Rotator.component.forceSlide(" + index + ")");
		
		
		this.div.btns.div.appendChild(btn);
		
		var maxH = this.div.btn_slot.height;
		var w = box.offsetWidth;
		var h = box.offsetHeight;
		var diff = maxH - h;
		if(diff) {
			var pad = diff / 2;
			//Debug.debug(h + ":" + diff + ":" + pad);
			box.style.paddingTop = pad + "px";
		}
		
		
	},
	
	rotate: function() {
		this.component.rotate();
	},
	
	
	cbk_SWF_SlideFocus: function(focusIndex) {
		for(var i=0;i<this.Slides.length;i++) {
			var btn = document.getElementById("rotator_btn_" + i);
			if(i == focusIndex) {
				btn.className = "rotator_btn_slot rotator_btn_slot_Focused";
			} else {
				btn.className = "rotator_btn_slot";
			}
		}
	},
	
	
	
	////////////////////blah blah blah
	show_debug: false,
	debugBox: {
		div: 0,
		
		init: function() {
			this.div = document.createElement("div");
			this.div.style.border		= "1px solid #990000";
			this.div.style.fontFamily	= "Arial";
			this.div.style.fontSize		= "12px";
			this.div.style.color		= "#009900";
			this.div.style.width		= "800px";
			this.div.style.height		= "200px";
			this.div.style.marginTop	= "20px";
			this.div.style.padding		= "20px";
			this.div.style.overflow		= "scroll";
			document.body.appendChild(this.div);
		},
		
		put: function(s) {
			if(typeof(s) != "string") {
				var t;
				for(var i in s) {
					t += i + ": " + s[i] + "<br/>";
				}
				s = t;
			} else {
				s += "<br/>";
			}
			this.div.innerHTML += s += "<br/>";
		}
	},
	
	debug: function(s) {
		if(!this.show_debug) { return; }
		if(!this.debugBox.div) {
			this.debugBox.init();
		}
		this.debugBox.put(s);
	}
	
		
}






function Rotator_Init() {
	Rotator.init();
}


