/*Copyright 2005-2099 Potentialist LLC*/

window.is_top=true;
function $(e) { return typeof e == 'string' ? document.getElementById(e) : e; }
function E(e) { return document.createElement(e); }

function show(id,s) {
  var l = $(id);
  if (l) l.style.display = s ? s : 'block';
}

function hide(id) {
  var l = $(id);
  if (l) l.style.display = 'none';
}

function get_value(f) {
  var e = $(f); if (!e) return null;
  if (e.options==undefined && e.length) for (var i=0; i<e.length; i++) { if (e[i].checked) return e[i].value; }
  else if (e.checked==undefined || e.checked || (e.type && e.type.toLowerCase()!='checkbox' && e.type.toLowerCase()!='radio')) return e.value;
  return null;
}

function has_value(f) {
  return !is_empty(get_value(f));
}

function get_option(f) {
  var e = $(f);
  return e && e.selectedIndex > -1 ? e.options[e.selectedIndex].text : null;
}

function has_option(f) {
  return !is_empty(get_option(f));
}

function set_value(f,v) {
  var e = $(f);
  if (e) e.value = v;
}

function set_option(f,v) {
  var e = $(f); if (!e) return;
  var o = e.options; var n = o.length;
  for (var i=0; i<n; i++) if (o[i].text==v) {
    o[i].selected = true;
    break;
  }
}

function set_text(e,t) {
  var e = $(e); if (!e) return;
  if (e.innerText) e.innerText = t;
  else if (e.textContent) e.textContent = t;
  else {
    while (e.firstChild) e.removeChild(e.firstChild);
    e.appendChild(document.createTextNode(t));
  }
}

function set_html(f,t) {
  var f = $(f);
  if (f) f.innerHTML = t;
}

function set_cookie(f,v,e) {
  if (is_empty(v)) { e = new Date(); e.setTime(e.getTime()-100000); }
  var d = e ? 'expires=' + e.toGMTString() + '; ' : '';
  document.cookie = f+"="+v+"; "+d+"path=/";
}

function get_cookie(f) {
  var cs = document.cookie.split(';'); for (var i in cs) { var nv = cs[i].split('=');
    if (string_trim(nv[0])==f) return nv.length>1 ? unescape(string_trim(nv[1])) : null; 
  }
  return null;
}

function copy_value(from,to) {
  set_value(to,get_value(from));
}

function has_class(e,c) {
  e = $(e); if (!e) return false;
  var p = e.className.split(" "); var l = p.length;
  for (var i=0; i<l; i++) if (p[i]==c) return true;
  return false;
}

function add_class(e,c) {
  e = $(e); if (!e || has_class(e,c)) return;
  e.className = e.className + ' ' + c;
  if (e.tagName.toLowerCase()=='td' && is_gecko()) e.parentNode.parentNode.innerHTML = e.parentNode.parentNode.innerHTML;
}

function remove_class(e,c) {
  e = $(e); if (!e) return;
  var p = e.className.split(" "); var l = p.length;
  for (var i=0; i<l; i++) if (p[i]==c) {
    if (p.splice) p.splice(i,1); else p[i] = null;
    e.className = p.join(" ");
    return;
  }
}

function blank_class(e,c) {
  e = $(e); if (!e) return;
  if (e.value=='') add_class(e,c);
  else remove_class(e,c);
}

function check_enter(event,handler) {
  if (event.keyCode==13) {
    handler();
    return false;
  }
  return true;
}

function is_gecko() {
  var a = window.navigator.userAgent.toLowerCase();
  return a.indexOf('gecko')>=0 && a.indexOf('safari')<0;
}

function is_opera() {
  return window.opera != null;
}

function is_empty(v) {
  return !v || v=='';
}

function is_integer(event) {
  var key = event.keyCode ? event.keyCode : event.which;
  return is_digit(key) || is_control(key) || (is_nav(key) && !event.shiftKey);
}

function is_decimal(field, event) {
  var key;
  if (event.keyCode) key = event.keyCode;
  else key = event.which;
  return is_digit(key) || is_control(key) || (is_nav(key) && !event.shiftKey);
}

function is_digit(key) {
  return key > 47 && key < 58;
}

function is_control(key) {
  return key == 8 || key == 9 || key == 10 || key == 13 || key == 46 || key == 127;
}

function is_nav(key) {
  return key > 34 && key < 41;
}

function limit_area(f, limit) {
  var c = f.value.length - limit; if (c<=0) return;
  if (document.selection) { // ie
    var sel = document.selection.createRange();
    sel.moveStart('character', -c);
    sel.text = '';
  } else if (f.selectionStart || f.selectionStart=='0') { // gecko
    var s = Number(f.selectionStart);
    f.value = f.value.substring(0,s-c) + f.value.substring(s);
  } else {
    f.value = f.value.substring(0, limit);
  }
}

function set_days(month, day) {
  if (month==0) return;
  var daysInMonth = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[month-1];
  var selectedDay = day.selectedIndex == null ? 1 : day.selectedIndex;
  var oldDays = day.options.length;
  day.options.length = daysInMonth + 1;
  for (var i=oldDays; i <= daysInMonth; i++) day.options[i] = new Option(i,i);
  day.selectedIndex = Math.min( selectedDay, daysInMonth );
}

function flag(v) {
  document.cookie = 'flag=' + (v ? v : 1);
}

