// @todo: support for global ajax popups.
Event.observe(window, 'load', function() {
	
	var show_popup = function () {
		//$('global_popup_title').update("Postcode niet gevonden.");
		//$('global_popup_message').update(message);		
		$('global_popup_container').show();
	};	
	
	if ($('global_popup_container')) {
		show_popup(); 
	}
});

var oActiveSubmenu;
var sMijngegevensSiteSection = 'ibw_mijngegevens';
var loggedin;

/**
 * show submenu and hide active one
 * 
 * @param string sDivID
 * @return void
 */
function getSubmenu(sDivID)
{
	if(oActiveSubmenu){
		oActiveSubmenu.addClassName('hide');
	}
	
	if(oSubmenu = $(sDivID))
	{
		oSubmenu = $(sDivID);
		oSubmenu.removeClassName('hide');
		oActiveSubmenu = oSubmenu;
	}
}

function CheckAll(elId){
	checkboxen = document.getElementById(elId).getElementsByTagName('input');
	count = checkboxen.length;
    for (i=0; i < count; i++) 
	{
     checkboxen[i].checked = 1;
	}
}
function UncheckAll(elId){
	checkboxen = document.getElementById(elId).getElementsByTagName('input');
	count = checkboxen.length;
    for (i=0; i < count; i++) 
	{
     checkboxen[i].checked = 0;
	}
}


function hideSelectBox(sId){
	if(isIE6())
	{
		$(sId).select('select').each(function(oEl){oEl.style.visibility = 'hidden';});
	}
}

function showSelectBox(sId)
{
	if(isIE6())
	{
		$(sId).select('select').each(function(oEl){oEl.style.visibility = 'visible';});
	}
}

function isIE6()
{
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		var iVersion = parseFloat(navigator.appVersion.split("MSIE")[1])
		
		if(iVersion < 7)
		{
			return true;
		}
	}
	return false;
}

function isIE7()
{
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		var iVersion = parseFloat(navigator.appVersion.split("MSIE")[1])
		
		if(iVersion == 7)
		{
			return true;
		}
	}
	return false;
}

///////////////////////////
/**
 * Onload functie gaat door onloadFunctions array om die functies uit te voeren.
 */
//window.onload = function( )
//{
//	for(var myIndex = 0; myIndex < onloadFunctions.length; myIndex++ )
//	{
//		if(onloadFunctions[myIndex])
//		{
//			onloadFunctions[myIndex]( );
//		}
//	}
//}

Event.observe(window, 'load', function( ) {
	for(var myIndex = 0; myIndex < onloadFunctions.length; myIndex++ )
	{
		if(onloadFunctions[myIndex])
		{
			onloadFunctions[myIndex]( );
		}
	}
});


function updateContentHeight()
{
	var bElementsExists = $('content') && $('page');
	if (!bElementsExists)
	{
		return;
	}
	
	var iNewHeight;
	
	var iMinHeight = 450;
	
	var iCurrentHeight = ($('page').getHeight() - 150);
	if(iCurrentHeight > iMinHeight)
	{
		iNewHeight = iCurrentHeight;
	}
	else
	{
		iNewHeight = iMinHeight;
	}
	
	$('content').style.height = iNewHeight + "px";
	$('footer').style.marginTop = "200px";
}

function updateOptionBasket()
{
	new Ajax.Updater('optionbasket', '/output/html/mijndomesta/'+sMijngegevensSiteSection+'/showoptionbasket',{evalScripts:true,onSuccess: updateContentHeight});
}

function changeField(field, empty)
{					
	if (field.alt == '')
	{
		field.alt = field.value;
	}
	if (empty && field.value == field.alt)
	{
		field.value = '';
	}
	else if(field.value == '')
	{
		field.value = field.alt;
	}
}

function changePasswordField(field, toText)
{
	if (toText == true)
	{
		if (field.value == '')
		{
			field.style.display = 'none';
			var switchTo = document.getElementById('passwordfieldText');
			switchTo.style.display = 'inline';
		}
	}
	else
	{
		field.style.display = 'none';
		var switchTo = document.getElementById('passwordfield');
		switchTo.style.display = 'inline';
		switchTo.value = '';
		if(typeof(switchTo) != "undefined")
		{
			switchTo.focus();
		}
	}
}

