function $e(el)
{
  if (!el)
    return el;
  else if (typeof(el) == "string")
    return document.getElementById(el)
  else
    return el;
}

function mainWindowOpen(url)
{
  wnd = window.open(url, "main");
  if (wnd && wnd.focus)
    wnd.focus();
  return false;
}

function mySelClass()
{
  function action(action, id, loadContent)
  {
    var data = {};
    data['op'] = action;
    if (id)
      data['id'] = id;
    $("#hotlistPlaceholder").load("/?screen=accommodation&action=hotlist&response=slot", data);
  }
  this.action = action;

  function add(id)
  {
    this.action("add", id);
  }
  this.add = add;

  function loaded(ids)
  {
    $("a.selection").removeClass("disabled");
    if (ids)
		  for (var i in ids)
			  $("#hl_" + ids[i]).addClass("disabled");
  }
  this.loaded = loaded;
}

mySel = new mySelClass();


function FormPersistHelper()
{
  this.inputs = {};
  this.cookieParams = {};

  function inputOnBlur(el, e)
  {
    this.persist(el);
  }
  this.inputOnBlur = inputOnBlur;

  function addInput(el, prefix, cookieParams)
  {
    if (typeof(el) == "string")
      el = document.getElementById(el);

    if (!el || el.type == "hidden")
      return;

    var _this = this;
    var include = true;
    if (el.type == "text")
      el.onblur = function(e) { _this.inputOnBlur(this, (e ? e : event)); };
    else
      include = false;

    if (include) {
      var name = (prefix ? prefix : '') + el.name;
      el.fpName = name;
      el.fpCookieParams = cookieParams;
      this.inputs[name] = el;
    }
  }
  this.addInput = addInput;

  function regFormOnSubmit(_this, f)
  {
    f.fpPrevOnSubmit = f.onsubmit;
    f.onsubmit = function(e) {
      _this.persist();
      if (f.fpPrevOnSubmit)
        return f.fpPrevOnSubmit(e);
    };
  }
  this.regFormOnSubmit = regFormOnSubmit;

  function add(el, prefix, cookieParams)
  {
    if (el.tagName == "FORM") {
      var f = el;
      this.regFormOnSubmit(this, f);
      for (var i = 0; i < f.elements.length; i++) {
        el = f.elements[i];
        this.addInput(el, prefix, cookieParams);
      }
    } else
      this.addInput(el, prefix, cookieParams);
  }
  this.add = add;

  function remove(el)
  {
    if (typeof(el) == "string")
      el = document.getElementById(el);
    if (el.fpName)
      delete this.inputs[el.fpName];
  }
  this.remove = remove;

  function retrieve(el, replace)
  {
    if (el) {
      if (!el.value || replace)
        var v = $.cookie(el.fpName);
        if (v || v === '0')
          el.value = v;
    } else {
      for (var name in this.inputs)
        this.retrieve(this.inputs[name], replace);
    }
  }
  this.retrieve = retrieve;

  function persist(el)
  {
    if (el) {
      var cp = (el.fpCookieParams ? el.fpCookieParams : this.cookieParams);
      $.cookie(el.fpName, el.value, cp);
    } else {
      for (var name in this.inputs)
        this.persist(this.inputs[name]);
    }
  }
  this.persist = persist;

  function regOnUnload(_this)
  {
    window.fpPrevOnUnload = window.onunload;
    window.onunload = function() {
      _this.persist();
      if (window.fpPrevOnUnload)
        return window.fpPrevOnUnload();
    }
  }
  this.regOnUnload = regOnUnload;

  this.regOnUnload(this);
}
formPersistHelper = new FormPersistHelper();


function AppHelper()
{
  function getUrlFragment(url)
  {
    var url = ((url ? url : document.location) + "");
    var i = url.indexOf('#');
    if (i >= 0)
      return url.substr(i + 1);
    return false;
  }
  this.getUrlFragment = getUrlFragment;

  function showMap(id)
  {
    $('#cMap').html('<iframe class="map" frameborder="0" src="/?screen=accommodation&action=map&id=' + id + '"></iframe>');
  }
  this.showMap = showMap;

  function initAccommodationView(id)
  {
    tabberAutomatic(tabberOptions);

    var tabber = $('#tabber')[0].tabber;
    if (tabber) {
      tabber.onClick = function(args) {
        // args = {'tabber':self, 'index':tabberIndex, 'event':event};
        var t = args.tabber;
        var i = args.index;
        var div = this.tabs[i].div;
        if (div.id == 'tabMap' && !div.mapInitialized) {
          div.mapInitialized = true;
          appHelper.showMap(id);
        }
      };

      var listMapShownByFragment = (appHelper.getUrlFragment() == 'mapa' && $('#mapOverlay iframe')[0]);

      if (!listMapShownByFragment) {
        var tab = tabber.showTabForUrlFragment();
        if (tab) {
          if ($('#aTop')[0]) {
            $('#aTop')[0].scrollIntoView();
            setTimeout("$('#aTop')[0].scrollIntoView();", 100);
          }
          for (var i in tabber.tabs)
            if (tab == tabber.tabs[i])
              tabber.onClick({tabber: tabber, index: i, event: null});
        }
      }
    }
  }
  this.initAccommodationView = initAccommodationView;
}

appHelper = new AppHelper();



