/*
    Robert J Reid 
	-Created 3rd Dec 2007
    Cross Browser Scroller
    Uses 2 pages of content that replace each other to give impression of continous scrolling.
    Ability to load scrollable HTML content from external files or use content outputed on page.
    Ability to have multiple scrollers on the same page.
	-Ammended 28-Aug-2008
	Replaced iframe reference functions with global functions, added code to make it more backward compatible and cross browser.
	
    Gary P Rutland
	-Modified 22 Sept 2008
		Horizontal Scroll Added
		
		AddScroller("ScrollerName", "", "", "H") - Horizontal
		AddScroller("ScrollerName", "", "", "V") - Vertical	
	
	-Modified 09 March 2009
		Scroll Speed Added
		
		Can still be executed as above or as below
		
		AddScroller("ScrollerName", "", "", "V, 1")
		AddScroller("ScrollerName", "", "", "V, 2")
*/

//global array that holds pointers to any scrollers used on the page
var arrScrollers = new Array();
var IFrameID     = ""; //if we are loading HTML from external files a name must be given. Don't need an iframe per scroller just one on the page
var MovementSpeed = 2;

/*
    call this to add a scroller to the array.
    will create new scroller object and then store the pointer
*/
function AddScroller(scrollerID, FileSrc, IFrameLoader, scrollDim)
{
	//default to vertical
	var re = /,\s/i;
	var testSpeed = re.test(scrollDim);
	if(testSpeed == true){
		MovementSpeed = scrollDim.replace(/[V|H],[\s]*?/i, "");
		scrollDim = scrollDim.replace(/,[\s]*?\d+/i, "");
	}else{
		scrollDim = (scrollDim) ? scrollDim : "V";
	}
	ShowDebug("scrollDim = " + scrollDim);
	ShowDebug("MovementSpeed = " + MovementSpeed);
	
    //create new instance of the scroller
    var objNewScroller = new Scroller(scrollerID);
       
    if(FileSrc!=""){
        //make sure the iframe exists
        if(!getEl(IFrameLoader)){
            alert("Scroller cannot be setup due to no loading iframe with the id of '" + IFrameLoader + "'");
            return false;
        }        
        objNewScroller.LoadFromFile     = true; //flag saying we want to load the scrolling HTML from an external file
        IFrameID                        = IFrameLoader //the id of the iframe to load the HTML through
        objNewScroller.SourceFile       = FileSrc;     //the external HTML file to load   
    }else{
        objNewScroller.LoadFromFile = false;
    }
	
	objNewScroller.ScrollerDim = scrollDim;
	
    //store pointer to this scroller in array as there may be multiple scrollers
    arrScrollers[arrScrollers.length] = objNewScroller;
}


function Scroller(scrollerID)
{
    var self = this;
    var canvas;
    
    canvas = getEl(scrollerID);
    
    //setup default css
    getEl(scrollerID).style.position 					= "relative";
    getEl(scrollerID).style.overflow 					= "hidden";    
    getEl(scrollerID + "_firstPage").style.position		= "absolute";
    getEl(scrollerID + "_secondPage").style.position	= "absolute";    
    
    //make sure there is a first and second page
    //should have the name format of scrollerID_firstPage and scrollerID_secondPage
    var firstPageName = scrollerID + "_firstPage";
    var secondPageName = scrollerID + "_secondPage";
    
    if(!getEl(firstPageName) || !getEl(secondPageName)){
        alert("Scroller setup incorrectly must specify a first and second page (id = '"+scrollerID+"_firstPage' and '"+scrollerID+"_secondPage')");
        return false;
    }
    
    this.ScrollerName   = canvas.id;
    this.FirstPage      = getEl(firstPageName);
    this.SecondPage     = getEl(secondPageName);
    
    //defailt properties of scroller can all be overriden if you want
    this.LoadFromFile   = false;
    this.SourceFile     = "";
    this.HTML           = ""; // the HTML from the external file
    
    this.MoveInterval   = 50;  // The number of milliseconds between each vertical movement.
    this.ScrollDelay    = 20;  // time from instanstiation to start scroll  
    this.CanvasColor   	= "transparent"; // background color of scroller, named or hex
    this.LeftPadding   	= 0;    
    
	if(Browser.dom==3 || Browser.name=="Opera"){
		this.px			= "";
	}else{
		this.px			= "px";
	}
	this.canvasStyle	= (canvas.style) ? canvas.style : canvas;

    this.canvasLeft     = this.canvasStyle.left;
    this.canvasTop      = this.canvasStyle.top;

    this.canvasWidth    = getWidth(scrollerID); //this.canvasStyle.width;
    this.canvasHeight   = getHeight(scrollerID); //this.canvasStyle.height;

    this.canvas         = canvas;
        
    this.UpperPage      = "";
    this.LowerPage      = "";

    this.ScrollSpeed	= MovementSpeed;   //Specify scroll speed (larger is faster 1-10) as its used for the distance (in pixels) of each movement.
	this.DoPause		= 1;  //1 = allow pausing when highlighted with mouse 0 = no pause
		
	this.CopySpeed      = this.ScrollSpeed;
	this.PauseSpeed     = (this.DoPause==0) ? this.CopySpeed:0;
} 

