$(window).load(function(){
	
	// Set target to All esternal links (this is because of depreciated target attribute in XHTML Strict)
	var externals = $('a.external');
	externals.attr('target', '_blank');
	
	// Run lightbox
	$('a.lightbox').lightBox();
	
	
	// Products slide effect
	var openAtStart = 0;
	var multipleOpen = true;
	
	var maxHeight = 0;
	var productDetails = $('.product-details');
	
	var padding = $(productDetails).children('.padding').innerHeight();
	$(productDetails).each(function(){
		var currentHeight = $(this).height() + 20;
		if (maxHeight < currentHeight) {maxHeight = currentHeight}
	});
	$(productDetails).height(maxHeight);
   	
	$(productDetails).hide();
	
	var myFile = document.location.toString();
	if(myFile.match('#') && myFile.split('#')[1]){
		var myAnchor = myFile.split('#')[1];
		$("a[name="+myAnchor+"]").parent('h3').nextAll('.product-details').show();
	}else{
		if(openAtStart != -1){
			$('.product-details:eq('+openAtStart+')').show();
		}
	}
	
	$('div.product').click(function(){
		if($(this).children('.product-details').is(':hidden')){
			$(productDetails).each(function(){
				if(!$(this).is(':hidden')){
					$(this).slideUp();
				}
			});
			$(this).children('.product-details').slideDown();
		}
	});
	
	
	// Search Product
	// Inspired by http://net.tutsplus.com/tutorials/javascript-ajax/using-jquery-to-manipulate-and-filter-data/
	$('.product').addClass('visible');
	
	$('#search').keyup(function(event) {
    	if (event.keyCode == 27 || $(this).val() == '') {
			$(this).val('');
			
			var openFirst = true;
			$('.product-details').each(function(){
				if($(this).is(':visible')){
					openFirst = false;
				}
			});
			if(openFirst){$('.product-details:eq(0)').show()}
      		$('.product').removeClass('visible').show().addClass('visible');
    	} else {
   			filter('.product-heading', $(this).val());
    	}
	});

	function filter(selector, query) {
		query	=	$.trim(query);
 		query = query.replace(/ /gi, '|');
  
  		$(selector).each(function() {
  			if($(this).text().search(new RegExp(query, "i")) < 0) {
  				$(this).nextAll('.product-details').hide();
  				$(this).parent('div').hide().removeClass('visible')
  			 }else{
  				$(this).parent('div').show().addClass('visible');
  			 }
  		});
	}
	
	var search = $('#search');
	$(search).focus(function(){
		if($(this).val() == 'search product ...'){$(this).val('')}
	});
	$(search).blur(function(){
		if($(this).val() == ''){$(this).val('search product ...')}
	});

	
	// Reset Contact Form
	$('#reset').click(function(){
		$('input.inputbox').val('');
		$('textarea.textarea').val('');
	});
	
});
