/**
 * - onload function loader
 * - clear input elements and save default value
 */

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: JQuery version of the above
 * 
 * @author Bhe, Kha
 * @version 0.1
 * @copyright Copyright (c) 2011, Zig Websoftware
 * @buildOn jQuery
 */

function jQuery_initClearElementDefaults() 
{
    var aFormInputs = jQuery('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/)) 
        {       
            jQuery(oElement).focus(clearDefaultText);
            jQuery(oElement).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(jQuery_initClearElementDefaults);

/**
 * Change styling onfocus and onblur (Kha: translated to jQuery)
 */
function changeColor(oElement, sEventType)
{
	if (sEventType != 'blur')
	{
		jQuery(oElement).css({'border' : '1px solid #009DE0', 'color' : '#333333'});
	}
	else
	{
		jQuery(oElement).css({'border' : '1px solid #72BED8', 'color' : '#72BED8'});
	}
}



/**
 * toggle FAQ Items
 *
 * @param id 		the id of the FAQ item to hide or show
 * @param single	true to show only one item at a time, false the open as many as you want
 */
function custom_tx_irfaq_toggleFaq(id, hash) {
	
	var faq_id = '#irfaq_q_'+id+'_'+hash; //question
	bActive = jQuery(faq_id).hasClass('active_faq_item');
	
	// show only one Q+A at a time
	custom_tx_irfaq_closeAll(false, hash);
	
	if(!bActive)
	{
		custom_tx_irfaq_showFaq(id, false, hash);
	}
}

/**
 * shows or hides a FAQ item at a time depending on the given status
 *
 * @param id 		the id of the FAQ item to hide or show
 * @param show	true to show the item, false to hide it
 */
function custom_tx_irfaq_showFaq(id, show, hash) {
	var faq_id = 'irfaq_a_'+id+'_'+hash; //answer
	
	jQuery('#irfaq_q_'+id+'_'+hash).addClass('active_faq_item');
	jQuery('#irfaq_a_'+id+'_'+hash).addClass('tx-irfaq-dynans-visible');
	jQuery('#irfaq_a_'+id+'_'+hash).removeClass('tx-irfaq-dynans-hidden');
}

/**
 * shows or hides all FAQ items with one click
 *
 * @param mode	true to show the items, false to hide them
 */
function custom_tx_irfaq_closeAll(mode, hash) {
	
	jQuery('div.tx-irfaq-question').removeClass('active_faq_item');
	jQuery('div.tx-irfaq-answer').removeClass('tx-irfaq-dynans-visible');
	jQuery('div.tx-irfaq-answer').addClass('tx-irfaq-dynans-hidden');
}


function toggleThis(sId)
{
        jQuery('#' + sId).toggle(200);
       
        if(jQuery('#' + sId + '-header').find('span.togglesymbol').text() == '+')
        {
                jQuery('#' + sId + '-header').find('span.togglesymbol').html('&minus;');
        }
        else
        {
                jQuery('#' + sId + '-header').find('span.togglesymbol').text('+');
        }
       
        return false;
}

// KHa: creates a nice datepicker for field with class "datepicker"
// Used for the mm_forum profile edit
// Needs jQuery-ui!

function activateDatePickers()
{
	jQuery(document).ready(function(){
			jQuery( ".datepicker" ).each(function(iCounter, oInput){
				
				oInput = jQuery(oInput);
				
				var oConfig = {
					changeMonth: true,
					changeYear: true,
					dateFormat: 'dd-mm-yy',
					yearRange: '-67:+0',
					monthNamesShort: ['Jan', 'Feb', 'Mrt', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'],
					dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za']
				};
				
				var sId = oInput.attr('id');
				
				if (sId) {
					
					var oAltInput = jQuery('#' + sId + '-alt').get(0);
					if (typeof oAltInput != 'undefined') {
						oConfig.onSelect = function(sDateText, oInst)
						{
							sAltInputId = '#' + jQuery(this).attr('id') + '-alt';
							// KHa: add 3600+1 instead of 3600 because we want 1-1-1970 (timestamp 0) to be a valid date (will become 1)
							var sTimestamp = (jQuery.datepicker.formatDate('@', jQuery(this).datepicker('getDate')) / 1000) + 3601;
							jQuery(sAltInputId).val(sTimestamp); 
						}
					}
				}
				
				oInput.datepicker(oConfig);
				
				
			
			});
	});
}

