
//-----------------------------------------------------------------------------
// menu functions
//-----------------------------------------------------------------------------

fotomenu_vis=0;
hobbymenu_vis=0;

$(document).ready(function(){
   //$('#guestbook_help').hide();
   //$('#guestbook_help').css('visibility','hidden');//for IE shit:

   //załądowanie pliku ze zdjęciem
   $('#fotka_img').load(function(){
      foto_show();
   });

   // prawy klik na zdjęcie
   $('#cover_div').contextMenu("context_div",{
      onContextMenu: function(e) {
         foto_hide();
         return false;
      }
   });

   //menu
   $('#foto_img').hover(
      function(){$('#fotomenu').show();},
      function(){}
   );
   $('#fotomenu').hover(
      function(){},
      function(){$('#fotomenu').hide();}
   );
   $('#hobby_img').hover(
      function(){$('#hobbymenu').show();},
      function(){}
   );
   $('#hobbymenu').hover(
      function(){},
      function(){$('#hobbymenu').hide();}
   );
   $('#rangemap_krakow').click(function(){rangemap(1)});
   $('#rangemap_przysucha').click(function(){rangemap(2)});
   $('#rangemap_lask').click(function(){rangemap(3)});
   $('#rangemap_wyjazd').click(function(){rangemap(4)});
	
   $('#fullscreen').click(function(){
		window.open('kml/fullscreen.php?r='+range_selected,'Fullscreen','width='+screen.width+','+screen.height+',resizable=yes,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no')
	});

   $('.speaker').click(function(){
		movie($(this).attr("alt"));
	});
});

//-----------------------------------------------------------------------------
// rangemap function
//-----------------------------------------------------------------------------

var range_selected=0;
function rangemap(range){
   range_selected=range;
   if(GBrowserIsCompatible()){
      var opt=new Array();
      opt.size=new GSize($("#mapa").width(),600);
      var mapa = new GMap2(document.getElementById("rangemap"),opt);

      mapa.setCenter(new GLatLng(0,0),1);GMapTypeControl
      mapa.addControl(new GLargeMapControl3D());
      mapa.addControl(new GMapTypeControl());

      //var path=new GGeoXml("http://stawka.hopto.org/stawiarz/kml/range.php?r="+range+"&x="+(1+Math.floor(Math.random()*1000000000)) );
      var path=new GGeoXml("http://stawiarz.pl/kml/range.php?r="+range+"&x="+(1+Math.floor(Math.random()*1000000000)) );
      mapa.addOverlay(path);
      path.gotoDefaultViewport(mapa);

      $('#rangemap').show();
      $('#gpxfile').show();
      $('#kmlfile').show();
      $('#fullscreen').show();
   };
};
//-----------------------------------------------------------------------------
// map popup functions
//-----------------------------------------------------------------------------

function map(numer){
   $("#shadow").css({height:"100%",top:$(window).scrollTop()+"px",opacity:0.8});
   $("#shadow").show();
   $("#map").center();

   if(GBrowserIsCompatible()){
      var opt=new Array();
      opt.size=new GSize(900,600);
      var mapa = new GMap2(document.getElementById("map"),opt);

      mapa.setCenter(new GLatLng(0,0),1);
      mapa.addControl(new GLargeMapControl3D());

      var path=new GGeoXml("http://stawiarz.pl/kml/"+numer+".kml");
      mapa.addOverlay(path);
      path.gotoDefaultViewport(mapa);

      $("#map").show();
   };
   $("#shadow").click(function(){map_hide()});
};
function map_hide(){
   $("#shadow").hide();
   $("#map").hide();
};

//-----------------------------------------------------------------------------
// movie popup functions
//-----------------------------------------------------------------------------

