//Set jQuery into no conflict mode
var $jq = jQuery.noConflict();

$jq().ready(function() {
	// Sets the Kwiucks Accordion in motion
	$jq('.kwicks').kwicks({	max : 445, spacing : 10, duration:1000, easing: 'easeOutCubic'});
});
		
$jq(function(){
	// This is the CUFON FONT target. All elements are targeted individually for complete control.
   Cufon.replace('h1, h2, h4, .accordion-textbox h2, span.accordion-title, .business-box h2,  span.title , #tagline a, .homepage-box h2, .footer-widgets h4, li.sliderImage span strong, .magazine-frontpage-toolbox h3, .magazine-frontpage-toolbox h4, #left-content h2, #fullwidth-content h2, #left-content h3, #fullwidth-content h3, li.widget h2.widgettitle, #modal-info-wrapper h2, #modal-info-wrapper h3, #modal-info-wrapper h4, .portfolio-header  h2, a.portfolio-visit-anchor, span.BookNow', {hover: true});
});

 $jq(document).ready(function() {	
 
	// Forced jQuery Injected Styling for the Contact Form 7 
	$jq('div.wpcf7 p:first').addClass("left");		
	$jq('div.wpcf7 p:nth-child(3)').addClass("left");		
	$jq('div.wpcf7 p:nth-child(4)').addClass("left");		
	$jq('div.wpcf7 input:last').addClass("contact-submit");
		
	//hover effect for the kwicks accordion	
	$jq('.kwicks li div.accordion-overlay').hover(		
		function(){			
			$jq(this).children('div.accordion-textbox').stop().animate({top: '200px'}, 600, 'easeOutCubic');},			
		function(){			
			$jq(this).children('div.accordion-textbox').stop().animate({top: '300px'}, 1000, 'easeOutCubic');
	});		
	
	
	$jq('.kwicks li div.accordion-overlay').hover(		
		function(){			
			$jq(this).children('span.modern-position').stop(true,true).animate({left: '-170px'}, 600);
			$jq(this).children('span.ie-position').stop(true,true).animate({left: '-34px'}, 300);
		},			
		function(){			
			$jq(this).children('span.modern-position').stop(true,true).animate({left: '-134px'}, 600);
			$jq(this).children('span.ie-position').stop(true,true).animate({left: '0px'}, 300);
	});	
	
	// Browser Specific Code to make Accordion degrade geacefully 
	if (jQuery.browser.msie) { 
		if(parseInt(jQuery.browser.version) == 8) { 
			$jq('span.accordion-title').removeClass('modern-position').addClass('ie-position'); 
		} 
	} 
	if (jQuery.browser.msie) { 
		if(parseInt(jQuery.browser.version) == 7) { 
			$jq('span.accordion-title').removeClass('modern-position').addClass('ie-position'); 
		} 
	} 	
			
	if($jq.browser.safari && parseInt($jq.browser.version.substr(0,3))<528){
	   $jq('span.accordion-title').hide(300);
	}
	
	if($jq.browser.opera) {
	   $jq('span.accordion-title').hide(300);
	}
			
	if($jq.browser.mozilla && $jq.browser.version <= "1.9") {
	   $jq('span.accordion-title').hide(300);
	}
		
		
	// hover effect for the navigation dropdown
	$jq('ul#menu li').hover(	
		function(){					
			$jq(this).children('ul').stop(true,true).slideDown(300);},		
		function(){	
			
			$jq(this).children('ul').animate({opacity: '+=0'}, 100).slideUp(300); 		
	}); 		
 
	// Hover effect for the RSS Link
	$jq('a#navigation-rss').children('span').css('opacity', 0);
	$jq('a#navigation-rss').children('span').css('display', 'block');
	$jq('a#navigation-rss').hover(
		function(){
			$jq(this).children('span').stop().fadeTo(900, 1);},
		function(){
			$jq(this).children('span').stop().fadeTo(900, 0);;
	});			
 
	// Hover effect for the default Magazine post 
	$jq('a.magazine-anchor-default').hover(	    		
			function(){				
				$jq(this).children('img').stop().animate({opacity: .5}, 600);
				},				
			function(){								
				$jq(this).children('img').stop().animate({opacity: 1}, 600);
	});
	
	// Hover effect for the Video Magazine post
	$jq('a.video-overlay-anchor').hover(			
			function(){				
				$jq(this).parent('div').parent('div').children('div').children('img').stop().animate({opacity: .6}, 600);},				
			function(){
				$jq(this).parent('div').parent('div').children('div').children('img').stop().animate({opacity: 1}, 600);	
	});	
	
	// Hover effect for the image Magazine post 
	$jq('a.image-overlay-anchor').hover(	    		
			function(){				
				$jq(this).children('img').stop().animate({opacity: .5}, 600);
				$jq(this).parent('div').children('h2').stop().animate({bottom: '0px'}, 300);},				
			function(){					
				$jq(this).children('img').stop().animate({opacity: 1}, 600);				
				$jq(this).parent('div').children('h2').stop().animate({bottom: '-20px'}, 300);
	});		
	
	// Hover effect for the Homepage widgets
	$jq('.bb-image img, .bb-video img').hover(
			function(){
				$jq(this).stop().animate({opacity:0.4}, 1000);
				
				},
			function(){
				$jq(this).stop().animate({opacity:1}, 1000);
	});
	
	$jq('.bb-image h2, .bb-video h2').hover(
			function(){
				$jq(this).parent('div').children('a').children('img').stop().animate({opacity:0.4}, 1000);
				},
			function(){
				$jq(this).parent('div').children('a').children('img').stop().animate({opacity:1}, 1000);
	});
		
	// Magazine Toolbox (Mini-sidebar) click Events - 				
	$jq('div.toolbox-animate h3').toggle(			
		function(){
			$jq('div.toolbox-margin').hide(500);	
			$jq('div.toolbox-animate').addClass('hide-me');       														// (1) - add class .hide-me to all child divs 
			$jq(this).parent().removeClass('hide-me');     																// (2) - remove class .hide-me on *this* div 
			$jq(this).parent().children('div.jScrollPaneContainer, div.toolbox-search-wrapper').show(300); 				// (3) - expand *this* div's ul list scrollbar styled container 
			$jq(this).parent().children('div.toolbox-bottom-corners, div.toolbox-bottom-corners2').show(300); 			// (4) - expand *this* div's bottom corners
			$jq(this).parent('div').css( {backgroundPosition: "165px -148px"} ); 										// (5) - animate background position
			$jq('div.hide-me').hide(500);},  																			// (6) - hide all .hide-me divs			
		function(){																										// (7) - callback >> hide internal ul and unhide other divs	
		$jq('div.toolbox-margin').show(500);
		$jq('div.hide-me').show(500);
		$jq(this).parent().children('div').hide(300);
		$jq(this).parent('div').css( {backgroundPosition: "165px 2px"} ); 	
	});
	
	// Controls the scroll bar in the magazine homepage (inside the toolbox)
	$jq(function(){
			$jq('.scroll-pane').jScrollPane({
				scrollbarWidth: 20
			});
		});
	
	// Onload Fades in the Magazine Toolbox
	$jq('div.magazine-frontpage-toolbox').animate({opacity: '+=0'}, 600).fadeTo(1000, 1)
	
	//Onload Fades out the Spans in the Portfolio Page 	
	$jq('a.portfolio-modal-anchor, a.portfolio-video-modal-anchor').children('span').animate({opacity: 1}, 100);
		
	// Hover effect for the Portfolio Galleria Items
	$jq('a.portfolio-modal-anchor, a.portfolio-video-modal-anchor').hover(	    		
			function(){				
				$jq(this).children('img').stop().animate({opacity: .4}, 600);
				$jq(this).children('span').stop().animate({opacity: 1, bottom: '0px'}, 300);},				
			function(){					
				$jq(this).children('img').stop().animate({opacity: 1}, 600);
				$jq(this).children('span').stop().animate({opacity: 0, bottom: '-20px'}, 600);				
	});
	
	// Dynamically adds ID of "embedded-video" to relevant div containing hidden video object	
	$jq('div.portfolio-wrapper-video').hover(	    		
			function(){			
				$jq('div.video-target').removeClass('embedded-video');  
				$jq(this).children('div').children('div.video-target').addClass('embedded-video');},				
			function(){					
				$jq(this).children('div').children('div.video-target').removeClass('embedded-video');
	});	
	
	$jq('div.business-box').hover(	    		
			function(){			
				$jq('div.video-target').removeClass('embedded-video');  
				$jq(this).children('div').children('div.video-target').addClass('embedded-video');},				
			function(){					
				$jq(this).children('div').children('div.video-target').removeClass('embedded-video');
	});	
	
	
	$jq("#bookingForm").validationEngine();
	

	$jq("input[type=text]").focus(function(){
		 $jq(this).addClass("highlight");
	});
	
	 $jq("input[type=text]").focusout(function(){
		 $jq(this).removeClass("highlight");
		
	});

			
	
});

