$(document).ready(function() {
	// stretch background of small pages (we don't use body because we want to keep it free from bg colour so spaw in admin doesn't look like shit)
	function resizeViewport() {
		if ($('#background').height() < $(window).height()) {
			$('#background').height($(window).height())
		}
	}
	
	resizeViewport();
	
	// hook on window change
	$(window).resize(function() {
		resizeViewport();
	});

	// slimbox
	$('a[rev^="lightbox"]').slimbox({}, null, function(el) {
		return (this == el) || ((this.rev.length > 8) && (this.rev == el.rev));
	});
	
	// external links
	$('a.external').click(function() {
		window.open($(this).attr('href'), 'blank');
	
		return false;
	});
	
	// back links
	$('a.back').click(function() {
		history.go(-1);
	
		return false;
	});
	
	// copy input title content into value only if value is empty (allows for fields to be pre-populated e.g. from php)
	$('input[title], textarea[title]').each(function() {
		var el = $(this);
		
		function sticky() {
			if (el.attr('value').length == 0) {
				el.attr('value', el.attr('title'));
			}
		}
		
		// reset on mouse out
		$(this).blur(function() {
			sticky();
		});
		
		// clear on mouse in
		$(this).focus(function() {
			if (el.attr('title') == el.attr('value')) {
				el.attr('value', '');
			}
		});
		
		// initialise
		sticky();
	});
	
	// clean junk on form submit
	$('form').submit(function() {
		$(this).find('input[title], textarea[title]').each(function() {
			if ($(this).attr('title') == $(this).attr('value')) $(this).attr('value', '');
		});
	});
	
	// google map
	if ($('#map').length > 0) {
		load();
	}
	
	//Tabs
	$('.tabs:not(.jq-ignore)').tabs();
	
	// Gallery image switching
	var slimboxPopupSize = '640x480';
	var mainImage = '#main-image';
	var thumbnails = '#product div.thumbnails';
	
	$(thumbnails + ' a').each(function(i) {
		var link = $(this).attr('href');
		var desc = $(this).attr('title');
		var alt = $(this).attr('rev');
	
		$(this).click(function() {
			// Replace large image
			$(mainImage).find('img').attr('src', link);
			// Modify slimbox popup link
			$(mainImage).find('a').attr('href', link.replace(/[0-9]+x[0-9]+/, slimboxPopupSize));
			// remove current activated class
			$(thumbnails).find('a').fadeTo(0, 0.3).removeClass('active');
			// add activated class
			$(this).fadeTo(0, 1).addClass('active');
			// Prevent link loading
			return false;
		});
		
		// set initial view state
		$(thumbnails + ' a:gt(0)').fadeTo(0, 0.3);
	});
	
	// dialog
	$('#dialog').dialog({
		title: $(this).find('#dialog .title:first'),
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
				if (delayDialog) $(delayDialog).dialog('open');
		}
	});
	
	// remove unqanted jQuery styles
	$('#content .ui-corner-all').removeClass('ui-corner-all');
	
	// add basket quantity check
	$('form.add-basket').submit(function () {
		if ($(this).find('input[value!=0]').length < 1) {
			$('<div>You need to enter a quantity for at least one product before adding to the basket.</div>').dialog({
				title: 'Oops!',
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog('close');
					}
				}
			});
		
			return false;
		}
	});
	
	// send friend form
	if ($('#send-friend-form').length > 0) {
		$('#send-friend-form').dialog({
			autoOpen: false,
			modal: true,
			title: $(this).find('#send-friend-form .title:first'),
			buttons: {
				Close: function() {
					$(this).dialog('close');
				}
			}
		});
			
		$('#send-friend').click(function() {
			$('#send-friend-form').dialog('open');
			
			return false;
		});
	}

	// list active link toggler
	$('.active-link-toggle:has(a)').each(function(el) {
		$(el).find('a').click(function() {
			$(el).find('a.active').removeClass('active');
			$(this).addClass('active');
			var id = $('#' + $(this).attr('href').replace(/\#/i, '') + '');
			var originalBackground = id.css('background-color');
			id.css('background-color', '#E4E5E6').animate({backgroundColor: originalBackground}, 1500);
		});
	});

	// main column 100% height
	if ($('.main-column').height() < $('#content .right-column').height()) {
		$('.main-column').height($('#content .right-column').height());
	}
	
	// scrollable
	if ($('.scrollable').length > 0) {
		$('.scrollable').scrollable({
			'size': 1,
			'clickable': false,
			'interval': 8000,
			'vertical': true,
			'loop': true
		});
	}
	
	// cycle
	if ($('.cycle').length > 0) {
		$('.cycle').cycle({
			'fx': 'fade',
			'timeout': 5000
		});
	}
	
	// ie 6 specific fixes
	if ($.browser.msie && $.browser.version < 7) {
		// PNG fix
		jQuery.ifixpng(url + 'images/blank.gif');
		jQuery('img[src$=.png], .bgPng').ifixpng();
		
		// fix for broken nav functionality
		$('#nav li:has(a.more)').hover(
		function() {
			$(this).addClass('ie6-active').find('ul:first').show();
		},
		function() {
			$(this).removeClass('ie6-active').find('ul:first').hide();	
		});
	}
});
