﻿function Scroller(sHolder, sConfig)
{   
    this._Holder = document.getElementById(sHolder);
    this._Text
    this._Images = new Array();
    this._Config = sConfig;
    this._MoveTimer = 0;
    this._MoveCount = 0;   
   
    this.Create = function()
    {
        this.RetrieveURL();
    }
    
    this.CreateText = function()
    {
        this._Text = document.createElement("div");
        this._Text.style.position = "absolute";
        this._Text.style.width = 270 + "px";
        this._Text.style.left = 0 + "px";
        this._Text.style.top = 85 + "px";
        this._Text.style.textAlign = "center";
        this._Holder.appendChild(this._Text);
        
        this._Text.innerHTML = "" ;
    }  
 
    this.ScrollRight = function()
    {        
        var i;
        var bMove = true;
    
        for (i in this._Images)
        {
            if (this._Images[i].IsMoving())
            {
                bMove = false;
            }
        }
    
        if (bMove)
        {
            var tmp;
            tmp = this._Images.pop();
            this._Images.unshift(tmp);
            
            for (i in this._Images)
            {
                this._Images[i].MovePos(-1, parseInt(i) + 1, i);
            }
            
            this._Text.innerHTML = this._Images[2].GetText();
        } 
        
        return false;               
    }
    
    this.ScrollLeft = function()
    {        
        var i;
        var bMove = true;
    
        for (i in this._Images)
        {
            if (this._Images[i].IsMoving())
            {
                bMove = false;
            }
        }
    
        if (bMove)
        {
            var tmp;
            tmp = this._Images.shift();
            this._Images.push(tmp);
            
            for (i in this._Images)
            {
                this._Images[i].MovePos(1, this._Images.length - i, i);
            }
            
            this._Text.innerHTML = this._Images[2].GetText();
        }
        
                          
    }
    
    this.RetrieveURL = function() 
    {    	
	    if (window.XMLHttpRequest) 
	    { // Non-IE browsers	
		    var _XML = new XMLHttpRequest();
		    var t = this;
		    
		    _XML.onreadystatechange = function()
	        {
	            if (_XML.readyState == 4) 
                {
                    if (_XML.status == 200) 
                    {
                       t.ReadXML(_XML.responseXML);     		  			    		
                    }
                    else
                    {
	                    t.Error("Couldn't load config");
                    }
                }
	        }
		    
		    	
		    try 
		    {
			    _XML.open("GET", this._Config, true);
			    _XML.setRequestHeader('Content-Type','application/x-www-form-urlencoded');				
		    } 
		    catch (e) 
		    {
			    this.Error("Couldn't load config");
		    }
		    _XML.send(null);
    	
	    } 
	    else if (window.ActiveXObject) 
	    { // IE
		    _XML = new ActiveXObject("Microsoft.XMLHTTP");
		    if (_XML)
		    {
		        var t = this;
    		    
		        _XML.onreadystatechange = function()
	            {
	                if (_XML.readyState == 4) 
                    {
                        if (_XML.status == 200) 
                        {
                           t.ReadXML(_XML.responseXML);     		  			    		
                        }
                        else
                        {
	                        t.Error("Couldn't load config");
                        }
                    }
	            }
		        
			    _XML.open("GET", this._Config, true);
			    _XML.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			    _XML.send();
		    }
		    else
		    {
		        this.Error("Couldn't load config");
		    }
	    }
    }
    
    this.ReadXML = function(XMLDoc)
    {           		    
       
       if (XMLDoc == null)
       {
        this.Error("Couldn't load config");
       }
       else
       {
           var i;
           var oImages = XMLDoc.getElementsByTagName("image");
           var oFile;
           var oText;
           var oLink;
           
           for (i=0; i<oImages.length; i++)
           {
            oFile = oImages[i].getElementsByTagName("file");
            oText = oImages[i].getElementsByTagName("text");
            oLink = oImages[i].getElementsByTagName("link");
            
            this._Images.push(new ScrollerImage(oText[0].firstChild.nodeValue, oFile[0].firstChild.nodeValue, this._Holder, i, oLink[0].firstChild.nodeValue));
           } 
           
           //this.CreateText();        
           this.ScrollRight();
       }        
    } 
    
    this.Error = function(sMessage)
    {
        
    }                
}

