/**
 * Parking Fee Calculator for Hobart Airport v2
 * <http://hobartairport.com.au>
 * 
 * @author: Additional View <http://additionalview.com>
 * @date: 22 March,2010
 * @version: 2
 * @dependencies: date.js,jQuery
 */

function ParkingCalculator()
{
	var self = this;

	// Parking zones with algorithms
	this.zones = {	
		// Public parking
		public : function(weeks, days, hours, minutes)
		{		
			var dailyRate = 15;
		
			var months = 0;
			var cost = 0;		
			
			var totalDays = weeks * 7 + days;
			
			if (totalDays >= 1 && (hours >= 1 || minutes >= 1)) {
				totalDays++;
				hours = 0;
				minutes = 0;
			}
			
			while (totalDays >= 12) {
				months++;
				totalDays = totalDays - 30;
			}
			
			var remainingDays = (totalDays > 0 ? totalDays : 0);
			
			weeks = 0;
			
			if (remainingDays >= 5) {		
				while (remainingDays >= 5) {
					weeks++;
					remainingDays = remainingDays - 7;
				}
			}
			
			remainingDays = (remainingDays > 0 ? remainingDays : 0);
			
			// Days and weeks
			var cost = ((months * 150) + ( weeks * 75 ) + ( remainingDays * dailyRate ))	
				// 8 - 24 hours
				if ( hours >= 8 )
					return cost += dailyRate;
				// 6 - 8 hours
				if ( hours >= 6 )
					return cost += dailyRate;
				// 2 - 6 hours
				if ( hours >= 2 )
					return cost += 10
				//  60 - 90 minutes
				if ( hours >= 1 )
					return cost += 8;
				// 30 - 60 minutes
				if ( minutes >= 30 )
					return cost += 6;
				// 7 - 30 minutes
				if ( minutes >= 7 )
					return cost += 3;
				// <7 minutes
			return cost;
		},
		// Outdoor valet parking
		outdoor : function(weeks, days, hours, minutes)
		{
			//  Minimum 2 days
			if ( weeks == 0 && days < 2 )
			{
				alert('Minimum of 2 days parking in outdoor valet car park');
				return 0;
			}
			
			var dailyRate = 15;
		
			var months = 0;
			var cost = 0;		
			
			var totalDays = weeks * 7 + days;
			
			if (totalDays >= 1 && (hours >= 1 || minutes >= 1)) {
				totalDays++;
				hours = 0;
				minutes = 0;
			}
			
			while (totalDays >= 12) {
				months++;
				totalDays = totalDays - 30;
			}
			
			var remainingDays = (totalDays > 0 ? totalDays : 0);
			
			weeks = 0;
			
			if (remainingDays >= 5) {		
				while (remainingDays >= 5) {
					weeks++;
					remainingDays = remainingDays - 7;
				}
			}
			
			remainingDays = (remainingDays > 0 ? remainingDays : 0);
			
			// Days and weeks
			var cost = ((months * 150) + ( weeks * 75 ) + ( remainingDays * dailyRate ))	
				// 8 - 24 hours
				if ( hours >= 8 )
					return cost += dailyRate;
				// 6 - 8 hours
				if ( hours >= 6 )
					return cost += dailyRate;
				// 2 - 6 hours
				if ( hours >= 2 )
					return cost += 10
				//  60 - 90 minutes
				if ( hours >= 1 )
					return cost += 8;
				// 30 - 60 minutes
				if ( minutes >= 30 )
					return cost += 6;
				// 7 - 30 minutes
				if ( minutes >= 7 )
					return cost += 3;
				// <7 minutes
			return cost;
		},		
		// Undercover valet parking
		undercover : function( weeks,days,hours,minutes )
		{
			// Minumum 2 days
			if ( weeks == 0 && days < 2 ){
				alert( 'Minimum of 2 days parking in undercover valet car park' );
				return 0;
			}
			
			var cost = self.zones.outdoor(weeks, days, hours, minutes);			
				cost = Math.ceil(cost * 1.25); // Cost = Outdoor  + 25%
			return cost;
		}
	};
	
	this.leap_year = function( year )
	{
		year = parseInt(year);

		if( year % 4 == 0 )
		{
			if( year % 100 == 0 )
			{
				if( year % 400 == 0 )
				{
					return true;
				}
				return false;
			}
			return true;
		}		
		return false;
	};
	
	// Perform calculation
	this.calculate = function ( zone,arrive,leave )
	{
		if ( !this.zones[zone] || arrive == undefined || leave == undefined )
			return false;
			
		var duration = this.splitTime( arrive,leave );
		return this.zones[zone]( duration['weeks'],duration ['days'],duration['hours'],duration['minutes'] );
	};
	
	// Accepts arrival and leave time and breaks down difference into weeks,days,minutes
	this.splitTime = function( arrive,leave )
	{
		var difference = leave - arrive;
		
		if ( difference < 0 )
			return false;
		
		var times = [];
		times['weeks'] = 60 * 60 * 24 * 7;
		times['days'] = 60 * 60 * 24;
		times['hours'] = 60 * 60;
		times['minutes'] = 60;
				
		var duration = [];// Length of stay broken down into time periods
		for ( i in times ) {
			duration[i] = Math.floor( difference / times[i] );
			if ( duration[i] > 0 )
				difference = difference % times[i];
		}
		return duration;
	};
}


