/*
 * todo
 * zjistit, zda je k dispozici css, pripadne disablnout imagine a barimage
 *
 */
if (!Array.prototype.some)
{
   Array.prototype.some = function(fun /*, thisp*/) {
      var len = this.length;
      if (typeof fun != "function") throw new TypeError();
      var thisp = arguments[1];
      for (var i = 0; i < len; i++) {
         if (i in this && fun.call(thisp, this[i], i, this)) return true;
      }
      return false;
   };
}

Array.prototype.find = function(value)
{
   for (var i = 0, li = this.length; i < li; i++) {
      if (this[i] == value) return true;
   }
   return false;
};

String.prototype.replace = function(pattern, replacement)
{
   return this.split(pattern).join(replacement);
};

String.prototype.has = function()
{
   var pattern = arguments[0]; /* todo: vice argumentu? */
   return ((" " + this + " ").search(pattern) > -1) ? true : false;
}

String.prototype.add = function()
{
   var list = this.split(" ");
   var items = new Object();
   for (var i = 0, li = list.length; i < li; i++) {
      items[list[i]] = true;
   }
   for (var i = 0, li = arguments.length; i < li; i++) {
      var args = arguments[i].split(" ");
      for (j = 0, lj = args.length; j < lj; j++) {
         items[args[j]] = true;
      }
   }
   list = new Array();
   if (items[""]) delete items[""];
   var i = 0;
   for (var item in items) list[i++] = item;
   return list.join(" ");
}

String.prototype.remove = function()
{
   var list = this.split(" ");
   var items = new Object();
   for (var i = 0, li = list.length; i < li; i++) {
      items[list[i]] = true;
   }
   if (items[""]) delete items[""];
   for (var i = 0, li = arguments.length; i < li; i++) {
      var args = arguments[i].split(" ");
      for (j = 0, lj = args.length; j < lj; j++) {
         delete items[args[j]];
      }
   }
   list = new Array();
   var i = 0;
   for (var item in items) list[i++] = item;
   return list.join(" ");
}

function ctoa(c)
{
   var a = new Array();
   for (var i = 0, j = 0, li = c.length; i < li; i++) a[j++] = c[i];
   return a;
}

function sprintf()
{
   if (!arguments || arguments.length < 1) return;
   if (arguments[0] == "") {
      var a = new Array();
      for (var i = 0; i < arguments.length; i++) a[a.length] = arguments[i];
      return a.join(" ");
   }
   if (!RegExp) return;
   var str = arguments[0];
   var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
   var a = b = [], numSubstitutions = 0, numMatches = 0;
   while (a = re.exec(str)) {
      var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
      var pPrecision = a[5], pType = a[6], rightPart = a[7];
      numMatches++;
      if (pType == '%') {
         subst = '%';
      }
      else {
         numSubstitutions++;
         if (numSubstitutions >= arguments.length) {
            warning("sprintf", "Not enough function arguments (" + (arguments.length - 1) + ", excluding the string) for the number of substitution parameters in string (" + numSubstitutions + " so far).");
         }
         var param = arguments[numSubstitutions];
         var pad = '';
         if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
         else if (pPad) pad = pPad;
         var justifyRight = true;
         if (pJustify && pJustify === "-") justifyRight = false;
         var minLength = -1;
         if (pMinLength) minLength = parseInt(pMinLength);
         var precision = -1;
         if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
         var subst = param;
         if (pType == 'b') subst = parseInt(param).toString(2);
         else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
         else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
         else if (pType == 'u') subst = Math.abs(param);
         else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
         else if (pType == 'o') subst = parseInt(param).toString(8);
         else if (pType == 's') subst = param;
         else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
         else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
      }
      str = leftpart + subst + rightPart;
   }
   return str;
}

function warning(source, message)
{
   alert(source + ": " + message);
}

function alerts()
{
   if (!arguments || arguments.length < 1) return;
   if (arguments.length == 1 && typeof arguments[0] == "object") {
      var a = new Array();
      for (var key in arguments[0]) a[a.length] = key + ": " + arguments[0][key];
   }
   else {
      var a = new Array();
      for (var i = 0; i < arguments.length; i++) a[a.length] = arguments[i];
   }
   alert(a.join(", "));
}

function hasClass(e)
{
   if (!e || typeof e.className == "undefined") return;
   var list = e.className.split(" ");
   for (var i = 1, li = arguments.length; i < li; i++) {
      for (var j = 0, lj = list.length; j < lj; j++) {
         if (arguments[i] == list[j]) return arguments[i];
      }
   }
   return null;
}