function unpop(p) {
  if (p) p.destroy();
  return null;
}

function process(r,f) {
  var s = /<script(.|\s)*?\/script>/img; var scripts = [];
  r = r.replace(s,function(v) { scripts.push(v.replace(/<\/?script(.|\s)*?>/img,'')); return ''; });
  f(r);
  for (var is=0; is<scripts.length; is++) { eval(scripts[is]); }
}

function render(p,r) {
  process(r,function(t) {
    p.setBody(t);
    p.render(document.body);
    p.show();
  });
}

function ajax_init() {
  if (window.XMLHttpRequest) { return new XMLHttpRequest(); }
  try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (oc) {}
  return null;
}

function ajax_call(uri, target) {
  var x = ajax_init();
  if (!x) return true;
  x.open("GET", uri, true);
  x.onreadystatechange = function() { ajax_response(x,target); }
  x.send('');
}

function ajax_post(uri, params, target) {
  var x = ajax_init();
  if (!x) return true;
  show('loading');
  x.open("POST", uri, true);
  x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  x.setRequestHeader("Content-length", params.length);
  x.setRequestHeader("Connection", "close");
  x.onreadystatechange = function() {
    ajax_response(x,target);
  }
  x.send(params);
}

function ajax_response(r,target) {
  if (r.readyState!=4) return;
  var d = r.responseText;
  process(d,function(t) {
    if (typeof target=="function") target(d);
    else set_html(target,t);
  });
  delete r;
}

function vote(id,t,e) {
  ajax_call("/parts/vote?video="+id+"&type="+t,new function() { eval("tick("+id+",'"+t+"')"); });
  $('uplink_'+id).setAttribute('href','javascript:;'); remove_class('uplink_'+id,'up'); add_class('uplink_'+id,'notup');
  $('downlink_'+id).setAttribute('href','javascript:;'); remove_class('downlink_'+id,'up'); add_class('downlink_'+id,'notdown');
}

function tick(id,t) {
  var v = parseInt($(t+'_'+id).innerHTML)+1;
  set_text(t+'_'+id,v+' vote'+(v==1?'':'s'));
}

function flag(id) {
  if (!confirm("Are you sure you want to flag this video as inappropriate, broken, duplicate, or miscategorized?")) return;
  $('flag_'+id).setAttribute('href','javascript:;'); ajax_call("/parts/flag?video="+id,new function() { alert("The video has been flagged, thank you."); });
}

YAHOO.util.Event.onContentReady("menuBar", function () {
  var ua = YAHOO.env.ua,mAnim;

  function onSubmenuBeforeShow(p_sType, p_sArgs) {
    var mBody,mElement,mShadow,mUL;
    if (this.parent) {
      mElement = this.element;
      mShadow = mElement.lastChild;
      mShadow.style.height = "0px";
      if (mAnim && mAnim.isAnimated()) {
        mAnim.stop();
        mAnim = null;
      }
      mBody = this.body;
      if (this.parent &&
        !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
        if (ua.gecko || ua.opera) {
          mBody.style.width = mBody.clientWidth + "px";
        }
        if (ua.ie == 7) {
          mElement.style.width = mElement.clientWidth + "px";
        }
      }
      mBody.style.overflow = "hidden";
      mUL = mBody.getElementsByTagName("ul")[0];
      mUL.style.marginTop = ("-" + mUL.offsetHeight + "px");
    }
  }

  function onTween(p_sType, p_aArgs, p_mShadow) {
    if (this.cfg.getProperty("iframe")) {
      this.syncIframe();
    }
    if (p_mShadow) {
      p_mShadow.style.height = this.element.offsetHeight + "px";
    }
  }

  function onAnimationComplete(p_sType, p_aArgs, p_mShadow) {
    var mBody = this.body,mUL = mBody.getElementsByTagName("ul")[0];
    if (p_mShadow) {
      p_mShadow.style.height = this.element.offsetHeight + "px";
    }
    mUL.style.marginTop = "";
    mBody.style.overflow = "";
    if (this.parent && !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
      if (ua.gecko || ua.opera) {
        mBody.style.width = "";
      }
      if (ua.ie == 7) {
        this.element.style.width = "";
      }
    }
  }

  function onSubmenuShow(p_sType, p_sArgs) {
    var mElement,mShadow,mUL;
    if (this.parent) {
      mElement = this.element;
      mShadow = mElement.lastChild;
      mUL = this.body.getElementsByTagName("ul")[0];
      mAnim = new YAHOO.util.Anim(mUL,
        { marginTop: { to: 0 } },
        .5, YAHOO.util.Easing.easeOut);
        mAnim.onStart.subscribe(function () {
        mShadow.style.height = "100%";
      });
      mAnim.animate();
      if (YAHOO.env.ua.ie) {
        mShadow.style.height = mElement.offsetHeight + "px";
        mAnim.onTween.subscribe(onTween, mShadow, this);
      }
      mAnim.onComplete.subscribe(onAnimationComplete, mShadow, this);
    }
  }

  var mb = new YAHOO.widget.MenuBar("menuBar", {autosubmenudisplay: true, showdelay: 1, hidedelay: 1, lazyload: true });
  mb.subscribe("beforeShow", onSubmenuBeforeShow);
  mb.subscribe("show", onSubmenuShow);
  mb.render();
});
