/******************************************************************************
 * Jelen programkód tulajdonosa: FotoMarket Kft.                              *
 *                                                                            *
 * A kód részének vagy egészének felhasználása a tulajdonos írásos engedélye  *
 * nélkül SZIGORÚAN TILOS!                                                    *
 *                                                                            *
 * Copyright (c) FotoMarket Kft. 2006-2008. Minden jog fenntartva.            *
 ******************************************************************************/

String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
        return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
        return this.replace(/\s+$/,"");
}

function addCurrentImageToCart(producttype) {
  var pictureid = $("#picturedata_id").text();
  if (pictureid) {
    addToCart(pictureid, false, producttype);
  }
}
/*
function assignAjaxPager() {
  $("div.pager span a,div.pagerbottom a.nextlink,div.pagerbottom a.prevlink").remote("div#contentpanel_body", {hashPrefix: "page-", useAttr: "id"}, initContents);

  $("#page").ScrollTo('normal');
}
*/
var slideshowtimeout = 0;

function startSlideShow() {
  $(".nav_next").click();
  slideshowtimeout = setTimeout(startSlideShow, 3000);
}

function stopSlideShow() {
  if (slideshowtimeout) {
    clearTimeout(slideshowtimeout);
  }
}

var inlineslideshowtimeout = 0;

function startInlineSlideShow() {
  $(".inlinenavigation .nav_next").click();
  inlineslideshowtimeout = setTimeout(startInlineSlideShow, 3000);
}

function stopInlineSlideShow() {
  if (inlineslideshowtimeout) {
    clearTimeout(inlineslideshowtimeout);
  }
}

function initInlineWindow() {
  $(".inlinenavigation a").unbind();
  $(".inlinenavigation a.nav_first,.inlinenavigation a.nav_prior,.inlinenavigation a.nav_next,.inlinenavigation a.nav_last").click(function() {
    $('.inlinewindow_content').html("").load(this.href, {}, initInlineWindow);
    return false;
  });
  $(".inlinenavigation a.nav_start").click(function() { startInlineSlideShow(); return false;});
  $(".inlinenavigation a.nav_stop").click(function() { stopInlineSlideShow(); return false;});
}

function showPreviewWindow(h) {
  stopSlideShow();
  stopInlineSlideShow();
  $('.inlinewindow_content').html("").load(h.t.href.replace("viewlarge", "ajax"), {}, initInlineWindow);
  h.w.slideDown("slow");
}

function hidePreviewWindow(h) {
  stopInlineSlideShow();
  h.w.fadeOut("slow", function(){
    h.o.remove();
  });
}

function showExifWindow(h) {
  stopSlideShow();
  stopInlineSlideShow();
  $('.exifwindow_content').html("").load(h.t.href);
  h.w.slideDown("slow");
}

function hideExifWindow(h) {
  h.w.fadeOut("slow", function(){
    h.o.remove();
  });
}

function initContents() {
  $("img#previewpicture").load(function(){
    $("div#previewpicture_loading").hide();
    $("div#previewpicture_content").show();
  });

//  assignAjaxPager();

  var currentpicturenumber = $("#picturenumber").text();
  if (currentpicturenumber == "- / ") {
    $("#picturenumber").hide();
  }

  $("a.nav_start").click(function() { startSlideShow(); return false; });
  $("a.nav_stop").click(function() { stopSlideShow(); return false; });

  $("#picturepropertiestable td.picturedata:empty, #pictureexifdatatable td.exifdata:empty").parent().hide();
  $("#albumpropertiesdata span:empty").parent().remove();
  if ($("#pictureexifdatatable tr:visible").size() == 0) {
    $("#pictureexifdatatable").hide();
    $(".exifdatalink").hide();
    $(".noexifdata").hide();
  } else {
    $("#pictureexifdatatable").show();
    $(".exifdatalink").show();
    $(".noexifdata").hide();
  }

  $(".adplace-728-90")
    .insertBefore(".pager");
}

var tempcaptiontext = '';
function bindInlineForm() {
  $("div.inlinewindow_content").find("form").ajaxForm({
    beforeSubmit: function(formdata, formobject, options) {
      tempcaptiontext = $("div.inlinewindow_header_caption").html();
      $("div.inlinewindow_header_caption").html("Az adatok mentése folyamatban...");
      $("div.inlinewindow_content").hide();
      $("div.inlinewindow_content_loading").show();
    },
    success: function(content){
      if (content == "EXIT") {
        $(".jqmOverlay").remove();
        $("div#inlinewindow").jqmHide();
      } else {
        $("div.inlinewindow_header_caption").html(tempcaptiontext);
        $("div.inlinewindow_content").html(content);
        $("div.inlinewindow_content").show();
        $("div.inlinewindow_content_loading").hide();
        bindInlineForm();
      }
    }
  });
}

