$(document).ready(function (){

	$("#spaSelectorForm :input").bind("change", function (){
		doSpaSearch();
	});
	
	$("#spaSelectorForm input:text").bind("keyup", function (){
		if (window.spaTimer) clearTimeout(window.spaTimer);
		window.spaTimer = setTimeout(doSpaSearch, 500);
	});

	/*$('#ajaxResult').cycle({
				fx: 'scrollHorz', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				next:   '.next', 
    			prev:   '.prev', 
				speed:  300, 
				timeout: 0 
	});*/
	
	$(".productElement").live("click", function (){
		location.href=$(this).find("a").attr("href");
	});
	
	$('#ajaxResult').carousel({
		loop: false, 
		pagination: true,
		paginationPosition: 'inside',
		btnsPosition: 'inside',
		callback: function (step){
			setCurrentSlide(step);
		}
	});
	bindHover();

});

function setCurrentSlide(step) {
	$.get("./?action=ajaxSetStep&step=" + step);
}

function bindHover() {
	$("#ajaxResult a.productElement").bind("mouseenter", function (){
		$(this).addClass("productElementHover");
	})
	$("#ajaxResult a.productElement").bind("mouseleave", function (){
		$(this).removeClass("productElementHover");
		
	})
	
}

function doSpaSearch(serializedForm) {
	if (serializedForm) {
		serializedForm = serializedForm;
	} else {
		serializedForm = $("#spaSelectorForm").serialize();
	}

	$.get("./?action=ajaxreturn&" + serializedForm,
		  function(data) { 
		    $('#ajaxResult').html(data);

			$('#ajaxResult').carousel({
				loop: false, 
				pagination: true,
				paginationPosition: 'inside',
				btnsPosition: 'inside',
				callback: function (step){
					setCurrentSlide(step);
				}
			});
			bindHover();
		  }
	);
		  
	var spapostvariabel = $("#spaSelectorForm").serialize();
	return false;
}

function resetForm() {
	$("#spaSelectorForm").clearForm();
}


$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};
