/**
 *  isMSIE
 *
 *  @return {boolean}		true if browser is internet explorer
 */
function isMSIE()
{
	var ismsie/*@cc_on = {}@*/;
	return ismsie;
}

if (isMSIE()) {
	var currentNode;
	var currentFadeValue;
	var currentCallBack;
}

/**
 * Passes scope to child function
 */
Function.prototype.bind = function(superclass)
{
	var method = this;
	return function()
	{
		method.apply(superclass, arguments);
	};
}

/**
 * getSelection
 *
 * @return {object}			selection
 */
function getSelection(context, get_text_range)
{
	if (isMSIE())
	{
		if (typeof context == 'undefined')
			selection = document.selection;
		else
			selection = window.top.frames[context].document.selection;
		text_range = selection.createRange();
	}
	else
	{
		if (typeof context == 'undefined')
			selection = document.contentWindow.getSelection();
		else
			selection = document.getElementById(context).contentWindow.getSelection();
		text_range = selection.getRangeAt(0);
	}

	if (typeof get_text_range == 'undefined' || get_text_range == false)
		return selection;
	else
		return text_range;
}

/**
 * selectionIsText
 *
 * @return {boolean}			true if selection is a text node/range
 */
function selectionIsText(context)
{
	_selection = getSelection(context);
	
	if (isMSIE())
		return (_selection.type == 'Text')
	else
		return (_selection.toString() == _selection.getRangeAt(0).toString());
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload)
				oldonload();
			func();
		}
	}
}

function addUnloadEvent(func) {
	var oldonload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	} else {
		window.onunload = function() {
			if (oldonload)
				oldonunload();
			func();
		}
	}
}

function setOpacity(node, value)
{
	if (isMSIE()) {
		node.style.filter = "progid:DXImageTransform.Microsoft.Alpha";
		node.filters.item("DXImageTransform.Microsoft.Alpha").enabled = 1;
		node.filters.item("DXImageTransform.Microsoft.Alpha").opacity = value;
	}
	else {
		value = value / 100;
		node.style.opacity = value;
	}
}

function getOpacity(node)
{
	if (isMSIE()) {
		return node.filters.item("DXImageTransform.Microsoft.Alpha").opacity;	
	}
	else {
		return node.style.opacity * 100;
	}
}

function opacityFade(node, fadeToValue, callBack)
{
	currentOpacity = getOpacity(node);
	if (currentOpacity == fadeToValue)
	{
		if (typeof callBack == 'function') {
			callBack();
		}
		return;
	}
	
	if (typeof fadeToValue == 'undefined' || fadeToValue == -1) {
		fadeToValue = (getOpacity(node) > 0)? 0: 100;
	}
	if (getOpacity(node) < fadeToValue){
		setOpacity(node, currentOpacity + 2);
	}
	else {
		setOpacity(node, currentOpacity - 2);
	}
	if (isMSIE()) {
		currentNode = node;
		currentFadeValue = fadeToValue;
		currentCallBack = callBack;
		//IE evaluates first argument as a string, outside the context of this function. late arguments are used for language setting.
		window.setTimeout("opacityFade(currentNode,currentFadeValue,currentCallBack)", 10);
	} else {
		window.setTimeout(opacityFade, 1, node, fadeToValue, callBack);
	}
}

function isd(value)
{
   return (value - (value % 2)) / 2;
}

function Cache()
{
	if (typeof this._initialised_ == 'undefined')
	{
		this.images = Array();
		this.images_c = 0;
		
		this._initialised_ = true;
	}
}

Cache.prototype.loadImage = function(src, width, height, on_load)
{
	this.images_c = this.images.push(new Image(width, height));
	
	if (typeof on_load == 'function')
	{
		this.images[this.images_c - 1].call_back = on_load;
		this.images[this.images_c - 1].onload = function()
		{
			this.call_back(this);
		};
	}
	this.images[this.images_c - 1].src = src;
}

var cache = new Cache();

var global = this;

function makeSingleton(ofMe) {
   var _ofMe     = global[ofMe];
   var _instance = null;
   
   global[ofMe] = new function() {
      this.getInstance = function() {
         if (_instance == null) {
            _instance = new _ofMe();
            _instance.constructor = null;
         }
         
         return _instance;
      }
   }
}

$(function(){
   $.fn.hideText = function(){
      return this.each(function(){
         $this = $(this);
         
         var defaultValue = $this.attr('value');
         $this.click(function(){
            if ($this.attr('value') == defaultValue)
               $this.attr('value') = '';
         });
      });
   };
});

$(function(){
	$('form a.select_all').click(function(e){
		e.preventDefault();

		$('form :checkbox').each(function(){
			this.checked = true;
		});
	});
	$('form input.select_all').click(function(e){
		var that = this;
		$('form :checkbox').each(function(){
			this.checked = that.checked;
			
			$(this).siblings('label').text(
				"select "
				+ (this.checked? 'none' : 'all')
			);
		});
	});
   
   // jQuery selector -- case-insensitive version of :contains
   $.expr[':'].icontains = function(obj, index, meta, stack) {
      return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
   };
});