var cnnHorizontalSlider = Class.create();
cnnHorizontalSlider.prototype = {
	initialize: function(objName,elContainer,elIdentifier,navContainer,displayWidth) {
	try {
		this.locked = false;
		this.objName = objName;
		this.elIdentifier = elIdentifier;
		this.container = elContainer;
		this.navDiv = navContainer;
		this.viewPort = displayWidth;
		this.sliderWidth = this.findPanels();
		this.numScreens = Math.round(this.sliderWidth/2);
		this.negativeOffSetMax = this.setOffSet();
		this.positiveOffSetMax = 0;
		this.currentPanel = 0;
		this.inactiveDot = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/gray_status.gif";
		this.activeDot = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/gray_active_status.gif";
		this.setSliderWidth() || 0;
		this.buildNav();
		this.getCurrentOffSet();
	} catch(e) {};
	},
	findPanels: function() {
		var panels = $(this.container).getElementsByTagName('div');
		var panelCount = 0;
		for(var i = 0; i<panels.length;i++) {
			if(panels[i].className == this.elIdentifier+' cnnMar9L' || panels[i].className == this.elIdentifier) {
				panelCount++;
			}
		}
		return panelCount;
	},
	setCurrentPanel: function(val) {
		this.getCurrentOffSet();
		this.currentPanel = (this.currOffSet/this.viewPort) * -1;
		this.updateNav();
	},
	setOffSet: function() {
		return ((this.numScreens * this.viewPort) - this.viewPort) * -1;
	},
	calculateSliderWidth: function() {
		return this.viewPort * this.numScreens;
	},
	setSliderWidth: function() {
		$(this.container).style.width = this.calculateSliderWidth() + "px";
	},
	buildNav: function() {
		var btnContainer = document.createElement('div');
		btnContainer.className = "cnnMpVidBtns";

		var previousBtnLnk = document.createElement('a');
		previousBtnLnk.setAttribute('href','javascript:void(0);');

		var previousBtn = document.createElement('img');
		previousBtn.setAttribute('src','http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/left_gray_btn.gif');
		previousBtn.setAttribute('width','26');
		previousBtn.setAttribute('height','19');
		previousBtn.setAttribute('border','0');		
		previousBtn.setAttribute('id','cnnMpVidBtnL');


		previousBtnLnk.appendChild(previousBtn);
		btnContainer.appendChild(previousBtnLnk);

		for(var i = 0; i<this.numScreens;i++) {
			var dotBtnLnk = document.createElement('a');
			dotBtnLnk.setAttribute('href','javascript:'+this.objName+'.btnSlide(\''+i+'\')');
			
			var dotBtn = document.createElement('img');
			if(i<1) {
				dotBtn.setAttribute('src',this.activeDot);
			} else {
				dotBtn.setAttribute('src',this.inactiveDot);

				//dotBtn.setAttribute('onmouseout','this.src=\''+this.inactiveDot+'\'');
				//dotBtn.setAttribute('onmouseover','this.src=\''+this.activeDot+'\''); 
                                var imgPointer = this;
                                dotBtn.onmouseover = function() {
                                      this.src=imgPointer.activeDot;
                                 }
                                dotBtn.onmouseout = function() {
                                      this.src=imgPointer.inactiveDot;
                                 }				
			}
			dotBtn.setAttribute('width','5');
			dotBtn.setAttribute('height','5');
			dotBtn.setAttribute('border','0');		
			dotBtn.setAttribute('id','cnnMpVidDot'+(i+1));			
			dotBtn.className = "cnnMpVidBtnStatus";
			
			dotBtnLnk.appendChild(dotBtn);
			btnContainer.appendChild(dotBtnLnk);
		}

		var nextBtnLnk = document.createElement('a');
		nextBtnLnk.setAttribute('href','javascript:'+this.objName+'.btnSlide(\'1\')');

		var nextBtn = document.createElement('img');
		nextBtn.setAttribute('src','http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_red_btn.gif');
		nextBtn.setAttribute('width','26');
		nextBtn.setAttribute('height','19');
		nextBtn.setAttribute('border','0');		
		nextBtn.setAttribute('id','cnnMpVidBtnR');
		nextBtn.setAttribute('onmouseover','this.src="http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_red_over_btn.gif"');
		nextBtn.setAttribute('onmouseout','this.src="http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_red_btn.gif"');
		nextBtnLnk.appendChild(nextBtn);
		btnContainer.appendChild(nextBtnLnk);		
		
		$(this.navDiv).appendChild(btnContainer);
		
		
	},
	updateNav: function() {
		var activeBtn = this.currentPanel+1;
		var dots = $(this.navDiv).getElementsByTagName('img');
		for(var i = 0; i<dots.length;i++) {
			var currImg = dots[i];
			if(currImg.id.indexOf('cnnMpVidDot') > -1) {
				var btnIDSubStr = currImg.id.split('cnnMpVidDot')[1];
				if (btnIDSubStr == activeBtn) {
					currImg.src = this.activeDot;
					currImg.onmouseover=function() {};
					currImg.onmouseout=function(){};
					currImg.style.cursor = "";
				} else {
					currImg.src = this.inactiveDot;
					currImg.style.cursor = "pointer";
					var imgPointer = this;
					currImg.onmouseover = function() {
						this.src=imgPointer.activeDot;
					}
					currImg.onmouseout = function() {
						this.src=imgPointer.inactiveDot;
					}
					
				}
			} else if(currImg.id == 'cnnMpVidBtnR') {
				if((this.currentPanel+1) < this.numScreens) {
					currImg.src = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_red_btn.gif";
					currImg.style.cursor = "pointer";
					currImg.parentNode.href = "javascript:"+this.objName+".btnSlide('"+(this.currentPanel+1)+"')";
				} else {
					currImg.src = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_gray_btn.gif";
					currImg.style.cursor = "default";
					currImg.parentNode.href = "javascript:void(0);";
					currImg.removeAttribute('onmouseover');
					currImg.removeAttribute('onmouseout');

				}
			
			} else if(currImg.id == 'cnnMpVidBtnL') {
				if(this.currentPanel > 0) {
					currImg.src = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/left_red_btn.gif";
					currImg.style.cursor = "pointer";
					currImg.parentNode.href = "javascript:"+this.objName+".btnSlide('"+(this.currentPanel-1)+"')";					
					currImg.setAttribute('onmouseover','this.src="http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/left_red_over_btn.gif"');
					currImg.setAttribute('onmouseout','this.src="http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/left_red_btn.gif"');

				} else {
					currImg.src = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/left_gray_btn.gif";
					currImg.style.cursor = "default";
					currImg.parentNode.href = "javascript:void(0);";					
					currImg.removeAttribute('onmouseover');
					currImg.removeAttribute('onmouseout');

				}
			}
		}
	},
	getCurrentOffSet: function(val){
		this.currOffSet = (!isNaN(parseInt($(this.container).style.left))) ? parseInt($(this.container).style.left) : 0;
	},
	btnSlide: function(arg) {
		if(!this.locked) {
			this.locked = true;
			var timeOutPointer = this;
			this.timer = setTimeout(function() {
				timeOutPointer.getCurrentOffSet();
				var finalCoord = (arg * timeOutPointer.viewPort) * -1;
				var moveByVal = (finalCoord > timeOutPointer.currOffSet) ? (finalCoord - timeOutPointer.currOffSet): (timeOutPointer.currOffSet - finalCoord) * -1;
				var duration = (moveByVal > 0) ? 0.3 * (moveByVal/timeOutPointer.viewPort) :  (0.3 * (moveByVal/timeOutPointer.viewPort)) * -1;
				if(duration < 0) {
					duration = duration * -1;
				}
				if(timeOutPointer.currOffSet > timeOutPointer.negativeOffSetMax || timeOutPointer.currOffSet < timeOutPointer.positiveOffSetMax) {
					new Effect.MoveBy( $(timeOutPointer.container).id, 0, moveByVal, {duration: duration,afterFinish:function() {timeOutPointer.setCurrentPanel()}} );
				}


				timeOutPointer.locked = false;
				
			},300);
		}
	}
}


var cnnAskCommunitySlider = Class.create();
cnnAskCommunitySlider.prototype = Object.extend(new cnnHorizontalSlider(), {
	initialize: function(objName,elContainer,elIdentifier,navContainer,displayWidth) {
		try {
			this.locked = false;
			this.objName = objName;
			this.elIdentifier = elIdentifier;
			this.container = elContainer;
			this.navDiv = navContainer;
			this.viewPort = displayWidth;
			this.sliderWidth = this.findPanels();
			this.numScreens = this.sliderWidth;
			this.negativeOffSetMax = this.setOffSet();
			this.positiveOffSetMax = 0;
			this.currentPanel = 0;
			this.inactiveDot = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/gray_status.gif";
			this.activeDot = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/gray_active_status.gif";
			this.setSliderWidth() || 0;
			this.buildNav();
			this.getCurrentOffSet();
		} catch(e) {};
	}
});