$(document).ready(function()
{
	$('#email_keep').hide();
	$('#emailfield').hide();
	$('#keepemaildiv').hide();
	$('#updateemaildiv').hide();
	
	$('#pass_keep').hide();
	$('#passfield').hide();
	$('#keeppassdiv').hide();
	$('#updatepassdiv').hide();
	   
   function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 20, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 300, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
	
	
});
	
$(function() {
	$("#tabs").tabs( {
		/*cookie: {
			// store cookie for a day, without, it would be a session cookie
			expires: 1
		}*/	
	});
});

// User Settings Email Show/Hide
$(function() {
	$("#emailKeep").click(function() {
		$('#update').hide();
		$('#labelkeep span').css({fontWeight:"bold"});
		$('#emailfield').hide();
		$('#labelupdate span').css({fontWeight:"normal"});
		$('#keepemaildiv').hide(); // User doesnt need to see this
		$('#updateemaildiv').hide();
	});
});

$(function() {
	$("#emailUpdate").click(function() {
		$('#update').show();
		$('#labelkeep span').css({fontWeight:"normal"});
		$('#emailfield').show();
		$('#labelupdate span').css({fontWeight:"bold"});
		$('#keepemaildiv').hide();
		$('#updateemaildiv').show();
	});
});

// User Settings Password Show/Hide
$(function() {
	$("#passKeep").click(function() {
		$('#pupdate').hide();
		$('#plabelkeep span').css({fontWeight:"bold"});
		$('#passfield').hide();
		$('#plabelupdate span').css({fontWeight:"normal"});
		$('#keeppassdiv').hide(); // User doesnt need to see this
		$('#updatepassldiv').hide();
	});
});

$(function() {
	$("#passUpdate").click(function() {
		$('#pupdate').show();
		$('#plabelkeep span').css({fontWeight:"normal"});
		$('#passfield').show();
		$('#plabelupdate span').css({fontWeight:"bold"});
		$('#keeppassdiv').hide();
		$('#updatepassdiv').show();
	});
});

$(function()
{
	$("#offers_filter_submit").click(function(event)
	{
		
		event.preventDefault();
		
		var numnights = $('#filterNights').val();
		var boardtype = $('#filterBoard').val();
		var starrating = $('#filterStars').val();
		var pricefilter = $('#filterPrice').val();
		var airportfilter = $('#filterAirport').val();
		var locationID = $('#locationId').val();
		var offersType = $('#type').val();
	
		$.ajax({  
			type: "POST",
			url: "filterOffers",
			data: {nights: numnights, board: boardtype, stars: starrating, price: pricefilter, airport: airportfilter, locationId: locationID, type: offersType},  
			success: function(data)
			{ 
			  $('#offersdiv').html(data);
			  $('#offersdiv').hide();
			  $('#offersdiv').show('slow');
			}
		});
	});
});


$(function()
{
	$("#reviews_filter_submit").click(function(event)
	{
		
		event.preventDefault();
		
		var criteria = $('#filterCriteria').val();
		var order = $('#filterOrder').val();
		var arrivalpointid = $('#arrivalpointid').val();
		var resortid = $('#resortid').val();
	
		$.ajax({  
			type: "POST",
			url: "filterReviews",
			data: {criteria: criteria, order: order,  arrivalpointid: arrivalpointid, resortid: resortid},  
			success: function(data)
			{ 
			  $('#reviewsdiv').html(data);
			  $('#reviewsdiv').hide();
			  $('#reviewsdiv').show('slow');
			}
		});
	});
});

function checkform()
{
	if (document.review.comments.value == '')
	{
		// something is wrong
		alert("You must enter some comments.");
		return false;
	}
	else
	{
		return true;
	}
}
function Inint_AJAX()
{
	try
	{
		return new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) {} //IE

	try
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {} //IE
	
	try
	{
		return new XMLHttpRequest();
	}
	catch(e) {} //Native Javascript
	
	alert("XMLHttpRequest not supported");
	
	return null;
};

function getDest(id)
{
	var req = Inint_AJAX();
	
	req.onreadystatechange = function ()
	{
		if (req.readyState==4)
		{
			if (req.status==200)
			{
				document.getElementById('destdiv').innerHTML=req.responseText; //retuen value
			}
		}
	};
	
	req.open("GET", "/javascript/getDest.php?country="+id); //make connection
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
	req.send(null); //send value
}

