$(document).ready(function () {
	var that = this;
	// External websites open in new window
	$('a.external').click(function(){
		window.open(this.href);
		return false;
	}); 
	if ($('form#q1').length > 0) {
		$('a.submit').click(function () {
			$('form#q1').submit();
		});
	}
	if ($('form#q2').length > 0) {
		$('a.submit').click(function () {
			$('form#q2').submit();
		});
	}
	if ($('form#q3').length > 0) {
		$('a.submit').click(function () {
			$('form#q3').submit();
		});
	}
	//sidebar select box change
	if ($('div.finder').length > 0) {
		$('div.finder').children('select').each(function () {
			$(this).change(function () {
				if ($(this).val() != 'null') {
					var url = $('#url').val() + $(this).attr('name') + '_' + $(this).val();
					//console.log(url);
					document.location = url;
				}
			});
		});
	}
	//Product
	if ($('div.products').length > 0) {
		//image zoom and change
		var options = {
			zoomWidth: 400,
			zoomHeight: 350,
			lens: true,
			xOffset: 10,
			yOffset: 0,
			position: "right"
		};
		$('a.productImage').jqzoom(options);
		$('div.additional a').click(function () {
			$('a.productImage').attr('href', $(this).attr('href'));
			$('a.productImage img').attr('src', $(this).attr('rel'));
			return false;
		});
	}
	// Basket char limit
	if ($('div.front').length > 0) {
		$('textarea#message').keyup(function(){
			limitChars('message', 60, 'remaining');
		})
	}
	function limitChars(textid, limit, infodiv) {
		var text = $('#'+textid).val();
		var textlength = text.length;
		if(textlength > limit) {
			$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
			$('#'+textid).val(text.substr(0,limit));
			return false;
		} else {
			$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left');
			return true;
		}
	}
	// Basket invoice page enabled/disable alternative delivery address
	if ($('div.orderinfo').length > 0) {
		//disable delivery fields firstly
		$('input#delivery').attr("checked","")
		$('div.delivery').addClass('disabled');
		$('div.delivery div.inputs input, div.delivery div.inputs select').attr("disabled","disabled");
		//checkbox change accordingly
		$('input#delivery').click(function () {
			if ($('input#delivery').attr("checked") == true) {
				$('div.delivery').toggleClass('disabled');
				$('div.delivery div.inputs input, div.delivery div.inputs select').removeAttr("disabled");
			} else {
				$('div.delivery').toggleClass('disabled');
				$('div.delivery div.inputs input, div.delivery div.inputs select').attr("disabled","disabled");
			}
		});
		// Form Validation
		$("form").submit(function() {
			var err = false;
			var msg = "";	
			if ($('form select#inv_title').val() == 'null' || $('form select#inv_title').val() === undefined) {
				err = true;
				msg += "- Your invoice title\n";
			}
			if ($('form input#inv_first_name').val() == '' || $('form input#inv_first_name').val() === undefined) {
				err = true;
				msg += "- Your invoice first name\n";
			}
			if ($('form input#inv_last_name').val() == '' || $('form input#inv_last_name').val() === undefined) { 
				err = true;
				msg += "- Your invoice last name\n";
			}
			if ($('form input#customer_email').val() == '' || $('form input#customer_email').val() === undefined) {
				err = true;
				msg += "- Your invoice email address\n";
			}
			if ($('form input#customer_phone').val() == '' || $('form input#customer_phone').val() === undefined) {
				err = true;
				msg += "- Your invoice telephone number\n";
			}
			if ($('form input#inv_add1').val() == '' || $('form input#inv_add1').val() === undefined) {
				err = true;
				msg += "- Your invoice address\n";
			}
			if ($('form input#inv_town').val() == '' || $('form input#inv_town').val() === undefined) {
				err = true;
				msg += "- Your invoice town/city\n";
			}
			if ($('form input#inv_postcode').val() == '' || $('form input#inv_postcode').val() === undefined) {
				err = true;
				msg += "- Your invoice postcode\n";
			}
			if ($('input#delivery').attr("checked") == true) {
				if ($('form select#del_title').val() == '' || $('form select#del_title').val() === undefined) {
					err = true;
					msg += "- Your delivery title\n";
				}
				if ($('form input#del_first_name').val() == '' || $('form input#del_first_name').val() === undefined) { 
					err = true;
					msg += "- Your delivery first name\n";
				}
				if ($('form input#del_last_name').val() == '' || $('form input#del_last_name').val() === undefined) {
					err = true;
					msg += "- Your delivery last name\n";
				}
				if ($('form input#del_add1').val() == '' || $('form input#del_add1').val() === undefined) {
					err = true;
					msg += "- Your delivery address\n";
				}
				if ($('form input#del_town').val() == '' || $('form input#del_town').val() === undefined) {
					err = true;
					msg += "- Your delivery town/city\n";
				}
				if ($('form input#del_postcode').val() == '' || $('form input#del_postcode').val() === undefined) {
					err = true;
					msg += "- Your delivery postcode\n";
				}
			}
			if (err==true) {
				alert('Please complete the following fields\n\n' + msg);
				return false;
			}
		});
	}
	// Felicity likes hover details
	if ($('div.felicitylikes').length > 0) {
		$('div.felicitylikes ul li').each(function () {
			//Set hover states
			$(this).children('a').each(function () {
				$(this).siblings('div').appendTo($(this));
				$(this).hover(
					//over 
					function() {
						$(this).children('img').css('opacity', 0.2);
						$(this).children('div.details').css({
							'display' : 'block',
							'left' : 0,
							'height' : $(this).css('height'),
							'overflow' : 'hidden',
							'position' : 'absolute',
							'top' : 0,
							'width' : $(this).css('width')
						});
					},
					//out
					function() {
						$(this).children('img').css('opacity', 1.0);
						$(this).children('div.details').css({
							'display' : 'none'
						});
					}
				);
			});
		});
	}
});
