//$Id: core.js,v 1.4.2.14 2009/01/15 21:47:33 lilstargazer Exp $
var isIE = (navigator.userAgent.indexOf('MSIE') != -1 && !window.opera) ? 1 : 0;

function addEvent(obj, type, fn) {
  if (obj.addEventListener) {
    obj.addEventListener(type, fn, false);
    EventCache.add(obj, type, fn);
  }
  else if (obj.attachEvent) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() { obj["e"+type+fn](window.event); }
    obj.attachEvent("on"+type, obj[type+fn]);
    EventCache.add(obj, type, fn);
  }
  else {
    obj["on"+type] = obj["e"+type+fn];
  }
}

var EventCache = function(){
  var listEvents = [];
  return {
    listEvents : listEvents,
    add : function(node, sEventName, fHandler){
      listEvents.push(arguments);
    },
    flush : function(){
      var i, item;
      for (i = listEvents.length - 1; i >= 0; i = i - 1){
        item = listEvents[i];
        if (item[0].removeEventListener){
          item[0].removeEventListener(item[1], item[2], item[3]);
        };
        if (item[1].substring(0, 2) != "on"){
          item[1] = "on" + item[1];
        };
        if (item[0].detachEvent){
          item[0].detachEvent(item[1], item[2]);
        };
        item[0][item[1]] = null;
      };
    }
  };
}();
addEvent(window,'unload',EventCache.flush);

function $(el)
{
  var obj;
  if (document.getElementById)
    obj = document.getElementById(el);
  else if (document.all)
    obj = document.all[el];
  return obj;
}

Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
    var i;
    for (i=0; i < this.length; i++) {
        // Matches identical (===), not just similar (==).
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};


function hasClass(el,className)
{
  var classSets = el.className.split(' ');
  if (classSets.inArray(className))
    return true;
  return false;
}

function addClass(el,className)
{
  el.className = el.className.length == 0 ? className : el.className+' '+className;
}

function removeClass(el,className)
{
  var classNames = el.className.split(' ');
  for (var i = 0; i < classNames.length; i++)
  {
    if (classNames[i] == className)
      classNames.splice(i,1);
  }
  el.className = classNames.join(' ');
}

function XHR()
{
  var req = false;
  if (window.XMLHttpRequest)
  {
    req = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
    req = new ActiveXObject('Microsoft.XMLHTTP');
  }

  return req;
}

function prepareURL(url)
{
  var ms = Date.parse(new Date().toString());
  url = url+'&async='+ms; // add random seed to requests
  return url;
}

function loadLogin(linkEl)
{
  var parent = linkEl.parentNode;
  parent.insertBefore($('loginPane'), linkEl);
  document.getElementById('loginPane').style.display = 'block';
  document.getElementById('loginUser').focus();
  return false;
}

function cancelLogin()
{
  document.getElementById('loginPane').style.display = '';
}

function categoryJump(catID)
{ if (catID != '') document.getElementById('catJump').submit(); }

function deleteCrush(name,list)
{
  if (!confirm('Are you sure you want to remove '+name+' from your '+list+' list?'))
    return false;
  // fill later
}

function deleteFriend(name)
{
  if (!confirm('Are you sure you want to remove '+name+' from your friends?'))
    return false;
  // fill later
}

function addFriend()
{
  // fill later
}

function addCrush()
{
  // fill later
}

function switchImage(imageID)
{
  var data = photoData[imageID];
  document.getElementById('largerPhoto').style.display = '';
  document.getElementById('largerPhotoImg').src = data.url;
  document.getElementById('largerPhotoImg').title = data.description.replace(/\n/g,' ');
  document.getElementById('photoDesc').innerHTML = (data.description == '') ? '' : '<strong>Description</strong>: '+data.description.replace(/\n/g,'<br />');
  document.getElementById('uploadDate').innerHTML = (data.uploadDate == '') ? '' : '<strong>Uploaded</strong>: '+data.uploadDate;
  return false; // stop link from flowing
}

