// Add in possible missing methods for Javascript objects
// Array.indexOf()
if (Array.prototype.indexOf == null) Array.prototype.indexOf = function (needle) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == needle) {
			return i;
		}
	}
	return -1;
}

// Array.remove()
if (Array.prototype.remove == null) Array.prototype.remove = function (needle) {
	var index = this.indexOf(needle);
	if (index >= 0) {
		this.splice(index,1);
		return true;
	}
	return false;
}

// String.trim()
if (String.prototype.trim == null) String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

// Shared utility functions
function cacheImage (img_src) {
	var img = new Image();
	img.src = img_src;
	return img;
}


function observeEvent (element, eventName, handler) {
	if (element.addEventListener) {
		// W3C
		element.addEventListener(eventName,handler,false);
	} else {
		// IE
		if (element == window) {
			element = document;
		}
		element["event_"+eventName+"_"+handler] = handler;
		element[eventName+"_"+handler] = function() {
			element["event_"+eventName+"_"+handler](window.event);
		}
		element.attachEvent("on"+eventName,element[eventName+"_"+handler]);
	}
}

function removeEvent (element, eventName, handler) {
	if (element.removeEventListener) {
		// W3C
		element.removeEventListener(eventName,handler,false);
	} else {
		// IE
		if (element == window) {
			element = document;
		}
		element.detachEvent("on"+eventName,element[eventName+"_"+handler]);
		element[eventName+"_"+handler] = null;
	}
}


function stopBubble (e) {
	if (!e) var e = window.event;
	
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function getPosition (obj) {
	var cur_left = 0;
	var cur_top = 0;
	if (obj.offsetParent) {
		cur_left = obj.offsetLeft
		cur_top = obj.offsetTop
		while (obj = obj.offsetParent) {
			cur_left += obj.offsetLeft
			cur_top += obj.offsetTop
		}
	}
	return [cur_left,cur_top];
}

function fade (element_id, fade_in, fade_time, fade_steps) {
	if (!fade_time) {
		fade_time = 0.2;
	}
	if (!fade_steps) {
		fade_steps = 5;
	}

	var time_step = (fade_time/fade_steps)*1000;
	var opacity_step = 100.0/fade_steps;
	
	if (fade_in == 1) {
		
		start = 0;
		for (var i = 1; i <= fade_steps; i++) {
			opacity = start+(i*opacity_step);
			setTimeout("setOpacity('"+element_id+"',"+opacity+");",i*time_step);
		}
	} else if (fade_in == 2) {
		
		start = 100;
		for (var i = 1; i <= fade_steps; i++) {
			opacity = start-(i*opacity_step);
			setTimeout("setOpacity('"+element_id+"',"+opacity+");",i*time_step);
		}
	} else if (fade_in == 0) {
		setOpacity(element_id,0);
	}
}

function setOpacity (element_id, opacity) {
	menu_item = document.getElementById(element_id);
	menu_item.style.opacity = opacity/100;
	menu_item.style.filter = "Alpha(Opacity=" + opacity + ")";
}

function explode_assoc (assoc_obj) {
	var attributes = new Array();
	for (var key in assoc_obj) {
		attributes.push(key+": "+assoc_obj[key]);
	}
	return attributes.join("; ");
}

// Button Actions
function doLink (link_url) {
	document.location = link_url;
}

// Class Definitions
function LWF_ArrayMap() {
	this.objects = new Object();
	this.keys = new Array();
	this.length = 0;
}

LWF_ArrayMap.prototype.containsKey = function (key) {
	if (this.keys.indexOf(key) == -1) {
		return false;
	}
	return true;
}

LWF_ArrayMap.prototype.containsValue = function (value) {
	for (var key in this.objects) {
		if (this.objects[key] == value) {
			return true;
		}
	}
	return false;
}

LWF_ArrayMap.prototype.put = function (key, value) {
	if (!this.containsKey(key)) {
		this.keys.push(key);
		this.length++;
	}
	this.objects[key] = value;
}

LWF_ArrayMap.prototype.get = function (key) {
	if (this.containsKey(key)) {
		return this.objects[key];
	}
	return null;
}

LWF_ArrayMap.prototype.remove = function (key) {
	var return_val = this.get(key);
	if (return_val != null) {
		delete this.objects[key];
		this.keys.remove(key);
		this.length--;
	}
	return return_val;
}

LWF_ArrayMap.prototype.values = function() {
	var return_array = new Array();
	for (var i = 0; i < this.keys.length; i++) {
		return_array.push(this.objects[this.keys[i]]);
	}
	return return_array;
}

LWF_ArrayMap.prototype.toString = function() {
	var attributes = new Array();
	for (var key in this.objects) {
		attributes.push(key+": "+this.objects[key]);
	}
	return attributes.join("; ");
}

LWF_ArrayMap.fromString = function (content_string) {
	var return_map = new LWF_ArrayMap();
	var attributes_array = content_string.split(";");
	for (var i = 0; i < attributes_array.length; i++) {
		var value_array = attributes_array[i].split(":");
		if (value_array.length >= 2) {
			return_map.put(value_array[0].trim(),value_array[1].trim());
		}
	}
	return return_map;
}

function LWF_Style (css_styles) {
	this.classes = new Array();
	this.styles = new LWF_ArrayMap();
	this.attributes = new LWF_ArrayMap();
	
	if (typeof css_styles == 'string') {
		this.addStyles(css_styles);
	}
}

LWF_Style.prototype.addClass = function (class_name) {
	this.classes[this.classes.length] = class_name;
}

LWF_Style.prototype.removeClass = function (class_name) {
	this.classes.remove(class_name);
}

LWF_Style.prototype.addStyle = function (attribute, value) {
	this.styles.put(attribute,value);
}

LWF_Style.prototype.addStyles = function (styles_str) {
	var styles_map = LWF_ArrayMap.fromString(styles_str);
	for (var key in styles_map.objects) {
		this.addStyle(key,styles_map.get(key));
	}
}

LWF_Style.prototype.addAttribute = function (attribute, value) {
	this.attributes.put(attribute,value);
}

LWF_Style.prototype.toString = function() {
	var output = "";
	if (this.classes.length) {
		output += ' class="'+this.classes.join(" ")+'"';
	}
	if (this.styles.length) {
		output += ' style="'+this.styles.toString()+'"';
	}
	if (this.attributes.length) {
		for (var key in this.attributes.objects) {
			output += " "+key+'"'+this.attributes.get(key)+'"';
		}
	}
	return output;
}

function strip_tags(str, allowed_tags) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Luke Godfrey
    // *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i>,<b>');
    // *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
    // *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>');
    // *     returns 2: 'Kevin van Zonneveld'
    
    var key = '', tag = '';
    var matches = allowed_array = [];
    var allowed_keys = {};
    
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_tags  = allowed_tags.replace(/[\<\> ]+/g, '');;
        allowed_array = allowed_tags.split(',');
        
        for (key in allowed_array) {
            tag = allowed_array[key];
            allowed_keys['<' + tag + '>']   = true;
            allowed_keys['<' + tag + ' />'] = true;
            allowed_keys['</' + tag + '>']  = true;
        }
    }
    
    // Match tags
    matches = str.match(/(<\/?[^>]+>)/gi);
    
    // Is tag not in allowed list? Remove from str! 
    for (key in matches) {
        // IE7 Hack
        if (!isNaN(key)) {
            tag = matches[key].toString();
            if (!allowed_keys[tag]) {
                // Looks like this is
                // reg = RegExp(tag, 'g');
                // str = str.replace(reg, '');
                str = str.replace(tag, "");
            }
        }
    }
    
    return str;
}