// Adds event to window.onload without overwriting currently assigned onload functions.
var woeTabs;

function addLoadEvent(func)
{    
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    } 
    else 
    {
        window.onload = function()
        {
            oldonload();
            func();
        }
    }
}

function toggleDiv(divName)
{
    if(typeof divName == "string")
    {
        if($PT(divName))
        {
            el=$PT(divName);
            if(!el.visible())
            {
                el.style.display="block";
            } else {
                el.style.display="none";
            }
        }
    }
}

function getRandomToken()
{
    var d = new Date();
    return d.getTime();
}

function QueryStringBuilder() {
  this.Url = '';
  this.Pairs = new Array();

  QueryStringBuilder.prototype.GetFullString = function() {
    var queryString = (this.Url.length > 0) ? this.Url + "&" : '';

    for(var key in this.Pairs) {
      if (key=='vari'||key=='edi') {
        queryString += escape(key) + "=" + escape(this.Pairs[key]) + "&";
      }
    }
    
    return queryString.substring(0, queryString.length - 1);
  }
}

function buildQuery(linkid,builder,name,value) {

  builder.Pairs[name] = value;
  document.getElementById(linkid).href = builder.GetFullString();
}



/*
 *
 * GX Functions from here - GX Functions from here - GX Functions from here
 *
 */
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest(); //Not IE
    } else if(window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP"); //IE
    } else {
        //Display your error message here. 
        //and inform the user they might want to upgrade
        //their browser.
        alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
    }
}


function showFlvPlayer(url, width, height) {
 document.write(unescape('%3Cembed src="http://www.vi.nl/static/vi/swf/player.swf" width="' + width + '" height="' + height + '" allowscriptaccess="always" allowfullscreen="true" flashvars="autostart=true&height=250&width=400&file=' + url + '"/%3E'));
}



function newMenu()
{
	$('#nav>li').mouseover(function(){
		showSubmenu($(this).attr("id"));
	});

	$('#nav>li').mouseout(function(){
		hideSubmenu($(this).attr("id"));
	});
}

function initStandBlock(selectedCompetitionId, tabId, baseUrl)
{
	selectedCompetitionId = selectedCompetitionId;
	tabId = tabId;
	selectedTab = 'stand';
	baseUrl = baseUrl; 
	updateStandBlock(selectedCompetitionId, selectedTab,baseUrl,tabId);
}

function changeCountry(countryId)
{
	selectedCompetitionId = countryId;

	if (selectedTab == 'stand')
	{
		updateStandBlock(countryId, 'stand',baseUrl,111);
	}
	else
	{
		updateStandBlock(countryId, 'fixtures',baseUrl,333);
	}
}

function standBlockStand()
{
		selectedTab = 'stand';
		updateStandBlock(selectedCompetitionId, selectedTab,baseUrl,111);
		
		$('#standButton').addClass('active');
		$('#speelweekButton').removeClass('active');
}

function standBlockWeek()
{
	selectedTab = 'fixtures';
	updateStandBlock(selectedCompetitionId, selectedTab,baseUrl, 333);		

	$('#standButton').removeClass('active');
	$('#speelweekButton').addClass('active');
}



function updateStandBlock(competitionId, typeOfUpdate,baseUrl,tabId)
{
	var weekNr = getWeekNr();
	
	if(competitionId == undefined)
	{
		competitionId = '4241';
	}

	if(typeOfUpdate == undefined)
	{
		typeOfUpdate = 'stand';
	}

	ajaxUrl = baseUrl + '?dbid=' + competitionId + '&homeStandWeek=' + weekNr + '&homeStandCompetitieID=' + competitionId + '&homeStandTabID=' + tabId;

	$.ajax({  
		type: "GET",  
		 
		url: ajaxUrl,  
		success: function (html) 
		{
			$("#home_ajax_content").html(html);
		}
	});
}


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 showSubmenu(id)
{
	var s_subMenu = "#submenu_" + id;
	var s_mainItem = "#nav>li#"+id;
	
	if(currentSubMenu != undefined && currentSubMenu != '')
	{
		var s_activeSubMenu = "#"+currentSubMenu;
		var s_activeMenuItem = "#"+activeMenuItem;
	}
	
	if(iconsOff == 'true')
	{
		if('#'+id != s_activeMenuItem)
		{
			$(s_activeMenuItem).removeClass('activeItem');
			$(s_activeMenuItem).addClass('activeItemNoTab');			
		}

		$(s_mainItem).addClass('activeItem');
		$(s_activeSubMenu).css('display', 'none');
		$(s_subMenu).css('display', 'block');
		$("#clubsSub").css('display', 'none');
	}
	else
	{
		$(s_mainItem).addClass('activeItem');
		$(s_subMenu).css('display', 'block');
		$("#clubsSub").css('display', 'none');
	}
}	

