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.inactiveOverLeft = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/left_red_btn.gif";
		this.activeOverLeft = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/left_red_over_btn.gif";
		this.inactiveOverRight = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_red_btn.gif";
		this.activeOverRight = "http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_red_over_btn.gif";		
		this.setSliderWidth() || 0;
		this.buildNav();
		this.getCurrentOffSet();
	} catch(e) {};
	},
	findPanels: function() {
		try{
			var panelCount = 0;
			if($(this.container)){
				var panels = $(this.container).getElementsByTagName('div');
				for(var i = 0; i<panels.length;i++) {
					if(panels[i].className == this.elIdentifier+' cnnMar9L' || panels[i].className == this.elIdentifier) {
						panelCount++;
					}
				}
			}
			return panelCount;
		} catch(e) {};
	},
	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() {
		try {
			$(this.container).style.width = this.calculateSliderWidth() + "px";
		} catch(e) {};
	},
	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');
		if(this.numScreens <= 1){
			btnContainer.appendChild(previousBtn);
		} else {
			previousBtnLnk.appendChild(previousBtn);
			btnContainer.appendChild(previousBtnLnk);
		}
		
		for(var i = 0; i<this.numScreens;i++) {
			var dotBtnLnk = document.createElement('a');
			
			var dotBtn = document.createElement('img');
			if(this.numScreens <= 1){
				dotBtn.style.cursor = "default";
				dotBtn.setAttribute('src',this.inactiveDot);
				btnContainer.appendChild(dotBtn);
			} else {
				dotBtnLnk.setAttribute('href','javascript:'+this.objName+'.btnSlide(\''+i+'\')');
				if(i<1) {
					dotBtn.setAttribute('src',this.activeDot);
				} else {
					dotBtn.setAttribute('src',this.inactiveDot);
					var imgPointer = this;
					dotBtn.onmouseover = function() {
						this.src=imgPointer.activeDot;
					}
					dotBtn.onmouseout = function() {
						this.src=imgPointer.inactiveDot;
					}				
				}
				dotBtnLnk.appendChild(dotBtn);
				btnContainer.appendChild(dotBtnLnk);
			}
			dotBtn.setAttribute('width','5');
			dotBtn.setAttribute('height','5');
			dotBtn.setAttribute('border','0');		
			dotBtn.setAttribute('id','cnnMpVidDot'+(i+1));			
			dotBtn.className = "cnnMpVidBtnStatus";
		}
		
		var nextBtnLnk = document.createElement('a');
		var nextBtn = document.createElement('img');
		if(this.numScreens <= 1){
			nextBtn.setAttribute('src','http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_gray_btn.gif');
			nextBtn.style.cursor = "default";
			nextBtnLnk.setAttribute = "javascript:void(0);";
		} else {
			nextBtnLnk.setAttribute('href','javascript:'+this.objName+'.btnSlide(\'1\')');
			nextBtn.setAttribute('src','http://i.cdn.turner.com/cnn/.element/img/2.0/content/in_the_news/right_red_btn.gif');	
			var nextImgPointer = this;
			nextBtn.onmouseover = function() {
				this.src=nextImgPointer.activeOverRight;
			}
			nextBtn.onmouseout = function() {
				this.src=nextImgPointer.inactiveOverRight;
			}
		}
		nextBtn.setAttribute('width','26');
		nextBtn.setAttribute('height','19');
		nextBtn.setAttribute('border','0');		
		nextBtn.setAttribute('id','cnnMpVidBtnR');

		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";
					var nextImgPointer = this;
					currImg.onmouseover = function() {
						this.src=nextImgPointer.activeOverRight;
					}
					currImg.onmouseout = function() {
						this.src=nextImgPointer.inactiveOverRight;
					}
					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.onmouseover = function() {};
					currImg.onmouseout = function() {};	
				}
			
			} 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";
					var prevImgPointer = this;
					currImg.onmouseover = function() {
						this.src=prevImgPointer.activeOverLeft;
					}
					currImg.onmouseout = function() {
						this.src=prevImgPointer.inactiveOverLeft;
					}
					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/left_gray_btn.gif";
					currImg.style.cursor = "default";
					currImg.parentNode.href = "javascript:void(0);";
					currImg.onmouseover = function() {};
					currImg.onmouseout = function() {};			
				}
			}
		}
	},
	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 cnnHealthMainExpertsSlider = Class.create();

cnnHealthMainExpertsSlider.prototype = Object.extend(new cnnHorizontalSlider(), {
	initialize: function(objName,elContainer,elIdentifier,navContainer,displayWidth) {
		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.setSliderWidth() || 0;
		this.buildNav();
		this.getCurrentOffSet();
	},
	buildNav: function() {
		var objRef = this;
		var listContainer = document.createElement('ul');
		listContainer.className = "cnnExpertMenu";
		for(var i = 0; i<this.numScreens;i++) {
			var listItem = document.createElement('li');
			if (typeof defaultTab == "undefined") {
				if(i == this.currentPanel) {
					listItem.className = "cnnSelectedBtn"+(i+1);
				} else {
					listItem.className = "cnnBtn"+(i+1);
				}
			} else {
				if(i == defaultTab) {
					$(this.container).style.left = ((defaultTab * this.viewPort) * -1)+"px";
					listItem.className = "cnnSelectedBtn"+(i+1);
				} else {
					listItem.className = "cnnBtn"+(i+1);
				}
			}
			var listItemLink = document.createElement('a');
			listItemLink.setAttribute('href','javascript:'+this.objName+'.btnSlide(\''+i+'\');'+this.objName+'.fixNavFocus(\''+i+'\')');
			listItem.appendChild(listItemLink);
			listContainer.appendChild(listItem);
		}
		$(this.navDiv).appendChild(listContainer);
	},
	updateNav: function() {
		var objRef = this;
		var activeBtn = this.currentPanel;
		var listItems = $(this.navDiv).getElementsByTagName('li');
		for(var i = 0; i<listItems.length;i++) {
			if(i == activeBtn) {
				listItems[i].className = "cnnSelectedBtn"+(i+1);
			
			} else {
				listItems[i].className = "cnnBtn"+(i+1);
			}	
		}
	},
	fixNavFocus: function(arg) {
		var listItems = $(this.navDiv).getElementsByTagName('li');
		for(var i = 0; i<listItems.length;i++) {
			if(i == arg) {
				listItems[i].className = "cnnSelectedBtn"+(i+1);
			} else {
				listItems[i].className = "cnnBtn"+(i+1);
			}
		}
	}
});

var DailyDoseCardSlider = Class.create();
DailyDoseCardSlider.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 / 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) {};
	}
});


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) {};
	}
});