if (typeof(Array.prototype.push) != 'function') {
  Array.prototype.push = function(o) {
    this[this.length] = o;
  };
}

if (typeof(String.prototype.trim) != 'function') {
  String.prototype.trim = function() {
    var _global = !_empty(arguments[0]);
    if (_global) {
      //this = this.replace(/\s/g, '');
      return this.replace(/\s/g, '');
    }
    else {
      //this = this.replace(/^\s*(\S.*\S)\s*$/, "$1");
      return this.replace(/^\s*(\S.*\S)\s*$/, "$1");
    }
  };
}

if (typeof($) != 'function') {
  $ = function() {
    var elements = new Array();
    for (var i=0; i<arguments.length; i++) {
      var element = arguments[i];
      if (typeof element == 'string')
        element = document.getElementById(element);
      if (arguments.length == 1)
        return element;
      elements.push(element);
    }
    return elements;
  };
}

_init = {
  'init': false,
  'functions': [],
  'load': function(op) {
      switch (typeof op) {
        case 'function':
          _init['register'](op);
          break;
        case 'boolean' || 'undefined':
          return op ? _init['invoke']() : _init['functions'];
          break;
      }
    },        
  'register': function(fn) {
      _init['functions'].push(fn);
    },
  'invoke': function() {
      for (var i=0; i<_init['functions'].length; i++) {
        _init['functions'][i]();
      }
      _init['init'] = true;
      return true;
    }
};

_load = _init['load'];
_loaded = function(op) { return _init['init'] };

window.name = self.location.host;

function init() {
  _load(true);
}

function _kill(evt) {
  evt = evt ? evt : window.event;
  if (evt && !_empty(evt.cancelBubble, false))
    evt.cancelBubble = true;
  return false;
}

function _empty(t, doValueCheck) {
  var test = t;

  if (test === null)
    return true;

  switch (typeof test) {
    case 'undefined':
      return true;
    case 'string':
      return doValueCheck ? test.trim(true) == '' : false;
    case 'number':
      return doValueCheck ? test == 0 : false;
    case 'boolean':
      return doValueCheck ? test : false;
    case 'function':
      return false;
    case 'object':
      if (_empty(test.value, doValueCheck)) return true;
      if (_empty(test.innerHTML, doValueCheck)) return true;
      if (_empty(test.length, doValueCheck)) return true;
  }

  return false;
} 

function _status(s) {
  var status = s === null || typeof(s) != 'string' ? '' : s;

  if (s !== null && typeof(s) == 'object') {
    var prop = ['title', 'href', 'alt'];
    for (var i=0; i<prop.length; i++) {
      if (typeof(s[prop[i]]) == 'string' && !_empty(s[prop[i]])) {
	  status = s[prop[i]];
        break;
      }
    }
  }

  window.status = s;
  return true;
}

function _err(msg) {
  alert(msg);
  return false;
}

function swap(img, src_old, src_new) {
  img.src = img.src.replace(eval('/' + src_old + '/'), src_new);
}

function hide(obj) {
  if (obj && obj.style) {
    if (_empty(obj.style.display) || obj.style.display != 'none')
      obj.style.display = 'none';
  }
}

function show(obj) {
  if (obj && obj.style) {
    if (_empty(obj.style.display) || obj.style.display != 'block')
      obj.style.display = 'block';
  }
}

function visibilize(o) {
  obj = $(o);
  if (obj && obj.style) {
    if (_empty(obj.style.visibility) || obj.style.visibility != 'visible')
      obj.style.visibility = 'visible';
  }
}

function invisibilize(o) {
  obj = $(o);
  if (obj && obj.style) {
    if (_empty(obj.style.visibility) || obj.style.visibility != 'hidden')
      obj.style.visibility = 'hidden';
  }
}

function _anchor(callingAnchorLink) {
  var blurb = $('splash-blurb');
  if (blurb) {
    blurb.style.display = 'none';
  }

  var containingAnchor = callingAnchorLink.parentNode;

  if (containingAnchor.id.slice(-7) == '-detail') {
    containingAnchor.style.visibility = 'hidden';
  }
  else {
    var targetAnchorDetail = $(containingAnchor.id + '-detail');

    if (targetAnchorDetail) {
      targetAnchorDetail.style.visibility = 'visible';
    }
  }

  return false;
}

function hide_multi(arr) {
  if (typeof(arr.length) == 'number') {
    for (var i=0; i<arr.length; i ++)
      hide($(arr[i]));
  }
}

function mail() {
  form = document.forms[0];
  if (form && !is_email(form.email))
    return _err("Please enter a valid email.");
  if (_empty(form.message))
    return _err("Please write a message if you're going to email us.");
  form.submit();
}

function is_valid_email(e) {
  var email = typeof(e) == 'string' ? e : !_empty(e.value) ? e.value : '';
  if (_empty(email)) return false;

  var _email = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return (email.match(_email) == null) ? false : true;
}

function _roomPop(o) {
	var w = window.open(o.href, '_RoomDetail', 'top=10,left=10,width=590,height=550,resizable');
	w.focus();
	return false;
}

window.onload = init;
