''/**
 * Various utilities
 */
var doc = document;
var $p = {
	//Small utility to trip white space from the beginning and end
	trim:function(str) { 
		var result = str.replace(/^\s*/, '').replace(/\s*$/, ''); 
		return result;
	},
	isEmpty:function(obj){
		for(var i in obj){ return false;}
		return true;
	},
	// Cookie functions from http://www.quirksmode.org/js/cookies.html
	clearCookie:function(name){
		$p.createCookie(name,"",-1);		
	},
	createCookie:function(name,value,days){
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else {
			var expires = "";
		}
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie:function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i<ca.length; i++){
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;		
	},
	errorState:function(){
		// If there are no errors, remove the box
		if($('#errors').find('li').length == 0){
			$('#errors').remove();
		}		
	},
	getCharsLeft:function(field, max){
		var count = field.value.split('').length;
		return max - count;
	},
	init:function(){
		$('.windowPop').click(function(event){
			event.preventDefault();
			window.open(this.href, 'newWindow', 'width=1070, height=600, resizable=yes, scrollbars=yes, toolbar=no, location=no, menubar=no')
		});	
		
		// Fancybox Popups 
		$('a.simplePop').fancybox({
			margin : 0,
			padding: 0,
			overlayShow : true,
			centerOnScroll : true,
			scrolling : 'no',
			overlayOpacity : 0.6,
			overlayColor : '#333',
			titleShow : false,
			showNavArrows : false
		});	
		
		// For jumping to a section from another page
		$('a.toSection').live('click', function(event){
			event.preventDefault();
			// Clear old cookie
			$p.clearCookie('pgScrollTo');
			// Break the the link into the URL and the id
			var places = $(this).attr('href').split('#');
			var link = places[0];
			// set the id to a cookie
			$p.createCookie('pgScrollTo', '#'+places[1])
			// go to the link so the scroller can do it's thing
			window.location = link;
		});		
	}
}

$(document).ready(function(){
	$p.init();
})