// ********************************************************************************* //
var ShipLuggage = ShipLuggage ? ShipLuggage : new Object();
//********************************************************************************* //

jQuery(function() {
    swapValues = [];
    jQuery(".swap_value").each(function(i){
        swapValues[i] = jQuery(this).val();
        jQuery(this).focus(function(){
            if (jQuery(this).val() == swapValues[i]) {jQuery(this).val("");}
        }).blur(function(){
            if (jQuery.trim(jQuery(this).val()) == "") {jQuery(this).val(swapValues[i]);}
        });
    });
});

jQuery(document).ready(function(){
	$('#member_sign_in').click(function(){
		$('#sign_in').fadeOut('slow');
		$('#sign_in_form').delay(800).fadeIn('slow');
		return false;
	});
	$('#one_way').click(function(){
		$('#return_date').fadeOut();
		$('#departure_date').attr('value','Luggage Delivery Date');
	});
	$('#round_trip').click(function(){
		$('#return_date').fadeIn();
		$('#departure_date').attr('value','Arrival Date');
	});
	$('#member_sign_out').click(function(){
		
	});
	// Open Fedex message box
	$('.drop_locations').live('click',
		function(){$('#drop_off_message').show();return false;}
	);
	$('#drop_off_close').live('click',
		function(){$('#drop_off_message').hide();return false;}
	);
	

	$('#sign_in_form form').ajaxForm({  
		success: function( data) { 
		
			// Is it a failrue?
			if (data.indexOf('<title>Error</title>') > 1)
			{
				$('#sign_in_form .error').show();
			}
			else
			{
				$('#sign_in_form').fadeOut('slow');
				$('#header_member').load('/core/member_login');
				$('#sign_in').delay(800).fadeIn('slow');
			}
	    }
    });
	
	$( ".datepicker" ).datepicker({ minDate: +2, maxDate: "+6M"});

	$('#add_luggage .add_to_cart').click(ShipLuggage.AddToCart);
	$('#cart .cart_remove_button').live('click', ShipLuggage.RemoveFromCart);
	$('#cart .insurance').live('change', ShipLuggage.UpdateLineCart); 
	$('#cart .dropoff').live('change', ShipLuggage.DropOff); 
	
	$('#advance_pick_up_time a.departure_date_button').live('click', ShipLuggage.AdvancedPickup);
	$('#return_pick_up_time a.return_date_button').live('click', ShipLuggage.ReturnDelivery); 
	
	$('#apply_coupon').live('click', ShipLuggage.ApplyCoupon); 
	
	// Validation
	$('#get_started_button').click(function(){
		
		Trip = $('input[name=trip_type]:checked').val();
		Regexx = '^([0-9]{1,3})\/([0-9]{1,2})\/([0-9]{1,4})$';

		// One way?
		if (Trip == 'one')
		{
			if ( $('#departure_date').val().match(Regexx) == null) 
			{
				alert ('Missing: Luggage Delivery Date');
				return false;
			}
		}
		else
		{
			if ( $('#departure_date').val().match(Regexx) == null) 
			{
				alert ('Missing: Arrival Date');
				return false;
			}
			
			if ( $('#return_date').val().match(Regexx) == null) 
			{
				alert ('Missing: Return Date');
				return false;
			}
		}

	
	});
	
	// Validation Amount of Products in Cart
	$('#cart #checkout a').click(function(){
		if ( $('#cart table tr.product').length == 0) 
			{
				alert ('Please add at least one product in your cart.'); 
				return false;
			}
	
	});
});

//********************************************************************************* //

ShipLuggage.AddToCart = function(event){

	$(event.target).addClass('LoadingCart');

	var Params = new Object();
	Params.insurance = 0;  
	Params.entry_id = $(event.target).attr('rel');
	
	$.post('http://www.shipluggage.com/ajax/add_cart/', Params, function(rData){ 
		$('#cart').replaceWith(rData);
		$(event.target).removeClass('LoadingCart');
	});
	
	return false;
};

//********************************************************************************* //

ShipLuggage.UpdateLineCart = function(event){

	$(event.target).addClass('LoadingCart');

	var Params = new Object();
	Params.insurance = $(event.target).closest('tr').find('select.insurance').val();
	Params.line = $(event.target).attr('rel')

	$.post('http://www.shipluggage.com/ajax/update_line_cart/', Params, function(rData){
		$('#cart').replaceWith(rData);
		$(event.target).removeClass('LoadingCart');
	});
	
	return false;
};

//********************************************************************************* //

ShipLuggage.RemoveFromCart = function(event){

	$(event.target).addClass('LoadingCart');

	Line = $(event.target).attr('rel');

	$.post('http://www.shipluggage.com/ajax/remove_cart/', {'line': Line}, function(rData){
		$('#cart').replaceWith(rData);
		$(event.target).removeClass('LoadingCart');
	});
	
	return false;
};

//********************************************************************************* //

ShipLuggage.DropOff = function(event){ 

	var Params = {};
	Params.direction = $(event.target).attr('rel');
	Params.val = (( $(event.target).is(':checked') == true) ? 'checked' : '');  


	$.post('http://www.shipluggage.com/ajax/dropoff/', Params, function(rData){$('#cart').replaceWith(rData);});

	 
};

//********************************************************************************* //

ShipLuggage.ApplyCoupon= function(event){

	$(event.target).addClass('LoadingCart');

	$.post('http://www.shipluggage.com/ajax/apply_coupon/', {'value' : $('#coupon_code').val()}, function(rData){ 
		$('#cart').replaceWith(rData);
		$(event.target).removeClass('LoadingCart'); 
	});
	
	return false;
};

//********************************************************************************* //

ShipLuggage.AdvancedPickup = function(event)
{
	$(event.target).addClass('LoadingCart');

	$.post('http://www.shipluggage.com/ajax/advanced_pickup/', {'data' : $(event.target).attr('rel')}, function(rData){ 
		$('#cart').replaceWith(rData);
		$(event.target).removeClass('LoadingCart'); 
	});
	
	return false;
}

//********************************************************************************* //

ShipLuggage.ReturnDelivery = function(event)
{
	$(event.target).addClass('LoadingCart');

	$.post('http://www.shipluggage.com/ajax/return_delivery/', {'data' : $(event.target).attr('rel')}, function(rData){ 
		$('#cart').replaceWith(rData);
		$(event.target).removeClass('LoadingCart'); 
	});
	
	return false;
}

//********************************************************************************* //