function movie(link){
   $("#shadow").css({height:"100%",top:$(window).scrollTop()+"px",opacity:0.8});
	$("#shadow").show();
	$("#playmovie").center();

	$("#playmovie").html('<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/'+link+'&hl=pl_PL&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+link+'&hl=pl_PL&fs=1&rel=0&color1=0x2b405b&color2=0x6b8ab6" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object><span id="close">POWRÓT</span>');
	$("#close").bind("click",movie_hide);
	$("#playmovie").show();

	$("#shadow").click(function(){movie_hide()});
};
function movie_hide(){
   $("#shadow").hide();
	$("#close").unbind("click");
	$("#playmovie").html("");
   $("#playmovie").hide();
};

//-----------------------------------------------------------------------------
// photo popup functions
//-----------------------------------------------------------------------------

function foto(numer){
   sandglass_on();
   $.post("pod/fotosize.php",{param1: numer},function(response){
      if(response!=""){
         tmp=response.split(';');
         $("#fotka_img").attr("src","pod/foto.php?wgm="+numer);
         $("#fotka_div").css({width:(parseInt(tmp[0])+10)+"px",height:(parseInt(tmp[1])+10)+"px"});
         $("#fotka_div").center();
         $("#shadow").css({height:"100%",top:$(window).scrollTop()+"px",opacity:0.8});
         $("#fotka_blank").css("height",$(window).height()+"px");
         $("#cover_div").css("top",$(window).scrollTop()+"px");
      };
   });
};
function foto_show(){
   if($("#fotka_img").attr("src")!=""){
      sandglass_off();
      $("#cover_div").show();
      $("#shadow").show();
      $("#fotka_img").show();
      $("#fotka_div").show();
   };
};
function foto_hide(){
   $("#cover_div").hide();
   $("#shadow").hide();
   $("#fotka_img").hide();
   $("#fotka_div").hide();
};

//-----------------------------------------------------------------------------
// guestbook popup functions
//-----------------------------------------------------------------------------

function Homepl(data){
   data=data.replace(/\n+/g,'');
   data=data.replace(/<script.+/,'');
   return data;
};

// array with input fields definition for class.Stawka.js
tab_inputs=new Array(10);
tab_inputs['nick']="Twoje imię (nick)";
tab_inputs['email']="Twój adres e-mail";
tab_inputs['www']="Twoja strona WWW";
tab_inputs['comm_nick']="Twój komunikator";
tab_inputs['city']="Twoje miasto";
tab_inputs['entry']="Twój wpis";
var enter_pressed=0;
var error_field='';

$(document).ready(function(){
	$("#guestbook_emoticons_show").click(function(){
		$("#guestbook_emoticons").show();
	});
	$("#guestbook_emoticons_hide").click(function(){
		$("#guestbook_emoticons").hide();
	});
	$("#guestbook_bold").click(function(){
		$("#entry").val($("#entry").val()+"[b] [/b]");
	});
	$("#guestbook_italic").click(function(){
		$("#entry").val($("#entry").val()+"[i] [/i]");
	});
	$("#guestbook_underline").click(function(){
		$("#entry").val($("#entry").val()+"[u] [/u]");
	});
	$("#guestbook_entry_show").click(function(){
		$("#guestbook_show").hide();
		$("#guestbook_help").hide();
		$("#guestbook_entry").show();
	});
	$("#guestbook_show_show").click(function(){
		guestbook_show();
	});
	$("#guestbook_help_show").click(function(){
		$("#guestbook_entry").hide();
		$("#guestbook_emoticons").hide();
		$("#guestbook_show").hide();
		$("#guestbook_help").show();
	});
});

function guestbook_on(){
	sandglass_off();
	$("#guestbook").center();
	$("#guestbook").show();
	$("#guestbook_entry").show();
};
function guestbook_hide(){
	$("#guestbook_entry").hide();
	$("#guestbook_emoticons").hide();
	$("#guestbook_show").hide();
	$("#guestbook_help").hide();
	$("#guestbook").hide();
};
function guestbook_emoticon_enter(emoticon){
   $("#entry").val($("#entry").val()+" "+emoticon);
   $("#guestbook_emoticons").hide();
};
function guestbook_entry_comm(){
   if($("#comm").val()==1)$("#comm_icon").attr("src","guestbook/comm-tlen.gif");
   if($("#comm").val()==2)$("#comm_icon").attr("src","guestbook/comm-gadu.gif");
   if($("#comm").val()==3)$("#comm_icon").attr("src","guestbook/comm-skype.gif");
   if($("#comm").val()==4)$("#comm_icon").attr("src","guestbook/comm-jabb.gif");
};
function guestbook_show(offset){
   if(offset){
      tmp=offset;
   }else{
      tmp=0;
   };
	sandglass_on();
   $.post("guestbook/show.php",{param1:tmp},function(response){
         sandglass_off();
         if(response!=""){
				response=Homepl(response);
            kod=response.slice(0,1);
		      odp=response.slice(1);
            if(kod=="0"){
               guestbook_on();
               $("#guestbook_show").html(odp);
               $("#guestbook_entry").hide();
               $("#guestbook_help").hide();
               $("#guestbook_show").show();
            }else{
               error_on("Błąd");
               error_field='';
            };
         };
   });
};
function guestbook_oneentry(id,offset){
   sandglass_on();
   $.post("guestbook/oneentry.php",{param1: id,param2: offset},function(response){
         sandglass_off();
         if(response!=""){
				response=Homepl(response);
            kod=response.slice(0,1);
		      odp=response.slice(1);
            if(kod=="0"){
               $("#guestbook_show").html(odp);
               $("#guestbook_entry").hide();
               $("#guestbook_help").hide();
               $("#guestbook_show").show();
            }else{
               error_on("Błąd");
               error_field='';
            };
         };
   });
};





// returns value of given input field
function field_value(input){
   return document.getElementById(input).value;
};
// sends the form if 'Enter' was pressed
function enter(evt,form){
   if(enter_pressed==0){
      if(evt.keyCode==13){
         guestbook_send();
         enter_pressed=1;
      };
   }else{
      enter_pressed=0;
   };
}
// check if  input field is filled
function empty(input,action){
   if(field_value(input)==""){
      if(action==1){
         error_message="Pole '" +tab_inputs[input]+"' jest wymagane.";
         error_on(error_message);
         error_field=input;
      };
      return true;
   };
   return false;
}
// checks if given nick input is filled correctly
function check_nick(input){
   if(empty(input,1)){
      return false;
   }else{
      var reg="";
      var reg=/^[0-9a-zA-Z- _ąćęłńóśżĽĄĆĘŁŃÓŚŻŹ]{1,30}$/;
      var wyn=document.getElementById(input).value.match(reg);
      	
      if(wyn==null){
         error_message="Proszę podać poprawny tekst w polu '" +tab_inputs[input]+"'! Może zawierać litery, cyfry oraz znaki '-','_',' '.";
         error_on(error_message);
         error_field=input;
         return false;
      };
   };
   return true;
};
// checks if given email input is filled correctly
function check_email(input){
   if(empty(input,0)){
      return true;
   };
   //maxlength-1(@)-3(.pl,.com ...)-1(wp,onet ...)
   var reg=/^[0-9a-zA-Z-_.]{1,45}@[0-9a-zA-Z-_.]+.[a-zA-Z]{2,3}$/;
   var wyn=document.getElementById(input).value.match(reg);
      	
   if(wyn==null){
      error_message="Proszę podać adres w polu '" +tab_inputs[input]+"'!";
      error_on(error_message);
      error_field=input;
      return false;
   };
   return true;
};
// checks if given www input is filled correctly
function check_www(input){
   if(empty(input,0)){
      return true;
   };
   var reg=/^[0-9a-zA-Z-/:.]{1,100}$/;
   var wyn=document.getElementById(input).value.match(reg);
      	
   if(wyn==null){
      error_message="Proszę podać poprawny adres www w polu '" +tab_inputs[input]+"'!";
      error_on(error_message);
      error_field=input;
      return false;
   };
   return true;
};
// checks if given tlen login is filled correctly
function check_tlen(input){
   if(empty(input,0)){
      return true;
   };
   var reg=/^[a-z]{1}[0-9a-z-_.]{2,124}(@tlen.pl)*$/;
   var wyn=document.getElementById(input).value.match(reg);
      	
   if(wyn==null){
      error_message="Proszę podać poprawny login tlen.pl w polu '" +tab_inputs[input]+"'!";
      error_on(error_message);
      error_field=input;
      return false;
   };
   return true;
};
// checks if given number input is filled correctly
function check_number(input,specified_length,flag){
   if(empty(input,0)){
      return true;
   };

   var reg=/^[0-9]+$/;
   var wyn=document.getElementById(input).value.match(reg);
   var input_length=document.getElementById(input).value.length;

   if(wyn==null){
	    error_message="Podany numer w polu '" +tab_inputs[input]+"' nie jest prawidlowy!";
       error_on(error_message);
       error_field=input;
       return false;
   };
   
   var err=0;
   if(flag=="MAX"){
      if(input_length>specified_length){
	      error_message="Pole '" +tab_inputs[input]+"' może zawierać maksymalnie "+specified_length+" cyfr!";
	      err=1;
      };
   }else if(flag=="EQ"){
      if(input_length!=specified_length){
	      error_message="Pole '" +tab_inputs[input]+"' musi zawierać "+specified_length+" cyfr!";
	      err=1;
      };
   };
   if(err==1){
      error_on(error_message);
      error_field=input;
      return false;
   };
   return true;
};
// checks if given skype login is filled correctly
function check_skype(input){
   if(empty(input,0)){
      return true;
   };
   var reg=/^[a-z]{1}[0-9a-zA-Z-_.,]{5,149}$/;
   var wyn=document.getElementById(input).value.match(reg);
      	
   if(wyn==null){
      error_message="Proszę podać poprawny login Skype w polu '" +tab_inputs[input]+"'!";
      error_on(error_message);
      error_field=input;
      return false;
   };
   return true;
};
// checks if given communicator input is filled correctly
function check_comm(input){
   if(field_value("comm")==1)return check_tlen(input);
   if(field_value("comm")==2)return check_number(input,7,"MAX");
   if(field_value("comm")==3)return check_skype(input);
   if(field_value("comm")==4)return check_email(input);

};
// checks if given city input is filled correctly
function check_city(input){
   if(empty(input,0)){
      return true;
   };
   var reg="";
   var reg=/^[0-9a-zA-Z- ąćęłńóśżĽĄĆĘŁŃÓŚŻŹ]{1,50}$/;
   var wyn=document.getElementById(input).value.match(reg);
      	
   if(wyn==null){
      error_message="Proszę podać poprawny tekst w polu '" +tab_inputs[input]+"'!  Może zawierać litery, cyfry oraz znaki '-',' '.";
      error_on(error_message);
      error_field=input;
      return false;
   };
   return true;
};
// checks if the form is filled correctly
function check_entry(){
	clear_error_field();
   if(
      check_nick('nick') &&
      check_email('email') &&
      check_www('www') &&
      check_comm('comm_nick') &&
      check_city('city') &&
      !empty('entry',1)
      )
      return true;
	return false;
}

//sends the form
function guestbook_send(){
   if(check_entry())advAJAX.post({
      url:"guestbook/entry.php",
      parameters:{
         param1: field_value("nick"),
         param2: field_value("email"),
         param3: field_value("www"),
         param4: field_value("entry"),
         param5: field_value("comm_nick"),
         param6: field_value("comm"),
         param7: field_value("city")
      },
      onInitialization: function(obj){
         sandglass_on();
      },
      onSuccess: function(obj){
         sandglass_off();
         if(obj.responseText!=""){
				response=Homepl(obj.responseText);
            kod=response.slice(0,1);
		      odp=response.slice(1);
            if(kod=="0"){
               guestbook_show();
            }else if(kod=="2"){
               error_on("Za długi wpis.");
               error_field='entry';
            }else if(kod=="3"){
               error_on("Zły adres e-mail.");
               error_field='email';
            }else if(kod=="4"){
               error_on("Zły adres Jabber.");
               error_field='comm_nick';
            }else if(kod=="5"){
               error_on("Wpisałeś się przed chwilą.");
               error_field='';
            }else if(kod=="6"){
               error_on("Taki wpis już istnieje.");
               error_field='';
            }else{
               error_on("Błąd");
               error_field='';
            };
         };
      }
   });
};

//-----------------------------------------------------------------------------
// additional divs functions
// !!! special divs in html part required !!!
//-----------------------------------------------------------------------------

// show sandglass
function sandglass_on(){
   $("#sandglass_div").center();
   $("#sandglass_div").show();
}

// hide sandglass
function sandglass_off(){
   $("#sandglass_div").hide();
}

function error_on(message){
   $("#error_div").center();
   $("#error").html(message);
   $("#error_div").show();
};

function error_off(){
   document.getElementById("error_div").style.visibility="hidden";
   if(error_field!=''){
      document.getElementById(error_field).focus();
      document.getElementById(error_field).style.borderColor="red";
      if(document.getElementById(error_field).type=="textarea"){
         document.getElementById(error_field).style.backgroundColor="#ffcbca";
         document.getElementById(error_field).style.color="#000000";
      }else{
         document.getElementById(error_field).style.backgroundImage="url('guestbook/html.InputError.gif')";
      };
   };
};

function clear_error_field(){
   if(error_field!=''){
      if(document.getElementById(error_field).type=="textarea"){
         document.getElementById(error_field).style.borderColor="#e7bd7b";
         document.getElementById(error_field).style.backgroundColor="#151515";
         document.getElementById(error_field).style.color="#e7bd7b";
      }else{
         document.getElementById(error_field).style.borderColor="#553810";
         document.getElementById(error_field).style.backgroundImage="url('guestbook/html.Input.gif')";
      };
      error_field='';
   };
}