//End of global variables
Scroller.prototype.MoveUp = function() {       
    var self = this;
    self.UpperPage.style.top = parseInt(self.UpperPage.style.top) - self.CopySpeed + this.px;
    self.LowerPage.style.top = parseInt(self.LowerPage.style.top) - self.CopySpeed + this.px;
        
    if (parseInt(this.LowerPage.style.top) <= 0) {      
        if(this.UpperPage.clientHeight){
            this.UpperPage.style.top = parseInt(this.UpperPage.style.top) + parseInt(this.UpperPage.clientHeight)*2 + this.px;
        }else{                                
            this.UpperPage.style.top = parseInt(this.UpperPage.style.top) + parseInt(this.UpperPage.innerHeight)*2 + this.px;
        }
        this.RotateThePages();
    }  
}


Scroller.prototype.MoveLeft = function() {      
	var self = this;
    self.UpperPage.style.left = parseInt(self.UpperPage.style.left) - self.CopySpeed + this.px;
    self.LowerPage.style.left = parseInt(self.LowerPage.style.left) - self.CopySpeed + this.px;
        
    if (parseInt(this.LowerPage.style.left) <= 0) {      
        if(this.UpperPage.clientWidth){
            this.UpperPage.style.left = parseInt(this.UpperPage.style.left) + parseInt(this.UpperPage.clientWidth)*2 + this.px;
        }else{                                
            this.UpperPage.style.left = parseInt(this.UpperPage.style.left) + parseInt(this.UpperPage.innerWidth)*2 + this.px;
        }
        this.RotateThePages();
    }  
}


Scroller.prototype.RotateThePages=function(){
	ShowDebug("IN RotateThePages")
    if (this.UpperPage == this.FirstPage) {
		ShowDebug("UPPER == FIRST")
        this.UpperPage		= this.SecondPage;
        this.LowerPage		= this.FirstPage;
		this.UpperPageStyle = this.SecondPageStyle;
		this.LowerPageStyle = this.FirstPageStyle;
    }else{
		ShowDebug("UPPER == SECOND")		
		this.UpperPage		= this.FirstPage;
		this.LowerPage		= this.SecondPage;
		this.UpperPageStyle = this.FirstPageStyle;
		this.LowerPageStyle = this.SecondPageStyle;
	}    
}

Scroller.prototype.ScrollPages= function() {   
    var self = this;   
	self.SetPage("UpLo"); //set style types for upper and lower pages
    if(self.ScrollerDim == "V"){
		setTimeout(function(){lefttime=setInterval(function(){self.MoveUp();},self.MoveInterval);}, 0);
	}else if(self.ScrollerDim == "H"){
		setTimeout(function(){lefttime=setInterval(function(){self.MoveLeft();},self.MoveInterval);}, 0);		
	}
}

Scroller.prototype.LaunchScroller = function() {     
    
	if(this.ScrollerDim == "V"){
	    if(this.SecondPage.clientHeight){
	        this.SecondPage.style.top = parseInt(this.SecondPage.clientHeight) + this.px;        
	    }else{
	       this.SecondPage.style.top = parseInt(this.SecondPage.innerHeight) + this.px;        
	    }       
	}else if(this.ScrollerDim == "H"){
		if(this.SecondPage.clientWidth){
			this.SecondPage.style.left = parseInt(this.SecondPage.clientWidth) + this.px;			
	    }else{
	       this.SecondPage.style.left = parseInt(this.SecondPage.innerWidth) + this.px;        
	    }       
	}
	
    this.ScrollPages();
	
}

Scroller.prototype.MakeSecondPage = function() {
    var self = this;    
    this.SecondPageStyle.left = this.LeftPadding;  
    addEvent( self.SecondPage, 'mouseover', function(){self.CopySpeed=self.PauseSpeed;} ); 
	addEvent( self.SecondPage, 'mouseout', function(){self.CopySpeed=self.ScrollSpeed;} );	
    this.LowerPage = this.SecondPage;
    Show(this.SecondPageStyle,"on");	    
    this.LaunchScroller();     
}