function getRes(id)
{
	var req = Inint_AJAX();
	
	req.onreadystatechange = function ()
	{
		if (req.readyState==4)
		{
			if (req.status==200)
			{
				document.getElementById('resdiv').innerHTML=req.responseText; //retuen value
			}
		}
	};
	
	req.open("GET", "/javascript/getRes.php?dest="+id); //make connection
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
	req.send(null); //send value
}

function clickonclickoff(id)
					{
						// If it's currently hidden
						if(document.getElementById(id).style.display == "none")
						{	
							// Make it visible
							document.getElementById(id).style.display = 'block';
						}
						else
						{
							// If it's currently visable, make it hidden
							document.getElementById(id).style.display = 'none';
						}
					}


// --------------------- Register ------------------ //

	$(document).ready(function(){
	
	var emailok = false;
	var myForm = email = $("#email"), emailInfo = $("#emailInfo");
	
	//send ajax request to check email
	email.blur(function(){
		$.ajax({
			type: "POST",
			data: "email="+$(this).attr("value"),
			url: "checkEmailAddress",
			beforeSend: function(){
				emailInfo.html("Checking Email...");
			},
			success: function(data){
				if(data == "invalid")
				{
					emailok = false;
					emailInfo.html("<img src='images/information.png' style='background-position: center;'> Invalid Email");
					$("#submitRegistration").attr('disabled','disabled');
				}
				else if(data != "0")
				{
					emailok = false;
					emailInfo.html("<img src='images/information.png'  style='background-position: center;'> Email Already Exists");
					$("#submitRegistration").attr('disabled','disabled');
				}
				else
				{
					emailok = true;
					emailInfo.html("<img src='images/tick.png'  style='background-position: center;'> Email OK");
					$("#submitRegistration").attr('disabled','');
				}
			}
		});
	});
});

$(document).ready(function()
{

	var emailok = false;
	var myForm = email = $("#emailNew"), emailInfo = $("#emailInfo");
	
	//send ajax request to check email
	email.blur(function(){
		$.ajax({
			type: "POST",
			data: "email="+$(this).attr("value")+"&old="+$("#oldemail").val(),
			url: "checkEmailAddressChange",
			beforeSend: function(){
				emailInfo.html("Checking Email...");
			},
			success: function(data){
				if(data == "invalid")
				{
					emailok = false;
					emailInfo.html("<img src='images/information.png' style='background-position: center;'> Invalid Email");
					$("#submitRegistration").attr('disabled','disabled');
				}
				else if(data != "0")
				{
					emailok = false;
					emailInfo.html("<img src='images/information.png'  style='background-position: center;'> Email Already Exists");
					$("#submitRegistration").attr('disabled','disabled');
				}
				else
				{
					emailok = true;
					emailInfo.html("<img src='images/tick.png'  style='background-position: center;'> Email OK");
					$("#submitRegistration").attr('disabled','');
				}
			}
		});
	});
});



// --------------------- Register ------------------ //

	/*$(document).ready(function(){
	
	var dnok = false;
	var myForm = dn = $("#dn"), displaynameInfo = $("#displaynameInfo");
	
	//send ajax request to check email
	dn.blur(function(){
		$.ajax({
			type: "POST",
			data: "dn="+$(this).attr("value"),
			url: "checkDisplayName",
			beforeSend: function(){
				displaynameInfo.html("Checking Display Name...");
			},
			success: function(data){
				if(data == "invalid")
				{
					dnok = false;
					displaynameInfo.html("<img src='images/information.png' style='background-position: center;'> Invalid Display Name");
					$("#submitRegistration").attr('disabled','disabled');
				}
				else if(data != "0")
				{
					dnok = false;
					displaynameInfo.html("<img src='images/information.png'  style='background-position: center;'> Display Name Already Exists");
					$("#submitRegistration").attr('disabled','disabled');
				}
				else
				{
					dnok = true;
					displaynameInfo.html("<img src='images/tick.png'  style='background-position: center;'> Display Name OK");
					$("#submitRegistration").attr('disabled','');
				}
			}
		});
	});
});*/

function clickonclickoff(id)
{
	// If it's currently hidden
	if(document.getElementById(id).style.display == "none")
	{	
		// Make it visible
		document.getElementById(id).style.display = 'block';
	}
	else
	{
		// If it's currently visable, make it hidden
		document.getElementById(id).style.display = 'none';
	}
}

$(function()
{
	//$('#average_review').bt($("#hiddenDiv").html(), {trigger: 'click', positions: 'top', fill: '#FFF', width: '250px', closeWhenOthersOpen: true, padding: '4px' });
});
