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

$(document).ready(function(){

	areaReveals.start();	
	emails.render();	
	if ( $("ul.sf-menu").length ) $("ul.sf-menu").superfish();
	
});

var areaReveals = {
	
	heights: null,
	
	start: function() {
		
		areaReveals.heights = new Array;
		areaReveals.resolveRegionVisibility();
		areaReveals.applyRegionActions();
		
	},
	
	resolveRegionVisibility: function() {
		
		$('.areaAffected').each( function() {
			
			var thisDiv = $(this);
			var divID = thisDiv.parent().attr('id');
				
			// for divs when content is coming later, we can't establish final height so we
			// will ignore those however all others we can record the height so we can have smooth
			// scroll downs
			if ($('.areaAffected').find('.fckArea, .fckSimple, .fckNarrow').length == 0) {
				
				areaReveals.heights[thisDiv] = thisDiv.height();
				
				$(this).css({
					height: thisDiv.height(), 
					overflow: 'hidden'
				});
				
			}
			
			// we dont want the toggle link going anywhere
			$(this).siblings('.areaToggle').attr('href', 'javascript:;');
			
		});
		
		// go through all the panes, interrogate the cookie and if the file is open
		// set the style to be area visible otherwise area hidden
		$('.areaAffected').parent().each( function() {
			
			if ( areaReveals.isOpen( $(this).attr('id') ) ) 
				$(this).addClass('areaVisible').removeClass('areaHidden');			
			else
				$(this).addClass('areaHidden').removeClass('areaVisible');			
				
		});
		
		// ensure that if we've got a .selcted item or anything else 
		// which needs to be seen in the div it stays open
		$('.areaHidden').each( function() {
			if ( $(this).find('.selected, .alerts, .fieldError').length ) $(this).addClass('areaVisible').removeClass('areaVisible');
		});
		
		$('.areaHidden .areaAffected').hide(); 
	},
	
	isOpen: function( divID ) {
		
		if ($.cookie('openPanes') != null) {
			var cookieContents = $.cookie('openPanes');
			var openPaneArray = cookieContents.split(' ');
			for (var i=0; i < openPaneArray.length; i++ ) { 
				if (divID == openPaneArray[i]) return true;
			}

		}
			
		return false;
	
		
	},
	
	applyRegionActions: function() {

		$('.areaToggle').unbind('click').bind("click", function() {
				
			var thisLink = $(this);
			var parentID = $(this).parent().attr('id');
			areaReveals.updateCookie(parentID);
	
	
			var thisParent = $(this).parent();
		
			if ( thisParent.hasClass('areaHidden') ) {
				
				thisParent.addClass('areaVisible');
				thisParent.removeClass('areaHidden');
				thisLink.siblings('.areaAffected').slideDown('slow');
				
			} else {
				
				thisParent.addClass('areaHidden');
				thisParent.removeClass('areaVisible');
				thisLink.siblings('.areaAffected').slideUp('slow');
				
			}
			
		
		}); 
		
		
	},
	
	updateCookie: function( divID, cookieName ) {
		
		if (! cookieName )  cookieName = 'openPanes';
		
		//load cookie
		if ($.cookie('openPanes') != null) {
			var cookieContents = $.cookie( cookieName );
		} else {
			var cookieContents = '';
		}
			
		var extant = 0;
		var newList = '';
						
		if (cookieContents!='') {
			var openPaneArray = cookieContents.split(' ');
			for (var i=0; i < openPaneArray.length; i++ ) { 
				if (divID != openPaneArray[i]) {
					if (openPaneArray[i]!='') {
						newList += openPaneArray[i] + ' ';
					}
				} else {
					extant++;
				}
			}
		}
			
		if (extant==0) {
			newList += divID;
		}

			
		$.cookie( cookieName, newList, {
			path: '/'
		} );			
		
	}
	
}

var emails = {
	
	render: function() {
		
		$('.email').each( function() {		
			var tmp = $(this).html();
			$(this).html( emails.unmask( tmp ) ) 		
		});
		
	},
	
	unmask: function(str) {
		str = str.replace(/_DOT_/gi, '.');
		str = str.replace(/_AT_/gi, '@');
		return str;
	}

}