function addClass2(e, list)
{
   if (!e || typeof e.className == "undefined") return;
   e.className = e.className.add(list);
}

function removeClass2(e, list)
{
   if (!e || typeof e.className == "undefined") return;
   e.className = e.className.remove(list);
}

function getParentByTagName(e, tag)
{
   while (e && e.tagName != tag) e = e.parentNode;
   return e;
}

function getElementsByTagNames(e, depth)
{
   if (!e) return;

   if (arguments.length == 3 && typeof arguments[1] == "object") {
      var tags = arguments[2];
   }
   else {
      var tags = new Object();
      for (var i = 2, li = arguments.length; i < li; i++) tags[arguments[i]] = true;
   }

   var c = 0;
   var childs = new Array();
   if (tags[e.tagName]) childs[c++] = e;

   if (--depth == 0) return childs;

   if (e.childNodes) {
      for (var i = 0, li = e.childNodes.length; i < li; i++) {
         childs = childs.concat(getElementsByTagNames(e.childNodes[i], depth, tags));
      }
   }

   return childs;
}

function pageLeft()
{
   return window.pageXOffset != null ? window.pageXOffset : document.body != null ? document.body.scrollLeft : null;
}

function pageTop()
{
   return window.pageYOffset != null ? window.pageYOffset : document.body != null ? document.body.scrollTop : null;
}

function pageRight()
{
   return window.innerWidth != null ? window.innerWidth : document.body != null ? document.body.clientWidth : null;
}

function pageBottom()
{
   return window.innerHeight != null ? window.innerHeight : document.body != null ? document.body.clientHeight : null;
}

function maximize(e)
{
   //todo
}

function center(e)
{
   //e.style.position = "absolute";

   var left = ($(window).width() - $(e).outerWidth()) / 2 + $(window).scrollLeft();
   if(left < 0) left = 0;
   e.style.left = left +"px";

   var top = ($(window).height() - $(e).outerHeight()) / 2 + $(window).scrollTop();
   if(top < 0) top = 0;
   e.style.top = top +"px";
}

Date.prototype.getWeek = function()
{
   var onejan = new Date(this.getFullYear(), 0, 1);
   return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
}

/************************************************************************************/

stepisko = new Object();

stepisko.init = function()
{
   /*
   $("#top a.over").each(function() {
      var top = this;
      var body = $("#body a.over[@name=" + top.name + "]")[0];
      if (!body) return;

      body.Over = top;
      body.onmouseover = function() { addClass2(this.Over, "hover") };
      body.onmouseout = function() { removeClass2(this.Over, "hover") };

      top.Over = body;
      top.onmouseover = function() { addClass2(this.Over, "hover") };
      top.onmouseout = function() { removeClass2(this.Over, "hover") };
   });
   */

   $(".mailing").each(function() {
      if (this.href) {
         this.href = encodeURI(decodeURI(this.href).replace("{a}", "@"));
      }
      if (this.title) this.title = this.href.replace("(a)", "@");
      this.innerHTML = this.innerHTML.replace("(a)", "@");
   });

   for (var i = 1, suffix = ""; i <= 2; i++, suffix = i) {
      var infotext = document.getElementById("infotext" + suffix);
      if (infotext) {
         infotext.Default = infotext.innerHTML;
         $("a.infotext" + suffix).each(function() {
            this.Infotext = infotext;
            if (typeof this.title != "string" || this.title == "") this.title = "?";
            this.onmouseover = function() { stepisko.overInfotext(this) };
            this.onmouseout = function() { stepisko.outInfotext(this) };
         });
      }
   }

   stepisko.barImage = document.getElementById("barimage");
   if (stepisko.barImage) {
      $("a.barimage").each(function() {
         this.Image = new Image();
         this.Image.src = this.href;
         this.onclick = function() { return stepisko.selectBarImage(this) };
         if (hasClass(this, "active")) stepisko.barLink = this;
      });
      if (stepisko.barLink && stepisko.barImage.offsetHeight != stepisko.barImage.height) {
         removeClass2(stepisko.barLink, "active");
      }
   }

   stepisko.initImagine();
}

