/**
 * Search
 */

(function(window, undefined){
	
	jQuery.easing.def = "easeOutExpo";
	
	var searchInt;
	var last_words;
	
	// DOM OBJECTS
	var form = $('#searchBlock');
	var input = $('#search');
	var submit = form.find("input[type='submit']");	
	var results = $("#searchResults");	
	
	// LISTENERS
	
	input.focus(function(){
		input.animate({width:"245px"}, 250);
		input.val("");
		ajaxSearch();
		
	});
	input.blur(function(){
		input.animate({width:"130px"}, 250);
	});
	input.keyup(function(e){
		//log(e.keyCode);
		if(e.keyCode == 13) {
			submitSearch();
		}
		
		ajaxSearch();
	});
	submit.click(function(){
		submitSearch();
	});
	
	form.click(function(event){
		event.stopPropagation();
	});
	results.click(function(event){
		event.stopPropagation();
	});
	
	$('body, html').click(function(e) {
			
		results.addClass("hidden");
		results.html("");
		last_words = "";
		
	});
	
	
	// PRIVATE METHODS
	
	function renderSearchResults(json) {
		if(json['success'] == true) {
			//log("success == true");
			
			d = json['items'];
			
			var html = "";
			
			for (i in d) {
				html += getElementHtml(d[i]);
			}
			
			results.removeClass("hidden");
			results.html(html);
			
		} else {
			//log("success == false");
			
			if (json['msg'] == 'empty') {
				
				//log("msg[empty]");
				
				results.removeClass("hidden");
				results.html("<p class='msg'>Brak wyników dla wybranej frazy wyszukiwania</p>");
			}
		}
		//log(json);
	}
	function getElementHtml(e){
		var html = "";
			
		html += "<article id='p_"+e['id']+"'>"
			html += "<a href='/"+e['url']+"'>";
				
				html += "<span class='small thumb'>";
					html += "<img src='"+e['thumb']+"' alt='' height='50'>";
				html += "</span>";
				
				html += "<span class='opis'>";
					html += "<span class='title'>"+e['name']+"</span><br>";
					
					if(e['price_brutto'] && e['price_brutto'] > 0 && !(e['available'] == 0 && e['count_cache'] <= 0) && e['available'] != 2) {
						html += '<span class="price">Cena: <span class="lt"><span class="old_price">'+( ((e['old_price_brutto'] > 0)) ? e['old_price_brutto']+" zł" : "")+'</span></span> <strong>'+e['price_brutto']+' zł</strong></span>';
					} else if(e['price_brutto_from'] > 0) {
						html += '<span class="price">Cena od: <strong>'+e['price_brutto_from']+' zł</strong></span>';
					} else {
						html += '<span class="price">niedostępny</span>';
					}
				html += "</span>";
				
			html += "</a>";
		html += "</article>";
	
		return html;
	}
	
	function ajaxSearch() {
		
		var val = input.val();
		
		if (val.length >= 4 && last_words != val) {
			
			clearTimeout(searchInt);
			
			searchInt = setTimeout(function(){
				val = parseWords(val);
				$.ajax({
					url				: "/ajax/site/search",
					global			: false,
					type			: "GET",
					scriptCharset 	: "UTF-8",
					data			: 'words='+val+'&type='+type,
					dataType		: "json",
					success			: function(json) {
						renderSearchResults(json);
					}
				});
				last_words = val;
				
			},300);
		}
		
	}
	
	function submitSearch() {
		
		var val = input.val();
		
		if (val.length >= 1) {
			val = parseWords(val);
			document.location.href = "/szukaj/" + val;
		}
	}
	
	function parseWords(words){
		matches = words.match(/([a-zA-ZęółśążźćńĘÓŁŚĄŻŹĆŃ0-9\-\_]+)/g);
		return matches.join("_");
	}
	
})(window);
