/*
INFSScroller objects for infocus.com
created by Charles Wyatt for InFocus Corp.

usage:
splashScroller = new INFSScroller(document.getElementById("splash"));
        
        splashScroller.AddItem(new INFSScrollerItem("/~/media/flash/learnbig%20swf.ashx", "", "learn", 3000, true,"527", "304"));
        splashScroller.AddItem(new INFSScrollerItem("/~/media/flash/playbig%20swf.ashx", "", "play", 5000, true,"527", "304"));
        splashScroller.AddItem(new INFSScrollerItem("/~/media/flash/workbig%20swf.ashx", "", "work", 2000, true,"527", "304"));
        splashScroller.Start();

*/


var INFSScrollerItem;


INFSScrollerItem = function(imgSrc, url, alt, duration, isFlash, width, height)
{
    
    this.src = imgSrc;
    this.alt = alt;
    this.isImage = !isFlash;
    this.w = width;
    this.h = height;
    this.dur = duration;
    this.url = url;
    
    this.GetMarkUp = GetMarkUp;
    this.ImgMarkUp = ImgMarkUp;
    this.FlashMarkUp = FlashMarkUp;  
    
        //preload image;
    var tempImg = new Image();
    tempImg.src = this.src;  
}    

    function GetMarkUp()
    {
        if(this.isImage)
            return this.ImgMarkUp();
        else
            return this.FlashMarkUp();
    }
    
    function ImgMarkUp()
    {
        imgMarkUp = "<a id='homecallout' href='" + this.url + "'><img src='" + this.src + "' alt='" + this.alt + "' width='" + this.w + "' height='" + this.h + "' /></a>";
        return imgMarkUp;
    }
    
    function FlashMarkUp()
    {
        var so = new SWFObject(this.src, this.alt, this.w, this.h, "8", "#ffffff");
		so.addParam("wmode", "transparent");
		return so.getSWFHTML();
    }

var INFSScroller;

INFSScroller = function(name, container, navContainer)
{
    this.navContainer = navContainer;
    this.container = container;
    this.pause = false;
    this.items = new Array();
    this.canceled = 0;
    this.itemIndex = -1;
    this.AddItem = AddItem;
    this.Start = Start;
    this.Next = Next;
    this.SetNewTimeout = SetNewTimeout;
    this.Prev = Prev;
    this.DoNext = DoNext;
    this.DoPrev = DoPrev;
    this.TogglePause = TogglePause;
    this.itemCount = 0;
    this.lastIndex;
    
    this.name = name;
    var self = this;
    this.SwapIn = function()
    {
        ChangeText(this.className, self);
        SwapImg("/img/dev/button_rollover.jpg", self, this);
    };
    this.SwapOut = function()
    {
        ChangeText(self.itemIndex, self);
        SwapImg("/img/dev/button.jpg", self, this);
    };
    this.Goto = function()
    {
        index = this.id.charAt(this.id.length - 1);
 
        self.itemIndex = index - 1;
        self.Next(true);
        self.canceled++;
    };
    
}

    function ChangeText(index, obj)
    {
        var item = obj.items[index];
        spanEle = document.getElementById(GetKey(obj, index));     
        var divEle = spanEle.parentNode.firstChild;  
        divEle.innerHTML = item.alt;
    }

    function AddItem(scrollerItem)
    {
        this.items.push(scrollerItem);
         
         newEle = document.createElement("span");
         newEle.id = GetKey(this, this.itemCount);
         newEle.className = "ChevNavButton";
         newEle.href = "#";
         
         //newEle.innerHTML = "<img src='' alt='' onmouseout='SwapImg(this, \"/img/dev/square.jpg\");' onmouseover='SwapImg(this, \"/img/dev/square_rollover.jpg\");'  />" ;
         var self = this;
         newEle.onclick = this.Goto

         this.navContainer.appendChild(newEle, null);
         
         
         img = document.createElement("img");
         img.id = this.itemCount;
         img.className = this.itemCount;
         img.onmouseover = this.SwapIn;
         img.onmouseout = this.SwapOut;
         img.src = "/img/dev/button.jpg";
         newEle.appendChild(img);
         
         
         this.itemCount++;
         
    }
    

    
    function SwapImg(src, obj, img)
    {     
        //alert("obj index " + obj.itemIndex + " : " + img.id);
        if(obj.itemIndex != img.className)
            img.src = src;

    }

    function Start()
    {
        this.SetNewTimeout(1);
    }
    


    function Next(fromUser)
    {
    
        if(this.canceled > 0 && fromUser == false)
        {
              this.canceled--;
              return;
        }
        
        if(this.pause == true)
            return;
        
        SetOldImageBack(this, this.lastIndex);
        
        this.itemIndex += 1;
        if(this.itemIndex >= this.items.length)
            this.itemIndex = 0;
        
        var item = this.items[this.itemIndex];
        
        this.container.innerHTML = item.GetMarkUp();
        
        var spanEle = document.getElementById(GetKey(this, this.itemIndex));
        img = spanEle.getElementsByTagName("img")[0]; 
        img.src = "/img/dev/button_playing.jpg"

        var divEle = spanEle.parentNode.firstChild;  
         
        divEle.innerHTML = item.alt;
        
        this.lastIndex = this.itemIndex;
        this.SetNewTimeout(item.dur);
    }
    
    function SetOldImageBack(obj, index)
    {
        var spanEle = document.getElementById(GetKey(obj, index));
        if(spanEle == null)
            return;
         
        
        img = spanEle.getElementsByTagName("img")[0];
        img.src = "/img/dev/button.jpg";
        
    }
    
    function SetNewTimeout(dur)
    {
        var self = this;
        var proc = function()
        {
            self.Next(false);
        }
        setTimeout(proc, dur);
    }
    
    function TogglePause()
    {
        unpausing = false;
        if(this.pause == true)
            unpausing = true;
            
        this.pause = !this.pause;
        
        if(unpausing)
            this.SetNewTimeout(1);
    }

    function Prev()
    {
        if(this.pause == true)
            return ;
        
        this.itemIndex -= 1;
        if(this.itemIndex < 0)
            this.itemIndex = this.items.length - 1;
        
        var item = this.items[this.itemIndex];
        
        this.container.innerHTML = item.GetMarkUp();
        this.SetNewTimeout(item.dur);
    }
    
    function DoNext()
    {
        this.Next(true);
        this.canceled++;
    }
    
    function DoPrev()
    {
        this.Prev();
        this.canceled++;
    }
    
    function GetKey(container, num)
    {
 
        return container.name + "_mark" + num;
    }