function openWindow(link) // inspired by porneL - http://pornel.net/pups/
{
  try
  {
    var new_window = window.open(link, '_blank');
    if(new_window) return false;
  }
  catch(e){}
  return true;
}

function checkRequiredFields(form_id)
{
  ok = true;
  fields = document.getElementsByTagName('input');
  for(var i in fields) if(fields[i].className=='text required')
  {
    if(fields[i].parentNode.parentNode.parentNode.id==form_id && !fields[i].value) ok = false;
  }
  fields = document.getElementsByTagName('textarea');
  for(var i in fields) if(fields[i].className=='required')
  {
    if(fields[i].parentNode.parentNode.parentNode.id==form_id && !fields[i].value) ok = false;
  }
  if(ok)
  {
    //document.getElementById('submit_'+form_id).disabled = 'disabled';
    return true;
  }
  alert('You have to fill all the required fields (marked with asterisk).');
  return false;
}

function addMenuHover()
{
  m = document.getElementsByTagName('li');
  for(var i in m) if(m[i].innerHTML && m[i].parentNode.parentNode.id=='header' && m[i].className=='dd')
  {
    id = m[i].id;
    m[i].onmouseover = function()
    {
      document.getElementById('s'+id).style.display = 'block';
    }
    m[i].onmouseout = function()
    {
      document.getElementById('s'+id).style.display = 'none';
    }
  }
}

onload = function()
{
  a = document.getElementsByTagName('a');
  for(var i in a) if(a[i].innerHTML && (a[i].className=='nw' || a[i].className=='photo nw'))
  {
    a[i].target = '_blank';
  }
}

var akpM = 0;
var akpS = -1;
var leftContent = 0;
var intval = '';
var imageH = '';

function refreshPage(pid, inmenu)
{
    clearSpBfore();
    
    //count position in menu
    var mPos = $('#menuCounter').val();
    mPos++;
    
    //close actual submenu
    if(akpM!=inmenu) for(i=1;i<mPos;i++){ 
        if($('#sm_b_'+i)) $('#sm_b_'+i).removeClass('active');
        if($('#sm_'+i)) if($('#sm_'+i).css('display')!='none'){
          $('#sm_'+i).slideUp('fast');
        }  
    } 
    
    //open submenu
    if(inmenu){
      if($('#sm_b_'+inmenu)) $('#sm_b_'+inmenu).addClass('active');
      if($('#sm_'+inmenu)){ 
        smPos = $('#smenuCounter_'+inmenu).val();
        smPos++;
        //hide actual
        for(e=1;e<smPos;e++){ 
            if($('#sm_b_'+inmenu+'_'+e)) $('#sm_b_'+inmenu+'_'+e).removeClass('active');
        }
        if(akpM!=inmenu) $('#sm_'+inmenu).slideDown('fast');
      }
    }
    //change background outer
    if(akpM!=inmenu || pid==1)
	$.ajax({
       type: "POST",
       url: "index.php?a=getBigImage",
       data: "pid="+pid,
       success: function(msg){
         $('#bgrImg1').after(msg);
		 
         $('#bgrImg1').css('z-index','2');
         $('#bgrImg2').css('z-index','3');
          
         $('#bgrImg1').fadeOut('normal');
         $('#bgrImg2').fadeIn('normal',function(){
              $("#bgrImg1").remove();
              $('#bgrImg2').attr('id','bgrImg1'); 
         }); 
         
                                                       
       }
    });
    
    //change background inner 
    if(akpM!=inmenu || akpS!=0) $.ajax({
       type: "POST",
       url: "index.php?a=getSmallImage",
       data: "pid="+pid,
       success: function(msg)
	   {
         $('#contentImage').html(msg);         	 
		 if($('div##contentImage').children('img').size() > 1) { $('div#contentLeft-pause').css('display', 'block'); }		 
       }
    });
    
    
    
    //change content
    if(pid==1){
       if(leftContent){
         $('#contentLeft-exit').fadeOut('normal');
         $('#contentLeft-text').fadeOut('normal',function(){
              $('#contentLeft-in').animate({ marginLeft: '-370px' }, 1000, function() {
                $('#contentLeft-in').css('display','none');
                
              });
         });
         leftContent = 0;
       }
    }else{ 
       if(akpM!=inmenu || akpS!=0) $.ajax({
           type: "POST",
           url: "index.php?a=getMain",
           data: "pid="+pid,
           success: function(msg){
             if(!leftContent){
                $('#contentLeft-in').css('display','block');
                $('#contentLeft-text').html(msg);
                $('#contentLeft-in').animate({
                  marginLeft: '0px'
                }, 1000, function() {
                   leftContent = 1; 
                   $('#contentLeft-text').fadeIn('normal');
                   $('#contentLeft-exit').fadeIn('normal');
                });
                
             }else{
                $('#contentLeft-text').fadeOut('normal',function(){
                    $('#contentLeft-text').html(msg);
                });
                $('#contentLeft-text').fadeIn('normal');
                $('#contentLeft-exit').fadeIn('normal');
             }
           
           }
       });
       //roll in because its rolled out!!!
       else if( !leftContent)
       {
       	$('#contentLeft-in').animate({
                  marginLeft: '0px'
                }, 1000, function() {
                   leftContent = 1; 
                   $('#contentLeft-text').fadeIn('normal');
                   $('#contentLeft-exit').fadeIn('normal');
                });
       }
    }
    
    if(pid==1) $('#newArrivals').css('display','block');
    else $('#newArrivals').css('display','none');
    
    if(pid==9) $('#contentRight').html('<iframe width="370" height="539" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="./googlemap.html"></iframe>');
    else $('#contentRight').html('');
    
    akpM = inmenu;
    akpS = 0;
} 

