/**
 * - onload function loader
 * - clear input elements and save default value
 */


var aOnloadFunctions = new Array();
Event.observe(window, 'load', doOnload);
/**
 * 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);


/**
 * Function change input type fields for Password
 */
function onFocusPassword()
{
	if($('loginfield_text'))
	{
		closeId('loginfield_text');
		showId('loginfield_pass');
		$('loginfield_pass').focus();
	}
}

function onBlurPassword()
{
	if($('loginfield_pass') && $('loginfield_pass').value == '' )
	{
		showId('loginfield_text');
		closeId('loginfield_pass');
	}
}


/**
 * 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;
	}
}


/**
 * ZigPopup Album observer
 * looks if a with rel is clicked
 */
document.observe("dom:loaded", function()
{
	$$('a[rel^=popup]').each(function(oAnchor)
	{	
		$(oAnchor).observe('click', function(oEvent)
		{
			var oTargetAnchor = oEvent.target;
			oEvent.stop();
			
			if (typeof(oTargetAnchor.rel) == 'undefined')
			{
				// target has no "rel" attribute
				// maybe we clicked on the image and we have to select its parent anchor
				oTargetAnchor = oTargetAnchor.parentNode;
			}
			
			var sAlbumName = oTargetAnchor.rel.split('[').shift();

			// determine index
			var iIndex = 0;
			var aAlbumAnchors = $$('a[rel^='+sAlbumName+'\[])');
			
			for (var i = 0 ; i < aAlbumAnchors.length ; i++)
			{	
				if (aAlbumAnchors[i] == oTargetAnchor)
				{
					break;
				}			

				iIndex++;	
			}

			var oImagePopup = new ZigImagePopup($('popup'), {minWidth: 400, verticalScroll: false});
			oImagePopup.setImagesFromSelector(sAlbumName);
			oImagePopup.show(iIndex);
		});
	});		
});

/**
 * ZigPopup Alert function
 * oConfig looks like: {title:'text',content:'text'}
 */
function showAlertPopup(oConfig, bClearClose)
{	
	if(typeof(oConfig) == 'object')
	{
		if(typeof(oConfig.title) == 'undefined')
		{
			oConfig.title = '';
		}
		oAlertPopup = new ZigAlertPopup($('popup'));
		oAlertPopup.setTitle(oConfig.title);
		oAlertPopup.setContent(oConfig.content);
		oAlertPopup.show();
		
		if(bClearClose)
		{
			oAlertPopup.oCloseButtonNode.stopObserving('click');
			oAlertPopup.oCloseButtonNode.observe('click', function() 
			{
				this.hide();
				oAlertPopup.setContent('');
			}.bind(oAlertPopup));
		}
	
		return oAlertPopup;
	}	
}

/**
 * ZigPopup Confirm function
 * in oConfig can be set:
 * - title
 * - content
 * - location Clicking the OK button will go to this location
 * - className Custom class name for the popup
 * - blocking (prevents closing the popup. If blocking, no "closing cross" is shown)
 * - labelClose Label of the close button
 * - labelOk Label of the OK button
 */
function showConfirmPopup(oConfig)
{	
	var oConfirmPopup = new ZigConfirmPopup($('popup'));

	if(typeof(oConfig) == 'object')
	{
		if(typeof(oConfig.title) == 'undefined')
		{
			oConfig.title = '';
		}
		oConfirmPopup = new ZigConfirmPopup($('popup'));
		
		if (typeof(oConfig.blocking) == 'boolean')
		{
			oConfirmPopup.bBlocking = false;
			oConfirmPopup.preparePopupNode();
		}

		if (typeof(oConfig.className))
		{
			oConfirmPopup.oPopupNode.addClassName(oConfig.className);
		}
		
		if (typeof(oConfig.labelClose) != 'undefined')
		{
			oConfirmPopup.setLabelCloseButton(oConfig.labelClose);
		}
		
		if(typeof(oConfig.labelOk) != 'undefined')
		{
			oConfirmPopup.setLabelOkButton(oConfig.labelOk);
		}
		
		oConfirmPopup.setTitle(oConfig.title);
		oConfirmPopup.setContent(oConfig.content);
		
		if(typeof(oConfig.location) != 'undefined')
		{
			oConfirmPopup.setOkHandler(function() {
					document.location = oConfig.location;
				}
			);
		}
		
		oConfirmPopup.show();
	}
}
