$(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 = '#content 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').removeClass('active');
			// add activated class
			$(this).addClass('active');
			// Prevent link loading
			return false;
		});
	});
	
	// dialog
	/*
	$('#dialog').dialog({
		title: $(this).find('#dialog .title:first'),
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
				if (delayDialog) $(delayDialog).dialog('open');
		}
	});
	*/
	if ($('#dialog').length > 0) {
         var dialog = $('#dialog');
         var buttons;

         if (dialog.hasClass('basket')) {
             buttons = {
                 'Continue Shopping': function() {
                     $(this).dialog('close');
                 },
                 'Go to Basket': function() {
                     window.location.href='basket.html';
                 }
             }
         } else {
             buttons = {
                 Ok: function() {
                     $(this).dialog('close');
                 }
             }			
         }

         dialog.dialog({
             title: $(this).find('#dialog .title:first'),
             modal: true,
             buttons: buttons,
            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').css('margin-top')) < $('#content .right-column').height()) {
		$('.main-column').height($('#content .right-column').height());
	}
	
	// toggle hidden elements
	if ($('.toggle').length > 0) {
		$('.toggle').each(function() {
			var hidden = $(this);
			
			// hide by default
			hidden.hide();

			// add toggle handler
			$('#' + hidden.attr('id') + '-toggle').click(function() {
				// toggle hidden content
				hidden.toggle('slow');
				
				// return false to prevent link
				return false;
			});
		});
	}
	
	// scrolling product groups
	if ($('#product-groups').length > 0) {
		$('#product-groups').scrollable({
			'size':2,
			'loop':true
		}).autoscroll({
			'steps':4,
			'interval':5000
		});
	}
	
	// PNG fix
	jQuery.ifixpng(url + 'images/blank.gif');
	jQuery('img[src$=.png], .bgPng').ifixpng();
	
	// auto suggest
	if ($('#ajax-search').length > 0) icom.suggest.create('ajax-search', 'ajax-search-results');
});