var sleep=1;

function refreshSubPage(pid,inmenu,insmenu){ 
   clearSpBfore();
   
   //count position in submenu
   var smPos = $('#smenuCounter_'+inmenu).val();
   smPos++;
   
    //hide actual
    for(i=1;i<smPos;i++){ 
        if($('#sm_b_'+inmenu+'_'+i)) $('#sm_b_'+inmenu+'_'+i).removeClass('active');
    }  
    
    //show actual
    if(insmenu){
      if($('#sm_b_'+inmenu+'_'+insmenu)) $('#sm_b_'+inmenu+'_'+insmenu).addClass('active');
    } 

    //change background inner 
    if(akpS!=insmenu) $.ajax({
       type: "POST",
       url: "index.php?a=getSmallImage",
       data: "pid="+pid,
       success: function(msg){
         $('#contentImage').html(msg);
       }
    });

    //change content
    if(akpS!=insmenu) $.ajax({
           type: "POST",
           url: "index.php?a=getMain",
           data: "pid="+pid,
           success: function(msg){
             //alert(msg);
             if(!leftContent){
                sleep=1;
                $('#contentLeft-in').css('display','block');
                $('#contentLeft-text').html(msg);
                $('#contentLeft-in').animate({
                  marginLeft: '0px'
                }, 1000, function() {
                   leftContent = 1; 
                   $('#contentLeft-text').fadeIn('normal');
                   $('#contentLeft-exit').fadeIn('normal');
                });
                
             }else{
                sleep=0;
                $('#contentLeft-in').css('margin-left','0px');
                $('#contentLeft-text').fadeOut('normal',function(){
                    $('#contentLeft-text').html(msg);
                });
                $('#contentLeft-text').fadeIn('normal');
                $('#contentLeft-exit').fadeIn('normal');
             }
           
           }
       });
       
    akpS = insmenu;
    
}

function changeImageBig(img){
     imageH = '<a href="gfx/img/'+img+'" rel="lytebox"><img src="gfx/img/g_'+img+'" alt="'+img+'" /></a>';
     
     $('#contentRight').fadeOut('fast',function(){
                    $('#contentRight').html(imageH); 
                    $('#contentRight').fadeIn('fast');
                    myLytebox = new LyteBox();                              
                    
                });
     
}