function previewText(text)
{
  var text = encodeURIComponent(text);
  var previewWindow = window.open(docRoot + '/utilities/formatPreview.php?text='+text,'previewWindow','scrollbars=1,width=600,height=400');
}

function getNameStatus(user)
{
  var xhr = XHR();
  if (!xhr)
  {
    alert('xhr bad');
    return false;
  }
  if (user.length < 4)
  {
    xhr = null;
    document.getElementById('nameAvailable').className = 'bad';
    document.getElementById('nameAvailable').innerHTML = 'Username must be at least 4 characters long.';
    return false;
  }
  var words = '';
  if (words = user.match(censoredWords))
  {
    words = words.join(', ');
    document.getElementById('nameAvailable').className = 'bad';
    document.getElementById('nameAvailable').innerHTML = 'The name contains the following prohibited words: '+words;
    return false;
  }
  if (user.match(usernameBadChars))
  {
    document.getElementById('nameAvailable').className = 'bad';
    document.getElementById('nameAvailable').innerHTML = 'Only characters A-Z,0-9, underscore(_), dash(-), and period(.) allowed.';
    return false;
  }

  xhr.open('GET', prepareURL(docRoot + '/utilities/usernameAvailable.php?username='+user), true);
  xhr.onreadystatechange = function()
  {
    if (xhr.readyState == 4 && xhr.status == 200)
    {
      var name = eval('('+xhr.responseText+')');
      if (name.exists)
      {
        document.getElementById('nameAvailable').className = 'bad';
        document.getElementById('nameAvailable').innerHTML = user+' is already in use.';
      }
      else
      {
        document.getElementById('nameAvailable').className = 'good';
        document.getElementById('nameAvailable').innerHTML = user+' is available.';
      }
    }
  }
  xhr.send("");
}

function popTagboard(url)
{
  url += '&popup=1';
  window.open(url,'popup','scrollbars=1,resizable=1,width='+900+',height='+600);
  return false;
}

addEvent(window,'load',csStart);

function csStart(e)
{
  csUserInfo.init();
  var left = document.getElementById('pageLeft');
  if (isIE && left)
  {
    var imgs = left.getElementsByTagName('img');
    for (var i = 0; i < imgs.length; i++)
    {
      if (imgs[i].width > 560) imgs[i].width = 560;
    }
  }

}

var csUserInfo = {
  xPos : 0,
  yPos : 0,
  height : 100,
  intLen : 50,
  increment : 60,
  status : 0,
  data : '',
  uiDiv : Object,
  getXY : function(e) {
    if (e.pageX && e.pageY)
    { csUserInfo.xPos = e.pageX; csUserInfo.yPos = e.pageY }
    else if (e.clientX && e.clientY) {
      csUserInfo.xPos = e.clientX + document.documentElement.scrollLeft;
      csUserInfo.yPos = e.clientY + document.documentElement.scrollTop;
    }
    },
  init : function() {
    this.uiDiv = document.createElement('div');
    this.uiDiv.id = 'uiData';
    document.getElementsByTagName('body')[0].appendChild(this.uiDiv);
    var a = document.getElementsByTagName('a');
    for (var i=0,j=a.length;i < j;i++)
    {
      if (a[i].className.match(/\buserLink\b/))
      {
        addEvent(a[i],'mouseover',csUserInfo.show);
        addEvent(a[i],'mouseout',csUserInfo.hide);
      }
    }
  },
  show : function(e) {
    csUserInfo.getXY(e);
    csUserInfo.uiDiv.style.top = (csUserInfo.yPos+10)+'px';
    if (csUserInfo.xPos + csUserInfo.uiDiv.offsetWidth < screen.width)
      csUserInfo.uiDiv.style.left = (csUserInfo.xPos+10)+'px';
    else
      csUserInfo.uiDiv.style.left = (csUserInfo.xPos + 10 - csUserInfo.uiDiv.offsetWidth)+'px';
    csUserInfo.data = this.getAttribute('linkdata');
    csUserInfo.uiDiv.innerHTML = csUserInfo.data;
    csUserInfo.uiDiv.style.display = 'block';
  },
  hide : function(e) {
    csUserInfo.uiDiv.innerHTML = '';
    csUserInfo.uiDiv.style.display = '';
  }
}


