$(document).ready(function() {		
	// 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;
	});
	
	// slimbox
	if ($('a[rev^="lightbox"]').length > 0) {
		$('a[rev^="lightbox"]').slimbox({}, null, function(el) {
			return (this == el) || ((this.rev.length > 8) && (this.rev == el.rev));
		});
	}
	
	// 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').tabs();
	
	// Gallery image switching
	var mainImage = '#main-image';
	var thumbnails = '#gallery 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);
			// 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 gallery thumbs
	if ($('.scroll-horizontal').length > 0) {
		$('.scroll-horizontal').scrollable({ 
		    size: 4,
		    clickable: false
		});
	}
});
