// JavaScript Document
$(document).ready(function () {
	// Free flow search instructions
	$("#searchDeals").focus(function(){
		$("#searchInstr").fadeIn('slow');						 
	});
	$("#searchDeals").blur(function(){
		$("#searchInstr").fadeOut('slow');						 
	});
	
	// Free flow search
	$("#searchDeals").keyup(function(){
		$.getJSON("ajax/qry_searchDeals.cfm",{keyword: $(this).val(), ajax: 'true'}, 
		  function(j){
			  var deals = '';
			  if(j.length == 0){
			  	  deals += "<p class='mice'>Your search query returned no results</p>";
			  } else {
				  for (var i = 0; i < j.length; i++) {
					  deals += '<div class="dealBlock">' + '<div class="img sgb"><img src="images/thumbs/product_' + j[i].ID + '.jpg" alt="' + j[i].TITLE + '" /></div>' + '<div class="dealContent"><h2>' + j[i].TITLE + '</h2><p class="shortDescription">' + j[i].SHORT + '</p><br /><p align="right"><img src="images/icon-dots.gif" alt=".:" /> <a href="index.cfm?page=products.viewDeal&deal=' + j[i].ID +'">view details</a></p></div></div>';
				  }
			  }
			  $('#right-content').html(deals);
		  }
		);
	});
	
	// Reset button
	$("#resetDealsFormBtn").click(function() {
		$.getJSON("ajax/qry_resetDeals.cfm",{keyword: $(this).val(), ajax: 'true'}, 
		  function(j){
			  var deals = '';
			  for (var i = 0; i < j.length; i++) {
				deals += '<div class="dealBlock">' + '<div class="img sgb"><img src="images/thumbs/product_' + j[i].ID + '.jpg" alt="' + j[i].TITLE + '" /></div>' + '<div class="dealContent"><h2>' + j[i].TITLE + '</h2><p class="shortDescription">' + j[i].SHORT + '</p><br /><p align="right"><img src="images/icon-dots.gif" alt=".:" /> <a href="index.cfm?page=products.viewDeal&deal=' + j[i].ID +'">view details</a></p></div></div>';
			  }
			  $('#right-content').html(deals);
		  }
		);
	});
	
	// Form search elements
	$("#price_range").slider({
		range: true,
		min: 0,
		max: 20000,
		step: 500,
		values: [2000, 7000],
		slide: function(event, ui) {
			$("#priceDisplay").val('$' + ui.values[0] + ' - $' + ui.values[1]);
			$("#priceFrom").val(ui.values[0]);
			$("#priceTo").val(ui.values[1]);
		}
	});
	$("#priceDisplay").val('$' + $("#price_range").slider("values", 0) + ' - $' + $("#price_range").slider("values", 1));
	
	$("#duration_range").slider({
		range: true,
		min: 0,
		max: 45,
		step: 1,
		values: [7, 20],
		slide: function(event, ui) {
			$("#durationDisplay").val(ui.values[0] + ' - ' + ui.values[1] + ' days');
			$("#durationFrom").val(ui.values[0]);
			$("#durationTo").val(ui.values[0]);
		}
	});
	$("#durationDisplay").val($("#duration_range").slider("values", 0) + ' - ' + $("#duration_range").slider("values", 1) + ' days');
	
	// Form search submission
	$("#searchDealsFormBtn").click(function(){
		$.getJSON("ajax/qry_searchDealsForm.cfm", {
			  travelDateMonth: $("#travelDateMonth").val(),
			  travelDateYear: $("#travelDateYear").val(),
			  priceFrom: $("#priceFrom").val(),
			  priceTo: $("#priceTo").val(),
			  durationFrom: $("#durationFrom").val(),
			  durationTo: $("#durationTo").val(),
			  destination: $("#destination").val(),
			  product: $("#product").attr("checked"),
			  promotion: $("#promotion").attr("checked"),
			  cruise: $("#cruise").attr("checked"),
			  vacation: $("#vacation").attr("checked")
		},
		  function(data){
			  var deals = '';
			  if (data.length == 0) {
			  	  deals += "<p class='mice'>Your search query returned no results</p>";
			  } else {
				  for (var i = 0; i < data.length; i++) {
					deals += '<div class="dealBlock">' + '<div class="img sgb"><img src="images/thumbs/product_' + data[i].ID + '.jpg" alt="' + data[i].TITLE + '" /></div>' + '<div class="dealContent"><h2>' + data[i].TITLE + '</h2><p class="shortDescription">' + data[i].SHORT + '</p><br /><p align="right"><img src="images/icon-dots.gif" alt=".:" /> <a href="index.cfm?page=products.viewDeal&deal=' + data[i].ID +'">view details</a></p></div></div>';
				  }
			  }
			  $('#right-content').html(deals);
		  }
		);
	});
	
	// Request Information Processing
	$("#requestInfoDialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		buttons: {
			Submit: function() {
				formData = "deal=" + $('#deal').val() + "&region=" + $('#region').val() + "&firstName=" + $('#firstName').val() + "&lastName=" + $('#lastName').val() + "&address=" + $('#address').val() + "&city=" + $('#city').val() + "&province=" + $('#province').val() + "&postalCode=" + $('#postalCode').val() + "&country=" + $('#country').val() + "&phoneNumber=" + $('#phone1').val() + $('#phone2').val() + $('#phone3').val() + "&email=" + $('#email').val() + "&myagent=" + $('#myagent').val() + "&modeOfContact=" + $("input[name='modeOfContact']:checked").val() + "&request=" + $('#request').val();
				if ($("#firstName").val() == "") {
					throwError("Please enter your first name");
				} else if ($("#lastName").val() == "") {
					throwError("Please enter your last name");
				} else if ($("#email").val() == "") {
					throwError("Please enter your email");
				} else if (/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test($("#email").val()) == false) {
					throwError("Please enter valid email");
				} else {
					$.ajax({
						type: "GET",
						url: "ajax/qry_sendRequest.cfm",
						data: formData,
						success: function () {
									$("#requestInfoDialog").html('Your request has been successfully submitted');
									$("#requestInfoDialog").dialog('option', 'buttons', { "Ok": function() { $(this).dialog("close"); } });
								 }
					});
				}
			}
		}
	});
	// Request validation
	function throwError(message) {
		$("#errorContainer").fadeIn("slow");
		$("#errorContainer").text(message);	
	}
	// Open information request dialog when link is clicked
	$(".contactAgent").click(function(){
		$("#requestInfoDialog").dialog('open');				
	});
});