function hideSubmenu(id)
{
	var s_subMenu = "#submenu_" + id;

	if(currentSubMenu != undefined && currentSubMenu != '')
	{
		var s_activeSubMenu = "#"+currentSubMenu;
		var s_activeMenuItem = "#"+activeMenuItem;
	}

	if(iconsOff == 'true')
	{
		$(s_subMenu).css('display', 'none');
		
		$(s_activeMenuItem).removeClass('activeItemNoTab');
		$(s_activeMenuItem).addClass('activeItem');
	
		$(s_activeSubMenu).css('display', 'block');
		$("#nav>li").removeClass('activeItem');
		$(s_activeMenuItem).removeClass('activeItemNoTab');
		$(s_activeMenuItem).addClass('activeItem');
	}
	else
	{
		$(s_subMenu).css('display', 'none');
		$("#clubsSub").css('display', 'block');
		$("#nav>li").removeClass('activeItem');
	}
}


//Media tabs
function switchMediaTabs(categoryId)
{
	var catToShow = "#"+categoryId.replace("show_", "");
	
	$('.tabContent').css('display', 'none');
	$('#mainTabs>li>a').removeClass('active');
	$(catToShow).css('display','block');
	$('#'+categoryId).addClass('active');
}

function addItemToList(createDate, link, title, itemType)
{
	createDate = makeDate(createDate);
	
	if(firstItem == '')
	{
		firstItem = itemType;
	}

	//Highlights
	if(itemType == 'highlights' && i_highlightsCounter < 5)
	{
		s_highlightsContent += '<li><span class="articleDate">'+ createDate + '</span><a href="' + link + '">' + title +'</a></li>';		
	}
	else if(itemType == 'media' && i_mediaCounter < 5)
	{
		s_mediaContent += '<li><span class="articleDate">'+ createDate + '</span><a href="' + link + '">' + title +'</a></li>';
	}
	else if(itemType == 'radio' && i_radioCounter < 5)
	{
		s_radioContent += '<li><span class="articleDate">'+ createDate + '</span><a href="' + link + '">' + title +'</a></li>';
	}
	else if(itemType == 'rtl' && i_rtlCounter < 5)
	{
		s_rtlContent += '<li><span class="articleDate">'+ createDate + '</span><a href="' + link + '">' + title +'</a></li>';
	}
}

function buildLists()
{
	if(buildedList != true)
	{
		s_highlightsContent += '</ul>';
		s_mediaContent += '</ul>';
		s_radioContent += '</ul>';
		s_rtlContent += '</ul>';

		$("#articles").append(s_highlightsContent);
		$("#viMedia").append(s_mediaContent);
		$("#viRadio").append(s_radioContent);
		$("#viRtl").append(s_rtlContent);	
		
		buildedList = true;
	}

	if(firstItem == 'highlights')
	{
		$("#show_articles").addClass('active');
		$("#articles").css('display', 'block');
	}
	else if(firstItem == 'media')
	{
		$("#show_viMedia").addClass('active');
		$("#viMedia").css('display', 'block');
	}
	else if(firstItem == 'radio')
	{
		$("#show_viRadio").addClass('active');
		$("#viRadio").css('display', 'block');
	}
	else
	{
		$("#show_viRtl").addClass('active');
		$("#viRtl").css('display', 'block');		
	}
	
	return true;
}

function makeDate(s_date)
{
	var a_newdate = s_date.split('-');
	var s_articleDate = a_newdate[2] + '-' + a_newdate[1]; 
	return(s_articleDate);
}


//Podium @ homepage 
function initPodium()
{
	selectedItem = 0;
	autoActive = false;
	counter = 0;
	itemArray = new Array();
	$('#headlinesPodium>ul').find('li').each(function(){
		itemArray[counter] = $(this).attr("id");
		counter++;

		$(this).mouseover(function(){changeMain($(this).attr("id")); stopAutoAnimation();});
		$(this).mouseout(function(){startAutoAnimation();});
	});
	
	$("#articleContent").mouseover(function(){stopAutoAnimation();});
	$("#articleContent").mouseout(function(){startAutoAnimation();});
	
	firstItem = true;
	nextItemId = itemArray[0];
	changeMain(nextItemId);
	startAutoAnimation();
}


function changeMain(id)
{
	var strippedId = id.replace('title_', '');
	var divToSelect = "#item_"+strippedId;
	var titleToSelect = "#title_"+strippedId;

	$('.articleContent').stop().animate({
		opacity: 0
		}, 350, function(){}).css('z-index', '0');

	$(".articleItem").removeClass('active');
	

	$(divToSelect).stop().css('z-index', '3').animate({
		opacity: 1
		}, 350, function(){});
	
	selectedItem = id;
	$(titleToSelect).addClass('active');
}


//Function to start the caroussel animation (again)
function startAutoAnimation()
{
	autoActive = true;
	changeAuto();
}

//Function to stop the caroussel animation
function stopAutoAnimation()
{
	$("#caroussel").stopTime('controlled');
}

