var postCodeFormatMsg = 'Niepoprawny kod pocztowy. Oczekiwany format to XX-XXXX.';

function validPostCode(postCode){
	if(postCode == null || postCode.length == 0){
		alert (postCodeFormatMsg);
		return false;
	}
	
	var len=postCode.length;
	digits="0123456789";
	
	if(len != 6){ 
		alert (postCodeFormatMsg);
		return false;
	}

	for(i=0; i<5; i++){
		if(i == 2){
			if(postCode.charAt(2) != '-'){
				alert (postCodeFormatMsg);	
			}
		}else{
			if (digits.indexOf(postCode.charAt(i))<0){ 
				alert (postCodeFormatMsg);
				return false; 
			}
		}
	}
	return true;
}

function showProgressBar(){
	$('#loaderBar').show();
	$('#loaderBarQuard').show();
}

function hideProgressBar(){
	$('#loaderBar').hide('slow');
	$('#loaderBarQuard').hide();
}

function prepareOnOverImg(elem, imgSelector, styleSelector){
	var main=$(elem);
	
	if(!(imgSelector == null)){
		var img = main.find(imgSelector);
		var src1= img.attr('src');
	
		var newSrc = src1.substring(0, src1.lastIndexOf('.'));
		var ext =  /[^.]+$/.exec(src1);
		
		imgOn = function(img, newSrc, ext){ img.attr('src', newSrc+ '-over.' + ext);};
		imgOut = function(img, newSrc, ext){ img.attr('src', newSrc + '.' + ext);};
	}else{
		imgOn = function(){};
		imgOut = function(){};
	}
	if(!(styleSelector == null)){
		var styleElem = main.find(styleSelector);
	}else{
		var styleElem = main;
	}
	
	main.hover(function(){
		imgOn(img, newSrc, ext);
		styleElem.addClass("over");
	}, function(){ 
		imgOut(img, newSrc, ext);
		styleElem.removeClass("over");
	});
}

$(document).ready(function(){
//	$('div.shopLinkBox a').each(function(){
//		var t=$(this);
//		var src1= t.attr('src');
//		if(src1.lastIndexOf('selected') > -1) return;
//		prepareOnOverImg(this, "img", null);
//   	});
	
	$('div.category_lev_0 a').each(function(){
		var t=$(this);
		var src1= t.attr('src');
		prepareOnOverImg(this, "img", "div.categoryName");
   	});
	
	/*$(document).ajaxStart(function(){           
        blockAjaxUI();           
    })
    .ajaxStop(function() {
        unblockAjaxUI();
    });*/
});


function blockAjaxUI(){
    $.blockUI({
    		message: $("#loaderBar"),
    		css: {
               width: '190px',
               border: '1px solid #AAAAAA'
             }
    });
}

function unblockAjaxUI(){
    $.unblockUI();
}

function hiddenDiv(id){
	$("#"+id).hide();
}


(function($,sr){
	 
	  // debouncing function from John Hann
	  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
	  var debounce = function (func, threshold, execAsap) {
	      var timeout;
	 
	      return function debounced () {
	          var obj = this, args = arguments;
	          function delayed () {
	              if (!execAsap)
	                  func.apply(obj, args);
	              timeout = null; 
	          };
	 
	          if (timeout)
	              clearTimeout(timeout);
	          else if (execAsap)
	              func.apply(obj, args);
	 
	          timeout = setTimeout(delayed, threshold || 100); 
	      };
	  }
		// smartresize 
		jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };
	 
})(jQuery,'smartresize');