function ScrollerImage(sName, sImage, oParent, iPos, sLink)
{  
    this._Pos1 = 0;
    this._Pos2 = 85;
    this._Pos3 = 195;    
    
    this._YSmall = 10;
    this._YBig = 0;
    
    this._Speed = 10;
    
    this.GetText = function()
    {
        return '<a href="' + this._Link + '">' + this._Text + '</a>';
    }
    
    this.Size = function()
    {        
        this._Image.width = this._Width;
        this._Image.height = this._Height;
    }
    
    this.IsMoving = function()
    {
        return this._Moving;
    }
    
    this.GetPos = function()
    {        
        return this._Pos;
    }
    
    this.Move = function()
    {
        this._Image.style.top = this._Y + "px";
        this._Image.style.left = this._X + "px";
    }
    
    this.MovePos = function(iDir, iIndex, iTo)
    {
        this._Moving = true;
        this._Image.style.zIndex = iIndex;     
        
        if (iDir > 0)
        {
            if ((iTo > 0) && (iTo <= 4))
            {
                this._Image.style.display = "block";            
            }
            else
            {
                this._Image.style.display = "none";
            }
        }
        else
        {
            if ((iTo >= 0) && (iTo < 4))
            {
                this._Image.style.display = "block";            
            }
            else
            {
                this._Image.style.display = "none";
            }
        }       
        
        this.Scroll(iTo);                
    }    
    
    this.Scroll = function(iTo)
    { 
        var bReload = false;
          
        if (iTo == 1)
        {
            this._Width -= 5;
            this._Height -= 5;
            
            if (this._Width < 75)
            {
                this._Width = 75;
            }
            
            if (this._Height < 60)
            {
                this._Height = 60;
            }
            
            if (this._Pos < iTo)
            {            
                this._X += 10;                
                if (this._X >= this._Pos1)
                {
                    this._X = this._Pos1;
                    this._Pos = 1;
                    this._Moving = false;
                    this._Y = this._YSmall;
                }
                else
                {
                    bReload = true;
                }                            
            }
            else if (this._Pos > iTo)
            {
                this._X -= 10;
                if (this._X <= this._Pos1)
                {
                    this._X = this._Pos1;
                    this._Pos = 1;
                    this._Moving = false;
                    this._Y = this._YSmall;
                }
                else
                {
                    bReload = true;
                }
            }
        }
        else if (iTo == 2)
        {          
            this._Width += 5;
            this._Height += 5;
            
            if (this._Width > 100)
            {
                this._Width = 100;
            }
            
            if (this._Height > 80)
            {
                this._Height = 80;
            }
            
            if (this._Pos < iTo)
            {            
                this._X += 10;
                                
                if (this._X >= this._Pos2)
                {
                    this._X = this._Pos2;
                    this._Pos = 2;
                    this._Moving = false;
                    this._Y = this._YBig;
                }
                else
                {
                    bReload = true;
                }                            
            }
            else if (this._Pos > iTo)
            {
                this._X -= 10;
                
                if (this._X <= this._Pos2)
                {
                    this._X = this._Pos2;
                    this._Pos = 2;
                    this._Moving = false;
                    this._Y = this._YBig;
                }
                else
                {
                    bReload = true;
                }
            } 
        }
        else if (iTo == 3)
        {        
            this._Width -= 5;
            this._Height -= 5;
            
            if (this._Width < 75)
            {
                this._Width = 75;
            }
            
            if (this._Height < 60)
            {
                this._Height = 60;
            } 
            
            if (this._Pos < iTo)
            {            
                this._X += 10;
                if (this._X >= this._Pos3)
                {
                    this._X = this._Pos3;
                    this._Pos = 3;
                    this._Moving = false;
                    this._Y = this._YSmall;
                }
                else
                {
                    bReload = true;
                }                            
            }
            else if (this._Pos > iTo)
            {
                this._X -= 10;
                if (this._X <= this._Pos3)
                {
                    this._X = this._Pos3;
                    this._Pos = 3;
                    this._Moving = false;
                    this._Y = this._YSmall;
                }
                else
                {
                    bReload = true;
                }
            } 
        } 
        else if (iTo <= 0)
        {            
            this._Pos = iTo;       
            this._X = this._Pos1;
            this._Moving = false;
            this._Y = this._YSmall;
        }
        else if (iTo >= 4)
        {            
            this._Pos = iTo;       
            this._X = this._Pos3;
            this._Moving = false;
            this._Y = this._YSmall;
        }  
        
        this.Move();
        this.Size();
        if (bReload)
        {
            this._Callwrapper = new CCallWrapper(this, this._Speed, 'Scroll', iTo);
            CCallWrapper.asyncExecute(this._Callwrapper);
        }              
    }    
    
    this._Moving = false;    
    
    this._Pos = iPos;
    this._Callwrapper;
    
    this._X = 0;
    this._Y = 0;
    
    this._Width = 75;
    this._Height = 60;
        
    this._Image = document.createElement("img")
    this._Image.src = sImage;
    this._Image.style.cursor = "pointer";
    
    this._Image.onclick = function()
    {
        window.location = sLink;
    }
        
    this._Image.style.position = "absolute";
    
    this._Text = sName;
    this._Link = sLink;
    
    if (this._Pos == 1)
    {
            this._Image.style.display = "block";
            this._X = this._Pos1;
            this._Y = this._YSmall;
    }
    else if (this._Pos == 2)
    {
            this._Image.style.display = "block";
            this._X = this._Pos2;
            this._Y =  this._YBig;
            this._Width = 100;
            this._Height = 80;
    }
    else if (this._Pos == 3)
    {
            this._Image.style.display = "block";
            this._X = this._Pos3;
            this._Y = this._YSmall;
    }
    else if (this._Pos < 1)
    {
            this._Image.style.display = "none";
            this._X = this._Pos1;
            this._Y = this._YSmall;
    }
    else if (this._Pos > 3)
    {
            this._Image.style.display = "none";
            this._X = this._Pos3;
            this._Y = this._YSmall;     
    }
    
    this.Move();
    this.Size();
    
    oParent.appendChild(this._Image);   
}

