//Copyright set-soft A.Ş. 2008

TYPE.registerNamespace('Web.UI');

//v.1.0.2.0 

//Depends On
//--Web.Core.Browser
//--Web.Core.BrowserManager

//Browsers
//--IE 6+
//--Firefox 2+
//--Opera 9+

Web.UI.ContentSwitcher=function(contentContainerElement,interval,showAsBlockElement,browser)
{
    var me=this;
    
    this.contentContainer=document.getElementById(contentContainerElement);
    this.intervalSec=interval;
    this.activated=false;
    this.showAsBlockelement=showAsBlockElement;
    this.currentItemIndex=-1;
    
    this.interval=null;
    this.timeout=null;
    this.currentDirection='';
    this.browser=(browser==null) ? new Web.Core.BrowserManager() : browser;
    
    this.rate=9;
    if(this.browser.browser.name=='Opera' | this.browser.browser.name=='Firefox')
        this.rate=1;
    
    this.currentOpacity=0;
    
    this.activate=function(delaySec)
    {
        if(this.activated)return;
        if(this.interval != null)
            window.clearInterval(this.interval);
        
        if(this.timeOut != null)
            window.clearTimeout(this.timeOut);
        
        this.showChild(-1);
        this.activated=true;
        
        if(delaySec>0)
            window.setTimeout(function(){me.startFadeWork('in');},delaySec);
        else
            this.startFadeWork('in');
    }
    
    this.deactivate=function()
    {
         if(this.interval != null)
            window.clearInterval(this.interval);
        
        if(this.timeOut != null)
            window.clearTimeout(this.timeOut);
        
        this.showChild(-1);
        this.activated=false;
    }
    
    this.startFadeWork=function(direction)
    {
        if(this.interval != null)
           window.clearInterval(this.interval);
        me.currentDirection=direction;
        if(me.contentContainer.childNodes.length == 0) return;
        
        if(direction=='in')
        {
            if(me.currentItemIndex > -1)
                me.contentContainer.childNodes[me.currentItemIndex].style.display='none';
            me.currentItemIndex=me.getNextAvailableChildNodeIndex();
            me.contentContainer.childNodes[me.currentItemIndex].style.filter='alpha(opacity=0)';
            me.contentContainer.childNodes[me.currentItemIndex].style.opacity=0;
            me.contentContainer.childNodes[me.currentItemIndex].style.display=(me.showAsBlockelement) ? 'block' : 'inline';
        }
        
        me.interval=window.setInterval(me.doFadeWorkInner,10);
    }
    
    this.doFadeWorkInner=function()
    {
        if(me.currentDirection=='in')
        {
            me.currentOpacity=me.currentOpacity + me.rate;
            if(me.currentOpacity>100)
            {
                me.currentOpacity=100;
                window.clearInterval(me.interval);
                me.timeout=window.setTimeout(function(){me.startFadeWork('out');},me.intervalSec);
                return;
            }
        }
        else
        {
            me.currentOpacity=me.currentOpacity - me.rate;            
            if(me.currentOpacity<0)
            {
                me.currentOpacity=0;
                window.clearInterval(me.interval);
                me.timeout=window.setTimeout(function(){me.startFadeWork('in');},100);
                return;
            }
        }
        
        var str = 'alpha(opacity=' + me.currentOpacity + ')';
        me.contentContainer.childNodes[me.currentItemIndex].style.filter=str;
        me.contentContainer.childNodes[me.currentItemIndex].style.opacity=eval(me.currentOpacity / 10);
    }
    
    this.showChild=function(index)
    {
        for(var i=0;i<this.contentContainer.childNodes.length;i++)
            if(this.contentContainer.childNodes[i].nodeName != '#text')
                this.contentContainer.childNodes[i].style.display='none';
        if(index>0)
            this.contentContainer.childNodes[index].style.display=(this.showAsBlockelement) ? 'block' : 'inline';
    }
    
    this.getNextAvailableChildNodeIndex=function()
    {
         for(var i=me.currentItemIndex;i<this.contentContainer.childNodes.length;i++)
            if(i>-1)
                if(this.contentContainer.childNodes[i].nodeName != '#text' && me.currentItemIndex!=i)
                    return i;
                  
         
         me.currentItemIndex=-1;
         return me.getNextAvailableChildNodeIndex();
    }
}


