
var carousel = new Object();
carousel.slideArray = new Array();
carousel.currentSlide = 0;
carousel.slideLock = false;

carousel.addSlide = function(title, img, text, href){
	obj = document.createElement("div");
	obj.id = "cSlide" + this.slideArray.length;
	obj.className = "cSlide";
	obj.innerHTML = "<div class='cSlideTitle'>" + title + "</div>";
	obj.innerHTML += "<div class='cSlideImageFrame'><img class='cSlideImage' src='" + img + "' width='120' height='86'/></div>";
	obj.innerHTML += "<div class='cSlideText'>" + text + "</div>";
	obj.innerHTML += "<div class='cSlideReadMore'><a href='" + href + "'>Read More</a></div>";
	this.slideArray.push(obj);
}

carousel.cloneSlide = function(slide){
	obj = document.createElement("div");
	obj.id = "cSlide" + slide;
	obj.className = "cSlide";
	obj.innerHTML = this.slideArray[slide].innerHTML;
	return obj;
}

carousel.getSlide = function(slide){
	return document.getElementById("cSlide" + slide);
}

carousel.init = function(t){
	this.t = t;
	this.cMain = document.getElementById('cMain');
	this.cBody = document.getElementById('cBody');
	this.cSlider = document.getElementById('cSlider');
	
	if (this.slideArray.length > 1)
	{
	
		var IE = /*@cc_on!@*/false;
		if(IE)
		{
			buttonTop = 25;
			this.slideDistance = "-340px"
		}
		else
		{
			buttonTop = 15;
			this.slideDistance = "-370px"
		}
		
			obj = document.createElement("div");
				obj.id = "leftCButton";
				obj.style.top = this.cBody.offsetHeight/2 + buttonTop + "px";
				obj.className = "cButton";
				this.preloadImage(obj.id);
			this.cMain.appendChild(obj);	
		
			obj = document.createElement("div");
				obj.id = "rightCButton";
				obj.style.top = this.cBody.offsetHeight/2 + buttonTop + "px";
				obj.style.left = this.cMain.offsetWidth - 31 + "px";
				obj.className = "cButton";
				this.preloadImage(obj.id);
			this.cMain.appendChild(obj);
					
			$("#rightCButton").click(function(){carousel.slide("right")});
			
			$("#leftCButton").click(function(){carousel.slide("left")});
			
			this.timer = setInterval("carousel.slide('right')", t);
			
			$("#cMain").mouseover(function(){window.clearInterval(carousel.timer)});
			$("#cMain").mouseout(function(){carousel.timer = setInterval("carousel.slide('right')", carousel.t);});
	}
		this.cSlider.appendChild(this.cloneSlide(this.currentSlide));
}

carousel.slide = function(direction){
	
	var c = carousel;
	
	if(c.slideLock)
		return;
		
	c.slideLock = true;
	
	currentObj = c.getSlide(c.currentSlide);
	if(direction == "right")	
	{
		if(c.currentSlide == c.slideArray.length - 1)
			c.currentSlide = 0;
		else
			c.currentSlide++;

		c.cSlider.appendChild(c.cloneSlide(c.currentSlide));
		$("#cSlider").animate({ marginLeft: c.slideDistance }, 500, "jswing",  
			function(){ 
				c.cSlider.style.marginLeft = "0px"; 
				c.cSlider.removeChild(currentObj);
				c.slideLock = false;
			});
	}
	else
	{
		if(c.currentSlide == 0)
			c.currentSlide = c.slideArray.length - 1;
		else
			c.currentSlide--;
		
		c.cSlider.style.marginLeft = c.slideDistance;	
		
		c.cSlider.insertBefore(c.cloneSlide(c.currentSlide),currentObj);
		$("#cSlider").animate({ marginLeft: "0px" }, 500, "jswing",  
			function(){ 
				c.cSlider.removeChild(currentObj); 
				c.slideLock = false;
			});	
	}
}

carousel.preloadImage = function(id){
	pl = document.createElement("div");
	pl.id = id + "Preload";
	document.body.appendChild(pl);
}