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

// preload images
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

$(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({
		errorPlacement: function() {
				copyInput();
			}
		});
	
	if ($.cookie('admin')){
	    $('li#edit-link').css('display','inline');
	}
	
	function addMega(){
	    $(this).find(".b1").slideDown('fast');
	}

	function removeMega(){
	    $(this).find(".b1").slideUp('fast');
	}

	var megaConfig = {
	    interval: 200,
	    sensitivity: 7,
	    over: addMega,
	    timeout: 500,
	    out: removeMega
	};
	$('#catalogues-items').hoverIntent(megaConfig);
	$('#products-items').hoverIntent(megaConfig);

	// 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();
	if($.browser.msie){
	    $('#callback #formtrigger').click(function(){
		window.open('/call-back', "", "top=200,left=250,width=320,height=320,scrollbars");
		return false;
	    });
	}
	else {
		$('#callback #formtrigger').colorbox({width:288, height:310, opacity: 0.7, initialWidth:310, initialHeight:285, iframe:true, href:"call-back", scrolling:false, close:'x'});
	}

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

	$("#top-nav").topNavEffect({
		fx: "backout",
		speed: 700
	});

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

	// filters
	// get all pages associated with products on the current page
	var associations = "";
	$('#listings li').each(function() {
	    associations += $(this).attr('class');
	});
	associations = associations.split(' ').unique();
	
	// show only filters that are relevant to products on the current page
	$('#prodfilters li li').hide();
	$('#prodfilters li li').each(function() {
	    var filterId = $(this).attr('id');
	    $(associations).each(function() {
		var assoc = this;
		if(filterId == assoc){
		    $('#'+filterId).show();
		}
	    });
	});

	// dropdowns for IE6
	$('#prodfilters li').hover(function(){
		$(this).find('ul').show();
	}, function() {
		$(this).find('ul').hide();
	});

	$('#prodfilters :checkbox').click(function(){
	    $('#listings li').hide();
	    // for all checked checkboxes, show corresponding products
	    $('#message').empty();
	    $('#message').append('<p></p>');
	    $('#message p').append('Showing only selected products ');
	   
	    $('#prodfilters :checkbox:checked').each(function(){
		    $('.' + $(this).attr('value')).show();
		    $('#message p').append('<em>' + $(this).parent().text() + '</em> ');
	    });
	    $('#message p').append('<span>show&nbsp;all</span>');
	});
	$('#message p span').live("click", function(){
	    $('#message').empty();
	    $('#listings li').show();	    
	});

	$('#models').click(function(){ 
		location.href = $('#models p a').attr('href'); 
	});

	/*$('#building').cycle({
		fx: 'fade'
	});*/
	
	$.preLoadImages('/images/subnav-left.png', '/images/subnav-mid.gif', '/images/subnav-mid.png', '/images/subnav-right.png', '/images/subnav-shadow.png');
	$('#cboxClose').bgiframe();
});

