$(document).ready(function(){
	
	// **************************************************************************************************************************
	// GENERAL ACTIONS
	
	// external links
	$("a[rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});
	$('.hide-with-js').hide(); // use this class to hide the message "please open this link in new window"
	
	// fade in messages
	$('.successMsg').hide();
	$('.successMsg').fadeIn(1000);
	$('.errorMsg div').hide();
	$('.errorMsg div').fadeIn(1000);
	$('.warningMsg').hide();
	$('.warningMsg').fadeIn(1000);
	
	// non-clickable links
	$(".return-false").click( function() {
		return false;
	});
	
	
	// JUMP MENU
	$("select.jump-menu").change(function(x){
		var url = $("option:selected", this).attr("title");
		if (url.length) {
			window.location.href = url;
		}
	});
	
	
	// clear inputs
	$("#newsletter-form input").each(function(i){
		var inputValue = $(this).val();
		var inputTitle = $(this).attr("title");
		
		if (inputValue=="" || inputValue==inputTitle) {
			$(this).focus(function(){ 
				if ($(this).val()==inputValue) {
					$(this).val("");
				}
			});
			$(this).blur(function(){ 
				if ($(this).val()=="") {
					$(this).val(inputValue);
				}
			});
		}
		
	});
	
	
	// Header error message
	$(".errorMsg-overlay, .errorMsg-after").hide();
	$(".errorMsg-overlay, .errorMsg-after").fadeIn(300, function(){ 
		var elem = $(this);
		setTimeout(function(){
			$(elem).fadeOut(300);
		}, 1200);
	});
	
	
	// Button disable on click
	$('form').submit(function(){
		$(":submit span", this).text("Please wait");
		$(':submit', this).addClass("inactive");
		//$(':submit', this).attr("disabled", true);
		$(':submit', this).click(function() {
			return false;
			//alert('submitted twice');
		});
	});
	//var formSubmitted = false;
	/*$(".submitButton").click(function(){
		//if (formSubmitted==true) {
		//	return false;
		//} else {
			$("span", this).text("Please wait");
			$(this).addClass("inactive");
			formSubmitted = true;
			return true;
			$(this).attr("disabled", "disabled");
		//}
	});*/	
	
	// END GENERAL ACTIONS
	// **************************************************************************************************************************
	
	
	// Fading links
	$(".fading-link img").fadeTo(1000, 0.70);
	$(".fading-link").hover(
		function(){
			$("img", this).fadeTo(50, 1);
		},
		function(){
			$("img", this).fadeTo(50, 0.70);
		}
	);
	
	
	// Font replacement
	replaceFonts();
	
	
	// Index panels same height
	var panel1Height = parseInt($("#content.index .panel.item1 .bg").height());
	var panel2Height = parseInt($("#content.index .panel.item2 .bg").height());
	if (panel1Height>panel2Height) {
		$("#content.index .panel.item2 .bg").height(panel1Height);
	} else {
		$("#content.index .panel.item1 .bg").height(panel2Height);
	}
	//alert(panel1Height + ", " + panel2Height);
	
	
	// Calendar homepage
	if (isIE()==false) {
		var needCalendarShow = false;
		var timer;
		$("#calendar-table td a").hover(
			function(){
				
				$("#calendar-popup").html("");
				
				clearTimeout(timer);
				var htmlContent = $(this).parent().find(".popup-content").html();
				var position = $(this).position();
				var calendarWidth = $("#calendar-table").innerWidth();
				var calendarHeight = $("#calendar-table").innerHeight();
				
				$("#calendar-popup").html(htmlContent);
				$("#calendar-popup").show();
				
				var cssObj = {
					'bottom' : calendarHeight - position.top - $(this).innerHeight() + 'px',
					'right' : calendarWidth - position.left + 1 + 'px'
				}
				$("#calendar-popup").css(cssObj);
				needCalendarShow = true;
			},
			function(){
				needCalendarShow = false;
				timer = setTimeout(function(){ if (needCalendarShow==false) $("#calendar-popup").html("");$("#calendar-popup").hide(); }, 300);
			}
		);
		$("#calendar-popup").hover(
			function() {
				needCalendarShow = true;
				clearTimeout(timer);
			},
			function() {
				needCalendarShow = false;
				clearTimeout(timer);
				timer = setTimeout(function(){ if (needCalendarShow==false) $("#calendar-popup").html("");$("#calendar-popup").hide(); }, 300);
			}
		);
	} // end IF (non IE)
	
	// Calendar full popup
	
	$("#table-calendar-full td").hover(
		function() {
			$("h3", this).addClass("hover");
		},
		function() {
			$("h3", this).removeClass("hover");
		}
	);
	$("#table-calendar-full td ul li, #calendar-list ul li").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
		}
	);
	
	var needCalendarFullShow = false;
	var timerFull;
	$("#table-calendar-full td ul li.class").hover(
		function(){
			
			needCalendarFullShow = true;
			$("#calendar-popup").html("");
			
			clearTimeout(timerFull);
			var htmlContentPopup = $(".popup-content", this).html();
			var htmlContentShortInfo = $(".short-info", this).html();
			var position = $(this).position();
			var calendarWidth = $("#table-calendar-full").innerWidth();
			var calendarHeight = $("#table-calendar-full").innerHeight();
			
			$("#calendar-popup").html(htmlContentPopup);
			$("#calendar-popup").prepend(htmlContentShortInfo);
			$("#calendar-popup").show();
			
			var cssObj = {
				'top' : position.top + 'px',
				'left' : position.left + $(this).innerWidth() + 1 + 'px'
			}
			$("#calendar-popup").css(cssObj);
		},
		function(){
			needCalendarFullShow = false;
			timerFull = setTimeout(function(){ if (needCalendarFullShow==false) $("#calendar-popup").html("");$("#calendar-popup").hide(); }, 200);
		}
	);
	$("#calendar-popup").hover(
		function() {
			needCalendarFullShow = true;
			clearTimeout(timerFull);
		},
		function() {
			needCalendarFullShow = false;
			clearTimeout(timerFull);
			timerFull = setTimeout(function(){ if (needCalendarFullShow==false) $("#calendar-popup").html("");$("#calendar-popup").hide(); }, 200);
		}
	);


	// TABS PROGRAMS
	/*var firstTabElement = $("ul.submenu li a.link-grey:first");
	var firstTabElementName = $(firstTabElement).attr("id");
	//alert(firstTabElementName);
	$(firstTabElement).addClass("active");
	$("div.pan").hide();
	$("#panel-" + firstTabElementName).fadeIn(500);
	
	$("ul.submenu li a.link-grey").click(function(){
		$("ul.submenu li a.link-grey").removeClass("active");
		$(this).addClass("active");
		
		var thisElementName = $(this).attr("id");
		$("div.pan").hide();
		$("#panel-" + thisElementName).fadeIn(500);
		
		return false;
	});*/
	
	// Add another Academic
	/*$("#add-academic").click(function(){
		var nr = $("#nr_academic").val();
		var newNr = parseInt(nr) + 1;
		var academicHtml = '\
			<tr id="other'+newNr+'">\
				<td valign="top">\
					<input type="text" name="academic_info_school['+newNr+']" id="academic_info_school'+newNr+'" value="" class="textInput" style="width: 180px;" />\
				</td>\
				<td valign="top" class="dates">\
					<strong>From:</strong>\
					<label for="academic_year_from'+newNr+'">Year</label>\
					<select name="academic_year_from['+newNr+']" id="academic_year_from'+newNr+'" class="selectInput">\
						<option value="1">2010</option>\
						<option value="2">2011</option>\
					</select>\
					\
					<label for="academic_month_from'+newNr+'">Month</label>\
					<select name="academic_month_from['+newNr+']" id="academic_month_from'+newNr+'" class="selectInput">\
						<option value="January">January</option>\
						<option value="February">February</option>\
						<option value="March">March</option>\
						<option value="April">April</option>\
						<option value="May">May</option>\
						<option value="June">June</option>\
						<option value="July">July</option>\
						<option value="August">August</option>\
						<option value="September">September</option>\
						<option value="October">October</option>\
						<option value="November">November</option>\
						<option value="December">December</option>\
					</select>\
					\
					<label for="academic_day_from'+newNr+'">Day</label>\
					<select name="academic_day_from['+newNr+']" id="academic_day_from'+newNr+'" class="selectInput">\
						<option value="1">01</option>\
						<option value="2">02</option>\
					</select>\
					<div class="clear"></div>\
					\
					<strong>To:</strong>\
					<label for="academic_year_to'+newNr+'">Year</label>\
					<select name="academic_year_to['+newNr+']" id="academic_year_to'+newNr+'" class="selectInput">\
						<option value="1">2010</option>\
						<option value="2">2011</option>\
					</select>\
					\
					<label for="academic_month_to'+newNr+'">Month</label>\
					<select name="academic_month_to['+newNr+']" id="academic_month_to'+newNr+'" class="selectInput">\
						<option value="January">January</option>\
						<option value="February">February</option>\
						<option value="March">March</option>\
						<option value="April">April</option>\
						<option value="May">May</option>\
						<option value="June">June</option>\
						<option value="July">July</option>\
						<option value="August">August</option>\
						<option value="September">September</option>\
						<option value="October">October</option>\
						<option value="November">November</option>\
						<option value="December">December</option>\
					</select>\
					\
					<label for="academic_day_to'+newNr+'">Day</label>\
					<select name="academic_day_to['+newNr+']" id="academic_day_to'+newNr+'" class="selectInput">\
						<option value="1">01</option>\
						<option value="2">02</option>\
					</select>\
				</td>\
				<td valign="top">\
					<textarea name="academic_course_study['+newNr+']" id="academic_course_study'+newNr+'" rows="3" cols="35"></textarea>\
				</td>\
				<td valign="top">\
					<input type="text" name="academic_degree['+newNr+']" id="academic_degree'+newNr+'" value="" class="textInput" style="width: 140px;" />\
				</td>\
				<td align="center"><a href="#" class="remove-academic" rel="other'+newNr+'" onclick="removeAcademic(\'other'+newNr+'\'); return false;">Remove</a></td>\
			</tr>';

		$("#table-academic-info tbody").append(academicHtml);
		$("#nr_academic").val(newNr);

		//alert(newNr);
		return false;
	});*/

	$(".remove-academic").click(function(){
		var elem = $(this).attr("rel");
		removeAcademic(elem);
		return false;
	});


	
	// Add another Work
	/*$("#add-work").click(function(){
		var nr = $("#nr_work").val();
		var newNr = parseInt(nr) + 1;
		var workHtml = '\
			<tr id="other-work'+newNr+'">\
				<td valign="top">\
					<input type="text" name="work_employer['+newNr+']" id="work_employer'+newNr+'" value="" class="textInput" style="width: 120px;" />\
				</td>\
				<td valign="top">\
					<input type="text" name="work_address['+newNr+']" id="work_address'+newNr+'" value="" class="textInput" style="width: 180px;" />\
				</td>\
				<td valign="top">\
					<input type="text" name="work_city['+newNr+']" id="work_city'+newNr+'" value="" class="textInput" style="width: 100px;" />\
				</td>\
				<td valign="top">\
					<input type="text" name="work_state['+newNr+']" id="work_state'+newNr+'" value="" class="textInput" style="width: 100px;" />\
				</td>\
				<td valign="top">\
					<input type="text" name="work_zip['+newNr+']" id="work_zip'+newNr+'" value="" class="textInput" style="width: 60px;" />\
				</td>\
				<td valign="top">\
					<input type="text" name="work_phone['+newNr+']" id="work_phone'+newNr+'" value="" class="textInput" style="width: 100px;" />\
				</td>\
				<td align="center"><a href="#" class="remove-work" rel="other-work'+newNr+'" onclick="removeWork(\'other-work'+newNr+'\'); return false;">Remove</a></td>\
			</tr>';

		$("#table-work-info tbody").append(workHtml);
		$("#nr_work").val(newNr);

		//alert(newNr);
		return false;
	});
*/
	$(".remove-work").click(function(){
		var elem = $(this).attr("rel");
		removeWork(elem);
		return false;
	});
	
	
	//$("#social-security-number").hide();
	

	
});


