Array.prototype.in_array = function(p_val) {
  for(var i = 0, l = this.length; i < l; i++) {
	  if(this[i] == p_val) {
      return true;
	  }
  }
  return false;
}

Array.prototype.array_key_exists = function(key) {
  return (typeof this[key] != 'undefined');
}

Array.prototype.array_key_search = function(value) {
  for(key in this) {
    if(this[key] == value) return key;
  }
  return false;
}

// overeni platnosti emailu
function isEmail(value) {
  re = /^[-a-z0-9!#$%&\'*+/=?^_`{|}~]+(\.[-a-z0-9!#$%&\'*+/=?^_`{|}~]+)*@([^.]+[.])+[a-z]{2,3}$/;
  return value.search(re) == 0;
}

// overeni platnosti telefonu
function isPhone(value) {
	//if (value == '+420') return true;
  re = /^\+[0-9]{12}$/;
  return value.search(re) == 0;
}

// overeni platnosti telefonu
function isUrl(value) {
  if(value == 'http://') return true;
  re = /^\http:\/\/([a-z0-9-_]+\.)*[a-z0-9-_]+\.[a-z]{2,4}([^.]+)*$/;
  return value.search(re) == 0;
}

// overeni cisla
function isInt(value) {
  re = /^[0-9]+$/;
  return value.search(re) == 0;
}

// overeni data
function isDate(value) {
  re = /^[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}( [0-9]{1,2}:)*([0-9]{1,2}:)*([0-9]{1,2})*$/;
  return value.search(re) == 0;
}

// nastaveni timeoutu
$.fn.wait = function(time, type) {
  time = time || 1000;
  type = type || "fx";
  return this.queue(type, function() {
    var self = this;
    setTimeout(function() {
        $(self).dequeue();
    }, time);
  });
};

function bindLightboxEvents() {
  // external link
  $("a.external").attr('target', '_blank');
  
  // close lightbox
  $(".lightbox .close, .lightbox .overlay").live("click", function() { data_handler('lightbox_remove'); return false; });
}

function data_handler(type, data, layer) {
  switch(type) {
    case 'lightbox_create':
      $(".lightbox").remove();
      $("body").append(data);
      $("body").find(".lightbox").show().find(".content").hide().fadeIn("normal");
      $("body").find(".lightbox .overlay").hide().fadeIn("normal", function() {  bindLightboxEvents(); });
      break;
  
    case 'lightbox_remove':
      $(".lightbox").find(".content, .loading").fadeOut("normal", function() { $(this).parents(".lightbox").remove(); });
      $(".lightbox").find(".overlay").fadeOut();
      $("body").css({ overflow: "visible", height: "auto" });
      break;
      
    case 'lightbox_loading':
      if(data == 'off') {
        $(".lightboxLoading").remove();
      } else {
        $("body").append('<div class="lightbox lightboxLoading"><div class="loading"></div><div class="overlay"></div></div>');
        $(".lightboxLoading").click(function() { data_handler('lightbox_remove'); return false; });
      }
  }
}
