$(function() {
	$('[rel=lightbox]').lightBox({fixedNavigation:true});
});


function show_box(country){
  $(".travel_guide_link_box").hide();
  $("#box_" + country).show();
  $("#kontinents ul li a").removeClass();
  $("#link_" + country).addClass('active');
  $('#link_' + country ).blur();
}

function validate_newsletter_form(form){
  
  var check = false;
  var postal_code_pattern = /[A-Za-z]{2}[0-9]{3}[A-Za-z]{2}/i;
  
  var postal_code = form.postalcode.value;
  
  if(form.mail.value == ""){    
    alert('Please insert your mail.');
    form.mail.focus();
    return false;  
  }else if(form.name.value == ""){    
    alert('Please insert your first name.');
    form.name.focus();
    return false;  
  }
  else if(form.surname.value == ""){    
    alert('Please insert your last name.');
    form.surname.focus();
    return false;  
     
  }
  else if(form.postalcode.value == ""){    
    alert('Please insert your postal_code.');
    form.postalcode.focus();
    ///alert(document.form.list);
    return false;  
  }
  
   for(var i=0, l=form.length; i<l; i++){
    if (form[i].type == 'checkbox'){
      if(form[i].checked == true)
        check = true; 
    }
  }
  
  if(!postal_code.match(postal_code_pattern)){
	alert('Wrong format of postal code. Format should be LLNNNLL (L = letter, N = number)');
	form.postalcode.focus();
	return false;
  }
  
  if(!check){
	 for(var i=0, l=form.length; i<l; i++){
		if (form[i].type == 'checkbox')
		  form[i].checked = true;
	 }
	 
	 return true;
  } 
  
  
  return true;
}

function valid_weather_form(form){
  if(form.cities.value == 0){
  	 alert('Please choose the city for weather');
     return false;
  }
  return true;
}

function valid_advertise_form(form){
  if(form.company.value == ""){    
    alert('Please insert your company.');
    form.company.focus();
    return false;  
  }else if(form.contact_person.value == ""){    
    alert('Please insert name of contact person.');
    form.contact_person.focus();
    return false;  
  }
  else if(form.email.value == ""){    
    alert('Please insert your e-mail.');
    form.email.focus();
    return false;  
  }
   else if(form.phone.value == ""){    
    alert('Please insert your phone.');
    form.phone.focus();
    return false;  
  }
  
  return true;
}

function bookmark_page() {
  // Mozilla Firefox Bookmark
  if(window.sidebar)
    window.sidebar.addPanel("Pampo.net - Get inspired for your next holiday", "http://www.pampo.net","");
  
  // IE Favorite
  else if(document.all)
    window.external.AddFavorite("http://www.pampo.net", "Pampo.net - Get inspired for your next holiday");
  
  // Opera Bookmark
  else if(window.opera && window.print) {
    var obm = document.createElement('a');
    obm.setAttribute("title", "Pampo.net - Get inspired for your next holiday");
	obm.setAttribute("href", "http://www.pampo.net");
	obm.setAttribute("rel", "sidebar");    
    obm.click();
  }
  
  else if(window.chrome){
   	alert('Unfortunately, this browser does not support the requested action. Press ctrl+D to bookmark (Command+D for macs) after you click Ok');  
  }
}



//pocasie
function get_countries()
{
   
   $.post("/modules/pocasie/pocasie_ajax.php",{country:"1",kontinent:$("#kontinent").val(),krajina:$("#krajina").val(),mesto:$("#mesto").val()},
      function(xmldata) {
        process_country(xmldata);
      },"xml" );
}


function get_cities(){
   $.post("/modules/pocasie/pocasie_ajax.php",{city:"1",kontinent:$("#kontinent").val(),krajina:$("#krajina").val(),mesto:$("#mesto").val()},
      function(xmldata) {
        process_city(xmldata);
      },"xml" );
}

function process_country(xmldata){
  root = xmldata.documentElement;
  frm = document.forms['form_weather'];
  
  
  for(i = 0; i < root.childNodes.length; i++) {
    act_node = root.childNodes[i];
    
    //vynulovanie selecboxov
    BOX = act_node.nodeName;    
    frm.elements[BOX].options.length = 1;
    
    //naplnenie selectboxov
    for(j = 0; j < act_node.childNodes.length; j++){
      act_node2 = act_node.childNodes[j];
      frm.elements[BOX].options[j+ 1] = new Option(act_node2.getAttribute('text'),act_node2.getAttribute('value'));
    }
    
    } 
}

function process_city(xmldata){
  root = xmldata.documentElement;
  frm = document.forms['form_weather'];
  
  for(i = 0; i < root.childNodes.length; i++) {
    act_node = root.childNodes[i];
    //vynulovanie selecboxov
    BOX = act_node.nodeName;    
    frm.elements[BOX].options.length = 1;
    
    //naplnenie selectboxov
    for(j = 0; j < act_node.childNodes.length; j++){
      act_node2 = act_node.childNodes[j];
      frm.elements[BOX].options[j+ 1] = new Option(act_node2.getAttribute('text'),act_node2.getAttribute('value'));
    }
  }
}