function testFormReserv(formVal){
    //verify date
    var nowDate=new Date();
    var y1 = nowDate.getFullYear();
    var m1 = nowDate.getMonth()+1;
    if(m1<10) m1='0'+m1;
    var d1 = nowDate.getDate();
    if(d1<10) d1='0'+d1;
    var nDate = y1+'-'+m1+'-'+d1;
    if($('[name=data]').val()<nDate){ 
      alert('Nieprawidłowa data!');
      return false;
    }
    
    //verifi firstlastname
    if($('[name=firstlastname]').val()=='' || !$('[name=firstlastname]').val()){
      alert('Prosimy wpisać imię i nazwisko!');
      return false;
    }
    
    //verify mail
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if($('[name=email]').val().search(emailRegEx) == -1 && $('[name=email]').val()!=''){
      alert('Nieprawidłowy adres mailowy!');
      return false;
    }    
    
    if($('[name=contact]').val()==2 && ($('[name=email]').val()=='' || !$('[name=email]').val())){
      alert('Proszę wpisać adres mailowy ponieważ potwierdzenie rezerwacji ma być wsylano na maila!');
      return false;
    }
    
    if($('[name=contact]').val()==1 && ($('[name=phone]').val()=='' || !$('[name=phone]').val())){
      alert('Proszę wpisać telefon ponieważ potwierdzenie rezerwacji ma być na telefon!');
      return false;
    }
    
    return true;
}

function rollContentLeft()
{
	clearSpBfore();
  $('#contentLeft-exit').css('display','none');
	$('#contentLeft-in').animate({
                  marginLeft: '-370px'
                }, 1000, function() {
                   leftContent = 0; 
                   $('#contentLeft-text').fadeOut('normal');
                   
                });
	
}

var listaem = 1;
var listaemLast = 1;

function clearSpBfore(){
    listaem = 1;
    listaemLast = 1;
    var poz = 1;
    var zn = false;
    if($('#uContentLeft1').length) var zn = true;
    if($('#contentLeft-left').length) $('#contentLeft-left').remove();
    if($('#contentLeft-right').length) $('#contentLeft-right').remove();
    while(zn){
       if($('#uContentLeft'+poz).length){
          $('#uContentLeft'+poz).fadeOut('fast');
          $('#uContentLeft'+poz).remove();
       }else{
          zn=false;
       }
       poz=poz+1;
    }
}

function insertSpBefor(num,num2){
  var wwSp = num*370 + 370;
  $('#contentLeft').css('width',wwSp+'px');
  if(num==1) $('#contentLeft-in').after('<div class="universalContentLeft" id="uContentLeft'+num+'" style="display:none;"><div class="universalContentLeft-text" id="uContentLeft'+num+'t"></div></div>');
  $('#uContentLeft'+num2).after('<div class="universalContentLeft" id="uContentLeft'+num+'"><div class="universalContentLeft-text" id="uContentLeft'+num+'t"></div></div>');
  if(num==1) $('#uContentLeft'+num).fadeIn('fast');
  if(num==2){
     $('#contentConetent').before('<div id="contentLeft-left" style="display:none;"><a href="./" onclick="contentRollLeft(); return false;">&#171;</a></div>');
     $('#contentConetent').before('<div id="contentLeft-right"><a href="./" onclick="contentRollRight(); return false;">&#187;</a></div>');
  }
  listaemLast = num;
} 

function insertTextOnNewContent(num,mid){
  mid = parseInt(mid);
  $.ajax({
           type: "POST",
           url: "index.php?a=getModule",
           data: "mid="+mid,
           success: function(msg){
             //alert(msg);
             //if(num==1) $('#uContentLeft'+num+'t').css('display','none');
             $('#uContentLeft'+num+'t').html(msg);
             if(num==1) $('#uContentLeft'+num+'t').fadeIn('normal');
           }
  });
}

function contentRollLeft(){
    listaem--;
    if(listaem<1){
      listaem=1;
      
      return false;
    }   
    if(listaem==1) $('#contentLeft-left').css('display','none');
    if(listaem<listaemLast) $('#contentLeft-right').css('display','block');
    $('#contentLeft').animate({
                  left: '+=370px'
                }, 1000, function() {
    });
}
function contentRollRight(){
    listaem++;
    if(listaem>listaemLast){
       listaem = listaemLast;
       return false;
    }
    if(listaem==listaemLast) $('#contentLeft-right').css('display','none');
    if(listaem>1) $('#contentLeft-left').css('display','block');
    $('#contentLeft').animate({
                  left: '-=370px'
                }, 1000, function() {
    });
}

