/******************************************************************************
 * 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.            *
 ******************************************************************************/
function changeColorset(colorsetfile) {
  var replaceregexp = new RegExp("([^/]+\.css)", "i");
  var oldcolorset = $("#colorsetlink").attr("href");
  var newcolorset = oldcolorset.replace(replaceregexp, colorsetfile);
  $("#colorsetlink").attr("href", newcolorset);

  var parts = newcolorset.split('/');
  $.cookie("colorset", parts[parts.length-1], {expires: 3, path: '/', domain: basedomain});

  var replaceregexp = new RegExp("([^/]+\.ico)", "i");
  var oldcolorset = $("#colorsetfavicon").attr("href");
  var newcolorset = oldcolorset.replace(replaceregexp, 'favicon_' + colorsetfile.replace(".css", ".ico"));
  $("#colorsetfavicon").remove();
  $("head").append('<link id="colorsetfavicon" type="image/vnd.microsoft.icon" href="' + newcolorset + '" rel="shortcut icon" />');
}

function bindInlineWindow() {
  $("div#inlinewindow").jqm({
    trigger: ".inlinewindowopen",
    closeClass: "closelink",
    overlay: 60,
    modal: true,
    onShow: showInlineWindow
  });
}

function bindInlineForm() {
  $(".integerinput").numeric();
  $("#albumyear").keyup(function(){
    checkInputLength(this);
  });
  $("#albummonth").keyup(function(){
    checkInputLength(this);
  });
  $("#albumday").keyup(function(){
    checkInputLength(this);
  }).datePicker({
    startDate: '1900-01-01',
    clickInput: false
  }).bind('dateSelected', function(event, date, td, status) {
    var selecteddate = new Date(date);
    var dateparts = selecteddate.asString().split('-');
    $("#albumyear").val(dateparts[0]);
    $("#albummonth").val(dateparts[1]);
    $("#albumday").val(dateparts[2]);
  });

  $("div.inlinewindow_content").find("input,select,textarea").hover(function(){
    hoverInlineWindowItem(this, true);
  }, function(){
    hoverInlineWindowItem(this, false);
  }).end().find("img.formcontrolhelpicon").hide().end().
  find("form").ajaxForm({
    beforeSubmit: function(formdata, formobject, options) {
      $("div.inlinewindow_header_caption").html("Az adatok mentése folyamatban...");
      $("div.inlinewindow_footer").html("Kérem várjon!");
      $("div.inlinewindow_content").hide();
      $("div.inlinewindow_content_loading").show();
    },
    success: function(content){
      if ((content.indexOf("albumproperties") != -1) || (content.indexOf("pictureproperties") != -1)) {
        $("div#mainmain div.content,div#contentbody").html(content).evalScripts();
        bindInlineWindow();
        $(".jqmOverlay").remove();
        $("div#inlinewindow").jqmHide();
      } else {
        $("div.inlinewindow_header_caption").html(tempcaptiontext);
        $("div.inlinewindow_footer").html("");
        $("div.inlinewindow_content").html(content);
        $("div.inlinewindow_content").show();
        $("div.inlinewindow_content_loading").hide();
        bindInlineForm();
      }
    }
  });
}

var tempstatustext = "";
var tempcaptiontext = "";

function showInlineWindow(hash) {
  var ajaxcontent = '';
  var caption = '';
  var footertext = '';

  hash.w.height(580).find(".inlinewindow_content").height(490);

  var button = $(hash.t);
  var parameters = button.attr("id").split("_");
  switch(parameters[0]) {
    case "editalbumdata": {
      caption = "Album adatainak módosítása";
      ajaxcontent = "/?get=/index/func/edit/albumid/" + parameters[1];

      break;
    }

    case "editalbumtemplate": {
      if (basedomain == "fotoalbum2.hu") {
        caption = "Sablon kiválasztása - Fotóalbum.hu oldalra";
      } else {
        caption = "Sablon kiválasztása";
      }
      ajaxcontent = "/?get=/index/func/selecttemplate/albumid/" + parameters[1];

      break;
    }

    case "editgalleryview": {
      caption = "Galéria nézet beállításai";
      ajaxcontent = "/?get=/index/func/selectgalleryview/albumid/" + parameters[1];

      break;
    }

    case "editalbumaccess": {
      caption = "Engedélyek kezelése";
      ajaxcontent = "/?get=/index/func/accessrights/albumid/" + parameters[1];

      break;
    }

    case "editalbumindexpic": {
      caption = "Kezdőkép kiválasztása";
      ajaxcontent = "/?get=/index/func/selectindexpicture/albumid/" + parameters[1];

      break;
    }

    case "editalbuminsertcode": {
      caption = "Hivatkozás beillesztése másik weboldalba";
      ajaxcontent = "/?get=/index/func/generateinsertcode/albumid/" + parameters[1];

      break;
    }

    case "movepics": {
      caption = "Album képeinek áthelyezése másik albumba";
      ajaxcontent = "/?get=/index/func/movepics/albumid/" + parameters[1];

      break;
    }

    case "deletepictures": {
      caption = "Képek törlése az albumból";
      ajaxcontent = "/?get=/index/func/deletepictures/albumid/" + parameters[1];

      break;
    }

    case "renamepics": {
      caption = "Képek csoportos átnevezése";
      ajaxcontent = "/?get=/index/func/renamepics/albumid/" + parameters[1];

      break;
    }

    case "modifydescs": {
      caption = "Képek leírásainak csoportos módosítása";
      ajaxcontent = "/?get=/index/func/modifydesc/albumid/" + parameters[1];

      break;
    }

    case "edittags": {
      caption = "Címkék csoportos módosítása";
      ajaxcontent = "/?get=/index/func/edittags/albumid/" + parameters[1];

      break;
    }

    case "reorderpictures": {
      caption = "Képek rendezése egyéni sorrendbe";
      ajaxcontent = "/?get=/index/func/reorderpictures/albumid/" + parameters[1];

      break;
    }

    case "editpicturedata": {
      caption = "Kép adatainak módosítása";
      ajaxcontent = "/?get=/editpics/func/edit/albumid/" + parameters[1] + "/pictureid/" + parameters[2];

      break;
    }

    case "editpicture": {
      caption = "Kép szerkesztése";
      ajaxcontent = "/?get=/editpics/func/editpicture/albumid/" + parameters[1] + "/pictureid/" + parameters[2];

      break;
    }

    default: {

    }
  }

  if (ajaxcontent) {
    tempstatustext = footertext;
    tempcaptiontext = caption;

    $("div.inlinewindow_header_caption").html("Kérem várjon!");
    $("div.inlinewindow_footer").html("Az adatok betöltése folyamatban...");
    $("div.inlinewindow_content_loading").show();

    $("div.inlinewindow_content").html("").hide().load(ajaxcontent + '?' + new Date().getTime(), {}, function(){
      $("div.inlinewindow_header_caption").html(caption);
      $("div.inlinewindow_footer").html(footertext);
      $("div.inlinewindow_content_loading").hide();
      $("div.inlinewindow_content").show();
      bindInlineForm();
    });
  }

  hash.w.show();
}