function backToTop()
{
  document.getElementById('messageBox').style.display = '';
  window.scrollTo(0,0);
  return false;
}

function fixSize(el,properWidth,properHeight)
{
  if (properWidth)
  {
    el.width = el.width > properWidth ? properWidth : el.width;
  }
  if (properHeight)
  {
    el.height = el.height > properHeight ? properHeight: el.height;
  }
  return false;
}

function formatText(myCode, alt_name)
{
  var form  = '';
  if (typeof alt_name == 'undefined')
  {
    form = document.csForm.message;
  }
  else
    form = document.getElementById(alt_name);

  var bbCode = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[s]','[/s]','[l]','[/l]','[c]','[/c]','[r]','[/r]','[quote]','[/quote]','[divider]','','[img]','[/img]','[url]','[/url]','[email]','[/email]','[]','[/]','[]','[/]','[]','[/]','[]','[/]','[]','[/]','[]','[/]','[]','[/]','[]','[/]');
  bbopen = bbCode[myCode];
  myCode++;

  bbclose = bbCode[myCode];

  //IE support
  if (document.selection) {
    currentSelection = document.selection.createRange().text;
    form.focus();
    document.selection.createRange().text = bbopen + currentSelection + bbclose ;
  }

  //MOZILLA/NETSCAPE support
  else if (form.selectionStart || form.selectionStart == '0') {
    var startPos = form.selectionStart;
    var endPos = form.selectionEnd;
    form.value = form.value.substring(0, startPos)
    + bbopen + form.value.substring(startPos,endPos) + bbclose
    + form.value.substring(endPos, form.value.length);
  }

  else {
    form.focus();
    form.value += bbopen + bbclose;
  }

}

function useColor(myColor, alt_name)
{
  var form  = '';
  if (typeof alt_name == 'undefined')
    form = document.csForm.message;
  else
    form = document.getElementById(alt_name);

  if (myColor == "-")
    return;

  var colorStart = "["+myColor+"]";
  var colorEnd = "[/"+myColor+"]";

  //IE support
  if (document.selection) {
    currentSelection = document.selection.createRange().text;
    form.focus();
    document.selection.createRange().text = colorStart + currentSelection + colorEnd ;
  }

  //MOZILLA/NETSCAPE support
  else if (form.selectionStart || form.selectionStart == '0') {
    var startPos = form.selectionStart;
    var endPos = form.selectionEnd;
    form.value = form.value.substring(0, startPos)
    + colorStart + form.value.substring(startPos,endPos) + colorEnd
    + form.value.substring(endPos, form.value.length);
  }

  else {
    form.focus();
    form.value += colorStart + colorEnd;
  }

}

function useSize(mySize, alt_name)
{
  var form  = '';
  if (typeof alt_name == 'undefined')
    form = document.csForm.message;
  else
    form = document.getElementById(alt_name);
  if (mySize == "-")
    return;

var sizeStart = "[size="+mySize+"]";
var sizeEnd = "[/size="+mySize+"]";

  //IE support
  if (document.selection) {
    currentSelection = document.selection.createRange().text;
    form.focus();
    document.selection.createRange().text = sizeStart + currentSelection + sizeEnd ;
  }

  //MOZILLA/NETSCAPE support
  else if (form.selectionStart || form.selectionStart == '0') {
    var startPos = form.selectionStart;
    var endPos = form.selectionEnd;
    form.value = form.value.substring(0, startPos)
    + sizeStart + form.value.substring(startPos,endPos) + sizeEnd
    + form.value.substring(endPos, form.value.length);
  }

  else {
    form.focus();
    form.value += sizeStart + sizeEnd;
  }

}