Scroller.prototype.SetPage = function(tp){
	ShowDebug("IN SetPage = " + tp);
	var self =this;
	if(tp=="UpLo"){
		self.UpperPageStyle = (self.UpperPage.style) ? self.UpperPage.style : self.UpperPage;
		self.LowerPageStyle = (self.LowerPage.style) ? self.LowerPage.style : self.LowerPage;
	}else{
		self.FirstPageStyle = (self.FirstPage.style) ? self.FirstPage.style : self.FirstPage;
		self.SecondPageStyle = (self.SecondPage.style) ? self.SecondPage.style : self.SecondPage;
	}
}

Scroller.prototype.ShowAndScroll = function() {    
	
    var self = this;    
	self.SetPage("FiSe");
    this.FirstPageStyle.left = this.LeftPadding;
    this.FirstPageStyle.top = "0" + this.px;
    Show(this.FirstPageStyle,"on");
    this.UpperPage = this.FirstPage; 
    addEvent( self.FirstPage, 'mouseover', function(){self.CopySpeed=self.PauseSpeed;} ) 	
	addEvent( self.FirstPage, 'mouseout', function(){self.CopySpeed=self.ScrollSpeed;} ) 	
    this.MakeSecondPage();   
}

Scroller.prototype.FillAllPages = function() {   
    this.FirstPage.innerHTML    = this.HTML;
    this.SecondPage.innerHTML   = this.HTML;    
}


Scroller.prototype.MakeCanvas = function() {
	ShowDebug("IN MakeCanvas");
    var bS = false;       
    //if loading from file populate page 1 & 2 with the html
    if(this.LoadFromFile){
        this.FillAllPages();  
    }     
    if(this.canvas.clip){
        this.canvas.clip.width          = this.canvasWidth;
        this.canvas.clip.height         = this.canvasHeight;
    }else{
        this.canvas.style.clip = "rect(0px, " + parseInt(this.canvasWidth) + "px, " + parseInt(this.canvasHeight) + "px, 0px)";
    }    

	this.canvasStyle.width				= this.canvasWidth;
    this.canvasStyle.height				= this.canvasHeight;  
    this.canvasStyle.left				= this.canvasLeft;
    this.canvasStyle.top				= this.canvasTop;
    this.canvasStyle.backgroundColor	= this.CanvasColor; 
    Show(this.canvasStyle,"on");
	LookAtObj(this);
	ShowDebug("End of MakeCanvas")
}

function Show(el,m){
	if(m=="on"){
		if(Browser.dom==3){
			el.visibility = "show";
		}else{
			el.visibility = "visible";
		}
	}else{
		if(Browser.dom==3){
			el.visibility = "hide";
		}else{
			el.visibility = "hidden";
		}
	}
	return;
}

function LookAtObj(obj){
	ShowDebug("Look at all Scroller Properties");
	var str="";
	for(var i in obj){
		if(typeof(i)!="function"){
			str+="Scroller["+i+"] = " + obj[i] + "\n";
		}
	}
	ShowDebug(str);
}

Scroller.prototype.FillCanvas = function() {
	ShowDebug("IN FillCanvas");
    var self = this;
    if(this.LoadFromFile){
        //need to load th external files HTML from the iframe
        this.HTML = getIframeDoc(IFrameID).body.innerHTML.toString();	
		ShowDebug("HTML = " + this.HTML);
    }
   // alert("got html for " + this.ScrollerName + " = " + this.HTML);        
    this.MakeCanvas();        
    setTimeout(function(){self.ShowAndScroll();}, self.ScrollDelay);    
}


var Loaded = 0;
var intval
var stream = false; //for multiple scrollers on page we stream the load with timed gaps

function LoadScrollers(){
	ShowDebug("IN LoadScrollers")
    if(arrScrollers.length>0){
        if(arrScrollers.length==1){ //only 1 to load so no need for intervals between loads
            LoadScroller();
        }else{            
            stream = true;
            intval = setInterval(LoadScroller,200);
        }    
    }   
}

function LoadScroller(){
	ShowDebug("IN LoadScroller");
    if(Loaded<arrScrollers.length){
        var objScroll = arrScrollers[Loaded];
		ShowDebug("Load Scroller " + Loaded + " = " + typeof(objScroll));
        if(objScroll.LoadFromFile){ 
			ShowDebug("Load From External Src = " + objScroll.SourceFile);
            getIframe(IFrameID).src = objScroll.SourceFile;    
        }        
        setTimeout(function(){objScroll.FillCanvas();},100);
        Loaded++
    }else{
        if(stream) clearInterval(intval);
    }
}
