var new_rotate_boxes = 0;
var rotate_delay;
var TTT1, timer_active;

jQuery(document).ready(function(){

	/* Setup Popup YouTube Video */
	jQuery.fn.youTubePopup = function(){

		if($("#youtube_popup").length < 1){
			$("body").append('<div id="youtube_popup"><div class="close">CLOSE</div><div class="video"></div></div>');
		}

		$(this).overlay({
			target:'#youtube_popup',
			expose: '#888888',
			onBeforeLoad: function(){
				var yHTML = '<object width="640" height="480"><param name="movie" value="http://www.youtube.com/v/YOUTUBE_ID&hl=en_US&fs=1&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="autoplay" value="1"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/YOUTUBE_ID&hl=en_US&fs=1&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" autoplay="1" width="640" height="480"></embed></object>';
				yHTML = yHTML.replace(/YOUTUBE_ID/g,this.getTrigger().attr("alt"));
				$("#youtube_popup .video").html( yHTML );
			}
		});

	};

	if($("#signup_form").length > 0){
		$("#signup_form .email")
			.addClass("blurred")
			.bind("focus",function(){
				$(this).removeClass("blurred");
			})
			.bind("blur",function(){
				if($(this).val() == ""){
					$(this).addClass("blurred");
				}
			});
	}

	$('.global_popup .closebutton').click(function(){
		$('.global_popup').fadeOut();
		return false;
	});

	if(new_rotate_boxes == true){
		TTT1 = setTimeout( newRotateBox, rotate_delay );
		timer_active = 1;

		$("#rotate_box").bind("mouseover",function(){
			clearTimeout(TTT1);
			timer_active = 0;
		});

		$("#rotate_box").bind("mouseout",function(){
			if(timer_active == 0){
				TTT1 = setTimeout( newRotateBox, parseInt( rotate_delay/2, 10) );
				timer_active = 1;
			}
		});


	}

	inside_menu_bar = 0;
	inside_menu_ul  = 0;

	/* Top Nav Menus */
	$("#listMenuRoot > li > a").bind("mouseenter",function(){
		var targ = $(this).attr("alt");
		var height = $(this).outerHeight();
		var pos = $(this).offset();

		$(".drop_down_menus ."+targ)
			.css({top:pos.top+height-10, left:pos.left})
			.slideDown("fast",function(){
				$(".drop_down_menus ."+targ).siblings().hide();
			})
			.siblings()
			.hide();
		inside_menu_bar = 1;
	});
	$("#listMenuRoot > li > a").bind("mouseleave",function(){
		inside_menu_bar = 0;
		setTimeout( hideTopNavMenus, 333 );
	});
	$(".drop_down_menus > ul").bind("mouseenter",function(){
		inside_menu_ul = 1;
	});
	$(".drop_down_menus > ul").bind("mouseleave",function(){
		inside_menu_ul = 0;
		setTimeout( hideTopNavMenus, 333 );
	});


});

function hideTopNavMenus(){
	if(inside_menu_bar == 0 && inside_menu_ul == 0){
		$(".drop_down_menus > ul").hide();
	}
}

function newRotateBox(){
	var cur = $("#rotate_box .active");
	var curTab = $("#rotate_box .img_box_menu ul li.selected");
	var next = cur.next();
	var nextTab = curTab.next();

	if(next.length < 1){
		next = $("#rotate_box .rb:first");
		nextTab = $("#rotate_box .img_box_menu ul li:first")
	}

	cur.removeClass("active").removeClass("notactive").fadeOut();
	curTab.removeClass("selected");
	next.addClass("active").fadeIn();
	nextTab.addClass("selected");

	TTT1 = setTimeout( newRotateBox, rotate_delay );
}

function setRotateBox(pos){

	pos-=1;

	if(timer_active){
		clearTimeout(TTT1);
		timer_active = 0;
	}

	var cur = $("#rotate_box .active");
	var curTab = $("#rotate_box .img_box_menu ul li.selected");
	var next = $("#rotate_box .rb").eq(pos);
	var nextTab = $("#rotate_box .img_box_menu ul li").eq(pos);

	cur.removeClass("active").removeClass("notactive").fadeOut();
	curTab.removeClass("selected");
	next.addClass("active").fadeIn();
	nextTab.addClass("selected");

}

function recordOutboundLink(linkTag,param1,param2,param3, target){
//	try {

		pageTracker._trackEvent(param1, param2, param3);
		//if('_blank' == target){
//			setTimeout(function(){
//				$("#header > h1 > a").css({
//					backgroundImage: 'url(http://www.google.com/intl/en_ALL/images/logo.gif)'
//				});
//
//			},1000);
		//} else {
		//	setTimeout('window.location.href = "' + linkTag.href + '"', 120);
		//}
//	}catch(err){}

}

