$(document).ready(function(){
	
jQuery.fn.centerScreen = function(loaded) {
                var obj = this;
                if(!loaded) {
                        obj.css('top', $(window).height()/2-this.height()/2);
                        obj.css('left', $(window).width()/2-this.width()/2);
                        $(window).resize(function() { obj.centerScreen(!loaded); });
                } else {
                        obj.stop();
                        obj.animate({ top: $(window).height()/2-this.height()/2, left: $
(window).width()/2-this.width()/2}, 200, 'linear');
                }
        }
        
    $('#popupDiv').centerScreen();
    $('#popupDiv a').click(function(){
    	$('#popupDiv').fadeOut('normal');
    });
     
    $('#menu ul li a img').fadeTo("fast", 0);
    $('#menu ul li a img')  	
    	.mouseover(function(){
    		$(this).stop().fadeTo("normal", 1)
    		})
    	.mouseout(function(){
    		$(this).stop().fadeTo("normal", 0)
    		})
 
/*
     $('#leftColumn a img, img.btn')
    	.mouseover(function(){
    		$(this).stop().fadeTo("fast", 0.6)
    		})
    	.mouseout(function(){
    		$(this).stop().fadeTo("fast", 1)
    		})

if($('#nobox').length == 0){
	var left = $('#leftColumn').height();
	if($('.box1').length > 0){ 
		var box = $('.box1');
		var minus = 133;
	}else{
		var box = $('.box1m');
		var minus = 340;
	}
//alert(box.height() + ' / ' + left);

	if(box.height() < left ){
		box.css('height', left+'px');
		$('.textbox .text').css('height', (left - minus) + 'px');
	}
}	
*/
   if($('#gMap').length > 0) 
   {
 //kucyk
 //http://mapy.google.pl/maps?f=q&source=s_q&hl=pl&geocode=&q=Inwa%25C5%2582d,+&sll=52.025459,19.204102&sspn=9.239123,28.54248&ie=UTF8&hq=&hnear=Inwa%25C5%2582d,+Wadowicki,+Ma%25C5%2582opolskie&ll=49.877837,19.409409&spn=0.007591,0.022724&t=h&z=16
       if(GBrowserIsCompatible()) 
       { 
           var map = new GMap2(document.getElementById("gMap")); 
           map.setCenter(new GLatLng(49.871837,19.409409),12); 
           map.addControl(new GSmallMapControl());
      	   map.addControl(new GMapTypeControl());
           //var pointMarker = new GMarker(new GLatLng(49.877837,19.409409),{clickable: false}); 
           //map.addOverlay(pointMarker); 

			// Create our "tiny" marker icon
			var blueIcon = new GIcon(G_DEFAULT_ICON);
			blueIcon.image = "templates/default/images/kucyk.png";
			blueIcon.iconSize = new GSize(28, 34);       
			blueIcon.shadow = "templates/default/images/shadow.png";
			blueIcon.shadowSize = new GSize(39, 34);
			blueIcon.iconAnchor = new GPoint(11, 34);
			blueIcon.infoWindowAnchor = new GPoint(9, 2);        
			// Set up our GMarkerOptions object
			markerOptions = { icon:blueIcon, clickable: false };
			map.addOverlay(new GMarker(new GLatLng(49.877837,19.409409), markerOptions));

			// Create our "tiny" marker icon
			var blueIcon2 = new GIcon(G_DEFAULT_ICON);
			blueIcon2.image = "templates/default/images/park.png";
			blueIcon2.iconSize = new GSize(28, 34);   
			blueIcon2.shadow = "templates/default/images/shadow.png";
			blueIcon2.shadowSize = new GSize(39, 34);
			blueIcon2.iconAnchor = new GPoint(11, 34);
			blueIcon2.infoWindowAnchor = new GPoint(9, 2);

			            
			// Set up our GMarkerOptions object
			markerOptions2 = { icon:blueIcon2, clickable: false };
			map.addOverlay(new GMarker(new GLatLng(49.868198,19.410094), markerOptions2));

			// Create our "tiny" marker icon
			var blueIcon2 = new GIcon(G_DEFAULT_ICON);
			blueIcon2.image = "templates/default/images/dino.png";
			blueIcon2.iconSize = new GSize(28, 34);   
			blueIcon2.shadow = "templates/default/images/shadow.png";
			blueIcon2.shadowSize = new GSize(39, 34);
			blueIcon2.iconAnchor = new GPoint(11, 34);
			blueIcon2.infoWindowAnchor = new GPoint(9, 2);

			            
			// Set up our GMarkerOptions object
			markerOptions2 = { icon:blueIcon2, clickable: false };
			map.addOverlay(new GMarker(new GLatLng(49.867198,19.401000), markerOptions2));

//http://mapy.google.pl/maps?f=q&source=s_q&hl=pl&geocode=&q=inwa%C5%82d&sll=52.025459,19.204102&sspn=9.239123,28.54248&ie=UTF8&hq=&hnear=Inwa%C5%82d,+Wadowicki,+Ma%C5%82opolskie&ll=49.868198,19.410095&spn=0.018892,0.055747&t=h&z=15
           //var pointMarker2 = new GMarker(new GLatLng(49.868198,19.410094),{clickable: false}); 
           //map.addOverlay(pointMarker2); 


       } 
   }

}); 

