/******************************************************
* CG_slideScroll
* Description: slide scroll. 
* Author: Jiwon Han
* Email: iamjiwon@hotmail.com
* Last Modified : 9/25/2011
* Copyright (C) 2011 cybergolf.com
******************************************************/

function CG_slideScroll(content,divId,delay,width,height,padding,borderSize,borderColor,bgColor,backgroundImage,headerImage)
{
	this.content = content ? content : new Array("<i>Information coming soon!</i>");
	this.divId = divId ? divId : "CG_slideScroll";
	this.delay = delay ? parseInt(delay) : 3000;
	this.width = width ? parseInt(width) : 200;
	this.height = height ? parseInt(height) : 100;
	this.padding = padding ? parseInt(padding) : 5;
	this.borderSize = borderSize ? parseInt(borderSize) : 1;
	this.borderColor = borderColor ? borderColor : "000000";
	this.bgColor = bgColor ? bgColor : "transparent";
	this.backgroundImage = backgroundImage ? backgroundImage : "";
	this.headerImage = headerImage ? headerImage : "";
	this.currentDiv = 1;
	this.currentContent = 0;
	this.timer;
	this.isTimerOn = true;
	var slideScroll_Instance = this;
}

CG_slideScroll.prototype.start = function()
{
	if (this.headerImage) 
		document.write('<div><img src='+this.headerImage+'></div>');
	document.write('<div id="'+this.divId+'" style="position: relative; overflow: hidden; width:'+this.width+'; height:'+this.height+'; border:'+this.borderSize+'px solid #'+this.borderColor+'; background-color:'+this.bgColor+'; background-image:url('+this.backgroundImage+');">');
	document.write('<div id="'+this.divId+'_inner_1" style="position:absolute; width:100%; height:100%;" align=left></div>');
	document.write('<div id="'+this.divId+'_inner_2" style="position:absolute; width:100%; height:100%; visibility:hidden;" align=left>'+this.content[0]+'</div>');
	document.write('</div>');
	
	this.CG_slideScroll_1 = document.getElementById(this.divId+"_inner_1");
	this.CG_slideScroll_2 = document.getElementById(this.divId+"_inner_2");

	this.initialize();
	this.slide();
}

CG_slideScroll.prototype.initialize = function()
{
	this.CG_slideScroll_1.style.top = 0;
	this.CG_slideScroll_1.style.left = 0;
	this.CG_slideScroll_1.style.width = this.width-((this.borderSize+this.padding)*2);
	this.CG_slideScroll_1.style.height = this.height-((this.borderSize+this.padding)*2);
	this.CG_slideScroll_1.style.marginTop = this.padding;
	this.CG_slideScroll_1.style.marginLeft = this.padding;

	this.CG_slideScroll_2.style.top = this.height;
	this.CG_slideScroll_2.style.left = 0;
	this.CG_slideScroll_2.style.width = this.width-((this.borderSize+this.padding)*2);
	this.CG_slideScroll_2.style.height = this.height-((this.borderSize+this.padding)*2);
	this.CG_slideScroll_2.style.marginTop = this.padding;
	this.CG_slideScroll_2.style.marginLeft = this.padding;
	this.CG_slideScroll_2.style.visibility = "visible";

	var slideScroll_Instance = this;

	document.getElementById(this.divId).onmouseover = function(){slideScroll_Instance.pauseOn();}
	document.getElementById(this.divId).onmouseout=function(){slideScroll_Instance.pauseOff()}

}

CG_slideScroll.prototype.slide = function()
{
	var slideScroll_Instance = this;
	var topDiv;
	var bottomDiv;

	if (this.currentDiv == 1){
		topDiv = this.CG_slideScroll_1;
		bottomDiv = this.CG_slideScroll_2;
	}
	else{
		topDiv = this.CG_slideScroll_2;
		bottomDiv = this.CG_slideScroll_1;
	}

	//slide up both div, until bottom div reach up to top position 0
	if (parseInt(bottomDiv.style.top) > 0){
		topDiv.style.top = parseInt(topDiv.style.top)-5+"px";
		bottomDiv.style.top = parseInt(bottomDiv.style.top)-5+"px";

		setTimeout(function(){slideScroll_Instance.slide()}, 50);
	}
	//when finish slide up both div, move top div to bottom & swap bottom Div content
	else{
		//Move Top Div to down
		topDiv.style.top = this.height;
		if (this.currentDiv == 1)
			this.currentDiv = 2;
		else
			this.currentDiv = 1;
	
		if (this.currentContent >= this.content.length-1)
			this.currentContent = 0;
		else
			this.currentContent++;
				
		//Fill innerHTML code from Array this.content to bottom Div
		topDiv.innerHTML = this.content[this.currentContent];

		if (this.isTimerOn){
			slideScroll_Instance.timer = setTimeout(function(){slideScroll_Instance.slide()},this.delay);
			this.isTimerOn = true;
		}
	}
}

CG_slideScroll.prototype.pauseOn = function()
{
	window.clearTimeout(this.timer);
	this.isTimerOn = false;
}

CG_slideScroll.prototype.pauseOff = function()
{
	var slideScroll_Instance = this;
	this.timer = setTimeout(function(){slideScroll_Instance.slide()},this.delay);
}