function isIE() {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		return true;
	} else {
		return false;
	}
	
	/*if (ieversion>=8)
		document.write("You're using IE8 or above")
	else if (ieversion>=7)
		document.write("You're using IE7.x")
	else if (ieversion>=6)
		document.write("You're using IE6.x")
	else if (ieversion>=5)
		document.write("You're using IE5.x")
	}*/
}


function removeAcademic(itemId) {
	$("#"+itemId).remove();

	var nr = $("#nr_academic").val();
	var newNr = parseInt(nr) - 1;
	$("#nr_academic").val(newNr);
}

function removeWork(itemId) {
	$("#"+itemId).remove();

	var nr = $("#nr_work").val();
	var newNr = parseInt(nr) - 1;
	$("#nr_work").val(newNr);
}

/* Contact form*/
function ajax_programs_restrict(location_id, program_id) {
	var selected_location_id = $('#' + location_id).val();
	//alert(selected_location_id);
	//alert(program_id);
	$.ajax({
	    type: "POST",
	    url: path + "ajax/select-program",
	    data: {
	        location_id: selected_location_id
	    },
		
		dataType: 'json',
	   success: function(data){
	    		var options = '';
				for (var i = 0; i < data.length; i++) {
				  options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
				}
				$("select#" + program_id).html(options);
	    }
	});
}