function headerEmailSignup(){
	if(jQuery){
		jQuery.ajax({
			data:$("#signup_form").serialize(),
			dataType:'json',
			type: 'GET',
			url: $("#signup_form").attr("action"),
			success:function(obj){
				if(obj.error){
					fancyAlert(obj.error);
				} else if(obj.message){
					fancyAlert(obj.message);
					$("#signup_form").fadeOut();
				}
			}
		});
	}
	return false;
}

function fancyAlert(msg){
	$(".global_popup .inner").html(msg);
	centerDiv(".global_popup");

}

/* Center an absolute Div vertically within the view port.
Should be horizontally centered by way of CSS and margin:0 auto; */
function centerDiv(oDiv,desired_top_margin){

	var element = $(oDiv);
	if(!element) return false;

	var dims = {};
	dims.width = element.width();
	dims.height = element.height();

	var top_offset = (window.pageYOffset) ? window.pageYOffset :  document.body.scrollTop;

	if(document.documentElement.scrollTop){
		// Supported in IE7 and Firefox
		top_offset = document.documentElement.scrollTop;
	}

	var w = 0;	var h = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	x_pos = Math.round((w * 0.5 - dims.width / 2 ));
	y_pos = top_offset + Math.round( h * 0.5 - dims.height / 2 ) - 40;
	if(desired_top_margin){
		y_pos = 0 + top_offset + desired_top_margin;
	}

	element.css({top:y_pos, left:x_pos});
	element.fadeIn();

	return false;
}


// Trigger the accordion header (selector) depending on true/false value (hideshow)
function toggleAccordionHeader(selector,hideshow){
	if(hideshow == true && false == $(selector).hasClass("openheader") ){
		// Expand It
		$(selector).trigger("evt_accordion");

	} else if( hideshow == false && $(selector).hasClass("openheader") ){
		// hide it
		$(selector).trigger("evt_accordion");
	} else {
		// already in appropriate state
	}
}

function showCampusList(count)
{
	for (i=0;i<=count;i++)
	{
	document.getElementById('campusfinder'+i).style.display='';
	}
}

function showAllVideos(count)
{
	for (i=0;i<count;i++)
	{
	document.getElementById('video_div_'+i).style.display='';
	}
}

// Recaptcha Themes
var RecaptchaOptions = {
   theme : 'clean'
};

var currentHPImage = 1;
var rotateHPImageInterval;

function rotateHPImage(){
	var images = $(".hpimgrotate");
	var hideImage = 0;

	currentHPImage = currentHPImage == ctHPImages ? 1 : currentHPImage + 1;
	hideImage = currentHPImage > 1 ? currentHPImage - 1 : ctHPImages;

	$(".hpimgrotate[rel='" + currentHPImage + "']").fadeIn(500);
	$(".hpimgrotate[rel='" + hideImage + "']").hide();
	//$("a#hp_pic_url").attr('href', $(".hpimgrotate[rel='" + currentHPImage + "']").attr('title'));
}

function startRotateHPImage(){
	rotateHPImageInterval = setInterval('rotateHPImage();',6000);
}

function stopRotateHPImage(){
	clearInterval(rotateHPImageInterval);
}

var ctHPForms = 4;
var currentHPForm = 4;
var rotateHPFormInterval;
var disableHPForm = false;

function rotateHPForm(){
	var forms = $("#hp_forms .formbox");
	var hideForm = 0;

	currentHPForm = currentHPForm == ctHPForms ? 1 : currentHPForm + 1;
	hideForm = currentHPForm > 1 ? currentHPForm - 1 : ctHPForms;

	$("#hp_forms .formbox[rel='" + currentHPForm + "']").fadeIn(500);
	$("#hp_forms .formbox[rel='" + hideForm + "']").hide();
}

function startRotateHPForm(){
	if(!disableHPForm)
		rotateHPFormInterval = setInterval('rotateHPForm();',20000);
}

function stopRotateHPForm(){
	clearInterval(rotateHPFormInterval);
}

function toQueryParams(str){
	if(str === "" || typeof str != "string"){
		return null;
	}

	var retObj = {};
	str = str.replace("&amp;"," ");
	var arr = str.split("&");
	if(arr.length <= 0){
		return null;
	}

	var keyval;
	for(var x=0; x < arr.length; x+=1){
		if(typeof arr[x] == "string"){
			keyval = arr[x].split("=");
			if(typeof keyval[0] == "string" && keyval[0].length > 0){
				retObj[ qCleanString( keyval[0] ) ] = qCleanString( keyval[1] );
			}
		}
	}

	return retObj;
}

