var $j = jQuery.noConflict();
$j(document).ready(function(){

	//--------------------------------- Set google maps tooltip address opened by default
	if ( $j('#maplinks').length ) {
		myclick(6);
	}

	//--------------------------------- Tooltips for left sidebar
	$j('a.load-local').cluetip({
		local			: true, 
		showTitle		: false,
		cursor			: 'pointer', 
		sticky			: false, 
		positionBy		: 'fixed',
		topOffset		: 0,       
		leftOffset		: 0,  
		dropShadowSteps	: 10,
		attribute		: 'rel',
		fx				: {             
			open		: 'fadeIn', 
			openSpeed	: 'slow'
		}
	});
	
	
	$j('a.title').cluetip({splitTitle: '|'});
	$j('a.title').click(function() {
		return false;
	})
	
	//--------------------------------- Accordions in right sidebar
	
	//News accordian
	$j("#news-ul .panel").hide();
	$j("#news-ul .panel").eq(0).slideDown();
	$j("#news-ul li h5").click(function() {     
		var target = $j(this).next('ul.panel');
		$j("#news-ul li *").removeClass('active');
		$j(this).addClass('active');
		$j("#news-ul li ul.panel:visible").not(target).slideUp();
		target.slideToggle();
	});
	
	//Tracing Accordions
	$j("#formLinks div.forms").hide();
	$j("#formLinks .double-box h2").click(function() {
		var target = $j(this).next('div.forms');
		$j("#formLinks div.forms:visible").not(target).slideUp();
		target.slideToggle();
	});
	
	//Container Specs page in Ace Ocesn
	$j("#container_specs div.specs").hide();
	$j("#container_specs div.specs").eq(0).fadeIn();
	$j("#container_specs .specs_header").click(function() {  
		var target = $j(this).next('div.specs');
		$j("#container_specs div.specs:visible").not(target).slideUp();
		target.slideToggle();
	});
	
	//--------------------------------- Main Nav
	$j("ul#nav").superfish();
	
	//--------------------------------- Special Offers in right sidebar
	$j('#special').cycle({ 
		fx:     'scrollUp', 
		timeout: 6000, 
		delay:  -2000 
	});

	//--------------------------------- Header banner gallery
	$j('#gallery').cycle();

	//--------------------------------- Alternate colors for table rows
	$j('table tbody tr:odd').addClass('odd');
  	$j('table tbody tr:even').addClass('even');

	//--------------------------------- Fancybox
	
	//iFrame Mode
	$j("a.thickbox").fancybox({ 
		'frameWidth': 1000, 
		'frameHeight': 610,
		'overlayShow':	true,
		'hideOnContentClick': false
	}); 
	
	//Standard images
	$j("a.thickbox2").fancybox({ 
		'overlayShow':	true
	});
	
	//--------------------------------- Popup window for external applications
	function openPopupWindow(location) {
		$j.jqURL.loc(location, {
			win: 'Name',
			w: 850,
			h: 650,
			wintype: '_blank'
		});	
	}
	
	$j('a.popupWindow').click(function(e) {
		e.preventDefault();
		openPopupWindow(this.href);
	});
	

	
	
	//--------------------------------- Office directions summary on contact page
	$j("form#gDir").submit(function() {
     	$j('#directions').css("height","200px");
		$j('#directions').css("overflow","auto");
    });
	
	//-------------------------------------- Date picker
	$j('.date-pick-noback').datePicker({
			clickInput:true
	});
	
	//-------------------------------------- Booking form
		
	$j("input.quantity").each(function(){
		countForm($j(this).attr('id'));
	});
	$j("input.quantity").keyup(function () {
		var id = $j(this).attr('id');
	  	countForm(id);
	});
	
	if ($j('#vat_exempt').is(':checked')) $j('#vat_exempt_row').show(); 
	$j("#vat_exempt").click(function() {
		if ($j(this).is(':checked')) {
			$j('#vat_exempt_row').show();
			$j('td.total_vat span').html('0');
			updateFinalCost(0);
		} else {
			$j('#vat_exempt_row').hide();
			var vat_rate = parseFloat($j('input#vat_rate').val());
			$j('td.total_vat span').text(vat_rate);
			updateFinalCost(vat_rate);
		}
	});

});

function countForm(e) {
	var value = $j('#'+e).val();
	var e = $j('#'+e).attr('id');
	var unit_price = parseFloat($j('#'+e+'_unit_price').val());
	var total = parseFloat(value * unit_price);
	var discount = $j('#'+e+'_discount').val();	
	$j.ajax({
	   type: "POST",
	   url: 'booking-system/check-price',
	   data: 'unit_price='+unit_price+'&id='+e+'&discount='+discount+'&count='+value,
	   success: function(price){
		 price = parseFloat(price).toFixed(2);
		 $j('#'+e+'_total_price').val(price).parent().parent().css('background','none');
		 updateTotalPrice(); 
	   },
	   beforeSend: function() {
		   $j('#'+e+'_total_price').val('Loading...').parent().parent().css('background','#7eb0cd');
	   }
	 });
}
function updateTotalPrice() {
	var items = $j('input.price');	
	totalCost = 0;
	items.each(function () { 
		cost = parseFloat($j(this).val());
		totalCost = (parseFloat(totalCost) + cost).toFixed(2); 
	});
	$j('input#final_price').val(totalCost);
	updateVatRate();
}
function updateFinalCost(vat_rate) {
	totalCostWithVat = totalCost * ((vat_rate+100)/100);
	totalCostWithVat = totalCostWithVat.toFixed(2);
	$j('input#final_price_vat').val(totalCostWithVat);		
}
function updateVatRate() {
	if ($j('#vat_exempt').is(':checked')) {
		updateFinalCost(0);
		$j('td.total_vat span').html('0');
	} else {
		updateFinalCost( parseFloat( $j('input#vat_rate').val() ));
	}
}



function processForm(id) {
	//alert(id);
	countForm(id);
}	
