$(document).ready(function() {
 $("#loading").ajaxStart(function() { $(this).addClass('vv'); });
 $("#loading").ajaxStop(function() { $(this).removeClass('vv'); });

 //animate na .photos
 animateOnPhotos();
 
 //kasowanie komunikatow
 $("div.msg_ok").click(function() {
  $(this).remove();
 });
 
 //pokazywanie ukrywanie exifow
 $("a#toggleExif").click(
  function() {
   $("ul#exif").toggle();
  }
 ).toggle(function() { $(this).text("Pokaż dane EXIF"); }, function() { $(this).text("Ukryj dane EXIF"); });
 
 //pokazywanie ukrywanie mapy
 $("a#toggleMap").click(
  function() {
   var gps_lat = $("div#map input#gps_lat").val();
   var gps_long = $("div#map input#gps_long").val();
   var map_key = "ABQIAAAAjGYR5AfGRdjf5yfoMNJsaRRc5VsjHNYOgQC-tYwFbugQOdKtgxQpLJ6iGZ1gi5oS-m9RJxm6xbx8ew";
   $("div#map").toggle();
   $("div#map img").attr("src", "http://maps.google.com/staticmap?center="+gps_lat+","+gps_long+"&zoom=12&size=300x250&maptype=mobile\&markers="+gps_lat+","+gps_long+"&key="+map_key);
  }
 ).toggle(function() { $(this).text("Ukryj mapę"); }, function() { $(this).text("Pokaż mapę"); });
  
 //pokazywanie ukrywanie kodow
 $("a#toggleCode").click(
  function() {
   $("div#code").toggle();
  }
 ).toggle(function() { $(this).text("Ukryj kody"); }, function() { $(this).text("Pokaż kody"); });
  
 //pokazywanie ukrywanie opisu tagu
 $("a.toggleTagsDesc").click(
  function() {
   $("div#tagsDesc").toggle();
  }
 ).toggle(function() { $(this).text("ukryj opis"); }, function() { $(this).text("opis tagu"); });

 //wywolanie popupa
 $('a.popup').click(function() {
  if ($.browser.version=='6.0') {
   alert('Używasz przedpotopowej przeglądarki. Idź, zainstaluj coś nowszego i dopiero wróć!');
   return false;
  }
  
  var url = $('a.popup').attr('href');
  esc();
  loadImage(url);
  return false;
 });

 //tylko dla bloga
// $.ajax({ 
//  type: 'GET',
//  url: 'http://siek.pl/blog_photos.php',
//  dataType: 'html',
//  success: function(html) {
//   $('#last_photos').html(html);
//  },
//  error: function showError() {
//   $('#last_photos').html('Serwer jest w tej chwili nieosiągalny. :(');
//  }
// })
 
 
 

//end    
});






$.ajaxSetup( {
 type: "POST",
 url: "/sys/ajax.php",
 dataType: "xml"
});

function setActive(act) {
// $("li#navbar_"+body+" a").addClass("act");
 $("li#sidebar_"+act+" a").addClass("b");
}

function animateOnPhotos() {
 $(".photos a").hover(
  function() {
   $(this).children("span").animate({
   "height": "33px"
   }, 200);
  },
  function() { 
   $(this).children("span").animate({ "height": "15px" }, 200);
  }
 );
}


function vote(id, vote) {
 $("h4#voting").text("Przetwarzam...").addClass("n");
 $.ajax({
  data: "o=vote&id="+id+"&vote="+vote,
  success: function(xml) {
   var srednia = $('srednia', xml).text();
   var procent = $('procent', xml).text();
   $("h4#voting").text("Dzięki za głos. Nowa średnia to "+srednia+".").removeClass("n");
   $("li.current-rating").css({ "width": procent+"%" });
   $("ul.star-rating li a").removeAttr("onclick");
  }
 })
}


function th_m(content, id) {
 var rel = $('a.changeView').attr('rel');
 if (!rel) {
  $('a.changeView').text('ładowanie...');
  $.ajax({
   dataType: 'html', 
   data: 'o=th_m&dataType=html&content='+content+'&id='+id,
   success: function(html) {
    $('div.th_s').hide();
    $('div.th_m').fadeIn('slow').html(html);
    animateOnPhotos();
    $('a.changeView').text('małe miniatury').attr('onclick', 'th_s('+id+')').attr('rel', '1');
   }
  })
 }
 else {
  $('div.th_s').hide();
  $('div.th_m').fadeIn('slow');
  $('a.changeView').text('małe miniatury').attr('onclick', 'th_s('+id+')'); 
 }
 
}


function th_s(id) {
 $("div.th_m").hide();
 $("div.th_s").fadeIn("slow");
 $("a.changeView").text('duże miniatury').attr('onclick', 'th_m('+id+')');
}


function changeImg(img, id) {
 $("#viewer img").hide();
 $("#viewer img#viewer_"+id).attr("src", "/"+img).fadeIn("slow");
 
 $(".tabs li a").removeClass("act");
 $(".tabs li a#a_"+id).addClass("act");
}


function loadImage(url) {
 $('<div id="popup"><a onclick="$(\'#popup\').remove();" title="Zamknij"><img src="/i/lay/loading_big.gif" alt="loading" style="border:none;" /></a></div>').appendTo('body').toggle();
 var img = new Image();
 var width_window = $(window).width();
 var height_window = $(window).height();
 $(img).load(function () {
  $('#popup a').html(this).addClass('vh');
  var width_img = $(this).width();
  var height_img = $(this).height();
  var start_x = (parseInt(width_window)-parseInt(width_img))/2-10;
  var start_y = (parseInt(height_window)-parseInt(height_img))/2-10;
  
  $('#popup img').css({ 'left': start_x+'px', 'top': start_y+'px' });  
  $('#popup a').removeClass('vh');
 }).error(function() { console.log('upsss'); }).attr('src',url);
}

function esc() {
 $(document).keydown(function(e) {
  if (e.keyCode == 27) { // ESC
   e.preventDefault;
   $('#popup').remove();
   console.log('esc');
   e.stopPropagation(console.log('niby stop -TODO'));
  }
 });
}