function month_to_int( month )
{
	switch( month.toLowerCase() )
	{
		case 'january':
			return 1;
			break;
		case 'february':
			return 2;
			break;
		case 'march':
			return 3;
			break;
		case 'april':
			return 4;
			break;
		case 'may':
			return 5;
			break;
		case 'june':
			return 6;
			break;
		case 'july':
			return 7;
			break;
		case 'august':
			return 8;
			break;
		case 'september':
			return 9;
			break;
		case 'october':
			return 10;
			break;
		case 'november':
			return 11;
			break;
		case 'december':
			return 12;
			break;
	}
}
 
$(function() {

$('.parking-calculator').each(function(i){
	
	$(this).submit(function(e){
		return false;
	});
	
	$('.parking-to-month').change(function(e){
		var month = $(this).val();
		var form = $(this).parents('form');
		var year = $('.parking-to-year',form).val();
		var days = $('.parking-to-day',form);
		var day = days.val();
		var num_days = new Date(year, month_to_int( month ), 0).getDate();
		
		days.html('');
		
		for( var i = 1; i <= num_days; i++ )
		{
			days.append( $('<option value="'+i+'">'+i+'</option>'))
		}
		
	});
	
	$('.parking-from-month').change(function(e){
		var month = $(this).val();
		var form = $(this).parents('form');
		var year = $('.parking-from-year',form).val();
		var days = $('.parking-from-day',form);
		var day = days.val();
		var num_days = new Date(year, month_to_int( month ), 0).getDate();		
		
		days.html('');
		
		for( var i = 1; i <= num_days; i++ )
		{
			days.append( $('<option value="'+i+'">'+i+'</option>'))
		}
		
	});
	
	$('input[type=submit]',$(this)).click(function(e){
		
		var form = $(this).parent('div').parent('form');
		
		var calculator = new ParkingCalculator();
			
		if( $('.parking-calculator-output',form ).length )
		{
			var output = $('.parking-calculator-output',form);
		}
		else
		{
			var output = $('<div></div>').addClass('parking-calculator-output');
			form.append( output );
		}
				
		// From
		var from = [];
		from['hour'] = parseInt($('.parking-from-hour',form).val());
		from['day'] = parseInt($('.parking-from-day',form).val());
		from['month'] = $('.parking-from-month',form).val();
		from['year'] = parseInt($('.parking-from-year',form).val());

		// To
		var to = [];
		to['hour'] = parseInt($('.parking-to-hour',form).val());
		to['day'] = parseInt($('.parking-to-day',form).val());
		to['month'] = $('.parking-to-month',form).val();
		to['year'] = parseInt($('.parking-to-year',form).val());

		var from_date = new Date(from.month + ' ' + from.day + ', ' + from.year + ' ' + from.hour + ':00:00');
		var to_date = new Date(to.month + ' ' + to.day + ', ' + to.year + ' ' + to.hour + ':00:00');
		
		var fromUnixTime = from_date.getTime() / 1000;
		var toUnixTime = to_date.getTime() / 1000;
		
		var facility = $('.parking-facility-select',form).val();
		
		// Current timestamp
		var current_timestamp = new Date().getTime() / 1000;
		
		// Cost
		var cost = calculator.calculate( facility ,fromUnixTime,toUnixTime );
		
		// In the past
		if( fromUnixTime <= current_timestamp || toUnixTime <= current_timestamp )
		{
			cost = 0;
			alert('Dates cannot be in the past');
		}
		
		if( cost == 0 && (facility == 'outdoor' || facility == 'undercover') )
		{
			if(facility == 'outdoor')
			{
				cost = 'Minimum cost: <strong>$' + calculator.zones.outdoor(0,2,0,0) + '</strong>';
			}
			else if(facility == 'undercover' )
			{
				cost = 'Minimum cost: <strong>$' + calculator.zones.undercover(0,2,0,0) + '</strong>';
			}
		}
		else
		{			
			// Leave date is before arrival date. Assume visitor doesn't have a time machine		
			if( isNaN( cost ) )
			{
				cost = 0;
				alert('Parking leave date cannot come before parking arrival date')
			}			
			// Add cents
			if( cost % 1 == 0)
			{
				cost = cost + '.00';
			}			
			cost = 'Parking cost: <strong>$' + cost + '</strong>';
		}
		output.html(cost);
		
		e.preventDefault;
		return false;
	});
	
	
});

});