//filter matica functions
$(document).ready(function(){  
	/* filter values already selected */
	   
   $("#experience").change(searchByExperienece);
    $("#destination").change(searchByExperienece);

     $("div#thumbs li a ").mouseover(function()
      {
        if (!$(this).parent().hasClass("disabledthumb")){
           $(this).children('div').addClass("green");
          $(this).children('div').removeClass("green_light");
        }
      });
      
      $("div#thumbs li a").mouseout(function()
      {
       
        if (!$(this).parent().hasClass("disabledthumb")){
           $(this).children('div').addClass("green_light");
          $(this).children('div').removeClass("green");
        }
      });
	
	$("div#thumbs li a").tooltip({
		bodyHandler: function() {
			
     
      
      if (!$(this).parent().hasClass("disabledthumb")) {

				var hoverimghref = $(this).next("div").find(":first").find(":first").attr("href");
				var hoverimg = $(this).next("div").find(":first").find(":first").find(":first").attr("src");
				if (hoverimg == undefined) {
					hoverimg = $("<img></img>").attr('src', hoverimghref);
					$(this).next("div").find(":first").find(":first").append(hoverimg);
				}
				return $($(this).next("div")).html();
			}
			else
				return false;
		},
		showURL:false
	});	
	
	for (i=0;i<64;i++)
	{
		$("#img" + i).attr("src", $("#img" + i + "preload").attr("src"));
	}
	
	$("ul#pages :first-child").addClass("first");
	
	
});



function searchByExperienece(){
  
  $("div#thumbs li").each(function(){
    $(this).addClass("disabledthumb");
    
    if((parseInt($(this).attr('rel')) >= parseInt($("#amount_down").text())) && (parseInt($(this).attr('rel')) <= parseInt($("#amount_up").text()))){
      
      //ak bol setnuty exp experience a sucasne destination destination
        if($("#experience").val() != "" && $("#destination").val() != "" ){
          if($(this).attr('class').indexOf($("#experience").val()) != -1 && $(this).attr('class').indexOf($("#destination").val()) != -1){
            $(this).removeClass("disabledthumb");
          }
        }
        //ak bol setnuty experience
        else if($("#experience").val() != "" ){
          if($(this).attr('class').indexOf($("#experience").val()) != -1){
            $(this).removeClass("disabledthumb");
          }
        }
        //ak bol setnuty destination
        else if($("#destination").val() != "" ){ 
		   if($(this).attr('class').indexOf($("#destination").val()) != -1){
            $(this).removeClass("disabledthumb");
          }
        }else{
			$(this).removeClass("disabledthumb"); 	
		}
		
     }  
  }); 
}

function searchByPrice(){
   //var mb = $("#amount_down").text();
   //alert(mb);
    
    $("div#thumbs li").each(function(){
       $(this).addClass("disabledthumb");
       if((parseInt($(this).attr('rel')) >= parseInt($("#amount_down").text())) && (parseInt($(this).attr('rel')) <= parseInt($("#amount_up").text())))
       {
        //ak bol setnuty exp experience a sucasne destination destination
        if($("#experience").val() != "" && $("#destination").val() != "" ){
          if($(this).attr('class').indexOf($("#experience").val()) != -1 && $(this).attr('class').indexOf($("#destination").val()) != -1){
            $(this).removeClass("disabledthumb");
          }
        }
        //ak bol setnuty experience
        else if($("#experience").val() != "" ){
          if($(this).attr('class').indexOf($("#experience").val()) != -1){
            $(this).removeClass("disabledthumb");
          }
        }
        //ak bol setnuty destination
        else if($("#destination").val() != "" ){
           if($(this).attr('class').indexOf($("#destination").val()) != -1){
            $(this).removeClass("disabledthumb");
          }
        }
        else 
           $(this).removeClass("disabledthumb");
            
       }  
    });	
}

function returnToHomepage(s){
	if (document.referrer == s) {
		history.go(-1);
	}
	else {
		location.href = s;
	}
}

//calculator

function kalk(){
  $.post("/modules/kurzy/kurzy_ajax.php",{amount:$("#amount").val(),from:$("#from_ccy").val(),to:$("#to_ccy").val()},
      function(data) {
        process(data);
      } );
}
function process(data){
  var target = document.getElementById('final_amount')
  target.innerHTML = data;
  
}

function graf(){
  $.post("/modules/kurzy/kurzy_ajax.php",{days:$("#days").val(),ccy:$("#ccy").val()},
      function(data) {
        process_graf(data);
      } );
}

function process_graf(data){
  var target = document.getElementById('graf_target')
  target.innerHTML = data;
  
} 