//This function creates the caroussel effect after 4 seconds
function changeAuto()
{
	if(autoActive == true)
	{
		$("#caroussel").everyTime(4500, 'controlled', function() 
		{
			if (firstItem != false)
			{
				var currentItemPosition = 0;
				firstItem = false;
			}
			else
			{
				var currentItemPosition = jQuery.inArray(selectedItem, itemArray);
			}
			
			currentItemPosition = parseInt(currentItemPosition);

			if(currentItemPosition == 4)
			{
				nextItem = 0;
			}
			else
			{
				nextItem = currentItemPosition + 1;	
			}

			nextItemId = itemArray[nextItem];

			changeMain(nextItemId);
		});
	}
	else
	{
		return false;
	}
}

//Carousel functions
function initBottomRail()
{
	marginLeft = 0
	numberOfItems = $('#contentRailList>li').size();
	maxMarginLeft = (numberOfItems * -228)+912;

	$('a#previousItem').mousedown(function(){stopAutoAnimation(previousItem());});
	$('a#nextRailItem').mousedown(function(){stopAutoAnimation(showNextItem());});
}


function previousItem()
{
	if(marginLeft == 0)
	{
		return false;
	}
	else
	{
		marginLeft = parseInt(marginLeft) + 228;
		$('#contentRailList').stop().animate({
		left: marginLeft
		}, 750, function(){});
	}
}

function showNextItem()
{
	if(marginLeft == maxMarginLeft || marginLeft < maxMarginLeft)
	{
		return false;
	}
	else
	{
		 marginLeft = parseInt(marginLeft) - 228;
	 
		$('#contentRailList').stop().animate({
		left: marginLeft
		}, 750, function() { });
	}	
}

/* Shareblocks */

function activeShareBlock()
{
	var s_articleTitle = document.title;
	var s_articleUrl = location.href;
	var s_articleTitle = s_articleTitle.replace("Voetbal International - ", " ");

	//Twitter
	var s_tweetLink = 'http://twitter.com/share?&text= '+ s_articleTitle +' %23vi';
	var s_onClickLink = "window.open('http://twitter.com/share?via=vi_nl&text="+ s_articleTitle + " %23vi','Tweet dit item', 'width=715,height=450'); return false";
	
	document.getElementById('twitterLink').setAttribute("href", s_tweetLink);
	document.getElementById('twitterLink').setAttribute("onclick", s_onClickLink);
	
	
	//Mail a friend
	var s_articleUrl = location.href;
	var s_maf_url = "http://www.vi.nl/Mail-a-friend.htm?url="+ s_articleUrl;
	var s_maf_click_url = "window.open('http://www.vi.nl/Mail-a-friend.htm?url="+ s_articleUrl +"','Stuur dit item door', 'width=715,height=450'); return false";
	
	var appendMaf = '<a href="'+s_maf_url+'" onclick="'+s_maf_click_url+'" target="_blank" id="mafLink"><img src="http://www.vi-devel.nl/pieter/share/mail_icon.png" alt="Stuur dit bericht door" title="Stuur dit bericht door" /></a>';

	document.getElementById('mailHolder').innerHTML = appendMaf;		
}

function activeShareBlockHyves()
{
	//Hyves
	s_articleTitleHyves = document.title;
	s_articleTitleHyves = s_articleTitleHyves.replace("Voetbal International - ", "");
	s_articleUrlHyves = '[url=' + location.href + ']'+location.href+'[/url]';
}


/* Tabs on different pages (players, competitions, teams) */
function showSubmenuTabs(id,activeItem)
{
	var s_activeTab = "#navTabs>li#"+activeItem;
	var s_hoverTab  = "#navTabs>li#"+id;

	var s_subTabMenu = "#tabs_submenu_" + id;
	var s_activeSubMenu = "#tabs_submenu_" + activeItem;
	
	if(s_subTabMenu != s_activeSubMenu)
	{
		$(s_activeTab).removeClass('activeItem');
		$(s_activeTab).addClass('activeItemNoTab');			
	}
	
	$(s_hoverTab).addClass('activeItem');
	

	$(s_activeSubMenu).css('display', 'none');
	$(s_subTabMenu).css('display', 'block');
}	

function hideSubmenuTabs(id, activeItem)
{
	var s_activeTab = "#navTabs>li#"+activeItem;
	var s_hoverTab  = "#navTabs>li#"+id;
	
	var s_overMenu =  "#tabs_submenu_" + id;
	var s_activeSubMenu = "#tabs_submenu_" + activeItem;

	$(s_hoverTab).removeClass('activeItem');
	$(s_activeTab).removeClass('activeItemNoTab');
	$(s_overMenu).css('display', 'none');

	$(s_activeTab).addClass('activeItem');			
	$(s_activeSubMenu).css('display', 'block');
}



/*
    TABS
*/



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;
}*/