function showInlineWindow(hash) {
  var ajaxcontent = '';
  var caption = '';
  var currentpictureid = $("#picturedata_id").text();

  var button = $(hash.t);
  var parameters = button.attr("id").split("_");
  switch(parameters[0]) {
    case "insertalbumcode": {
      caption = "Hivatkozás beillesztése másik weboldalba";
      ajaxcontent = "/?get=/viewpicture/pictureid/" + currentpictureid + "/func/generateinsertcode";

      break;
    }

    case "viewcomments": {
      caption = "A képhez eddig beküldött hozzászólások";
      ajaxcontent = "/?get=/viewpicture/pictureid/" + currentpictureid + "/func/comments";

      break;
    }

    default: {

    }
  }

  if (ajaxcontent) {
    $("div.inlinewindow_header_caption").html("Kérem várjon!");
    $("div.inlinewindow_content_loading").show();

    $("div.inlinewindow_content").html("").hide().load(ajaxcontent + '&' + new Date().getTime(), {}, function(){
      $("div.inlinewindow_header_caption").html(caption);
      $("div.inlinewindow_content_loading").hide();
      $("div.inlinewindow_content").show();
      bindInlineForm();
    });
  }

  hash.w.show();
}

$(document).ready(function(){
  initContents();

  $(document).bind('keyup', function(e){
    // idea from Szantó Gyula, big thanks! :)
    e = e || window.event;
    var a=e.keyCode;
    if (a==37) {
      $(".nav_prior").click();
    } else if (a==39) {
      $(".nav_next").click();
    }

    return false;
  });

  $("a.nav_first,a.nav_prior,a.nav_next,a.nav_last").click(function() {
    $("div#previewpicture_loading").height($("div#previewpicture_content").height());

    $("div#previewpicture_loading").show();
    $("div#previewpicture_content").hide();
    $.get(this.href, {}, function(updatedata) {
      var currentpage = $("#current_page").text();

      setTimeout(function() {
        $("div#previewpicture_loading").hide();
        $("div#previewpicture_content").show();
      }, 8000);
      if (currentpage != "viewlarge") {
        $("#picturepropertiestable td.picturedata, #contentpanel_body #pictureexifdatatable td.exifdata").empty().parent().show();
        $(".exifdatalink").show();
        $(".noexifdata").hide();
      }

      $("pictureinfos/picturedatas/picturedata", updatedata).each(function(){
        var currentnode = $(this);
        if (currentnode.attr("id") == "size") {
          $("#picturedata_" + currentnode.attr("id")).html(currentnode.text() + " KB");
        } else if (currentnode.attr("id") == "viewcounter") {
          $("#picturedata_" + currentnode.attr("id")).html(currentnode.text() + " alkalommal");
        } else {
          $("#picturedata_" + currentnode.attr("id")).html(currentnode.text().trim());
        }
      });

      $("pictureinfos/exifdatas/exifdata", updatedata).each(function(){
        var currentnode = $(this);
        $("#exifdata_" + currentnode.attr("id")).html(currentnode.text().trim());
      });

      if (currentpage != "viewlarge") {
        $("#picturepropertiestable td.picturedata:empty, #contentpanel_body #pictureexifdatatable td.exifdata:empty").parent().hide();
        if ($("#contentpanel_body #pictureexifdatatable tr:visible").length == 0) {
          $(".exifdatalink").hide();
        }
      }

      var currentpictureid = $("#picturedata_id").text();
      $("img#previewpicture").attr("src", $("pictureinfos/updatecontent/picturepath", updatedata).text().trim());
      $("a#previewpicture_link,a#zoomimage").attr("href", "/?get=/viewlarge/pictureid/" + currentpictureid);
      $(".nav_prior").attr("href", "/?get=/" + currentpage + "/pictureid/" + $("pictureinfos/updatecontent/priorpictureid", updatedata).text().trim());
      $(".nav_next").attr("href", "/?get=/" + currentpage + "/pictureid/" + $("pictureinfos/updatecontent/nextpictureid", updatedata).text().trim());
      $("#picturenumber").html($("pictureinfos/updatecontent/imageindex", updatedata).text()).show();
      $(".exifdatalink").attr("href", "/?get=/exifdata/pictureid/" + currentpictureid);
    });

    return false;
  });

  $("#inlinewindow").jqm({
    trigger: ".inlinewindowopen",
    closeClass: "closelink",
    zIndex: 5000,
    onShow: showInlineWindow/*,
    onHide: hideExifWindow*/
  });

  $("#exifwindow").jqm({
    trigger: ".exifdatalink",
    closeClass: "closelink",
    zIndex: 5000,
    onShow: showExifWindow,
    onHide: hideExifWindow
  });

  $("#cse-search-box input")
    .removeAttr("style");

  $(".google_adsense_middlecol iframe")
    .attr("width", 426);

  $.ajaxHistory.initialize(function() {
    $("div#contentpanel_body").load("/", {}, initContents);
  });
});