$jq(document).ready(function(){	
	
	//The Colourbox Modal Window is initiated here 	
	$jq(".colourbox-image").colorbox();
	$jq(".portfolio-modal-anchor").colorbox({iframe:true, innerWidth:950, innerHeight:560});
	$jq(".homepage-modal-anchor").colorbox({iframe:true, innerWidth:950, innerHeight:560});
	$jq(".portfolio-video-modal-anchor").colorbox({inline:true, href:".embedded-video"});
	$jq(".homepage-video").colorbox({inline:true, href:".embedded-video"});

	//The S3 Slider is initiated here	 
	var slider = $jq('#slider');
	if(slider.length > 0){
		slider.s3Slider({ timeOut: 8000 });
	}
	
});

function daysbetween(d1,d2){
		var one_day=1000*60*60*24;
		//Calculate difference between the two dates, and convert to days
		var days = Math.ceil((d2.getTime()-d1.getTime())/(one_day));
		return days+1;
	}
	function calc_weekends(start_day, duration) {
		var days_until_weekend = new Array(5, 4, 3, 2, 1, 1, 6);
		var adjusted_duration = duration - days_until_weekend[start_day]
		var weekends = 0;
		if (adjusted_duration < 0){
			weekends = 0;
		} else{
			weekends = (adjusted_duration/7)+1;
		}
		if ((start_day == 5) && ((duration % 7) == 0)){ //Saturday to Saturday is an exception
			weekends += 1;
		}
		return weekends
	}
	
	function calcBusinessDays(dDate1, dDate2) {         // input given as Date objects

		var iWeeks, iDateDiff, iAdjust = 0;
	
		if (dDate2 < dDate1) return -1;                 // error code if dates transposed
	
		var iWeekday1 = dDate1.getDay();                // day of week
		var iWeekday2 = dDate2.getDay();
	
		iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1;   // change Sunday from 0 to 7
		iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
	
		if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1;  // adjustment if both days on weekend
	
		iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1;    // only count weekdays
		iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
	
		// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
		iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
	
		if (iWeekday1 <= iWeekday2) {
			iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
		} else {
			iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
		}
	
		iDateDiff -= iAdjust                            // take into account both days on weekend
	
		return (iDateDiff);                         // add 1 because dates are inclusive

	}
	function calcPrice(startdate,enddate,theweekdayprice,theweekendprice,theweekprice){
		var one_day=1000*60*60*24;
		var daycount = daysbetween(startdate,enddate);
		if(daycount == 0){
			daycount = 1;
		}
		if(daycount < 0){
			daycount *= -1;
		}
		var weekcount = 0;
		if(daycount > 7){
			weekcount = (daycount-(daycount%7))/7;
		} else if(daycount == 7){
			weekcount = 1;
		}
		
		var weekprice = theweekprice*weekcount;
		var finalprice = weekprice;
		
		if(daycount %7 != 0){
			
			// count the days and figure it out
			var tempstartdate = new Date();
			tempstartdate.setTime(enddate.getTime() - (one_day*((daycount%7))));
			var diff = daysbetween(tempstartdate,enddate);
			var price = 0
			if(theweekendprice != 0){
				var weekendcount = calc_weekends(tempstartdate.getDay(),diff);
				var busdays = calcBusinessDays(tempstartdate,enddate);
				price += (weekendcount*theweekendprice);
				price += (busdays*theweekdayprice);
			} else {
				price += (diff * theweekdayprice);
			}
			if(price > theweekprice){
				finalprice += theweekprice;
			}else{
				finalprice += price;
			}
		}
		if(finalprice < 0){
			finalprice *= -1;
		}
		finalprice = finalprice*1.20; 
		return finalprice.toFixed(2);
	}
