jQuery.noConflict();

jQuery.fn.centre = function () {
  this.css("position","absolute");
  this.css("top", ( jQuery(window).height() - this.height() ) / 2 + jQuery(window).scrollTop() + "px");
  this.css("left", ( jQuery(window).width() - this.width() ) / 2 + jQuery(window).scrollLeft() + "px");
  return this;
}
	
function showLoader() {	
	if (!jQuery("#coverup").length) {
		jQuery("#home").prepend("<div id='coverup'>&nbsp;</div>");
		
	}
	
	jQuery("#coverup").css({
		'height': jQuery(document).height(),
		'width': jQuery(document).width()
		}).show();
		
	jQuery("#loader").show().centre();
}


function hideLoader() {
	jQuery("#coverup").hide();
	jQuery("#loader").hide();
}

function populateDestination() {
  // Fill the left column with the address
  
  var daddress = jQuery("#dtown_name").val();
  if (jQuery("#dtown_name").val().length > 0) {
	  jQuery("#arrival").html(daddress);
	  jQuery("#rdeparture").html(daddress);
	  jQuery("#rdeparture").show();
	  jQuery("#destination_location").val(daddress);
	  jQuery("#satisfaction").hide();
	}
}

function populatePickup() {
	
  // Fill the left column with the address
  var address = jQuery("#ptown_name").val();

  if (jQuery("#ptown_name").val().length > 0) {
	  jQuery("#departure").html(address);
	  jQuery("#departure").show();
	  jQuery("#rarrival").html(address);
	  jQuery("#pickup_location").val(address);
	}
	if(jQuery("#pevent_purpose").val().length)
		jQuery("#event_purpose").val(jQuery("#pevent_purpose").val());
}

function step1()
{
		if (jQuery('.booking-form').css('height') != "265x") {
			jQuery('.booking-form').animate({height : "265px"});
		}
		jQuery('.step_selected').addClass('notyet').removeClass('step_selected');
		jQuery('#step1').removeClass('notyet').addClass('step_selected');
    jQuery(".textinput").hide();
    jQuery("#helper").html("Please select your pick up address.");
    jQuery("#pickuppointinput").show();
    
    jQuery(document).ready(function(){
        
        // On select of airport
        // POSTCODE STORED IN ADDRESS[1]
        jQuery("#airportlist").change(function(){
          var address = jQuery("#airportlist").val().split("||");
          jQuery("#departure").html(address[0] + "<br />" + address[1]);
          jQuery("#rarrival").html(address[0] + "<br />" + address[1]);
          jQuery("#pickup_location").val(address[0] + ", " + address[1]);
          jQuery("#departure").show();
        });
        
        
        // On select of address from dropdown
        jQuery("#crafty_postcode_result_display_1").change(function(){
        	populatePickup();
        });
        
    });
} // function step1


function step2()
{
  /*
  // On input of town name
  if(jQuery("#ptown_name").val() != "")
  {
    // Fill the left column with the address
    var address = jQuery("#ptown_name").val();
    jQuery("#departure").html(address);
    jQuery("#rarrival").html(address);
    jQuery("#pickup_location").val(address);
    jQuery("#jinfo").show();
  }
  */

  jQuery('#destinationinput').css({height : "261px"});
  if(jQuery('#pickup_location').val().length > 0) {
  	
			jQuery('.step_selected').removeClass('step_selected');
			jQuery('#step2').removeClass('notyet').addClass('step_selected');
			jQuery(".textinput").hide();
			
			jQuery("#helper").html("Please select your destination address.");
			jQuery("#destinationinput").show();
			
      jQuery(document).ready(function(){
  
        // On select of airport
        // POSTCODE STORED IN ADDRESS[1]
        jQuery("#dairportlist").change(function(){
          var address = jQuery("#dairportlist").val().split("||");
          jQuery("#arrival").html(address[0] + "<br />" + address[1]);
          jQuery("#rdeparture").html(address[0] + "<br />" + address[1]);
          jQuery("#rdeparture").show();
          jQuery("#destination_location").val(address[0] + ", " + address[1]);
          jQuery("#satisfaction").hide();

        });
  
         
        // On select of address from dropdown
        jQuery("#crafty_postcode_result_display_2").change(function(){
					populateDestination();
        });
        
      });

  }		
} // function step2


