
//////////////////////////////////////////
// Display Init Functions
//////////////////////////////////////////

// Hover buttons
$(function($)
{
	$(".hvr").hoverSwap( {overSuffix:'hvr'} );
});

String.prototype.trim = function()
{
	return $.trim(this);
}

//////////////////////////////////////////
// Fare Calculator Form
//////////////////////////////////////////

function fareAJAX(e){		
	
	if (e != null){
		e.preventDefault();
	}
	
	if( $("#spSuburb").val() == '' || $("#spSuburb").val() == 'Pickup Suburb') return;
	if( $("#sdSuburb").val() == '' || $("#sdSuburb").val() == 'Dropoff Suburb') return;
	
	var foundPickup = false;
	// check if the pickup location specified was in the list
	$("#spSuburb").first().autocomplete("widget").find("*[role=menuitem]").each(function(i,e){
    	if($(e).text().toLowerCase() ==  $("#spSuburb").val().toLowerCase() )
		{
			foundPickup = true;
			return;
		}
	});
	
	var foundDropoff = false;
	// check if the pickup location specified was in the list
	$("#sdSuburb").first().autocomplete("widget").find("*[role=menuitem]").each(function(i,e){
    	if($(e).text().toLowerCase() ==  $("#sdSuburb").val().toLowerCase() )
		{
			foundDropoff = true;
			return;
		}
	});

	if ( (foundPickup & foundDropoff) == false )
	{
		return;
	}
		
	$("#loading").fadeIn();
	
	$("#error_text").hide();
	$("#result_text").hide();
	
	$("#result_text").ajaxError(function()
	{
		$("#error_text").html("Invalid input");
		$("#error_text").fadeIn();
		$("#loading").hide();
	});
							
	$.post("/fare.cfm", 
			{
				sdSuburb: $("#sdSuburb").val() , 
				spSuburb: $("#spSuburb").val() 
			}, 
			fareChange);
}

function fareChange(total){
	$("#loading").hide();
	if ( total < 8 && total != 0 ) total = 8;

	if ($("#spSuburb").val() == '' || $("#sdSuburb").val() == '' || total == 0){
		$("#result_text").fadeOut();
	}
	else{
		$("#result_text").fadeOut('fast', function(){
			$("#dollars").html(getDollars(total));
			$("#cents").html(getCents(total));
		});
		$("#result_text").fadeIn();
	}
}

// Attach event to form submit
$(document).ready(function(){
	fareAJAX(null);
	$("#fare_submit").click(fareAJAX);
	$(".fare_select").change(fareAJAX);
	
});



// Get dollars from floating value
function getDollars(amount){
	return Math.floor(amount);
}

// Get cents from floating value
function getCents(amount){
	
	amount = Math.round(amount * 10) / 10;
	retVal = Math.round((amount - Math.floor(amount)) * 100);		// strange issue with calculation, so apply rounding
	if (retVal == 0){
		return "00";		// return "00", instead of 0
	}
	else if (retVal < 10)
	{
		retVal = retVal * 10;
	}
	else{
		return retVal;		// return value
	}
}


//////////////////////////////////////////
// Order Popup
//////////////////////////////////////////

// Attach event to form submit
$(document).ready(function(){
	$(".order_now").click(function(e){
		e.preventDefault();
		document.location.href = 'http://www.yellowcab.com.au/customer-services/content.cfm/Online/410/';
		//window.open('http://61.9.213.240/ecabcall/default.asp', 'OrderNow', 'height=600, width=850');
	})
});



//////////////////////////////////////////
// Select Box JQuery Styling http://www.adamcoulombe.info/lab/jquery/select-box/
//////////////////////////////////////////

(function($){
 $.fn.extend({
 
 	customStyle : function(options) {
	  if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
	  return this.each(function() {
	  
			var currentSelected = $(this).find(':selected');
			$(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({position:'absolute', opacity:0,fontSize:$(this).next().css('font-size')});
			var selectBoxSpan = $(this).next();
			var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
			var selectBoxSpanInner = selectBoxSpan.find(':first-child');
			selectBoxSpan.css({display:'inline-block'});
			selectBoxSpanInner.css({width:selectBoxWidth, display:'inline-block'});
			var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
			$(this).height(selectBoxHeight).change(function(){
				// selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
			selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
				// Thanks to Juarez Filho & PaddyMurphy
			});
			
	  });
	  }
	}
 });
})(jQuery);

			
