$(document).ready(function() {						   

	/* external links to open in a new window */
	$('a').each(function() {
		// for each link
		var href = $(this).attr('href');
		// if http:// at the beginning
		if(href.match(/^http\:\/\//gi)) {
			// then add new page target
			$(this).attr('target', '_blank');
		}
	});
	
	
	/* Reload product list */
	$('.sortProducts').click(function() {
		var classAdd = 'active';
		if($(this).hasClass('active')) {
			classAdd = 'reverse';
		}
		$('.sortProducts').removeClass('reverse');
		$('.sortProducts').removeClass('active');
		$('.searchResults').hide();		
		$('.new-results').html('<img src="/images/loading.gif" alt="loading..." class="load-image" />');
		$(this).addClass(classAdd);
		
		var catId = $('input[name="catId"]').val();
		// get the content
		$.get('handler.php?action=load-product-list&catId='+catId+'&sort='+$(this).attr('rel')+'', function(d) {
			$('.new-results').hide();
			$('.new-results').html(d);
			$('.new-results').fadeIn();
		});
	});
	
	/* Reload offers list */
	$('.sortProductsOffer').click(function() {
		var classAdd = 'active';
		if($(this).hasClass('active')) {
			classAdd = 'reverse';
		}
		$('.sortProductsOffer').removeClass('reverse');
		$('.sortProductsOffer').removeClass('active');
		$('.searchResults').hide();		
		$('.new-results').html('<img src="/images/loading.gif" alt="loading..." class="load-image" />');
		$(this).addClass(classAdd);
		
		// get the content
		$.get('handler.php?action=load-offers&sort='+$(this).attr('rel')+'', function(d) {
			$('.new-results').hide();
			$('.new-results').html(d);
			$('.new-results').fadeIn();
		});
	});
	
	/* Load product category */
	$('input[name="product-category-select-go"]').click(function() {
		//if($('.searchResults').length) {
		//	alert('maybe reload');
		//	window.location = $('select[name="product-category-select"]').val(); 
		//} else {
			window.location = $('select[name="product-category-select"]').val(); 
		//}
	});
	
	
	

	/******************** Input clear **********/	
		$('input[type="text"],textarea').each(function() {
			if($(this).attr('class') == 'noclear') {
			} else {
				var val = $(this).val();
				$(this).click(function() {
					if($(this).val() == val) {
						$(this).val('');
					}
				});	
				$(this).blur(function() {
					if($(this).val() == '') {
						$(this).val(val);
					}
				});
			}
		});			 
	/**************** End Input clear **********/
});