function step3()
{
	
	/*
  // On input of town name
  if(jQuery("#dtown_name").val() != "")
  {
    var address = jQuery("#dtown_name").val();
    jQuery("#arrival").html(address);
    jQuery("#rdeparture").html(address);
    jQuery("#destination_location").val(address);
    jQuery("#jinfo").show();
    jQuery("#satisfaction").hide();
  }
  */
  // Calculate the journey distance



  if(jQuery('#destination_location').val().length > 0 && jQuery('#pickup_location').val().length > 0) {
			jQuery("#destinationinput").hide();  	  	
			jQuery(".textinput").hide();
			
			jQuery('.step_selected').removeClass('step_selected');
			jQuery('#step3').removeClass('notyet').addClass('step_selected');

  		jQuery("#datepicker").show();
  		jQuery("#datepicker").datepicker('show');
  		jQuery("#cal_close_butt").hide();
  		jQuery("#cal_close").append('<div id="singlenext">Single journey</div> <div id="returnnext">Return journey</div>');
  		  		
  		jQuery("#singlenext").click(function() {
  		  jQuery("#cal_close_butt").click();
        
        if(jQuery("#contact_email").val().length && jQuery("#contact_phone").val().length)
          jQuery('#main_post').submit();
        else { alert('Please enter your email and your telephone before continuing');
            return false;
        }
        showLoader();
        return false;
      });  
  		jQuery("#returnnext").click(function() {
        jQuery("#cal_close_butt").click();
        jQuery("#step4").click();
        return false;
      }); 
  		jQuery("#helper").html("Please enter your pick up <strong>date</strong> and <strong>time</strong> from the calendar below.");
	}

} // function step3

function hideIt () {
    if(jQuery('#step3').hasClass('step_selected')) {
    	jQuery("#cal_close_butt").hide();
  		jQuery("#cal_close").append('<div id="singlenext">Single journey</div> <div id="returnnext">Return journey</div>');
  		jQuery("#singlenext").click(function() {
  		  jQuery("#cal_close_butt").click();
        jQuery('#main_post').submit();
        showLoader();
        return false;
      });  
  		jQuery("#returnnext").click(function() {
        jQuery("#cal_close_butt").click();
        jQuery("#step4").click();
        return false;
      }); 
   }
}

function step4()
{
  if(jQuery('#datepicker').val().length > 0 && jQuery('#destination_location').val().length > 0) {
			jQuery('.step_selected').removeClass('step_selected');
			jQuery('#step4').removeClass('notyet').addClass('step_selected');
			jQuery(".textinput").hide();
			jQuery("#rdatepicker").datepicker('show');
			jQuery("#cal_close_butt").click(function() {
        if(jQuery("#contact_email").val().length && jQuery("#contact_phone").val().length)
          jQuery('#main_post').submit();
        else { alert('Please enter your email and your telephone before continuing');
            return false;
        }
        showLoader();
        return false;
      }); 
  		jQuery("#helper").html("Please enter your return <strong>date</strong> and <strong>time</strong> from the calendar below.");
	}		

} // function step4