jQuery.fn.fadeThenSlideToggle = function(speed, easing, callback) {
	if (this.is(":hidden")) {
		return this.animate({ 
			height: 'show', 
			opacity: 'show' 
		}, 'slow');
		//return this.slideDown(speed, easing).fadeTo(speed, 1, easing, callback);
	} else {
		return this.animate({ 
			height: 'hide', 
			opacity: 'hide' 
		}, 'slow');//fadeTo(speed, 0, easing).slideUp(speed, easing, callback);
	}
};
jQuery.fn.exists = function(){return jQuery(this).length>0;}
jQuery(document).ready( function(){



	if((jQuery('#bookingSlideinside').exists() == false)&&(jQuery('#bookingSlide').exists() == false)){
		return;
	}

	
	// the cars the initial selection so set the prices
	window.mindailyprice = 35;
	window.minweekendprice = 59.50;
	window.minweeklyprice = 140;
	
	window.maxdailyprice = 80;
	window.maxweekendprice = 136;
	window.maxweeklyprice = 280;
	
	
	
	
	jQuery('#temporaryinsurancewarning').hide();
	var td = jQuery('#datepicker').val();
	var now = new Date();
	if((td.length != 10)&&(td.length != 9)){
		var todaysdate = now.getDate()+"/"+(now.getMonth()+1)+"/"+now.getFullYear();
		jQuery('#datepicker').val(todaysdate);
	}
	var tomorrowsdate = now.getDate()+"/"+(now.getMonth()+1)+"/"+now.getFullYear();
	td = jQuery('#datepicker2').val();
	if((td.length != 10)&&(td.length != 9)){
		now.setTime(now.getTime() + (1000*3600*24));
		
		jQuery('#datepicker2').val(tomorrowsdate);
	}
	
	jQuery('#formCar').change(function(){
		window.mindailyprice = 25;
		window.minweekendprice = 42.50;
		window.minweeklyprice = 125;
		window.maxdailyprice = 35;
		window.maxweekendprice = 59.50;
		window.maxweeklyprice = 140;
		duration();
		jQuery('#temporaryinsurancewarning').animate({ 
			height: 'hide', 
			opacity: 'hide' 
		}, 'slow');
	});
	jQuery('#formVan').change(function(){
		window.mindailyprice = 35;
		window.minweekendprice = 59.50;
		window.minweeklyprice = 140;
		
		window.maxdailyprice = 80;
		window.maxweekendprice = 136;
		window.maxweeklyprice = 280;
		duration();
		jQuery('#temporaryinsurancewarning').animate({ 
			height: 'hide', 
			opacity: 'hide' 
		}, 'slow');
	});
	jQuery('#formMiniBus').change(function(){
		window.mindailyprice = 90;
		window.minweekendprice = 0;
		window.minweeklyprice = 315;
		
		window.maxdailyprice = 100;
		window.maxweekendprice = 0;
		window.maxweeklyprice = 350;
		duration();
		jQuery('#temporaryinsurancewarning').animate({ 
			height: 'show', 
			opacity: 'show' 
		}, 'slow');
	});
	jQuery('#formTruck').change(function(){
		window.mindailyprice = 90;
		window.minweekendprice = 0;
		window.minweeklyprice = 315;
		window.maxdailyprice = 100;
		window.maxweekendprice = 0;
		window.maxweeklyprice = 350;
		duration();
		jQuery('#temporaryinsurancewarning').animate({ 
			height: 'show', 
			opacity: 'show' 
		}, 'slow');
	});
	/* .text(
				"Costing roughly \u00A3"+calcPrice(d1,d2,window.mindailyprice,window.minweekendprice,window.minweeklyprice) + " to \u00A3"+calcPrice(d1,d2,window.maxdailyprice,window.maxweekendprice,window.maxweeklyprice)
			)*/
	function duration (){
		var one_day=1000*60*60*24;
		var d1 = jQuery('#datepicker').datepicker( "getDate" );
		var d2 = jQuery('#datepicker2').datepicker( "getDate" );
		//var d1r = jQuery('#datepicker').datepicker.parseDate('mm/dd/yy', d1);
		//var d1r = $.datepicker.parseDate('yy-mm-dd', d1);

		//alert(d1r);
		jQuery('#datepicker2').datepicker( "option", "minDate", d1);
		
		var days = daysbetween(d1,d2);
		//alert(days);
		var hduration = jQuery('#hireduration');
		if(days > 1){
			
			hduration.animate({ 
				opacity: 'hide' 
			}, 'fast').animate({ 
				opacity: 'show' 
			}, 'fast');
		} else if(days == 1){
			hduration.animate({ 
				opacity: 'hide' 
			}, 'fast').text(
				"You cannot select the same date for pick up and return."
			).animate({ 
				opacity: 'show' 
			}, 'fast');
		}
	}
	
	
	jQuery('#datepicker').datepicker({
		onSelect: function(dateStr) {
			var inc = 1;
			var d = jQuery.datepicker.parseDate('dd/mm/yy', dateStr);
			if(d.getDay() == 6){
				inc = 2;
				alert("Pick ups on a Saturday must be returned from the following Monday onwards. The weekend rate will be applied to all bookings that are booked on a saturday.");
			}
			d.setDate(d.getDate() + inc);
			jQuery('#datepicker2').datepicker('setDate', d);
			jQuery('#datepicker2').datepicker("option", "minDate", d);
		},
		beforeShowDay: noSunday,
		dateFormat: 'd/m/yy',
		showAnim: 'fadeIn'
		
	});
	jQuery('#datepicker2').datepicker({
		beforeShow: function(dateStr) {
			var d1 = jQuery('#datepicker').datepicker( "getDate" );
			jQuery('#datepicker2').datepicker( "option", "minDate", d1);
		},
		beforeShowDay: noSunday,
		dateFormat: 'd/m/yy',
		showAnim: 'fadeIn'
	});
	
	/*$('#from').datepicker({
		onSelect: function(dateStr) {
			var d = $.datepicker.parseDate('mm/dd/yy', dateStr);
			d.setDate(d.getDate() + 3); // Add three days
			$('#to').datepicker('setDate', d);
		}
});*/

	

      function noSunday(date){
          var day = date.getDay();
             return [(day > 0), ''];
      }; 	
	
	//$jq('#datepicker').datepicker({ beforeShowDay: $.datepicker.noWeekends })
	

	jQuery('#datepickericon').click(function(){
		jQuery('#datepicker').datepicker('show');							
	});
	jQuery('#datepickericon2').click(function(){
		jQuery('#datepicker2').datepicker('show');							
	});
	
	/// Set Date
	
	var currentTime = new Date()
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate() +1
	var year = currentTime.getFullYear()
	//document.write(day + "/" + month + "/" + year)

	//var tomorrowsdateNew = (now.getDate()+1)+"/"+(now.getMonth()+1)+"/"+now.getFullYear();
	var tomorrowsdateNew2 = (day + "/" + month + "/" + year)
	//alert(tomorrowsdateNew2)
	jQuery('#datepicker2').val(tomorrowsdateNew2);
	
	
	///// THIS IS THE DATE CHECKER TO LIMIT THE BOOKING OF SAME DAY ///////
	
	jQuery('#datepicker2').blur(function() {
   		//alert(1);
		var d1 = jQuery('#datepicker').datepicker( "getDate" );
		var d2 = jQuery('#datepicker2').datepicker( "getDate" );
		//alert(d1);
		//alert(d2);		
		if (d1 == d2) {
			//alert("Please Select another end date. You cant return the vehicle on the same day you hired it.");
			//jQuery('#datepicker2').val('');
			//jQuery('#ui-datepicker-div').hide();
	
		} else {
			//alert('Good');
		}
		
		});








});