stepisko.initImagine = function(e)
{
   stepisko.imagine = jQuery('<div id="imagine"><div><a href="" class="prev" onclick="return stepisko.prevImagine()">Previous</a> | <a href="" class="next" onclick="return stepisko.nextImagine()">Next</a> | <a href="" class="enable" onclick="return stepisko.closeImagine()">Close</a></div><img alt=""/></div>');
   stepisko.imagine.Image = $("img", stepisko.imagine);
   stepisko.imagine.Prev = $("a.prev", stepisko.imagine);
   stepisko.imagine.Next = $("a.next", stepisko.imagine);
   stepisko.imagine.css({ display: "none", position: "absolute", top: 0, left: 0, zIndex: 999 });
   //stepisko.imagine.click(function() { return stepisko.closeImagine() });
   $("body").append(stepisko.imagine);

   stepisko.above = jQuery('<div id="above"></div>');
   stepisko.above.css({ display: "none", position: "absolute", top: 0, left: 0, zIndex: 998 });
   stepisko.above.click(function() { return stepisko.closeImagine() }).attr("title", "Close Preview");
   $("body").append(stepisko.above);

   var prev = null;
   $("a.preview").each(function() {
      this.Prev = prev;
      this.Next = null;
      if (prev) prev.Next = this;
      prev = this;
      this.Image = new Image();
      this.Image.src = this.href;
      $(this).click(function() { return stepisko.openImagine(this) });
   });
}

stepisko.escapeImagine = function(e)
{
   var key = (window.event) ? event.keyCode : e.keyCode;
   if (key == (window.event) ? 27 : e.DOM_VK_ESCAPE) stepisko.closeImagine();
}

stepisko.openImagine = function(e)
{
   stepisko.above.show();
   stepisko.imagine.fadeIn(250);
   return stepisko.setImagine(e);
}

function loaded(img)
{
   return (!img.complete) ? false : !(typeof img.naturalWidth != "undefined" && img.naturalWidth == 0);
}

stepisko.setImagine = function(e)
{
   if (!e) return false;

   stepisko.imagine.Image.attr("src", e.Image.src);
   if (false) { //todo: kontrola na zapnute obrazky a styl
      stepisko.closeImagine();
      return true;
   }

   stepisko.imagine.Actual = e;

   stepisko.above.width(0);
   stepisko.above.height(0);
   stepisko.imagine.width(0);
   stepisko.imagine.css("left", 0);

   stepisko.above.width($(document).outerWidth({ margin: true }));
   stepisko.above.height($(document).outerHeight({ margin: true }));
   stepisko.imagine.width($(stepisko.imagine.Image).outerWidth({ margin: true }));
   stepisko.imagine.css("left", Math.ceil(($(document).innerWidth() - stepisko.imagine.innerWidth()) / 2));

   stepisko.imagine.Prev2 = e.Prev;
   if (e.Prev) {
      stepisko.imagine.Prev.addClass("enable");
      stepisko.imagine.Prev.attr("href", e.Prev.Image.src);
   }
   else {
      stepisko.imagine.Prev.removeClass("enable");
      stepisko.imagine.Prev.attr("href", "");
   }

   stepisko.imagine.Next2 = e.Next;
   if (e.Next) {
      stepisko.imagine.Next.addClass("enable");
      stepisko.imagine.Next.attr("href", e.Next.Image.src);
   }
   else {
      stepisko.imagine.Next.removeClass("enable");
      stepisko.imagine.Next.attr("href", "");
   }

   return false;
}

stepisko.prevImagine = function(e)
{
   document.body.focus();
   return stepisko.setImagine(stepisko.imagine.Prev2);
}

stepisko.nextImagine = function(e)
{
   document.body.focus();
   return stepisko.setImagine(stepisko.imagine.Next2);
}

stepisko.closeImagine = function(e)
{
   stepisko.imagine.fadeOut(250, function() {
      stepisko.imagine.Image.attr("src", "");
      stepisko.above.hide();
   });
   return false;
}

stepisko.overHover = function(e)
{
   if (!e.Over) return;
   addClass2(e.Over, "hover");
}

stepisko.overHout = function(e)
{
   if (!e.Over) return;
   removeClass2(e.Over, "hover");
}

stepisko.overInfotext = function(e)
{
   e.Infotext.innerHTML = e.Infotext.Default + " " + e.title;
}

stepisko.outInfotext = function(e)
{
   e.Infotext.innerHTML = e.Infotext.Default;
}

stepisko.selectBarImage = function(e)
{
   if (stepisko.barLink) removeClass2(stepisko.barLink, "active")
   if (stepisko.barImage.offsetHeight != stepisko.barImage.height) return true;
   stepisko.barLink = e;
   addClass2(stepisko.barLink, "active");
   stepisko.barImage.src = e.Image.src;
   return false;
}

$(document).ready(function () {
   stepisko.init();
});