function centerElement(sId)
{
	var iHeight;
	
	var aOffsets = document.viewport.getScrollOffsets(); 
	
	iHeight = aOffsets['top'] + (document.viewport.getHeight() / 2);

	$(sId).style.top = iHeight+'px';
}

function fixFieldsetInlogIE6(){
	if(isIE7()){
		$('inloggen').style.marginLeft = "20px";
	}
}

function loadOnload(){
	Event.observe(window, 'load', fixFieldsetInlogIE6);
}

Event.observe(window, 'load', updateOptionBasket);
Event.observe(window, 'load', updateContentHeight);


/**
 *  Show this Hide active
 */
var activeShow;
function showHide(Id)
{
	if(activeShow)
	{
		$('tabContent' + activeShow).style.display = 'none';
		$('tab' + activeShow).removeClassName('active');
	}
	if(activeShow == Id)
	{
		$('tabContent' + Id).style.display = 'none';
		$('tab' + Id).removeClassName('active');
		Id = '';
	}
	if(Id != '')
	{
		activeShow = Id;

		$('tabContent' + Id).style.display = 'block';
		$('tab' + Id).addClassName('active');
	}
	else
	{
		activeShow = null;
	}
	return false;
}



/**
 * Make dButton fully clickable and hoverable for IE
 * by adding event to parent span
 * 
 * @author Sro
 * @version 0.2
 * @copyright Copyright (c) 2010, Zig Websoftware
 * @buildOn Prototype
 * @addon Dynamic Button CSS
 * 
 * Only works for IE sinds FF works with CSS
 */
function initDynamicButton() 
{
	var aButtons = $$('span.buttonContainer');
	aButtons.each(function(oButton, iKey)
	{
		oButton.observe('click', dButtonSimulateClick);
		oButton.observe('mouseover', dButtonDoMouseOver);
		oButton.observe('mouseout', dButtonDoMouseOut);
	});
}

addOnloadFunction(initDynamicButton);

function dButtonSimulateClick(oEvent)
{
	var oEl = oEvent.element();
	var oElement = oEl.down();
	if(typeof(oElement) == 'object' && oElement != null)
	{
		if(oElement.tagName != "INPUT" && oElement.tagName != "A" )
		{
			oElement = oEl.down(1);
		}
		if(oElement.tagName == "INPUT" || oElement.tagName == "A")
		{
			if(oElement.click)
			{
				oElement.click();
			}
			else if(document.createEvent)
			{
				if(oElement.tagName == 'A' && oEvent.target !== oElement)
				{
					//execute the link (FF only executes the onclick)
					var oEvt = document.createEvent("MouseEvents");
					oEvt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
					var bAllowDefault = oElement.dispatchEvent(oEvt);
					// you can check allowDefault for false to see if
					// any handler called evt.preventDefault().
				}
			}
		}
	}
}

function dButtonDoMouseOver(oEvent)
{
	var oElement = oEvent.element();//get the span element
	if(oElement.tagName == "SPAN" && oElement.hasClassName('buttonContainer'))
	{
		oElement.addClassName('hover');
	}
	else if(oElement.tagName == "INPUT")
	{
		oElement.up('span', 1).addClassName('hover');
	}
	else if(oElement.tagName == "SPAN" && !oElement.hasClassName('buttonContainer'))
	{
		oElement.up(0).addClassName('hover');
	}
}

function dButtonDoMouseOut(oEvent)
{
	var oElement = oEvent.element();//get the span element
	if(oElement.tagName == "SPAN" && oElement.hasClassName('buttonContainer'))
	{
		oElement.removeClassName('hover');
	}
	else if(oElement.tagName == "INPUT")
	{
		oElement.up('span', 1).removeClassName('hover');
	}
	else if(oElement.tagName == "SPAN" && !oElement.hasClassName('buttonContainer'))
	{
		oElement.up(0).removeClassName('hover');
	}
}
