var fadeTimeout;

$(document).ready( function(){

	// ====================
	// MESSAGES
	// ====================
	$('div.alert p, div.success p').append(' <a href="#" class="close">Close</a>');
	$('div.alert a.close, div.success a.close').click(function() {
		$(this).parent().parent().fadeOut(1000);	
	});
	fadeTimeout = setTimeout("fadeMessages()", "2000");
	
	// =====================
	// BASKET
	// =====================
	$('input#txtDiscountCode').keypress(function (e) {
		if(e.which == 13) {
			if($('input#btnApplyDiscount').length) {
				$('input#btnApplyDiscount').click();
				return false;
			} else if ($('input#btnApplyDiscountImage').length) {
				$('input#btnApplyDiscountImage').click();
				return false;
			}
		}
    });
    $('input.quantity').keypress(function (e) {
		if(e.which == 13) {
			$('input#ucBasket_btnUpdateQuantitiesImage, input#ucBasket_btnUpdateQuantities').click();
			return false;			
		}
    });
	
	// ===========================
	// IDENTIFICATION
	// ===========================
	$('input#txtLogInPassword, input#txtLogInEmail').keypress(function (e) {
		if(e.which == 13) {
			if($('input#btnLogIn').length) {
				$('input#btnLogIn').click();
				return false;
			} else if ($('input#btnLogInImage').length) {
				$('input#btnLogInImage').click();
				return false;
			}
		}
    });
    $('input#txtRegisterEmail, input#txtRegisterPassword, input#txtRegisterConfirmPassword').keypress(function (e) {
		if(e.which == 13) {
			if($('input#btnRegister').length) {
				$('input#btnRegister').click();
				return false;
			} else if ($('input#btnRegisterImage').length) {
				$('input#btnRegisterImage').click();
				return false;
			}
		}
    });
    
    // ====================
	// GENERAL FORMS
	// ====================
	$('div.form p.prompt').each(function(){
		$(this).hide();
		$(this).before('<a href="#" class="help" title="Help"><img src="../images/icons/help.png" alt="Help" title="Help" /></a>');
	});
	$('div.form a.help').click(function(){
		$(this).siblings('p.prompt').toggle();
		return false;
	});
	$('div.radiobutton-list ul li input').click( function() {
		var me = $(this).attr('id');
		$('div.radiobutton-list ul li input').each( function() {
			if ($(this).attr('id') != me) {
				$(this).attr('checked','');
			}
		});
	});

	// ====================
	// DETAILS
	// ====================
	// Billing
	if($('input#rdoBillingUseAddressYes').attr('checked')) {
		$('div#billing-details div.form-elements').hide();	
	}
	$('input#rdoBillingUseAddressYes').click( function(){
		if($('div#billing-details div.form-elements').is(':visible')) {
			$('div#billing-details div.form-elements').slideUp('slow');
		}
	});
	$('input#rdoBillingUseAddressNo').click( function(){
		if($('div#billing-details div.form-elements').is(':visible') == false) {
			$('div#billing-details div.form-elements').slideDown('slow');	
		}
	});
	
	// Shipping
	if($('input#rdoShippingUseAddressYes').attr('checked')) {
		$('div#shipping-details div.form-elements').hide();
	}
	$('input#rdoShippingUseAddressYes').click( function(){
		if($('div#shipping-details div.form-elements').is(':visible')) {
			$('div#shipping-details div.form-elements').slideUp('slow');	
		}
	});
	$('input#rdoShippingUseAddressNo').click( function(){
		if($('div#shipping-details div.form-elements').is(':visible') == false) {
			$('div#shipping-details div.form-elements').slideDown('slow');	
		}
	});	
	
	// =====================
	// DONATION
	// =====================
	if($('ul#donation-amounts').length) {
		var $divDonationOther = $('div#donation-other');
		var $rdoOther = $('#divDonation div.radiobutton-list ul li input#rdoDonationOther');
		if(! $rdoOther.attr('checked')) {
			$divDonationOther.hide();
		}
		$('#divDonation div.radiobutton-list ul li input').click(function(){
			$('input#btnClearDonationImage').show();
			if ($('div.radiobutton-list ul li:last input').attr('checked')){
				$divDonationOther.show();
			}else {
				$divDonationOther.hide();
				$divDonationOther.children('input').val('');
			}
		});
		$('input#btnClearDonationImage').hide();
		if($('ul#donation-amounts li input[type=radio]:checked').length) {
			$('input#btnClearDonationImage').show();		
		}
	}	
	
	// =====================
	// DELIVERY
	// =====================
	$('div#divDeliveryOptions td input').click( function() {
		var me = $(this).attr('id');
		$('div#divDeliveryOptions td input').each( function() {
			if ($(this).attr('id') != me) {
				$(this).attr('checked','');
			}
		});
	});
	
});

function fadeMessages() {
	$('div.fade').fadeOut(1000);
}