lastid = 0;

function muudanimi( base, id, page, update_id )
{
  update_id = "#" + update_id;

  if ( lastid != id )
  {
    lastid = id;
    link = base + 'fn_ajax.php?muudapiltinfo='+id+'&page='+page;
    $.ajax({
      url: link,
      cache: false,
      success: function(html){
        $( update_id ).html(html);
        $( update_id ).fadeIn();
      }
    });
  }
  else {
    //document.getElementById( update_id ).style.display = "none";
    $( update_id ).fadeOut();
    lastid = 0;
  }
}

function change(href) { window.location.href = href; }

function popup_size(w,h)
{
  window.resizeTo(w,h);
  var x = (screen.width - w) / 2
  var y = (screen.height - h) / 2
  window.moveTo(x,y);
}

function img_r_vasak( base, pilt_id ) {
 //var teeme = confirm('Soovid pöörata pilti vastupäeva?');;
 //if (teeme == true)
    xmlhttpPost( base + "fn_ajax.php?img_r_vasak=" + pilt_id, "src", "pilt_" + pilt_id ); }

function img_r_parem( base, pilt_id ) {
 //var teeme = confirm('Soovid pöörata pilti päripäeva?');;
 //if (teeme == true)
     xmlhttpPost( base + "fn_ajax.php?img_r_parem=" + pilt_id, "src", "pilt_" + pilt_id ); }

function xmlhttpPost(strURL, type, element) {
  var http = false;

  if(navigator.appName == "Microsoft Internet Explorer") {
    http = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    http = new XMLHttpRequest("Ajax");
  }

  http.open("GET", strURL, true);
  http.onreadystatechange=function()
  {
    if(http.readyState == 4) {
      var now = new Date();

      if ( type=='src' )
        updatepage_src ( element, http.responseText + '#' + now.getTime() );
      else if ( type == 'html' )
        updatepage_html ( element, http.responseText );
    }
  }
  http.send(null);
}

function updatepage_html(element, str){
    document.getElementById( element ).innerHTML = str; }

function updatepage_src(element, str){
    document.getElementById( element ).src = str; }

function wazaa( base )
{
  xmlhttpPost( base + "fn_ajax.php?wazaa", "html", "wazaa" );
}

function link_nav( where ) {
  if ( typeof where == "undefined" ) return;

  link = "#";

  if ( where == "previous" && typeof $('#nav_prev').attr('href') != "undefined" )
    link = $('#nav_prev' ).attr('href');
  if ( where == "next" && typeof $('#nav_next').attr('href') != "undefined" )
    link = $('#nav_next').attr('href');

  //alert( link );

  if ( link != "#" ) {
    console.log( "niih" );
    //$(document).append( "<div id='loading_rotator'><img src='/images/loading_image.gif' alt='' /></div>" );
    $('<div id=\"loading_rotator\">' + "<img src='/images/loading_image.gif' alt='' />" + '</div>').appendTo('body');
    centerOverlay(loading_rotator);
    $("#overlay-curtain").height( $(document).height() ).fadeIn();
  }

  change( link );
}

$(window).resize(function() {
  //centerOverlay(loading_rotator);
});

function centerOverlay( container ) {
  if ( $(container).length == 0 ) return;
  var overlay_width = ($(container).outerWidth() > $(window).width-60) ? $(window).width-60 : $(container ).outerWidth(); // overlay width
  $(container).
    css({
      left: $(window).width()/2 - overlay_width/2,
      top:  $(window).height()/2 - $(container).outerHeight()/2
  }); // set the position for the container
}

function isSet( muutuja ) {
  if ( typeof muutuja == "undefined ")
    return false;
  else
    return muutuja;
}

function grabParam(name,url){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( url );
  return ( results == null ) ? false : results[1];
}

$(document).unbind('keydown').keydown(function(e) {
  if( $('#img_container').is(":visible") && !$('#kommentaar').hasClass('has-focus') && !$(".inline_editable_textarea").hasClass('has-focus') ) {
      if (e.keyCode == 37) { link_nav('previous'); return false; }
      if (e.keyCode == 39) { link_nav('next'); return false; }
  }
});

$(document).ready(function()
{
  $('[title]').qtip({
    style: {
      classes: 'ui-tooltip-blue ui-tooltip-shadow ui-tooltip-rounded'
    },
    position: {
      my: 'top center',
      at: 'bottom center',
      viewport: $(window) // Attempt to keep it on screen at all times
    },
    show: {
      delay: 500
    }
  });
});

function scrollWin(){ /* useless */
  return; 
  position = $("#infodiv_container").offset().top - 5;
  console.log( "Moving to " + position + "!" );
  $('html, body').animate({
    scrollTop: position
  }, 1);
}

