/////////////
// Actions //
/////////////

// Set Global Variables
var currentLocationUri = window.location.pathname;

// SET THE CURRENT LOCATION URI BASED ON THE WINDOW.LOCATION.PATHNAME
var uriSplit = currentLocationUri.split("/");
(uriSplit[2] != undefined)? currentLocationUri = uriSplit[1] + "/" + uriSplit[2] : currentLocationUri = uriSplit[1];
// format is "about" or "about/objectives"

// Document Ready
$(document).ready( 
	function() {

		if($('#heroSlideshow').length == 0) { /* do nothing */ } else {

			$('#heroSlideshow').crossSlide({
			  sleep: 6,
			  fade: 1
			}, [
			  { src: '/crossSlide/slide1.jpg' },
			  { src: '/crossSlide/slide2.jpg' },
			  { src: '/crossSlide/slide3.jpg' },
			  { src: '/crossSlide/slide4.jpg' },
			  { src: '/crossSlide/slide5.jpg' },
			  { src: '/crossSlide/slide6.jpg' }
			]);		
		}

	// RUN PAGE LOAD FUNCTIONS
	addFirstClass();
	slideHandle();
	setTimeout(schoolStatusBar, 500);
	uriNavigation();
	rewriteEmails();
	
	
	// PAGE SPECIFIC
	// PROFILE / EDIT
	if (currentLocationUri == '') {
	
		heroNews();
		
		setTimeout(heroCalendar, 500);
		
	
	} else {
		
	}


	}
); // end doc ready



///////////////
// Functions //
///////////////


function rewriteEmails() {

	var rewrite = $('.email');
	
	rewrite.each( 
	
		function(e) {
	
			var rewriteText = $(this).text();
				
			var pieces = rewriteText.split('(at)');
			
			var theEmail = pieces[0] + "@" + pieces[1];
		
			$(this).html("<a href='mailto:" + theEmail + "'>" + theEmail + "</a>");
		}
	);
	
}


function heroCalendar() {
		
	var link = $('.heroCalendarLink');
	var container = $('#heroTable');
	var details = $('.heroCalendarDescription');
	
	// first, move the details to the container
	// details.appendTo(container);
			
	link.click(
		
		function(e) {
			
			e.preventDefault();
			
			// build the new details div
			if($('#heroCalendarContainer').length == 0) {
				
				$('<div id="heroCalendarContainer"><span class="close"></span><div id="heroCalendarInnerContainer"></div></div>').prependTo(container);
				
			}
			
			var subContainer = $('#heroCalendarContainer');
			
			var innerContainer = $('#heroCalendarInnerContainer');
			
			innerContainer.hide().empty();

			var desc = $(this).attr('id');
			
			$("." + desc).clone().appendTo(innerContainer).show();
			
			if($('#heroCalendarInnerContainer img').length == 0) {
				
				innerContainer.prepend('<img src="/wp-content/uploads/2010/10/default-calendar-photo.jpg" />');
			}
			
			$('#heroCalendarContainer').fadeIn('fast');
			
			innerContainer.fadeIn();
			
			close();
			
			return false;
		}
	);	
}

function uriNavigation() {

	var menuContainer = $('#access');
	var menuItem = $('#access li');
	var menuItemLink = $('#access li a');
	
	menuItemLink.each(
		
		function() {
		
			var thisHref = $(this).attr('href');
			
			if(thisHref == "http://www.pinares.org/" + uriSplit[1] + "/") {
				
				if(uriSplit[1] == 'about') {
				
					$(this).css('background','url("/green-theme-images/menu-1-hover.png")');
					
				} else {
					
					$(this).css('background','url("/green-theme-images/menu-234-hover.png")');
				}
				
			}
		
		}
		
	);
	
	
	$("#subMenuUl li.widget_nav_menu ul.menu li.menu-item a").mouseenter(
	
		function() {
			
			$(this).nextAll("ul").stop().animate({height:'100%'},250).children("li").stop().fadeTo(250,1,
				function() {
					
					$(this).parent().show()
					
				}
			);
		}
	);
	
	
	
	$('#subMenuUl').hoverIntent(
	
		function() {},
		function() {
			$('#subMenuUl ul.sub-menu').stop().animate({height:0},250).children("li").stop().fadeTo(250,0,
				function() {
					
					$(this).parent().hide()
					
				}
			);
		}
	);
	
	
	$("#subMenuUl li.widget_nav_menu ul.menu li.menu-item ul").hoverIntent(
	
		function() {},
		function() {
			
			$(this).stop().animate({height:0},250).children("li").stop().fadeTo(250,0,
				function() {
					
					$(this).parent().hide()
					
				}
			);
		}
	);
	
	/* add .arrow to specific drop down menu items in left-hand sub navigation */
	$("#subMenuUl li.widget_nav_menu ul.menu li.menu-item ul.sub-menu").parent().children('a').css('background','url("/green-theme-images/drop-down-arrow.png")').css('background-position','97%').css('background-repeat','no-repeat');
	
	
}

function close(subContainer) {

	$('.close').click(
		function () {
			
			var subContainer = $('#heroCalendarContainer');
			
			subContainer.fadeOut('fast');
			
		}
	);

}

function heroNews() {
	
	var rss = $('#fourth');
	
	// rss.appendTo('#heroTableRightScroll');
	rss.hide();
	
}


function schoolStatusBar() {
	
	var schoolStatus = $('#schoolStatus');
	
	schoolStatus.animate({ top: 0}, 600,
		function() {
			
			$(this).animate({ width: 1000}, 900,
				function () {
					
					var theSpan = $('#schoolStatus > div');
					
					theSpan.fadeIn();
					
				}
			);
			
		}
	);
	
	setTimeout(schoolStatusBarHide, 60000);

}

function schoolStatusBarHide() {
	
	var schoolStatus = $('#schoolStatus');
	
	schoolStatus.animate({ top: -38}, 900, 
		function() {
		
			$(this).remove();
		
		}
	);

}

function redirectToApplication() {
	
	window.location="http://www.pinares.org/jobs/online-application-form/";
	
}


function addFirstClass() {

	var firstNavLi = $('#access li:first-child');
	
	firstNavLi.addClass('first');

}


function slideUpContent() {

	var contentDiv = $('#content');
	
	contentDiv.animate({ marginTop: 40}, 500);

}


function slideHandle() {
	
	var slideHandle = $('#subMenuHandle');
	
	var contentDiv = $('#content');
	
	slideHandle.click(
		function() {
			
			if($(this).hasClass('up')) {
			
				contentDiv.animate({ marginTop: 320}, 500);
				$(this).removeClass('up');
				
			} else {
			
				contentDiv.animate({ marginTop: 40}, 500);
				$(this).addClass('up');
			
			}
			
		}
	);
	
	
	
}







/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:200,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);






