/**
 * vuBB
 * Copyright 2005-2006 the vuBB Group
 *
 * http://www.vubb.com/
 * http://community.vubb.com/
 */
 
var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);
var is_regexp = (window.RegExp) ? true : false;

String.prototype.vubblength = function()
{
	return (is_ie && this.indexOf('\n') != -1) ? this.replace(/\r?\n/g, '_').length : this.length;
}

////////////////////////////////////
// Cookies.
////////////////////////////////////

function set_cookie(name, value, expires)
{
	document.cookie = name + '=' + escape(value) + '; path=/' + (typeof expires != 'undefined' ? '; expires=' + expires.toGMTString() : '');
}

function delete_cookie(name)
{
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT' +  '; path=/';
}

function fetch_cookie(name)
{
	cookie_name = name + '=';
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (';', value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(' ', cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	return null;
}

////////////////////////////////////
// Toggle.
////////////////////////////////////

function vubb_toggle(objid)
{
	if (!is_regexp)
	{
		return false;
	}

	obj = getObject('toggleobj_' + objid);
	img = getObject('toggleimg_' + objid);
	cel = getObject('togglecel_' + objid);

	if (!obj) {
		if (img) {
			img.style.display = 'none';
		}
		return false;
	}

	if (obj.style.display == 'none' || obj.style.height == '0px')
	{
		obj.style.display = '';
		save_collapsed(objid, false);
		
		if (img)
		{
			img_re = new RegExp("_collapsed\\.gif$");
			img.src = img.src.replace(img_re, '.gif');
		}
		if (cel)
		{
			cel_re = new RegExp("^(alt|main)(_collapsed)$");
			cel.className = cel.className.replace(cel_re, '$1');
		}
	}
	else
	{
		obj.style.display = 'none';
		save_collapsed(objid, true);
		
		if (img)
		{
			img_re = new RegExp("\\.gif$");
			img.src = img.src.replace(img_re, '_collapsed.gif');
		}
		if (cel)
		{
			cel_re = new RegExp("^(alt|main)$");
			cel.className = cel.className.replace(cel_re, '$1_collapsed');
		}
	}
	return false;
}

function save_collapsed(objid, addcollapsed)
{
	var collapsed = fetch_cookie('vubb_collapse');
	var tmp = new Array();

	if (collapsed != null)
	{
		collapsed = collapsed.split('\n');

		for (var i in collapsed)
		{
			if (collapsed[i] != objid && collapsed[i] != '')
			{
				tmp[tmp.length] = collapsed[i];
			}
		}
	}

	if (addcollapsed)
	{
		tmp[tmp.length] = objid;
	}

	expires = new Date();
	expires.setTime(expires.getTime() + (1000 * 86400 * 365));
	set_cookie('vubb_collapse', tmp.join('\n'), expires);
}

function ShowQuickReply()
{
	if (getObject('QuickReply').style.height == '0px' && getObject('QuickReply').style.display != 'inline') {
		if (is_opera || is_moz) {
			resizeObject('QuickReply', 'auto', 'browser', 'auto', 0, 26, 400);
		} else {
			getObject('QuickReply').style.display = 'inline';
		}
	} else {
		if (is_opera || is_moz) {
			resizeObject('QuickReply', 'auto', 1, 'auto', 'auto', 26, 400);
		} else {
			getObject('QuickReply').style.display = 'none';
		}
	}
}

////////////////////////////////////
// Animations.
////////////////////////////////////

/*
 * resizeObject && takeStep by Detruire
 * Copyright  2006 Syridium SC
 * - http://www.syridium.biz.tm/
 */
function resizeObject(id, w, h, sw, sh, steps, time, hide)
{
	if (element = getObject(id)) {
		
		this.id      = id;
		this.element = element;
		this.id      = id;
		this.w       = (w  && w  != 'auto') ? parseInt(w)  : this.element.offsetWidth;
		this.h       = (h  && h  != 'auto') ? parseInt(h)  : this.element.offsetHeight;
		this.sw      = (sw && sw != 'auto') ? parseInt(sw) : this.element.offsetWidth;
		this.sh      = (sh && sh != 'auto') ? parseInt(sh) : this.element.offsetHeight;
		this.steps   = (steps > 1) ? parseInt(steps) : 2;
		this.step    = (steps > 1) ? parseInt(steps) : 2;
		this.time    = (time > 99) ? parseInt(time)  : 99;
		this.ow      = w;
		this.oh      = h;
		this.hide    = hide ? true : false;
		
		// Browser 'auto'?
		if (this.oh == 'browser') {
			this.element.style.display = '';
			this.element.style.visibility = 'hidden';
			this.element.style.height = 'auto';
			this.h = this.element.offsetHeight;
			this.element.style.height = this.sh;
		}
		if (this.ow == 'browser') {
			this.element.style.display = '';
			this.element.style.visibility = 'hidden';
			this.element.style.width = 'auto';
			this.w = this.element.offsetWidth;
			this.element.style.width = this.sh;
		}
		
		// Zero?
		this.h = (this.h == 1) ? 0 : this.h;
		this.w = (this.w == 1) ? 0 : this.w;
		
		// Play with the styles do we can work our magic.
		this.element.style.visibility = 'visible';
		this.element.style.display = ''; // 'inline' appears to cause errors in some cases
		this.element.style.overflow = 'hidden';
		
		this.element.style.width  = this.sw+'px';
		this.element.style.height = this.sh+'px';
		
		// Hmm.
		this.tbu = this;
		
		// Add Class functions.
		this.takeStep = takeStep;
		
		this.takeStep();
	}
}

function takeStep()
{
	with(this);
	
	if (this.step > 0) {
		
		// Current Size.
		var cWidth  = this.element.offsetWidth;
		var cHeight = this.element.offsetHeight;
		
		// Dimensions each time.
		var onew = (this.w - cWidth)  / this.step;
		var oneh = (this.h - cHeight) / this.step;
		
		this.element.style.width = (cWidth + onew)+'px';
		this.element.style.height = (cHeight + oneh)+'px';
		
		var timeout = this.time / this.steps;
		
		this.step--;
		
		window.setTimeout(this.takeStep.bind(this), timeout);
	} else if(this.element.style.height == '0px' && this.hide) {
		this.element.style.display = 'none';
	}
}

/* Function by Daniel Brockman.
 * Copyright  2004-2006 Daniel Brockman.
 * - http://www.brockman.se
 * - http://www.brockman.se/writing/method-references.html.utf8
 */
Function.prototype.bind = function (object) {
    var method = this;
    return function () {
        method.apply(object, arguments);
    };
}

function getObject(objectID)
{
	if (document.getElementById) {
		return document.getElementById(objectID);
	} else if (document.all) {
		return document.all[objectID];
	} else if (document.layers) {
		return document.layers[objectID].indexValue;
	} else {
		return '';
	}
}

////////////////////////////////////
// BBCode.
////////////////////////////////////
function getLength(){
  var length = form["body1"].value.length;
  alert("Your post is "+length+" characters.");

  return false;
}

function insertSmiley(smiley) {
  insertCode(getText() + ' ' + smiley);
  return false;
}

function insertCode(code) {
  var el = form["body1"];
  if (window.getSelection) {
    if (el.selectionStart != el.selectionEnd) {
      el.value = el.value.substring(0, el.selectionStart) + code
              + el.value.substring(el.selectionEnd, el.value.length);
      el.selectionStart = el.value.length;
      el.selectionEnd = el.value.length;
    } else {
      el.value = el.value.substring(0, el.selectionStart) + code
              + el.value.substring(el.selectionStart, el.value.length);
    }
  } else if (el.createTextRange && el.caretPos) {
    el.caretPos.text = code;
  } else {
    el.value += code;
  }
  el.focus();
}

function storeCaret(el) {
  if (el.createTextRange) {
    el.caretPos = document.selection.createRange().duplicate();
  }
}

function getText() {
  if (document.all) {
    if (form["body1"].createTextRange && form["body1"].caretPos) {
      return form["body1"].caretPos.text;
    } else {
      return "";
    }
  } else if (window.getSelection) {
    return form["body1"].value.substring(form["body1"].selectionStart, form["body1"].selectionEnd);
  } else {
    return "";
  }
}

function bbCode(e) {
  var tag = e.name;
  var text = getText();

  if (text) {
    var code = "[" + tag + "]" + text + "[/" + tag + "]";
  } else {
    if (e.title.indexOf("*") != -1) {
      var code = "[/" + tag + "]";
      e.title= e.title.substring(0,(e.title.length-1));
    } else {
      var code = "[" + tag + "]";
      e.title += "*";
    }
  }
  insertCode(code);
}

function bbcURL(e) {
  var type = e.name;
  var text = getText();
  var isURL = (text.substring(0,7) == "http://");

  if (type == 'img') {
    if (isURL) {
      var code = "[img]" + text + "[/img]";
    } else {
      var code = text + "[img]" + prompt("URL:","") + "[/img]";
    }
  } else {
    var code = "[" + type + "=" + (isURL ? text : prompt("Enter an address:","")) + "]" + ((text && !isURL) ? text : prompt("Enter a description:","")) + "[/" + type + "]";
  }
  insertCode(code);
}

function bbcFont(list) {
  var attrib = list.name.substring(1,list.name.length);
  var value = list.options[list.selectedIndex].value;
  if (value && attrib) {
    insertCode("[" + attrib + "=" + value + "]" + getText() + "[/" + attrib + "]");
  }
  setTimeout("form['"+list.name+"'].options[0].selected = true",10);
}