function useFont(myFont, alt_name)
{
  var form  = '';
  if (typeof alt_name == 'undefined')
    form = document.csForm.message;
  else
    form = document.getElementById(alt_name);

  if (myFont == "-")
    return;

  StartFont = "[font="+myFont+"]";
  EndFont = "[/font]";

  //IE support
  if (document.selection) {
    currentSelection = document.selection.createRange().text;
    form.focus();
    document.selection.createRange().text = StartFont + currentSelection + EndFont ;
  }

  //MOZILLA/NETSCAPE support
  else if (form.selectionStart || form.selectionStart == '0') {
    var startPos = form.selectionStart;
    var endPos = form.selectionEnd;
    form.value = form.value.substring(0, startPos)
    + StartFont + form.value.substring(startPos,endPos) + EndFont
    + form.value.substring(endPos, form.value.length);
  }

  else {
    form.focus();
    form.value += StartFont + EndFont;
  }

}


function toggleSmilies()
{
  if (document.getElementById('smiliesBox').style.display == '')
  { document.getElementById('smiliesBox').style.display = 'block'; document.getElementById('smiliesLink').firstChild.data = 'Hide Smilies'; }
  else
  { document.getElementById('smiliesBox').show(); document.getElementById('smiliesLink').firstChild.data = 'Show Smilies'; form.focus(); }
  return false;
}

function addSmile(theSmile, alt_name)
{
  var form  = '';
  if (typeof alt_name == 'undefined')
    form = document.csForm.message;
  else
    form = document.getElementById(alt_name);

  if (document.selection) {
    currentSelection = document.selection.createRange().text;
    form.focus();
    document.selection.createRange().text = currentSelection + " " + theSmile + " " ;
  }

  //MOZILLA/NETSCAPE support
  else if (form.selectionStart || form.selectionStart == '0') {
    var startPos = form.selectionStart;
    var endPos = form.selectionEnd;
    form.value = form.value.substring(0, startPos)
    + " " + theSmile + " "
    + form.value.substring(endPos, form.value.length);
  }

  else {
    form.focus();
    form.value += " " + theSmile + " ";
  }

}

function photobucket_complete(inurl)
{

  if (document.selection) {
    currentSelection = document.selection.createRange().text;
    document.csForm.message.focus();
    document.selection.createRange().text = currentSelection + " " + inurl + " " ;
  }

  //MOZILLA/NETSCAPE support
  else if (document.csForm.message.selectionStart || document.csForm.message.selectionStart == '0') {
    var startPos = document.csForm.message.selectionStart;
    var endPos = document.csForm.message.selectionEnd;
    document.csForm.message.value = document.csForm.message.value.substring(0, startPos)
    + " " + inurl + " "
    + document.csForm.message.value.substring(endPos, document.csForm.message.value.length);
  }

  else {
    document.csForm.message.focus();
    document.csForm.message.value += " " + inurl + " ";
  }

  document.csForm.message.focus();
}

function setChecked(value)
{
 var checkboxes = document.getElementsByTagName("input")
 for (i = 0; i < checkboxes.length; i ++)
 if (checkboxes[i].type == 'checkbox' && checkboxes[i].id != 'checkall')
  checkboxes[i].checked = value;
}

function popup(e, text, id)
{
  if (!e.pageX) {
    var scrollLeft = (document.body.scrollLeft || document.documentElement.scrollLeft);
    var scrollTop  = (document.body.scrollTop || document.documentElement.scrollTop);
    var tempX = e.clientX + scrollLeft;
    var tempY = e.clientY + scrollTop;
  }
  else
  {
    var tempX = e.pageX;
    var tempY = e.pageY;
  }

  var popdiv = document.createElement('div');
  popdiv.setAttribute('id', 'whatBox');
  popdiv.innerHTML = text;
  popdiv.style.maxWidth = 500 + 'px';
  popdiv.style.padding = 3 + 'px';
  document.getElementById(id).appendChild(popdiv);
  popdiv.style.left = tempX + 20 + 'px';
  popdiv.style.top = tempY + 20 + 'px';
}

function popup_close(id)
{
  var popdiv = document.getElementById('whatBox');
  var parent = document.getElementById(id);
  popdiv.display = "none";
  parent.removeChild(popdiv);
}
