$(document).ready(function(){ 
	
	//$("#menu-items li").hoverIntent(config);
	
	/*$("#menu").bind("mouseleave", function(e){
		remove_menu_margin();
	});*/
			
	$(".thumbnails img").mouseover(function(){
		$(this).animate({opacity: 0.5}, 200, function(){
			$(this).animate({opacity: 1}, 200); 
		}); 
	});
	
	$(".thumbnails-gallery").mouseover(function(){
		$(this).animate({opacity: 0.5}, 200, function(){
			$(this).animate({opacity: 1}, 200); 
		}); 
	});
	
	$("#dialog-box").dialog({
		bgiframe: true, resizable: false, autoOpen: false, modal: true,
		overlay: {backgroundColor: '#000',opacity: 0.5},
		buttons: 
		{
			'Ok': function() { $(this).dialog('close');}
		}
	});
	
	
	
});

function validateEmail(emailad) {
		
	var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;

	if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1)){
		return true;
	}
	else {
		return false;
	}
}

function sign_up(){
	// preiau numele si emailul
	var nwlName 	= $("#nwlName").val();
	var nwlEmail	= $("#nwlEmail").val();
	
	// facem validarile
	if ( nwlName.length == 0 || nwlName == "" || nwlName == " " ) 
	{
		inlineMsg("nwlName","Please specify your name!",3);
		return false;
	}
	
	if ( nwlEmail.length == 0 || nwlEmail == "" || nwlEmail == " " ) 
	{
		inlineMsg("nwlEmail","Please specify your email address!",3);
		return false;
	}
			
	if ( validateEmail(nwlEmail)) 
	{
		inlineMsg("nwlEmail","<b>Invalid</b> email address!",3);
		return false;
	}
	
	if ( check_duplicate_email( nwlEmail ) == true)
	{
		inlineMsg("nwlEmail","The email address already exists in our DB!",3);
		return false;		
	}

	// Ajax
	var sendData = "";
	sendData += "&nwlName="+escape(nwlName);
	sendData += "&nwlEmail="+escape(nwlEmail);

	$.ajax
	({
		type: "POST",
		url: "ajax/ajax_sign_up.php",
		data: sendData,
		async: false,
		success: function(msg)
		{
			// debug mode 0 or 1	
			var debug = 0;
			if ( debug == 1 ) alert(msg);
			
			if (msg == "1")
			{
				$("#nwlName").val("");
				$("#nwlEmail").val("");
				
				$("#dialog-box").html("Thank you for your interest, your subscription has been saved!");
				$("#dialog-box").dialog('open');
			}
			else
			{
				$("#dialog-box").html("Your email address already exists in our DB!");
				$("#dialog-box").dialog('open');
			}

						
		},
		error: function(msg) 
		{
			$("#nwlName").val("");
			$("#nwlEmail").val("");
				
			$("#dialog-box").html("An error occured while submitting the form, please refresh this window and try again!");
			$("#dialog-box").dialog('open');
		}
	});
}

function check_duplicate_email( email )
{	
	returnVal = true;
	
	$.ajax
	({
		type: "POST",
		url: "ajax/ajax_check_duplicate_email.php",
		data: "email="+escape(email),
		async: false,
		success: function(msg)
		{
			// debug mode 0 or 1	
			var debug = 0;
			if ( debug == 1 ) alert(msg);
			
			 returnVal = msg;
		},
		error: function(msg) 
		{
            returnVal = "-1";
		}
	});
	
	return returnVal;
}
