var slideTimer;

$(document).ready(function()
{
	
	//Initialize the main menu drop downs
	//$("ul.topnav li").hover(dropMenus);
	
	//Initialize the homepage carousel of featured photos
	$('.orange div p a').bind('click', function(event) { event.stopPropagation(); });
	
	$('#feature_thumbs a.thumb').click(thumbnailImage_Click);
	$('#slidePrev').click(slidePrev_Click);
	$('#slideNext').click(slideNext_Click);
	slideTimer = window.setInterval('nextSlide(1)', 6000);
	
	//Used on the Admissions FAQ page
	$('.answer').hide();
	$('.question').click(accordion);
	
	//Used on the Employment page
	$('.employment-description').hide();
	$('.employment-details').click(accordionEmployment);
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupAlertClose").click(function(){
		//set the cookie so the pop-up won't show again
		$.cookies.set('alertAccepted', 'acceptedNotice', {path : '/'});
		disablePopup();
	});
	var cookie_value_alertAccepted = $.cookies.get('alertAccepted', {path : '/'});
	
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	//Load Popup IF Cookie IS NOT Set
	
	$(window).load(function(){
		if(cookie_value_alertAccepted != "acceptedNotice"){
			//centering with css
			centerPopup();
			//load popup
			loadPopup();
		}
	});
	
});

function slideNext_Click()
{
	window.clearInterval(slideTimer);
	nextSlide(1);
	return false;	
}

function slidePrev_Click()
{
	window.clearInterval(slideTimer);
	nextSlide(-1);
	return false;	
}

function nextSlide(dir)
{
	var otherend = (dir > 0) ? 'first' : 'last';
	var selected = $('#feature_thumbs li.selected');
	if(selected != null)
	{
		// Find the next/prev slide
		next = (dir > 0) ? selected.next() : selected.prev();
		if(next.length == 0)
			next = $('#feature_thumbs li:' + otherend); // Cycle back to the other end
	}
	else
		next = $('#feature_thumbs li:' + otherend);
	
	scrollToThumbnail(next);
	activateSlide(next);
	
	return false;
}

function scrollToThumbnail(next)
{
	var last = $('#feature_thumbs li').length - 1;
	var index = $('#feature_thumbs li').index(next);
	if(index > 2)
		if(index != last)
			$('#feature_thumbs').scrollTo(next.prev().prev(), 800);
		else
		{
			// We dont want to advance too far if this is the last thumbnail
			$('#feature_thumbs').scrollTo(next.prev().prev().prev(), 800);
		}
	else
		$('#feature_thumbs').scrollTo('li:first', 800);	
}

function activateSlide(slide)
{
	// First, hide the current thumbnail
	$('#feature_thumbs li.selected').animate(
		{ width: 64 },
		{
			duration: 500,
			easing: 'linear',
			complete: function () {
				var color = $(this).children('a').attr('rel');
				$(this).removeClass(color + '_selected selected');//.find('div').hide();
		}
	});
	
	// Now, show the newly selected thumbnail
	var thumb = slide.find('a.thumb');
	var color = thumb.attr('rel');
	slide.addClass(color + '_selected selected').animate(
		{ width: 296 },
		{
			duration: 500,
			easing: 'linear'
		}
	);
	
	// Fade out the previously shown photo
	$('#feature .feature.selected').removeClass('selected').stop().fadeOut(500);
	
	// Fade in the new selected photo
	var index = $('#feature_thumbs a.thumb').index(thumb);
	var feature = $('.feature').eq(index);
	feature.addClass('selected').stop().fadeIn(500);
}

function thumbnailImage_Click(event)
{
	if (!$(this).parent().hasClass('selected'))
	{
		var slide = $(this).parent();
		activateSlide(slide);
	}
	
	return false;
}

/********** Used on the Admissions FAQ page **************/

function accordion(event)
{	
		  
	//NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
	$('.answer').slideUp('normal');

	//IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
	if($(this).next().is(':hidden') == true) {
		  
		//OPEN THE SLIDE
		$(this).next().slideDown('normal');
	 } 
}

/********** Used on the EMPLOYMENT page **************/

function accordionEmployment(event)
{	
		  
	//NO MATTER WHAT WE CLOSE ALL OPEN SLIDES
	$('.employment-description').slideUp('normal');

	//IF THE NEXT SLIDE WASN'T OPEN THEN OPEN IT
	if($(this).next().is(':hidden') == true) {
		  
		//OPEN THE SLIDE
		$(this).next().slideDown('normal');
	 } 
}

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupAlert").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupAlert").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupAlert").height();
	var popupWidth = $("#popupAlert").width();
	//centering
	$("#popupAlert").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		//"top": 10,
		"left": windowWidth/2-popupWidth/2
		//"left": 180
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

/******************************************************
	MAIN MENU
********************************************************/

var menu=function(){
	var t=15,z=50,s=2,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.mh=c.offsetHeight; c.style.height=0; c.style.width=177}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=100; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=100; c.style.filter='alpha(opacity='+(1*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();


var quickList=function(){
	var t=15,z=50,s=2,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.mh=c.offsetHeight; c.style.height=0; c.style.width=177}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=100; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=100; c.style.filter='alpha(opacity='+(1*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();