function formSend()
{

  $('#formError').hide();
  name = $('input[name=name]').val();  
  mail = $('input[name=mail]').val();
  phone = $('input[name=phone]').val();
  question = $('textarea[name=question]').val();
  
  mail = validateEmail2(mail);  

  if(name && mail && phone && question)
  {
    $('#contactFormSend').ajaxSubmit(function(msg) {

    if(msg.indexOf('true') != -1)
    {
      $('#formOk').show();
       $('input[name=name]').val('');  
  		$('input[name=mail]').val('');
  		$('input[name=phone]').val('');
  		$('textarea[name=question]').val('');
    }
  });  
  }
  else
  {
    $('#formError').show();
    return false;
  }
}

function validateEmail2(x) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) 
		return true;
	else 
	{
		//alert('Wpisz poprawny adres email!');
		return false;
	}
}

function sprawdzNIP( inputVal ) 
{ 
  var argh = inputVal.replace(/-/g,""); 
  var arg = argh.replace(/ /g,""); 
  var coefficients = "657234567"; 
  var sum=0; 
  var index=8; 
  for (index=8; index>=0 ;index--)
  { 
    sum += (parseInt(coefficients.charAt(index)) * parseInt(arg.charAt(index)));
  } 
   
   if((sum % 11) == parseInt(arg.charAt(9))) 
    	return true;
   if ( (sum % 11) == 10 ) 
   {
    if (parseInt(arg.charAt(9)) == 0)
    		return true; 
   }
  return false; 
}

function switchDisplay(id){
	var li = $('#'+id);

//wylacza pozostale
	$('#lmenu li.sub').each(function(){
		if(id == $(this).attr('id'))
		{
			if(li.css('display') == 'none')
				li.css('display', 'inline');
			else
				li.css('display', 'none');
		}else{
			$(this).css('display', 'none');
		}
	});
	
/* 
 * tylko wlacza wylacza :
 * 			if(li.css('display') == 'none')
				li.css('display', 'inline');
			else
				li.css('display', 'none');
 * 
 * 
 if ($.browser.msie )
 	var disp = 'inline';
 else 
 	var disp = 'table-row';

  	obj = document.getElementById(id);;
  	if (obj.style.display == 'none')
  		obj.style.display = disp;
  	else
  		obj.style.display = 'none';
  		* 
  		*/
}

function podmiana(elem, img) {
		//alert(img);
		$(elem).find('img').attr('src', img);
	}
	
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function inputOutline(jqstring, tekst, ok)
{

    var border_org = 'solid 1px #C2C2C2';
    var border_err = 'solid 1px red';
    
	if(!ok) {
	    $(jqstring).css('border', border_err);
		$(jqstring).parent().find('p.error').text(tekst);
		$(jqstring).parent().find('p.error').css('display', 'block');
	}else{
	    $(jqstring).css('border', border_org);
		$(jqstring).parent().find('p.error').css('display', 'none');    	
	}
}

