$(document).ready(function() {
	addPageEvents();
	
	if( $("#login_mid").length > 0){
		addLoginFunctionality()
	}
	
});

function addPageEvents() {
	$('#toggleFeedbackForm').bind('click',{id:'feedbackForm'},showHide);
	$('#closeFeedbackForm').bind('click',{id:'feedbackForm'},showHide);
	$('.comments').bind('click',toggleComments);
}

function showHide(event) {
	var elementName = "#" + event.data.id;
	var element = $(elementName);
	
	if(element.is(":hidden")) {
		element.slideDown("slow");
	}
	else {
		element.slideUp("slow");
	}
}

function toggleComments(e)
{
	
	
	var id = ($(this).attr('id')).split('_');
	
	var elementName = "#comments_" + id[1];
	var element = $(elementName);
	
	
	if(element.css('display')== 'none') {
		element.show();
	}
	else {
		element.hide();
	}
}

function addLoginFunctionality()
{
	$("#btn_login").click(function(){  
	$("#error_login").html('<img src="./images/load.gif" alt="" />');
    $("#error_login")
        .load("./ajax/login.php", {l_email: $("#li_email").val(),
								   remember: $("#l_remember").is(":checked")}, function(responseText){  
            $("#ajax_loader").html('');
			
			if (responseText == '')
			{
				window.location = "./index.php";
			}
			else
			{
				setTimeout('$("#error_login").fadeOut(2000, function() { $("#error_login").html("&nbsp;"); } )', 2000);
			}
        }).show();  
		return false;
	});  	 
	 
	$("#btn_register").click(function(){  
		var error = "";
		
		if($("#nu_firstname").val().length < 1 || $("#nu_firstname").val().length > 30 || $("#nu_firstname").val() == "FIRST NAME")
		{
			error += "Firstname must be between 1 and 30 characters long\n"; 
		}
		
		if($("#nu_surname").val().length < 1 || $("#nu_surname").val().length > 30 || $("#nu_surname").val() == "SURNAME")
		{
			error += "Surname must be between 1 and 30 characters long\n"; 
		}
		
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if (!emailPattern.test($("#nu_email").val()))
		{
			error += "Email address not valid";	
		}
		
		if (error != "")
		{
			alert(error);
			return;
		}
	
		$("#error_reg").html('<img src="./images/load.gif" alt="" />');
		$("#error_reg")
			.load("./ajax/login.php", {s_email: $("#nu_email").val(),
									   s_firstname: $("#nu_firstname").val(),
									   s_surname: $("#nu_surname").val(),
									   s_country: $("#nu_country").val(),
									   s_speciality: $("#nu_speciality").val(),
									   remember: $("#s_remember").is(":checked")}, function(responseText){  
				$("#ajax_loader").html('').fadeIn(100);
				
				if (responseText == '')
				{
					window.location = "./index.php";
				}
				else
				{
					setTimeout('$("#error_reg").fadeOut(2000, function() {$("#error_reg").html("&nbsp;");})', 2000);
				}
			}).show(); 
		return false;
	});  	
							  
	$("#nu_firstname").Watermark("FIRST NAME", "#aaa");
	$("#nu_surname").Watermark("SURNAME", "#aaa");
	$("#nu_country").Watermark("COUNTRY", "#aaa");
	$("#nu_speciality").Watermark("SPECIALITY", "#aaa");
	$("#nu_email").Watermark("EMAIL", "#aaa");
	$("#li_email").Watermark("EMAIL", "#aaa");
	
	$("#li_email").keypress(function(e) {
		if (e.keyCode == 13)
		{
			//setTimeOut($("#btn_login").click(), 100);
			return false;
		}
	});
	
	$("#nu_firstname").keypress(function(e){
		if (e.keyCode == 13)
		{
			return false;
		}
		});
	$("#nu_surname").keypress(function(e){
		if (e.keyCode == 13)
		{
			return false;
		}
		});
	$("#nu_email").keypress(function(e) {
		if (e.keyCode == 13)
		{
			//setTimeOut($("#btn_register").click(), 100);
			return false;
		}
	});
}


function Load()
{
	$("#login_mid").overlay({
		top: "center", 
		expose: { 
			color: '#555', 
			loadSpeed: 200, 
			opacity: 0.6 
		}, 
		closeOnClick: false, 
		closeOnEsc: false,
		api: true 
	}).load();
}
/*
function addGoogleTracking()
{
	//Google Analytics tracking code
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-1767378-23']);
	_gaq.push(['_trackPageview']);
	
	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
	
}
*/
