// variables throughout all scripts
var jsFunctionsRegistry = new Array;

// variables local to this page only
var pageRegionStates;

$(document).ready(function(){

	applyRegionActions();
	renderEmails();
	
});


// ======================================================== common onload function

function applyRegionActions() {

	resolveRegionVisibility();
	
	pageRegionStates = '';
	
	pageRegionStates = $('.areaHidden .areaToggle, .areaVisible .areaToggle').click( function() {
				
		var parentID = $(this).parent().attr('id');
		updateCookie(parentID);
	
		var thisParent = $(this).parent();
		var currentState = thisParent.attr('class');
		
		if (currentState == 'areaHidden') {
			thisParent.attr('class', 'areaVisible');
			$(this).siblings('.areaAffected').slideDown('slow');
		} else {
			thisParent.attr('class', 'areaHidden');
			$(this).siblings('.areaAffected').slideUp('slow');
		}
		
	}); 
	
	// initialise the showHide regions
	$(".areaToggleData").bind("click", function(event){ 
			event.preventDefault(); 
			
			var $this = $(this);
			
			dataToLoad = $this.attr('href');
			
			var $targetDiv = $this.next('.areaAffected');
			var $thisParent = $this.parent('.areaHidden, .areaVisible'); // .showhide locate IMMEDIATE parent
			
			if ($thisParent.is('.areaHidden')) {
				$targetDiv.load(dataToLoad, function () {
					$targetDiv.slideDown( 200, function() {
						$thisParent.addClass('areaVisible')
						$thisParent.removeClass('areaHidden')
					});
				});
			} else {
				$targetDiv.slideUp( 200, function() {
						$thisParent.addClass('areaHidden')
						$thisParent.removeClass('areaVisible')
						$targetDiv.html('');
				});
				
			}
			  				
	} );
	
	
}


function resolveRegionVisibility() {
	
	$('.areaAffected').each( function() {
		// remember final height of everything so that reveals are smooth
 		if ($('.areaAffected').find('.fckArea, .fckSimple, .fckNarrow').length == 0) $(this).attr('style', 'height: ' + this.offsetHeight + 'px');
		$(this).siblings('.areaToggle').attr('href', 'javascript:;');
	}); 
	$('.areaHidden .areaAffected').hide(); 
}


// ======================================================== old  disguise emails scripts

function econvert(str)
{
  str = str.replace(/_DOT_/gi, '.');
  str = str.replace(/_AT_/gi, '@');
  return str;
}

function renderEmails()
{
  var spans = document.getElementsByTagName("span");
  for (i=0; i< spans.length; i++) 
    if (spans[i].className=="email") 
      spans[i].innerHTML = econvert(spans[i].innerHTML);
}

// ======================================================== Cookie Update For Showing Hiding Areas

function updateCookie(changingStatus) {

			//load cookie
			if ($.cookie('openPanes') != null) {
				var cookieContents = $.cookie('openPanes');
			} else {
				var cookieContents = '';
			}
			
			var extant = 0;
			var newList = '';
						
			if (cookieContents!='') {
				var openPaneArray = cookieContents.split(' ');
				for (var i=0; i < openPaneArray.length; i++ ) { 
					if (changingStatus != openPaneArray[i]) {
						if (openPaneArray[i]!='') {
							newList += openPaneArray[i] + ' ';
						}
					} else {
						extant++;
					}
				}
			}
			
			if (extant==0) {
				newList += changingStatus;
			}
			
			$.cookie('openPanes', newList, { path: '/' } );	

} 




// ======================================================== Converts boxes to currency

function toCurrency(value) {
	if (is_numeric(value)) {
		newValue = new Number(value);
	} else {
		newValue = 0;
	}
	return newValue.toFixed(2);
}

function is_numeric( mixed_var ) {
    return !isNaN(mixed_var * 1);
}

function is_colour( hexcolor ) {
	var strPattern = /^#([0-9a-f]{1,2}){3}$/i;
	return strPattern.test(hexcolor);
}


function str_replace (search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'

    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
