// case insensitive :Contains
jQuery.expr[':'].Contains = function(a, i, m){
	return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};
// wyszukiwarka slow w produktach
$.fn.Search = function(){
	return this.each(function(){
		$(this).keyup(function(){
			var word = $(this).val();
			$(".files_list li").hide();
			$(".files_list li.lvl1:Contains('"+word+"')").each(function(){
				$(this).show();
				$(this).parents('li').show();
				$(this).find('ul').show();
				$(this).find('li').show();
			});
			if(!word){
				$(".files_list li").show();
				$(".files_list ul.childs1").hide();
			}
		})
	})
}
// sprawdzanie poprawnosci adresu email	
$.fn.CheckMail = function(){
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return this.each(function(){
		$(this).keyup(function(){
			var email = $(this).val();
			if(email != 0){
				if (pattern.test(email)) {
					$(this).addClass('valid');
					$(this).removeClass('invalid');
				}else{
					$(this).addClass('invalid');
					$(this).removeClass('valid');
				}
			}else{
				$(this).removeClass('invalid');
				$(this).removeClass('valid');
			}
		})
	})
}
			


$(document).ready(function(){
	// przechwytywanie wywolan zewnetrznych
	$('a[rel*=blank]').click(function(){
        window.open(this.href);
        return false;
    });
    
	// wylaczanie pustych a
    $('a[href=#]').click(function(){ return false; });
    
	// sprawdzanie poprawnosci maili
	$('input.check.email').CheckMail();
	
	// wyszukiwarka produktow
	$('#product_search input').Search();
	
	/* wysuwane menu boczne */
	$('#config_menu h3').bind('click',function(){
		var speed = 200;
		var content = $('#config_menu');
		if(content.width() == 30) content.animate({width:$(this).width()+$(this).siblings('.content').width()+'px'},speed); else content.animate({width:$(this).width()+'px'},speed);
	})
	
	/*
	 * 	Rozwijanie listy plików
	 */
	$('.files_list .dir').bind('click', function(){
		$(this).next('ul').toggle()
	});
    
	/*
     * 	Podpisy w polach input
     */
    $('.labeled').each(function(){
        $(this).focus(function(){
            if ($(this).val() == $(this).attr('rel')) {
				$(this).val('');
				$(this).addClass('active');
			}
        })
        $(this).blur(function(){
            if ($(this).val() == '') {
				$(this).val($(this).attr('rel'));
				$(this).removeClass('active');
			}
        })
    })
	
	/*
	 * Dodawnia zakladek
	 */
	$("a.bookmark").jFav({msg:'Twoja przeglądarka nie obłusguje tej funkcji.'});
	
	
	/*
	 * Pokazywanie realizacji w portfolio
	 */
	$('.pf .more').bind('click',function(){
		$('.pf .portfolio_item.hidden:lt(5)').removeClass('hidden');
		var hidden = $('.pf .portfolio_item:hidden').size();
		if(hidden==0) $(this).hide();
		
		return false;
	})
	
});