/*  Apply form */
function show_ssn(ssn_id){
	$("#" + ssn_id).show();
}

function hide_ssn(ssn_id){
	$("#" + ssn_id).hide();
}

function ajax_locations_restrict(program_id){
	
	
	var selected_program_id = $('#' + program_id).val();
	
	//alert(program_id);
	if (selected_program_id == ''){
		//alert('aa');
		$("#campuses").hide();
	}
	else{
		//alert('aa');
		$("#campuses").show();
	}
	
	$.ajax({
	    type: "POST",
	    url: path + "ajax/select-location",
	    data: {
	        program_id: selected_program_id
	    },
		
		dataType: 'json',
	   success: function(data){
	    		
				for(var i in data){
					if ( i == 'hide' ){
						for(var j in data[i]){
							$("#campus-" + data[i][j]).hide();
							$("label[for=campus-"+data[i][j]+"]").hide();
						}
					}
					else{
						for(var j in data[i]){
							$("#campus-" + data[i][j]).show();
							$("label[for=campus-"+data[i][j]+"]").show();
						}
					}
					
				}
	    }
	});
	
}


function ajax_days_restrict(month, year, day){
	var selected_month = $('#' + month).val();
	var selected_year = $('#' + year).val();
	//alert(selected_month);
	//alert(selected_year);
	
	
	$.ajax({
	    type: "POST",
	    url: path + "ajax/select-days",
	    data: {
	        month: selected_month,
			year: selected_year
	    },
		
		dataType: 'json',
	   success: function(data){
	    		var options = '';
				for (var i = 0; i < data.length; i++) {
				  options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
				}
				$("select#" + day).html(options);
	    }
	});
}