function checkSearch() {
	keyword = $('#searchForm input[name=keyword]').val();  
	if(keyword.length <= 4){
		alert('Wpisane słowo jest zbyt krótkie!');
		return false;
	}
	else if(keyword == 'wyszukaj...'){
		return false;
	}else{
		$('#searchForm').submit();
		return true;
	}
	
}

function checkCommentForm() {
    
    var ok = true;

    /*
    title = $('#commentForm input[name=title]').val();  
	  if(title.length < 6 || title.length > 80){
	    ok = false;
	  	inputOutline('#commentForm input[name=title]', 'Tytuł musi zawierać od 6 do 80 znaków!', false);
	  }else{
	  	inputOutline('#commentForm input[name=title]', 'Tytuł musi zawierać od 6 do 80 znaków!', true);
	  }
	  */

    txt = $('#commentForm textarea[name=txt]').val(); 
	  if(txt.length < 1 ) {
	    ok = false;
	  	inputOutline('#commentForm textarea[name=txt]', 'Wpisz treść komentarza!', false);
	  }else{
	  	inputOutline('#commentForm textarea[name=txt]', 'Wpisz treść komentarza!', true);
	  }
	   
  	if(!ok)
    {      
      //alert('nie wysylam');
      return false;
    }
    else 
    {
      $('#commentForm').submit();
      return false;
    }	  
}

function checkOfferForm() {
    
    var ok = true;

	category = $('#offerForm select[name=category]').val(); 
	  if(category == 0){
	    ok = false;
	  	inputOutline('#offerForm select[name=category]', 'Wybierz kategorię!', false);
	  }else{
	  	inputOutline('#offerForm select[name=category]', 'Wybierz kategorię!', true);
	  }

    title = $('#offerForm input[name=title]').val();  
	  if(title.length < 6 || title.length > 80){
	    ok = false;
	  	inputOutline('#offerForm input[name=title]', 'Tytuł musi zawierać od 6 do 80 znaków!', false);
	  }else{
	  	inputOutline('#offerForm input[name=title]', 'Tytuł musi zawierać od 6 do 80 znaków!', true);
	  }

    txt = $('#offerForm textarea[name=txt]').val(); 
	  if(txt.length < 1 ) {
	    ok = false;
	  	inputOutline('#offerForm textarea[name=txt]', 'Wpisz treść komentarza!', false);
	  }else{
	  	inputOutline('#offerForm textarea[name=txt]', 'Wpisz treść komentarza!', true);
	  }
	   
  	if(!ok)
    {      
      //alert('nie wysylam');
      return false;
    }
    else 
    {
      $('#offerForm').submit();
      return false;
    }	  
}

