/*
    TABS
*/
var woeTabs;


if(typeof defaultHomeLeagueID == "undefined") defaultHomeLeagueID=0;
if(typeof defaultHomeTabID == "undefined") defaultHomeTabID=0;

if(typeof getHomeStandData == "undefined") getHomeStandData=false;
if(typeof homeStandAjaxUrl == "undefined") homeStandAjaxUrl="";




Event.observe(window, 'load', initWoeTabs, false);

function initWoeTabs()
{
    woeTabs=new Tabs();  
    
    woeTabs.homeStandCompetitieID=defaultHomeLeagueID;  
    woeTabs.homeStandTabID=defaultHomeTabID;  
    
    if(getHomeStandData==true)
    {
        if(homeStandAjaxUrl!="")
        {
            woeTabs.homeStandAjaxUrl=homeStandAjaxUrl;
            woeTabs.homeStandWeek=getWeekNr();
            woeTabs.tabCallbackFunction("home_stand",woeTabs.homeStandTabID);
        }
        
    }   
}



function updateHomeStandWeek(newWeek)
{
    woeTabs.homeStandWeek=newWeek;
    woeTabs.updateHomeTab();
}




function getWeekNr()
{
    var today = new Date();
    Year = takeYear(today);
    Month = today.getMonth();
    Day = today.getDate();
    now = Date.UTC(Year,Month,Day+1,0,0,0);
    var Firstday = new Date();
    Firstday.setYear(Year);
    Firstday.setMonth(0);
    Firstday.setDate(1);
    then = Date.UTC(Year,0,1,0,0,0);
    var Compensation = Firstday.getDay();
    if (Compensation > 3) Compensation -= 4;
    else Compensation += 3;
    NumberOfWeek =  Math.round((((now-then)/86400000)+Compensation)/7);
    return NumberOfWeek;
}

function takeYear(theDate)
{
    x = theDate.getYear();
    var y = x % 100;
    y += (y < 38) ? 2000 : 1900;
    return y;
}






function Tabs()
{
    this.id=-1;
    this.parentDiv="";
    this.parentEl=null;
    this.functionName="";
    this.currentTab="";
    
    //this.homeStandCompetitieID=0;
    /*
    this.homeStandCompetitieID=0;
    this.homeStandTabID=0;
    */
    
}


// main callback function from the flash tabs
Tabs.prototype.tabCallbackFunction=function(functionname,id)
{
    this.parentDiv="";
    this.functionName="";
    if(typeof functionname == "string")
    {
        if(typeof id =="number")
        {
            this.id=parseInt(id);
            this.functionName=functionname;
            this.parentDiv="tabs_"+this.functionName;
            
            try{
                switch(functionname)
                {
                    case "clubstand" :
                    {
                        this.clubstand();
                        break;
                    }
                    
                    case "competitie" :
                    {
                        this.competitie();
                        break;
                    }
                    
                    case "speler" :
                    {
                        this.speler();
                        break;
                    }
                    
                    case "home_stand" :
                    {
                        this.home_stand();
                        break;
                    }                   
                    
                    
                    case "quiz" :
                    {
                        this.quiz();
                        break;
                    }
                    
                    case "abonnement" :
                    {
                        this.abonnement();
                        break;
                    }
                    
                    case "interactief" :
                    {   
                        this.interactief();
                    }
                    
                    case "shop" :
                    {   
                        this.shop();
                    }                   
                    
                }   
            } catch (e)
            {
                
            }
            
        }   
    }
}

/* -------------------------------------------------- */
// tab functions
/* -------------------------------------------------- */

Tabs.prototype.home_stand_debug=function()
{
    alert("homeStandTabID = " + this.homeStandTabID + "\n" + "homeStandCompetitieID = " + this.homeStandCompetitieID);  
}

Tabs.prototype.updateHomeStandCompetitie=function(id)
{
    if(typeof id == "number")
    {
        this.homeStandCompetitieID=id;  
    }
    
    //this.home_stand_debug();
    this.updateHomeTab();
}

Tabs.prototype.updateHomeTab=function(id)
{
    if(typeof id == "number")
    {
        this.homeStandTabID=id; 
    }
    
    //this.home_stand_debug();
    
    if(this.homeStandAjaxUrl!="")
    {
        new Ajax.Updater('home_ajax_content', this.homeStandAjaxUrl + 'dbid='+this.homeStandCompetitieID+'&typeofpage=84136&homeStandWeek=' + this.homeStandWeek + '&homeStandCompetitieID='+this.homeStandCompetitieID+'&homeStandTabID='+this.homeStandTabID, 
            {
             method: 'get', 
             parameters: { 
                 evalScripts: 'true'
                }
            }
        );  
    }
}

Tabs.prototype.home_stand=function()
{
    if(typeof this.homeStandTabID == "undefined")
    {
        this.homeStandTabID=0;
    }
    
    if(typeof this.homeStandCompetitieID == "undefined")
    {
        this.homeStandCompetitieID=0;
    }
    
    if(typeof this.homeStandWeek == "undefined")
    {
        this.homeStandWeek=1;
    }   
    
    
    this.updateHomeTab(this.id);
}


Tabs.prototype.shop=function()
{
    this.toggle();
}



Tabs.prototype.interactief=function()
{
    this.toggle();
}


Tabs.prototype.abonnement=function()
{
    this.toggle();
}

Tabs.prototype.quiz=function()
{
    this.toggle();
}

Tabs.prototype.clubstand=function()
{
    this.toggle();
}

Tabs.prototype.competitie=function()
{
    this.toggle();
}

Tabs.prototype.speler=function()
{
    this.toggle();
}

/* -------------------------------------------------- */
// private functions
/* -------------------------------------------------- */


Tabs.prototype.toggle=function()
{
    if(this.parentEl=$PT(this.parentDiv))
    {
        var childs;
        if(childs=this.parentEl.immediateDescendants())
        {
            var targetDiv="tab_" + this.functionName + "_" + this.id;
            
            var targetDivEl=$PT(targetDiv);
            if(targetDivEl)
            {
                // clear the current tab
                this.switchCurrentTab(targetDiv);
                
                // hide the childs in the target div                
                childs.each(function(item)
                    {
                        item.style.display="none";                  
                    }
                );
                
                
                // display the div
                targetDivEl.style.display="block";
                
                // some gui effects ;-)
                //Effect.Appear(targetDiv, { duration: 0.5 });
            } else {
                this.currentTab="";
                //alert("3");
            }
        } else {
            //alert("2");
        }
    } else {
        //alert("1");
    }   
}


Tabs.prototype.switchCurrentTab=function(newTab)
{
    if(this.currentTab!="")
    {
        //Effect.Fade(this.currentTab, { duration: 0.5 });
        $PT(this.currentTab).style.display="none";
    }   
    
    this.currentTab=newTab;
}


