String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
String.prototype.ltrim = function() { return this.replace(/^\s+/g, ''); }
String.prototype.rtrim = function() { return this.replace(/\s+$/g, ''); }

function ImgError(source){
    source.src = "/r/img/no-picture.png";
    source.onerror = "";
    return true;
}

$(document).ready(function() {
	$("form#search").submit(function() {

		var schd = $(this).find("#searched").val();
// 		var schd = $("#searched").val();//.toLowerCase();
		
		// wywalenie złych znaków
		schd = schd.replace(/[^a-zA-Z0-9ęóąśłżźćńĘÓĄŚŁŻŹĆŃ\-\+\"]/g,' ');
		
		//trim
		schd = schd.replace(/^\s+|\s+$/g,'');
		
		//dwie spacje na jedną
		schd = schd.replace(/[\ ]+/g,' ');
		
		// wywalenie ciągów znakowych " - " oraz "- "
		schd = schd.replace(/(\s\-\s)/g,' ');
		schd = schd.replace(/(\-\s)/g,' ');
		
		schd = schd.replace(/[ą]/g,'%C4%85');
		schd = schd.replace(/[ć]/g,'%C4%87');
		schd = schd.replace(/[ę]/g,'%C4%99');
		schd = schd.replace(/[ł]/g,'%C5%82');
		schd = schd.replace(/[ń]/g,'%C5%84');
		schd = schd.replace(/[ó]/g,'%C3%B3');
		schd = schd.replace(/[ś]/g,'%C5%9B');
		schd = schd.replace(/[ź]/g,'%C5%BA');
		schd = schd.replace(/[ż]/g,'%C5%BC');
		
		schd = schd.replace(/[Ą]/g,'%C4%84');
		schd = schd.replace(/[Ć]/g,'%C4%86');
		schd = schd.replace(/[Ę]/g,'%C4%98');
		schd = schd.replace(/[Ł]/g,'%C5%81');
		schd = schd.replace(/[Ń]/g,'%C5%83');
		schd = schd.replace(/[Ó]/g,'%C3%93');
		schd = schd.replace(/[Ś]/g,'%C5%9A');
		schd = schd.replace(/[Ź]/g,'%C5%B9');
		schd = schd.replace(/[Ż]/g,'%C5%BB');
		
		// kodowanie na siłę (nie odpowiada kodowaniu utf-8) musi być potem odkodwane "ręcznie"
		schd = schd.replace(/[\-]/g,'%C4%E1');
		schd = schd.replace(/[\+]/g,'%C4%E2');
		schd = schd.replace(/[\"]/g,'%C4%E3');
		
		// zakodowanie spacji jako minusa musi być potem odkodowane "ręcznie"
		schd = schd.replace(/\s/g,'-');
		
		if (schd == "")
			document.location.href = '/';
		else
			document.location.href = '/szukaj/'+schd+'/';
		
		return false;
	});

	$('.input:first').focus();

	hs.graphicsDir = '/r/highslide/';
	hs.wrapperClassName = 'wide-border';
	hs.align = 'center';

	$('.highslide').click(function() {
		hs.captionEval = 'this.a.title';
		return hs.expand(this);
	});

	$('.productImgClick').click(function() {
		document.location.href = $('.productLink[id="' + $(this).attr('id') + '"]').attr('href');
	}).css('cursor', 'pointer');

	$('body').click(function() {
		hs.close();
	});

	$('.ecommerceHeaderAd').click(function() {
		document.location.href = 'http://www.eCommerce24h.pl';
	});
	
	$(".remindPassword").click(function()
	{
		$(".remindPassword").hide();
		$(".remindResult").html("Proszę czekać...");
		$.ajax(
			{
				data : 
				{
					'uid' : $(this).attr("id")
				},
				url : "/ajax/wyslij-haslo/",
				success : function(msg)
				{
					var result = eval("("+msg+")");
					if(result.status)
					{
						$(".remindResult").html("Nowe hasło zostało wysłane na e-mail");
					}
					else
					{
						$(".remindPassword").show();
						$(".remindResult").html("Operacja nie powiodła się, prosimy spróbować ponownie");
					}
				},
				error : function(msg)
				{
					$(".remindPassword").show();
					$(".remindResult").html("Operacja nie powiodła się, prosimy spróbować ponownie");
				},
				timeout : 10000,
				type : 'POST'
			}
		);
	});

// 	$('.backHistory').click(function() {
// 		document.location.history(-1);
// 		return false;
// 	});
});




