/******************************************************************************
 * 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#contentbody_content", {hashPrefix: "page-", useAttr: "id"}, initContents);

  $("#page").ScrollTo('normal');
}

var slideshowtimeout = 0;

function startSlideShow() {
  $(".nav_start").addClass("nav_start_selected");
  $("img#previewpicture").bind('load', timeNextSlide);
  $(".nav_next").click();
}

function stepSlideShow() {
  if (slideshowtimeout) {
    $(".nav_next").click();
  }
}

function timeNextSlide() {
  slideshowtimeout = setTimeout(stepSlideShow, 3000);
}

function stopSlideShow() {
  if (slideshowtimeout) {
    $(".nav_start").removeClass("nav_start_selected");
    $("img#previewpicture").unbind('load', timeNextSlide);
    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; });

  $(".picturedata_value:empty").parent().hide();
  if ($("#exifdata p:visible").size() == 1) {
    $(".exifpanel").hide();
  } else {
    $(".exifpanel").show();
  }

  $("div.datapanel h3 a").click(function() {
    var handledbody = $(this).parent().parent().find(".datapanel_body").slideToggle().attr("id");
    $(this).parent().parent().toggleClass("datapanel_contentclosed");

    handledbody = "albumview_" + handledbody;
    var currentvalue = $.cookie(handledbody);
    $.cookie(handledbody, (currentvalue == "off" ? "on" : "off"));

    return false;
  });

  $(".datapanel_body").each(function() {
    if ($.cookie("albumview_" + $(this).attr("id")) == "off") {
      $(this).hide().parent().addClass("datapanel_contentclosed");
    }
  });
}

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();
}

function hideInlineWindow(h) {
  h.w.fadeOut("slow", function(){
    h.o.remove();
  });
}

function loadComments() {
  var currentpage = $("#current_page").text();
  if (currentpage == "viewlarge") {
    return;
  }

  $("#comments").load("/?get=/getcomments/pictureid/" + $("#picturedata_id").text(), {}, function() {
    $(".comments_newcomment_fieldset legend a").click(function() {
      $(".comments_newcomment_fieldset form").slideToggle();

      return false;
    });

    $("form#newcommentform").ajaxForm({
      success: function(content){
        loadComments();
      }
    });
  });
}

$(document).ready(function(){
  initContents();

  $("a.nav_first,a.nav_prior,a.nav_next,a.nav_last").click(function() {
    $("div#previewpicture_loading").height($("div#previewpicture_content").height());

    $("div#previewpicture_content").hide();
    $("div#previewpicture_loading").show();
    $.get(this.href, {}, function(updatedata) {
      var currentpage = $("#current_page").text();

      setTimeout(function() {
        $("div#previewpicture_loading").hide();
        $("div#previewpicture_content").show();
      }, 8000);
      $(".picturedata_value").parent().show();

      $("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());
      });

      $(".picturedata_value:empty").parent().hide();
      if ($("#exifdata p:visible").size() == 1) {
        $(".exifpanel").hide();
      } else {
        $(".exifpanel").show();
      }

      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);

      loadComments();
    });

    return false;
  });

  loadComments();

  $(".albuminvite_header .albuminvitelink a").click(function() {
    $(".albuminvite_body").slideToggle();
    $(".albuminvite_header").toggleClass("albuminvite_header_open");

    return false;
  });

  $("#inlinewindow").jqm({
    trigger: ".inlinewindowopen",
    closeClass: "closelink",
    zIndex: 5000,
    onShow: showInlineWindow,
    onHide: hideInlineWindow
  });

  $("#exifwindow").jqm({
    trigger: ".exifdatalink",
    closeClass: "closelink",
    zIndex: 5000,
    onShow: showExifWindow,
    onHide: hideExifWindow
  });

  $.ajaxHistory.initialize(function() {
    $("div#contentbody").load("/", {}, initContents);
  });
});
