function copyInput(){
    	// 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'));
				el.addClass('inactive');
			}
		}

		// 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', '');
				el.removeClass('inactive');
			}
		});

		// initialise
		sticky();
	});
}

$(document).ready(function() {
	copyInput();

	// external links
	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});

	// 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();
	}

	$("#callback-form").validate();
	
	if ($.cookie('admin')){
	    $('li#edit-link').css('display','inline');
	}

	// 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);
		});
	});

	// 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;
			});
		});
	}

	// light boxes
	$('a[rel^="lightbox"]').colorbox();

	// 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);
	});

	// auto suggest
	if ($('#ajax-search').length > 0) icom.suggest.create('ajax-search', 'ajax-search-results');

	$('#banners').after('<div id="pager">').cycle({
		fx: 'fade',
		timeout: 10000,
		pager: '#pager'
	});
	
	$('#latest ul').cycle({
		fx: 'scrollDown',
		timeout: 10000
	});
	
    // expand, collapse contact and remember its state
    $('#enquire a').each(function() {
		if($.cookie("contact-form") === 'expanded'){
			$("#enquire form").slideDown();
			$(this).addClass('minus');
		}
		else{
			$("#enquire form").hide();
			$(this).removeClass('minus');
		}
	});

	$('#enquire a').click(function() {
		if ($.cookie("contact-form") === 'expanded'){
			$.cookie("contact-form", 'collapsed');
			$(this).removeClass('minus');
			$("#enquire form").slideUp();
		}
		else{
			$.cookie("contact-form", 'expanded');
			$(this).addClass('minus');
			$("#enquire form").slideDown();		
		}
	});
	
	

});