function ajax_months_starting_program_restrict(month, year, day){
	
	var selected_year = $('#' + year).val();

	$.ajax({
	    type: "POST",
	    url: path + "ajax/select-months",
	    data: {
			year: selected_year
	    },
		
		dataType: 'json',
	   success: function(data){
	    		var options = '';
				for (var i = 0; i < data.length; i++) {
				  options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
				}
				$("select#" + month).html(options);
	    }
	});
}



function ajax_states_restrict(country_id, state_id, region_id) {
	var selected_country_id = $('#' + country_id).val();
	
	$.ajax({
	    type: "POST",
	    url: path + "ajax/get_select_state",
	    data: {
	        country_id: selected_country_id
	    },
		dataType: 'json',
	    success: function(data){
				if (data.length > 1){
					var options = '';
					for (var i = 0; i < data.length; i++) {
					  options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
					}
					$("select#" + state_id).html(options);
					$("label[for=birth-state]").html('State');
					$("select#" + state_id).css({ "display": "block" });
					$("input#" + region_id).css({ "display": "none" });
					
				}
	    		else{
					$("label[for=birth-state]").html('Region');
					$("select#" + state_id).css({ "display": "none" });
					$("input#" + region_id).css({ "display": "block" });
				}
	    }
	});
}

// Font replacement
function replaceFonts() {
	Cufon.replace('.replace-bold', { fontFamily: 'TodaySH-Ultra', hover: true, hoverables: { a: true } });
	Cufon.replace('.replace-medium', { fontFamily: 'TodaySH-Medium', hover: true, hoverables: { a: true } });
	Cufon.replace('.replace-regular', { fontFamily: 'TodaySH-Regular', hover: true, hoverables: { a: true } });
	
	// link ym
	Cufon.replace('#link-ym', { fontFamily: 'TodaySH-Regular' });
	Cufon.replace('#link-ym strong', { fontFamily: 'TodaySH-Ultra' });
	Cufon.replace('#link-ym span', { fontFamily: 'TodaySH-Medium' });
	
	// main menu
	Cufon.replace('#main-menu a strong', { fontFamily: 'TodaySH-Ultra', hover: true, hoverables: { a: true } });
	
	// page heading
	Cufon.replace('.page-heading', { fontFamily: 'TodaySH-Ultra' });
	Cufon.replace('.page-heading span', { fontFamily: 'TodaySH-Regular' });
}
