/**
 * file contains all default zig script
 * 
 * - onload function loader
 * - clear input elements and save default value
 * 
 */

Event.observe(window, 'load', doOnload);
var aOnloadFunctions = new Array();

/**
 * Onload function Load all functions stored in onloadFunctions
 * 
 * @return void
 */
function doOnload()
{
	for(var iIndex = 0; iIndex < aOnloadFunctions.length; iIndex++ )
	{
		if(aOnloadFunctions[iIndex])
		{
			aOnloadFunctions[iIndex]();
		}
	}
}

/**
 * Store a function to run in window.Onload
 * 
 * @param string sName
 * @return void
 */
function addOnloadFunction(sName)
{
	iIndex = aOnloadFunctions.length + 1;

	aOnloadFunctions[iIndex] = sName;
}

/**
 * Clear default texts
 * 
 * @author Bhe
 * @version 0.1
 * @copyright Copyright (c) 2009, Zig Websoftware
 * @buildOn Prototype
 */
function initClearElementDefaults() 
{
    var aFormInputs = $$('input');
    var iInputFields = aFormInputs.length;
    
    for (var iCounter = 0; iCounter < iInputFields; iCounter++) 
    {
        var oElement = aFormInputs[iCounter];

        if ((oElement.type == 'text' || oElement.type == 'password') && oElement.className.match(/\bcleardefault\b/)) 
        {       
            oElement.observe('focus', clearDefaultText);
            oElement.observe('blur', replaceDefaultText);
            
            if (oElement.value != '') 
            {
            	oElement.defaultText = oElement.value;
            }
        }
    }
}

function clearDefaultText(e) 
{
    var oTarget = window.event ? window.event.srcElement : e ? e.target : null;
    
    if (!oTarget)
    {
    	return;
    }
    
    if (oTarget.value == oTarget.defaultText) 
    {
    	oTarget.value = '';
    }
}

function replaceDefaultText(e) 
{
    var oTarget = window.event ? window.event.srcElement : e ? e.target : null;
    
    if (!oTarget)
    {
    	return;
    }
    
    if (oTarget.value == '' && oTarget.defaultText) 
    {
    	oTarget.value = oTarget.defaultText;
    }
}

addOnloadFunction(initClearElementDefaults);


/**
 * InfoBalloon
 * 
 * @author Ntb
 * @version 0.2
 * @copyright Copyright (c) 2010, Zig Websoftware
 * @buildOn Prototype
 */
var activeInfoId = '';
function showInfo(infoName)
{
	if(activeInfoId != '' && $(activeInfoId))
	{
		$(activeInfoId).fade({ duration: 0.1 });
	}
	if(!infoName && typeof(elId) !== 'undefined' && $(elId))
	{	
		$(elId).fade({ duration: 0.1 });
		$(elId).up('.information').style.zIndex = 1;
	}

	elId = 'info_'+ infoName;
	if(infoName != '' && $(elId))
	{
		$(elId).appear({ duration: 0.2, queue: 'end' });
		$(elId).up('.information').style.zIndex = 500;
		activeInfoId = elId;
	}
}