function hoverInlineWindowItem(element, hovered) {
  if (hovered) {
    tempstatustext = $("div.inlinewindow_footer").html();
    var footertext = $(element).attr("title") || $(element).parent().find("img.formcontrolhelpicon").attr("title");
    $("div.inlinewindow_footer").html(footertext);
  } else {
    $("div.inlinewindow_footer").html(tempstatustext);
  }
}

function checkInputLength(inputelement) {
  if (inputelement.value.length == inputelement.maxLength) {
    var checknext = inputelement.nextSibling;
    while ((checknext) && (checknext.nodeType != 1)) {
      checknext = checknext.nextSibling;
    }
    if (checknext) {
      if (checknext.select) {
        checknext.select();
      }
      if (checknext.focus) {
        checknext.focus();
      }
    }
  }
}

function initContents() {
  bindInlineWindow();

  $("a#prioralbum, a#nextalbum").click(function(){
    $("div#mainmain div.content,div#contentbody").load(this.href + '?' + new Date().getTime(), {}, function() {
      initContents();
    });

    return false;
  });

  $("p.albumdatatitle a").click(function() {
    var handledbody = $(this).parent().parent().find(".albumdatabody").slideToggle().attr("id");
    $(this).parent().parent().toggleClass("albummansectionclosed");

    var currentvalue = $.cookie(handledbody);
    $.cookie(handledbody, (currentvalue == "off" ? "on" : "off"));

    return false;
  });

  $("div.albumdatabody").each(function() {
    if ($.cookie($(this).attr("id")) == "off") {
      $(this).hide().parent().addClass("albummansectionclosed");
    }
  });
}

$(document).ready(function(){
  $.dpText.TEXT_CHOOSE_DATE = ' &nbsp;&nbsp;&nbsp;&nbsp; ';
  initContents();

  $("#addressbookform tr").hover(function() {
    $(this).addClass("tablerow_hover");
  }, function () {
    $(this).removeClass("tablerow_hover");
  }).find("td:gt(0)").click(function() {
    window.location.href= $(this).parent().find("td a").attr("href");
  });

  $("tr.commentlist_row").hover(function() {
    $(this).addClass("tablerow_hover").next().addClass("tablerow_hover");
  }, function () {
    $(this).removeClass("tablerow_hover").next().removeClass("tablerow_hover");
  }).find("table.commentlist_row_table td:gt(0)").click(function() {
    window.location.href= $(this).parent().find("td a:first").attr("href");
  });
  $("tr.commentlist_row_details").hover(function() {
    $(this).addClass("tablerow_hover").prev().addClass("tablerow_hover");
  }, function () {
    $(this).removeClass("tablerow_hover").prev().removeClass("tablerow_hover");
  }).find("td").click(function() {
    window.location.href= $(this).parent().prev().find("td a:first").attr("href");
  });
  $("#displaydetails").click(function() {
    if (this.checked) {
      $(".commentlist_row_details").show();
    } else {
      $(".commentlist_row_details").hide();
    }
  });

  $("h4:empty").remove();

  $(".toplist tbody tr").hover(function() {
    $(this).addClass("toplist_row_hover");
  }, function() {
    $(this).removeClass("toplist_row_hover");
  });

  $(".cameralist tbody td").hover(function() {
    $(this).addClass("hovercamerarow");
  }, function() {
    $(this).removeClass("hovercamerarow");
  }).click(function(){
    window.location.href = $(this).find("a").attr("href");
  });
});