function qCleanString(str){

	str = "" + str;

	return str.replace(/[^A-Z0-9_\s]/gi,"_")

}


function addBookmark(url,title)
{
    if(window.sidebar && window.sidebar.addPanel){
        window.sidebar.addPanel(title,url,'');
    }
    else{
        alert('Your browser does not support this feature. To bookmark your list: If you are using a Mac, press Command+D  If you are using Windows, press CTRL + D');
    }

}


/*
Javascript popup window
By Michael Butler 07/01/09

Usage:
to auto generate the popup window, simply include this script and add this click handler:
onclick="return Popup.display('Hello World',this)"
to use an existing div as the popup (the div must have 3 divs beneath it)
onclick="return Popup.display('Hello World',this,'Div_Id')"
*/

var Popup = {};

Popup.display = function(theText,clickedEl,divId,iWidth){
	if(!divId){
		divId = "myPopup";
	}

	var oDiv = $("#"+divId);

	if(oDiv.length < 1){
		oDiv = this.insertDiv();
	}

	if(oDiv.length < 1){
		alert("Div not found.");
		return false;
	}

	if(!oDiv.hasClass("styled")){
		this.styleDiv(oDiv);
		oDiv.addClass("styled");
	}

	if(iWidth){
		oDiv.css("width","" + iWidth + "px");
	}

	oDiv.show();
	this.moveDiv(oDiv,clickedEl);

	var children = oDiv.children();

	oDiv.find(".bd").html(theText);

	oDiv.find(".close").bind('click',function(e){
		$(this).parent().parent().fadeOut();
	});

	if(!this.dragg){

	}

	return false;
};

Popup.moveDiv = function(oDiv,clickedEl){
	var loc = this.findPos(clickedEl);

	oDiv.css("position","absolute");

	var dimen = { width: oDiv.width(), height: oDiv.height() };
	var wdimen = { width: $(document.viewport).width(), height: $(document.viewport).height() };
	var offset = { left: $(document.viewport).scrollLeft(), top: $(document.viewport).scrollTop() };
	var offsety = offset.top;
	var w,h;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

//	console.log("loc[1] " + loc[1]);
//	console.log("offsety " + offsety);
//	console.log("h " + h);
//	console.log("dimen.height " + dimen.height);
//
	if(loc[0] > w/2){
		oDiv.css("left", "" + (loc[0] - dimen.width) + "px" );
	} else {
		oDiv.css("left", "" + (loc[0]+16) + "px" );
	}
	if( (loc[1]-offsety) > (h/2) ){
		oDiv.css("top", "" + (loc[1] - dimen.height) + "px" );
	} else {
		oDiv.css("top", "" + (loc[1]+10) + "px" );
	}
};

Popup.styleDiv = function(oDiv,clickedEl){

	oDiv.css("position", "absolute");
	oDiv.css("margin", 0 );
};

Popup.insertDiv = function(){

	var html = '<div id="myPopup" style="display:none;"><div class="hd"><div class="close">&times;</div></div><div class="bd"></div><div class="ft"></div></div>';
	$("body").append(html);
	return $('#myPopup');
};

Popup.findPos = function(obj){
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	} else {
		curleft = 20;
		curtop = 20;
	}

	return [curleft,curtop];
};


function goToByScroll(id){
     	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}


this.tooltip = function(){
	/* CONFIG */
		xOffset = 10;
		yOffset = 20;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	/* END CONFIG */
	$("a.tooltip").hover(function(e){
		this.t = this.title;
		this.title = "";
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
    },
	function(){
		this.title = this.t;
		$("#tooltip").remove();
    });
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});
};



// starting the script on page load
$(document).ready(function(){
	tooltip();
});


function blogChangeCat(selBox){
	window.location.href = $(selBox).val();
}


function countdown(timeStr){
	dd = timeStr;
	if(dd*1 < 0){
		$("#course_countdown_1").hide().after("Time's up for this month! Refresh the page to see the next available time.");
	}else{
		dday=Math.floor(dd/(60*60*24));
		dhour=Math.floor((dd%(60*60*24))/(60*60)*1);
		dmin=Math.floor(((dd%(60*60*24))%(60*60))/(60)*1);
		dsec=Math.floor((((dd%(60*60*24))%(60*60))%(60))/1);
		$("#course_countdown_1 .days").html( dday );
		$("#course_countdown_1 .hours").html( dhour );
		$("#course_countdown_1 .minutes").html( dmin );
		dd -= 20;

		setTimeout("countdown(dd)",20000);
	}
};