/*
 *  Menu animation
 */

// Make the menus animate
$(document).ready(function() {
	// Drop-down code
	$('ul.menu > li ul').hide();

	$('ul.menu > li').hover(function() {
		$(this).find('> span').css('color', '#F47C2E');
		$(this).find('ul').slideDown('fast');
	}, function() {
		$('ul.menu > li > span').css('color', '#666666');
		$(this).find('ul').slideUp('fast');
	});

	// Fix widths - keep the menu size consistent
	$('#navigation ul.menu > li').each(function(){
		col_width = $(this).find('ul').width();
		$(this).width($(this).find('> a').width());
		$(this).width($(this).find('> span').width());
		$(this).find('a, li').width(col_width);
		$(this).find('ul').width(col_width)
	});

	// For IE 6 only...
	if ($.browser.msie) {
		if ($.browser.version == '6.0') {
			$('ul.menu > li > ul').each(function(){
				// Position absolute to prevent it from nudging everything else down
				$(this).css('position', 'absolute');

				/* Fix the vertical positioning first */
				$(this).css('margin-top', '15px');

				/* Position: absolute breaks the positioning of the drop-downs, so we
				need to do something a little drastic. The following code swaps around
				the 2nd level ULs and their corresponding headers (just the order in
				the markup).*/
				$(this).parent().prepend($(this).clone());
				$(this).remove();	
			});
		}
	}

/*
 *  Invoke region selector
 */

// See also: select country

/*
$(document).ready(function() {
	// Bring up the region selector modal window automatically
	if ($('body').hasClass('products') && $('body').hasClass('view')) {
  	$('#region_paragraph .thickbox').click();
	}
});
*/

/*
 *  Address autofill
 */

	// Make the "go to checkout" button work
	$('#CartAutofill').change(function() {
		if ($(this).attr('checked') == true) {
			$('#CartDeliveryForename').val($('#CartBillingForename').val());
			$('#CartDeliverySurname').val($('#CartBillingSurname').val());
			$('#CartDeliveryCompanyName').val($('#CartBillingCompanyName').val());

			if ($('#CartBillingSpecialAddress').attr('checked') == true) {
				$('#CartDeliverySpecialAddress').attr('checked', true);
			} else {
				$('#CartDeliverySpecialAddress').attr('checked', false);
			}

			$('#CartDeliveryAddressLine1').val($('#CartBillingAddressLine1').val());
			$('#CartDeliveryAddressLine2').val($('#CartBillingAddressLine2').val());
			$('#CartDeliveryTownOrCity').val($('#CartBillingTownOrCity').val());
			$('#CartDeliveryPostcode').val($('#CartBillingPostcode').val());

			$('#CartDeliveryForename').attr('disabled', true);
			$('#CartDeliverySurname').attr('disabled', true);
			$('#CartDeliveryCompanyName').attr('disabled', true);
			$('#CartDeliverySpecialAddress').attr('disabled', true);
			$('#CartDeliveryAddressLine1').attr('disabled', true);
			$('#CartDeliveryAddressLine2').attr('disabled', true);
			$('#CartDeliveryTownOrCity').attr('disabled', true);
			$('#CartDeliveryPostcode').attr('disabled', true);
		} else {
			$('#CartDeliveryForename').val('');
			$('#CartDeliverySurname').val('');
			$('#CartDeliveryCompanyName').val('');
			$('#CartDeliverySpecialAddress').attr('checked', false);
			$('#CartDeliveryAddressLine1').val('');
			$('#CartDeliveryAddressLine2').val('');
			$('#CartDeliveryTownOrCity').val('');
			$('#CartDeliveryPostcode').val('');

			$('#CartDeliveryForename').attr('disabled', false);
			$('#CartDeliverySurname').attr('disabled', false);
			$('#CartDeliveryCompanyName').attr('disabled', false);
			$('#CartDeliverySpecialAddress').attr('disabled', false);
			$('#CartDeliveryAddressLine1').attr('disabled', false);
			$('#CartDeliveryAddressLine2').attr('disabled', false);
			$('#CartDeliveryTownOrCity').attr('disabled', false);
			$('#CartDeliveryPostcode').attr('disabled', false);
		}

		return true;
	});

/*
 *  Gallery
 */

	// Make things light up orange!
	$('.gallery_box a').hover(function() {
		$(this).parents('div.gallery_box').find('div.gallery_image').css('border', '1px solid #F47C2E');
		$(this).parents('div.gallery_box').find('div.gallery_name a').css('color', '#F47C2E');
	}, function() {
		$(this).parents('div.gallery_box').find('div.gallery_image').css('border', '1px solid #CCCCCC');
		$(this).parents('div.gallery_box').find('div.gallery_name a').css('color', '#333');
	});

	// This is for the cart. They're not even submit images now!
	$('.mock_button').click(function() {
		$(this).parents('form').submit();
	});

	$('.mock_button').hover(function() {
		$(this).find('.mock_button_middle').css('background-color', '#F47C2E');
		$(this).find('.mock_button_left').attr('src', '/img/btn_left_hover.png');
		$(this).find('.mock_button_right').attr('src', '/img/btn_right_hover.png');
	}, function() {
		$(this).find('.mock_button_middle').css('background-color', '#999999');
		$(this).find('.mock_button_left').attr('src', '/img/btn_left.png');
		$(this).find('.mock_button_right').attr('src', '/img/btn_right.png');
	});

	// While we're here: the country select on the contact page
	$('#region_paragraph_2 a').hover(function() {
		$(this).parents('#region_paragraph_2').find('a').css('color', '#F47C2E');
	}, function() {
		$(this).parents('#region_paragraph_2').find('a').css('color', '#333333');
	});

/*
 *  Swatches
 */

	// Colour swatches
	$('#CartedProductColourId').parent().css('display', 'none');

	$('#colour_swatches div.swatch_outer_border div').click(function() {
		// Select the colour from the dropdown menu
		colourId = $(this).attr('class');
		$('#CartedProductColourId').val(colourId);

		// Make all the colour swatches have an invisible border...
		$('#colour_swatches div.swatch_outer_border').css('border', '1px solid #FFFFFF');

		// ...except for the selected one.
		$(this).parents('div.swatch_outer_border').css('border', '1px solid #000000');
	});

/*
 *  Go to checkout
 */

// Make the "go to checkout" buttons work
	$('#go_to_cart').click(function() {
		document.location = '/carts/view';
		return false;
	});

	$('#go_to_checkout').click(function() {
		document.location = '/carts/checkout_review';
		return false;
	});

// Skip logging in
	$('#btn_checkout_1').click(function() {
		document.location = '/carts/checkout_delivery';
		return false;
	});

// Let the user log in
	$('#btn_checkout_2').click(function() {
		$(this).parents('form').submit();
		return false;
	});

// Let the user log in
	$('#btn_update').click(function() {
		$(this).parents('form').submit();
		return false;
	});

/*
 *  Select country
 */

// See also: invoke region selector

	// A CSS :hover won't cut the mustard with these dynamic images
	$('#region_links img').hover(function() {
		not_keyword_class = $(this).attr('class');
		src = $(this).attr('src');
		$(this).attr('class', src);
		$(this).attr('src', not_keyword_class);
	}, function() {
		// The very same as above
		not_keyword_class = $(this).attr('class');
		src = $(this).attr('src');
		$(this).attr('class', src);
		$(this).attr('src', not_keyword_class);
	});

	if ($('body').hasClass('select_country')) {
		// Highlight default region
		old_selected_region = $('div#hidden_selected_country_slug').html();
		not_keyword_class = $('div#'+old_selected_region+' img').attr('class');
		src = $('div#'+old_selected_region+' img').attr('src');
		$('div#'+old_selected_region+' img').attr('class', src);
		$('div#'+old_selected_region+' img').attr('src', not_keyword_class);
	}

	$('#region_links img').click(function() {
		old_selected_region = $('div#hidden_selected_country_slug').html();

		// Un-highlight old region
		not_keyword_class = $('div#'+old_selected_region+' img').attr('class');
		src = $('div#'+old_selected_region+' img').attr('src');
		$('div#'+old_selected_region+' img').attr('class', src);
		$('div#'+old_selected_region+' img').attr('src', not_keyword_class);

		// Highlight new region
		not_keyword_class = $(this).attr('class');
		src = $(this).attr('src');
		$(this).attr('class', src);
		$(this).attr('src', not_keyword_class);

		$('div#hidden_selected_country_slug').html($(this).parent().parent().attr('id'));

		selected_lang = $('div#hidden_selected_language_slug').html();
		selected_region = $('div#hidden_selected_country_slug').html();

		$('a#save_preference').attr('href', '/countries/reload_parent/lang:'+selected_lang+'/region:'+selected_region);
	});

/*
 *  Select language
 */

	$('#LangLangId').change(function() {
		document.location = $(this).val();
		return false;
	});

/*
 *  Reseller login
 */

	$('#reseller_login').click(function() {
		$.showAkModal($(this).attr('href'), 'Log in', 450, 225);
		return false;
	});

/*
 *  Refund
 */

// Only let the user select a replacement item when she's actually requesting a product exchange
	$('body.orders.personal_refund select').attr('disabled', true);

	$('body.orders.personal_refund :radio').click(function() {
		identifier = $(this).attr('class');
		switch ((this).value) {
			case 'exchange':
				$('select#Replacement' + identifier).attr('disabled', false);
				break;

			default:
				$('select#Replacement' + identifier).attr('disabled', true);
				break;
		}
	});

/*
 *  Popup
 */

// Open any anchor references with a class of "popup" in a new window
	$('a.popup.ups').click(function() {
		window.open($(this).attr('href'), 'UPS Label', 'width=560,height=320,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0');
		return false;
	});

	// Bring up the region selector modal window automatically when appropriate
	if ($('body').hasClass('confirm_c_and_l')) {
  	$('#region_paragraph .thickbox').click();
	}

	// In the language/country select screen, update the hidden divs as appropriate
	$('body.countries.select_country select#CountryLangId').change(function() {
		$('div#hidden_selected_language_slug').html($('select#CountryLangId').val());

		selected_lang = $('div#hidden_selected_language_slug').html();
		selected_region = $('div#hidden_selected_country_slug').html();

		$('a#save_preference').attr('href', '/countries/reload_parent/lang:'+selected_lang+'/region:'+selected_region);
	});
});