function MapHelper()
{
  this.p = null;
  this.r = null;
  this.mapControls = {};

  this.mapLoaded;

  this.hideFilterFormNesting = 0;

  function hideShowFilterForm(i)
  {
    if (this.p)
      return this.p.hideShowFilterForm(id);

    this.hideFilterFormNesting += i;

    if (!appHelper.lteIE6)
      return;

    if (this.hideFilterFormNesting > 0)
      $('.headerFilter .body').hide();
    else
      $('.headerFilter .body').show();
  }
  this.hideShowFilterForm = hideShowFilterForm;

  function showMapOverlay(filterParamsUrlQuery)
  {
    this.hideShowFilterForm(1);

    this.mapOverlayShown = true;

    this.animations = !$.browser.safari; // Transition hangs in Chrome (http://dev.jqueryui.com/ticket/4324)

    if (this.mapLoaded) {
      $('#mapOverlay').css('display', '');
      // $('#mapOverlay').removeClass('a0');
      var f = function() {
        $('#mapOverlay iframe').show();
      };
      if (this.animations) {
        $('#mapOverlay').addClass('a0');
        $('#mapOverlay').removeClass('a0', 250, f);
      } else
        f();
    } else {
      this.mapLoaded = true;
      var mo = $('#mapOverlay')[0];
      mo.className = "mapOverlay";
      var f = function() {
        $('#mapOverlay').html('<iframe frameborder="0" src="/?screen=accommodation&action=list&view=map' + (filterParamsUrlQuery ? '&' + filterParamsUrlQuery : '') + '"></iframe>');
      };
      if (this.animations) {
        $(mo).addClass('a0');
        $(mo).removeClass('a0', 250, f);
      } else
        f();
      // $('#mapOverlay').html('<div class="inner">inner</div>');
    }
  }
  this.showMapOverlay = showMapOverlay;

  function hideMapOverlay(anim, timed)
  {
    if (this.p)
      return this.p.hideMapOverlay(anim, timed);

    anim = false;
    if (anim !== false && this.mapOverlayShown) {
      if (timed !== false) {
        $('#mapOverlay iframe').hide();
        return setTimeout(function() {
          mapHelper.hideMapOverlay(true, false);
        }, 10);
      }
      return $('#mapOverlay').addClass('a0', 250, function() {
        mapHelper.hideMapOverlay(false);
      });
    }

    this.mapOverlayShown = false;

    this.hideShowFilterForm(-1);

    if (this.mapLoaded) {
      $('#mapOverlay').css('display', 'none');
      $('#mapOverlay iframe').hide();
    }
  }
  this.hideMapOverlay = hideMapOverlay;

  this.infoOverlayShown = false;

  function showInfoOverlay()
  {
    if (this.p)
      return this.p.showInfoOverlay();

    if (!this.infoOverlayShown)
      this.hideShowFilterForm(1);

    this.infoOverlayShown = true;

    var io = $('#infoOverlay')[0];
    io.className = "infoOverlay";
    io.style.display = '';
    return io;
  }
  this.showInfoOverlay = showInfoOverlay;

  function hideInfoOverlay()
  {
    if (this.p)
      return this.p.hideInfoOverlay();

    if (this.infoOverlayShown)
      this.hideShowFilterForm(-1);

    this.infoOverlayShown = false;

    $('#infoOverlay').css('display', 'none');
  }
  this.hideInfoOverlay = hideInfoOverlay;

  function showAccommodation(id)
  {
    if (this.p)
      return this.p.showAccommodation(id);

    $('#infoOverlayBody').html("");
    $('.infoOverlayInner').addClass("loading");
    this.showInfoOverlay();
    $('#infoOverlayBody').load("/ubytovanie/" + id + " #contentBody", {}, function() { mapHelper.accommodationLoaded(id); });
  }
  this.showAccommodation = showAccommodation;

  function accommodationLoaded(id)
  {
    $('.infoOverlayInner').removeClass("loading");
    appHelper.initAccommodationView(id);
  }
  this.accommodationLoaded = accommodationLoaded;

  function construct()
  {
    this.p = parent.mapHelper;
    this.r = (this.p ? this.p.r : this);

    document.onkeydown = function(e) {
      e = (e ? e : event);

      if (e.keyCode == 27) {
        var h = mapHelper.r;
        var iw;
        if (h.infoOverlayShown)
          h.hideInfoOverlay();
        else if (h.mapOverlayShown && h.mapControls["accList"] && h.mapControls["accList"].map && (iw = h.mapControls["accList"].map.getInfoWindow()) && !iw.isHidden())
          iw.hide();
        else if (h.mapOverlayShown)
          h.hideMapOverlay();
      }
    };
  }
  this.construct = construct;

  this.construct();
}

mapHelper = new MapHelper();


function FilterHelper() {

  function initStayToDate(el)
  {
    if ($e('iSfd').value && $e('iSfm').value && !$e('iStd').value && !$e('iStm').value) {
      $e('iStd').value = $e('iSfd').value;
      $e('iStm').value = $e('iSfm').value;
    }
  }
  this.initStayToDate = initStayToDate;

  function init()
  {
    var _this = this;
    $e('iSfd').onchange = function() {
      _this.initStayToDate(this);
    };
    $e('iSfm').onchange = $e('iSfd').onchange;
  }
  this.init = init;
}

filterHelper = new FilterHelper;



