$(document).ready(function() {

    if (jQuery.isFunction(jQuery.fn.cycle)) {
		$('#flash').cycle();
    }


	$('#product-images a').click(function(e) {
		e.preventDefault();
		$('#tea-image').attr('src', unescape(this.href.split('?')[1]));
	});

	$('a.external').click(function(e) {
		e.preventDefault();
		window.open(this.href);
	});

	$('select#orderId').change(function() {
		window.location.href = '/my-account/view-order/id/' + this.value;
	});

	$('#email').focus(function() {
		if (this.value.toLowerCase() == 'email') {
			this.value = '';
		}
	}).blur(function() {
		if (this.value.toLowerCase() == '') {
			this.value = 'EMAIL';
		}
	});

	$('img').rightClick(function() {
		alert('Please email tea@luponde.co.uk if\nyou would like a copy of this image');
	});

	// CATCH CHANGES IN ELEMENT PROPERTIES TRIGGERED BY GOOGLE AUTOFILL
	$('input, select').bind('propertychange', restoreStyles);

	// CHAR COUNTDOWN FOR GIFT-NOTE
	$('textarea#gift-note').keyup(function() {
		var max_len = 140;
		if (this.value.length > max_len) {
			this.value = this.value.substring(0, max_len);
		}
		$('input#mcount').val(max_len - this.value.length);
	});

});

function restoreStyles(){
	if(event.srcElement.style.backgroundColor != "" && event.srcElement.style.backgroundColor != "#000") {
		event.srcElement.style.backgroundColor = "#000";
		event.srcElement.style.borderColor = "#ffffa0";
	}
}

