﻿jQuery.fn.search = function() {
	var args = arguments[0];
	var divID = $(this);
	var lang = args.lang;
	
	// we set the variables for faster programming
	var searchInput = $(this).find(".search-input");
	var searchButton = $(this).find(".search-button");
	var searchAdvancedButton = $(this).find(".search-advanced-button");
	var searchContainer = $(this).find("#search-container");
	
	// we hide the text on focus
	searchInput.focus(function(){
		var thisValue = $(this).val();
		if(thisValue == "Najdi izdelek...") $(this).val('');
	});
	
	// we display the search window on click of the button
	searchAdvancedButton.click(function(){
		var thisText = $(this).html();
		
		// we open the results
		if(thisText == "Zapri") {
			$('.sc-open').animate({ opacity: 0.0 }, 100).removeClass('sc-open');	
			divID.css('z-index', '0');
		} else {
			searchContainer.animate({ opacity: 1.0 }, 100).addClass('sc-open');	
			divID.css('z-index', '1');
		}
		// we open the results box
		if(checkValue(searchInput.val())){
		
			// change the search button text to close
			var newText = thisText=="Zapri"?"Napredno iskanje":"Zapri";
			var resetParams = thisText=="Zapri"?true:false;
			$(this).html(newText);
			
		} else {
			// display an alert
			alert("Niste vpisali iskalne besede ali pa le ta ni dovolj dolga (vsaj 5 črk).");
		}
	});
	
	searchButton.click(function(){
		// we check the params
		if(checkValue(searchInput.val())){ 
			// we send the form
			$("form[name=search-form]").submit();
		}	else { 
			alert("Niste vpisali iskalne besede ali pa le ta ni dovolj dolga (vsaj 5 črk)."); 
		}
	});
};

function checkValue(inputValue){
	//if(inputValue.length < 5 || inputValue == "Najdi izdelek...") return false;
	return true;
}