// Funkcja sprawdzająca formualrz rejestracyjny
  function checkRegisterForm( info ) {
    
    var ok = true;

/*
    if(info != 'edit')
    {
      $.ajax({
        type: 'GET',
        url: 'ajax/responses.php?what=checkEmail&email='+email,
        async: false,
        success: function(msg){
          if(msg.indexOf('true') == -1)
          {
            emailErr = 'Podany email już jest zarejestrowany!';          
          }
        }
      });
    }   
    if(emailErr != '')
    {
      $('#registerForm input[name=email]').css('border', 'solid 1px red');
      $('#registerForm input[name=email]').parent().parent().find('p.error').text(emailErr);
    }
*/
    login = $('#registerForm input[name=login]').val();
    if(info != 'edit')
    {
      if(login.length < 5 || login.length > 25) {
	      ok = false;
	  	  inputOutline('#registerForm input[name=login]', 'Login musi zawierać od 5 do 25 znaków!', false);
	   }else{
	  	  inputOutline('#registerForm input[name=login]', '', true);	
	   }
    }

    // Sprawdzanie hasła
    pass = $('#registerForm input[name=pass]').val();
    if(info != 'edit' || pass.length > 0)
    {
      if(pass.length < 6 || pass.length > 25) {
	      ok = false;
	  	  inputOutline('#registerForm input[name=pass]', 'Hasło musi zawierać od 6 do 25 znaków', false);
	   }else{
	  	  inputOutline('#registerForm input[name=pass]', '', true);	
	   }
    }

    pass2 = $('#registerForm input[name=pass2]').val();
    if(pass != pass2)
    {
      ok = false;
	    inputOutline('#registerForm input[name=pass2]', 'Podane hasła są różne!', false);
   }else{
	  	inputOutline('#registerForm input[name=pass2]', '', true);	
   }

    // Sprawdzanie adresu email   
    email = $('#registerForm input[name=email]').val();   
    var check = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!check.test(email)) 
    {
      ok = false;
	  	  inputOutline('#registerForm input[name=email]', 'Wpisz poprawny adres email!', false);
    }else{
	  	  inputOutline('#registerForm input[name=emsil]', '', true);	
    }

    if(info != 'edit')
    {
	    zgoda = $('#registerForm input[name=zgoda]:checked');
	    if(zgoda.length == 0)
	    {
	      ok = false;
	  	  inputOutline('#registerForm input[name=zgoda]', 'Musisz wyrazić zgodę aby się zarejestrować!', false);
	    }else{
	  	  inputOutline('#registerForm input[name=zgoda]', '', true);	
	    }
	    regulamin = $('#registerForm input[name=regulamin]:checked');
	    if(regulamin.length == 0)
	    {
	      ok = false;
	  	  inputOutline('#registerForm input[name=regulamin]', 'Musisz zaakceptowac regulamin aby się zarejestrować!', false);
	    }else{
	      if(zgoda.length != 0)
	      {
	  	  inputOutline('#registerForm input[name=regulamin]', '', true);	
	      }    	
	    }
    }


     
	imie = $('#registerForm input[name=name]').val(); 
	company = $('#registerForm input[name=company]').val();	
	if(imie.length < 3 && company.length < 3)
	{
      ok = false;
	  	inputOutline('#registerForm input[name=name]', 'Wpisz dane osobowe!', false);		
	}else{
	  	inputOutline('#registerForm input[name=name]', '', true);	
	}

	street = $('#registerForm input[name=street]').val(); 
	if(street.length < 3)
	{
      ok = false;
	  	inputOutline('#registerForm input[name=street]', 'Wpisz adres!', false);	
	}else{
	  	inputOutline('#registerForm input[name=street]', '', true);	
	}

	
	check = /^[0-9]{2}-[0-9]{3}|[0-9]{5}$/;
	zip = $('#registerForm input[name=zipCode]').val();	
	if(!check.test(zip) )
	{
      ok = false;
	  	  inputOutline('#registerForm input[name=zip_code]', 'Wpisz poprawny kod pocztowy!', false);	
	}else{
	  	  inputOutline('#registerForm input[name=zip_code]', '', true);	
	}
	
	miasto = $('#registerForm input[name=city]').val();	
	if(miasto.length < 3)
	{
      ok = false;
	  	  inputOutline('#registerForm input[name=city]', 'Wpisz nazwę miejscowosci', false);
	}else{
	  	  inputOutline('#registerForm input[name=city]', '', true);	
	}
  	if(!ok)
    {      
      //alert('nie wysylam');
      return false;
    }
    else 
    {
      $('#registerForm').submit();
      return false;
    }
  
} 
// Sprawdzanie ankiet  
function pollVote(id) {  
    var type = '';
    var error = 0;
    if($('#'+id).find('input[type=radio]').length > 0)
    { 
        if($('#'+id).find('input[type=radio]:checked').length == 0)
          error = 1;
    }    
    else if($('#'+id).find('input[type=checkbox]').length > 0) 
    {
        if($('#'+id).find('input[type=checkbox]:checked').length == 0)
          error = 1;
    }
    else if($('#'+id).find('select').length > 0) {
        if($('#'+id).find('select').val() == -1)
          error = 1;
    }
    if(error)
      $('#'+id).find('p.pollError').show();
    else  
      $('#'+id).submit();
    
    
}
