$(document).ready(function(){  
	//LOADING POPUP  
	//Click the button event!  
	$("#sign_up_button").click(function(){
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup('');
		resetPopup();
		clearValues();
		//console.log("centerPopup");
    // $("#popupContact").bgiframe();
		return false;
	});

	// Edit a course.
	$(".course_edit_link").click(function(){
	  
		//centering with css  
		centerPopup();  
		//load popup  
		loadPopup($(this).attr('href'));
		resetPopup();
		clearValues();
    // $("#popupContact").bgiframe();
		return false;
	});
	
	//CLOSING POPUP  
	//Click the x event!  
	$("#popupContactClose").click(function(){  
		disablePopup();
	});  
	
	$("#popupThankyouClose").click(function(){  
		disableThankyou();
		location.reload(true); // Force a page reload - ensures that new course is displayed.
	});
	//Click out event!  
	$("#backgroundPopup").click(function(){ 
		disablePopup();  
	});  
	//Press Escape event!  
	$(document).keypress(function(e){  
		if(e.keyCode==27 && popupStatus==1){  
			disablePopup();
		}  
	});
	
	$("select#option_type").change(function () {
    set_to_first_page();
  });
  $("select#option_price").change(function () {
    set_to_first_page();
  });
  $("select#option_date").change(function () {
    set_to_first_page();
  });
	
});


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

//loading popup with jQuery magic!  
function loadPopup(url){  
	//loads popup only if it is disabled  
	if(popupStatus==0){
		// Load the form HTML snippet from the server.
		if(url == ''){
			url = '/learn/find_a_course/course/get_form/';
		}
		$('#popupContact').load(url);
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#popupContact").fadeIn("slow");  
		popupStatus = 1;  
	}  
}


function clearValues(){
	$('#course_upload_form input:text').each(function(index,elm){
		$(elm).val('');
	});
	$('#course_upload_form textarea').val('');
	
}


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


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

	$("#backgroundPopup").css({  
		"height": document.body.clientHeight,
		"width": windowWidth
	}); 
  
  $("#search_filter select").hide();
}

function resetPopup(){
	$('#course_upload_form input:text').each(function(index,elm){
		$(elm).css('background-color','#ffffff');
	});
	$('#course_upload_form textarea').css('background-color','#ffffff');
	$('ul.errorlist').remove();
}

//centering popup  
function centerThankyou(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#popupThankyou").height();  
	var popupWidth = $("#popupThankyou").width();  
	
	var top = windowHeight/2-popupHeight/2;
	var left = windowWidth/2-popupWidth/2;
	
	if (top < 0) {
		top = 20;
	}
	
	//centering  
	$("#popupThankyou").css({  
		"position": "absolute",  
		"top": top,  
		"left": left,
		"z-index": 3000 
	});  
	//only need force for IE6  

	$("#backgroundPopup").css({  
		"height": document.body.clientHeight,
		"width": windowWidth
	}); 

}

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


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




function change_page(page){
	$('#page_num').val(page);
	$('#course_search form').submit();
}

function set_to_first_page(){
	$('#page_num').val(1);
	$('#course_search form').submit();
}
