// JavaScript Document
var loaderImg = '<img id="loader" src="images/loader.gif" alt="Loading..." />'; 
function getUrl(file,contener)
{
	$('#'+contener.toString()).html(loaderImg);   
	$('#'+contener.toString()).load(file.toString(), function(){ 
    $("#loader").hide(); 
	
	});
	$('#'+contener.toString()).hide(); 
	$('#'+contener.toString()).fadeIn(500);


}
function getUrlNoLoad(file,contener)
{
	$('#'+contener.toString()).load(file.toString())
}

function sendForm(form,div)
{
			var url = $(form).attr('action');
			var met = $(form).attr('method');				
			var data;
			
			$(div).append(loaderImg);  
			$(form+' :input').each
			(
				function (elementIndex)
				{
					var name = $(this).attr('name');
					var value = $(this).val();
 
					if (data == null)
						data = name+'='+value;
					else
						data += '&'+name+'='+value;
				}
			);
			$.ajax
			(
				{					
					type: met,
					url: url,
					data: data,
 
					error: function (XMLHttpRequest, textStatus, errorThrown)
					{
						$(div).show();
						$(div).append('Bladd obiektu o nazwie:'+XMLHttpRequest+'<br />Komunikat:'+textStatus+'<br />Rodzaj:'+ errorThrown);
					},
					success: function(html)
					{
						$(div).html(html);
					}
				}
			); 
	}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function textSizeChange(s){
	createCookie('textSize',s,100);
	$("#textRead").animate({ 
    fontSize: parseInt(s)+"px", 
    lineHeight: (parseInt(s)+8)+"px"
  }, 200 );
	
}
function showIt()
{
	$("#toNewsletter").css('display','none');
	$("#form").addClass("showDiv").show("slow");
	
}
function initEffects()
{

		// animacje na dolnych buttonach
       $('.mBlock').mouseenter(function(){
			$(this).children(".promoImg").stop(true, true).slideDown("slow");
		
			$(this).children("h1").stop(true, true).animate({ 
				    top: "-45px"
				   }, 1000 );
			 $(this).removeClass().addClass('mBlock_h').stop(true, true).fadeIn('slow');
			 $(this).children(".arrowMain").stop(true, true).fadeIn('slow');

	    }).mouseleave(function(){
	    
			$(this).children(".promoImg").stop(true, true).slideUp("slow");
			$(this).children("h1").stop(true, true).animate({ 
				    top: "0px"
				   }, 1000 );
			 $(this).removeClass().addClass('mBlock').stop(true, true).fadeIn('slow');
			  $(this).children(".arrowMain").stop(true, true).fadeOut('fast');
	    });
	    
	    //wielkosc tekstu na stronie
	    var fontSize = readCookie("textSize") 
	    if(fontSize != null){  
		    $('.textBox').css('font-size',fontSize+'px'); 
		    $('.textBox').css('line-height',(parseInt(fontSize)+8)+'px'); 
		}else{
		    $('.textBox').css('font-size','12px'); 
		}
		

}
function initNewsReader()
{
//countNews
var actvId;
var allElements = $('.newsList').children().size(); 
$("#nextButton").click(function () { 
	$(".newsContent").each(function (i) {
        if (this.style.display == "block") {
        actvId = i;
        } 
      });
	
	if((actvId+1)<allElements)
	{
	clearAllNewsStyle();
	readNews(actvId+1);
	}
	});
$("#prevButton").click(function () { 
	$(".newsContent").each(function (i) {
        if (this.style.display == "block") {
        actvId = i;
        } 
      });
	
	if((actvId-1)>=0)
	{
	clearAllNewsStyle();
	readNews(actvId-1);
	}
	});
}
function clearAllNewsStyle()
{
	$('.newsList').each(function(){
				 $(this).children().removeClass().addClass('newsListed').fadeIn('slow');
			
				 
				 }
			);
	$('.newsContent').each(function(){
				 $(this).css('display','none');

				 }
			);
}
function switchTabs()
{
	var allElements = $('.prodTabs').children().size();
	$(".prodTabs li").click(function () { 
		$(".prodTabs li").removeAttr("id");
		$(this).attr('id','currentTab');
	
	});
	return false;
}
function openTab(id)
{
	 $('#tabContener').children().css('display','none');
	  $('#tab'+parseInt(id)).css('display','block');
	  return false;
	 
}
function readNews(id)
{
var allElements = $('.newsList').children().size();
clearAllNewsStyle();
 
 $('#newsNag'+parseInt(id)).removeClass().addClass('newsFeatured').fadeIn('slow');
 $('#news'+parseInt(id)).fadeIn('slow');
 
 		if((id-1)==0 || id == 0)
		{
		 $('#prevButton').removeClass().addClass('newsMenuLeft_fade').fadeIn('slow'); 
		}
		if(id+1==allElements)
		{
		$('#nextButton').removeClass().addClass('newsMenuRight_fade').fadeIn('slow');
		}
		if((id-1)>0 || id > 0)
		{
		 $('#prevButton').removeClass().addClass('newsMenuLeft').fadeIn('slow'); 
		}
		if(id+1<allElements)
		{
		$('#nextButton').removeClass().addClass('newsMenuRight').fadeIn('slow');
		}
return false;	
}
//Gallery init
$(document).ready(function() {
			$(":text,textarea").labelify();
			 $('a[rel*=facebox]').facebox();

			$("a[rel=gallery_g]").fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});
		
			$("#newsletterForm").validate({
				
				rules: {
					mail: {
						required: true,
						email: true,
						remote: "mailCheck.php"
					}
				},
				messages: {
					mail: {
						required: "Podaj adres e-mail",
						email: "Podaj prawidłowy adres e-mail",
						remote: 'Ten adres już jest w naszej bazie'
						
					}
				}
			});
			$("#contactForm").validate({
		rules: {
			imie: {
				required: true,
				minlength: 2
			},
			tel: {
				required: true
			},
			mail_oso: {
				required: true,
				email: true
			},
			agree: "required"
		},
		messages: {
			imie: {
				required: "Podaj swoje imię",
				minlength: "Imię jest trochę za krótkie"
			},
			tel: {
				required: "Podaj telefon kontakowy. Oddzwonimy najszybciej jak to możliwe"
			},
			mail_oso: {
				required: "Podaj adres e-mail. ",
				email: "Podaj prawidłowy adres e-mail"
				
			},
			agree: "Prosimy o wyrażenie zgody. W przeciwnym wypadku nie będziemy mogli dostarczyć jej do odpowiedniej osoby."
		}
	});
		$("#addMail").click(function() {
		  $("#newsletterForm").submit();
		}); 
		$("#serchGo").click(function() {
		  $("#searchForm").submit();
		}); 	
		 $("#dataSet").tablesorter({widgets: ['zebra']});
	     
	     // main vertical scroll
		$("#mainScroller").scrollable({
			vertical: true,
			keyboard: 'static'
		}).navigator("#main_navi");
		//hover animation
		
		$("#main_navi li").click(function() {
		clicked = $(this);
			var position = $(this).position();
			$("#main_navi li a").removeClass("mainListActive");
		  y = position.top;
		  $('.currentArrow').animate({
		    top: y
		  }, 200, function() {
		   clicked.find('a').addClass("mainListActive");
		 

		  });
		});
	     
	     
	     
	     
		}); 