jQuery(document).ready(function() {
	jQuery("#home").prepend("<div id='loader' style='display:none;'>Loading...</div>");
	jQuery("#home").prepend("<div id='coverup'>&nbsp;</div>");

    
  

  load();
	/*
  jQuery('#chatshow').click(function(){
    jQuery('#col-left').show();
  });
  
  jQuery('#chathide').click(function(){
    jQuery('#col-left').hide();
  });
  */

	// NAV FUNCTIONS
	jQuery("#topnav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag
	
	jQuery("#topnav li").each(function() { //For each list item...
		var linkText = jQuery(this).find("a").html(); //Find the text inside of the a tag
		jQuery(this).find("span").show().html(linkText); //Add the text in the span tag

  	var pageID = jQuery("body").attr("id");// Get Page ID
    var navClass = jQuery(this).attr("class");	//Get current list class name	
    
		if (pageID == navClass) { //If page ID is not the same as hovered list class name...
			jQuery(this).find("span").stop().animate({ 
				marginTop: "-28" //Find the span tag and move it up 40 pixels
			}, 0);
		}
		
	});	
	
	var pageID = jQuery("body").attr("id");// Get Page ID
	//var pageID = "latest";
	
	jQuery("#topnav li").hover(function() {	//On hover...
		var navClass = jQuery(this).attr("class");	//Get current list class name	
		if (pageID != navClass) { //If page ID is not the same as hovered list class name...
			jQuery(this).find("span").stop().animate({ 
				marginTop: "-28" //Find the span tag and move it up 40 pixels
			}, 250);
		}
	} , function() { //On hover out...
	var navClass = jQuery(this).attr("class");	//Get current list class name	
		
	if (pageID != navClass) { //If page ID is not the same as hovered list class name...
		jQuery(this).find("span").stop().animate({
			marginTop: "0" //Move the span back to its original state (0px)
		}, 250);
	}
	
	});
	// END NAV FUNCTIONS
	
  // BOX SELECTOR
  
  // Prevent access to the next steps

	if (!jQuery('#step2').hasClass('nohide')) {
		jQuery('#step2').addClass('notyet');
	}
	if (!jQuery('#step3').hasClass('nohide')) {
		jQuery('#step3').addClass('notyet');
	}
	if (!jQuery('#step4').hasClass('nohide')) {
		jQuery('#step4').addClass('notyet');
	}
  jQuery(".textinput").hide();
  jQuery("#book-next").addClass("hidden");
  
  jQuery("#book-next").click(function() { 
      if(jQuery("#contact_email").val().length && jQuery("#contact_phone").val().length)
        jQuery('#main_post').submit();
      else { alert('Please enter your email and your telephone before continuing');
          return false;
      }
      showLoader();
      return false;
  });
  
  jQuery('#step1').click(function() {
  	if (!jQuery(this).hasClass("step_selected")) {
    	step1();
    }
  });

	jQuery('#step2').click(function() {
  	if (!jQuery(this).hasClass("step_selected")) {		
			step2();
		}
	});

	jQuery('#step3').click(function() {
			step3();
	});

	jQuery('#step4').click(function() {
    	step4();
	});
  

  // Departure Date
  jQuery("#datepicker").datepicker({
    	duration: '',
      showTime: true,
      constrainInput: false,
      stepMinutes: 5,  
      stepHours: 1,  
      altField: '#date',
      altTimeField: '#pickuptime',
      altFormat: 'd M yy',
      defaultDate: +2,
      minDate: +2,
      yearRange: +1, 
      time24h: true,
      onClose: function() {
   			// Update calendar
				dateText = jQuery("#date").val();
		    timeText = jQuery("#pickuptime").val()
		
				var arr_dateText = dateText.split(" ");
				jQuery("#pickupday").html(arr_dateText[0]);
		    jQuery("#pickupmonth").html(arr_dateText[1]);
		    jQuery("#departure_time").html(dateText);
		    jQuery("#departure_time2").html(timeText);
		    jQuery("#book-next").removeClass("hidden");
		    jQuery("#helper").html("Thanks! Please click <strong>continue</strong> to select your vehicle, or click on <strong>Return</strong> if you require a return taxi.");
      }
		});
	
  // Return Date
  jQuery("#rdatepicker").datepicker({
    	duration: '',
      showTime: true,
      constrainInput: false,
      stepMinutes: 5,  
      stepHours: 1,  
      altField: '#rdate',
      altTimeField: '#rpickuptime',
      altFormat: 'd M yy',
      defaultDate: +7,
      minDate: 0, 
      yearRange: +1,
      time24h: true,
      onClose: function() {
    		// Update calendar
    		rdateText = jQuery("#rdate").val();
        rtimeText = jQuery("#pickuptime").val()
        
    		var rarr_dateText = rdateText.split(" ");
    		jQuery("#rpickupday").html(rarr_dateText[0]);
        jQuery("#rpickupmonth").html(rarr_dateText[1]);
        jQuery("#rinfo").show();
        jQuery("#return").html(rdateText);
        jQuery("#return2").html(rtimeText);
        jQuery("#book-next").removeClass("hidden");
        jQuery("#helper").html("Thanks! Please click <strong>continue</strong> to select your vehicle.");

      }
	});
	  

  jQuery("#slider").slider({
    value:1,
    min: 1,
    max: 72,
    step: 1,
    slide: function(event, ui) {
      jQuery("#passengercount").html(ui.value,0);

      jQuery(".listing-item").each(function(index, vehicle){
        var classList = jQuery(this).attr('class').split(' ');
        jQuery.each( classList, function(index, item){
          if (item.substr(0,3) == "cap")
          {
            if (item.substr(3,2) < ui.value) {
              jQuery(vehicle).hide();
              jQuery(vehicle).addClass("dc");
            }
            else {
              jQuery(vehicle).removeClass("dc");
              if (!(jQuery(vehicle).hasClass("ds")) && !(jQuery(vehicle).hasClass("dl")))
              {
                jQuery(vehicle).show();
              }
            }
          }
        });      

      });
      
    }
  });
  

  jQuery("#slider2").slider({
    value:1,
    min: 1,
    max: 72,
    step: 1,
    slide: function(event, ui) {
      jQuery("#largebagscount").html(ui.value,0);      
      
      jQuery(".listing-item").each(function(index, vehicle){
        var classList = jQuery(this).attr('class').split(' ');
        jQuery.each( classList, function(index, item){
          if (item.substr(0,2) == "lb")
          {
            
            if (item.substr(2,2) < ui.value) {
              jQuery(vehicle).hide();
              jQuery(vehicle).addClass("dl");
            }
            else {
              jQuery(vehicle).removeClass("dl");
              if (!(jQuery(vehicle).hasClass("ds")) && !(jQuery(vehicle).hasClass("dc")))
              {
                jQuery(vehicle).show();
              }
            }
          }
        });      

      });

    }
  });


  jQuery("#slider3").slider({
    value:1,
    min: 1,
    max: 72,
    step: 1,
    slide: function(event, ui) {
      jQuery("#smallbagscount").html(ui.value,0);

      jQuery(".listing-item").each(function(index, vehicle){
        var classList = jQuery(this).attr('class').split(' ');
        jQuery.each( classList, function(index, item){
          if (item.substr(0,2) == "sb")
          {
            
            if (item.substr(2,2) < ui.value) {
              jQuery(vehicle).hide();
              jQuery(vehicle).addClass("ds");
            }
            else {
              jQuery(vehicle).removeClass("ds");
              if (!(jQuery(vehicle).hasClass("dc")) && !(jQuery(vehicle).hasClass("dl")))
              {
                jQuery(vehicle).show();
              }
            }
          }
        });      

      });

    }
  });


	jQuery("#step1next").click(function() {
		populatePickup();	
    jQuery('#step2').click();
    return false;
  });
  
	jQuery("#step2next").click(function() {
		populateDestination();
    jQuery('#step3').click();
    return false;
  });  
  
	
  
  if(jQuery(".cms-home").length || jQuery(".embed_form").length || jQuery(".embed_form_wid").length)
    setTimeout( function() { jQuery('#step1').click(); }, 1000 );  
	


	
});