/****************General Javascript Library*****************/
/**                  Copyright  (c) 2007                  **/
/**                   The Heaven Project                  **/
/**                      Version 1.0                      **/
/***********************************************************/
/******************Authored on 2006-11-01*******************/
/**                   by Lucian DiPeso                    **/
/***********************************************************/

//Inspired by a function from developer.apple.com
function getObject(object_id) {
	return $(object_id);
}

function get_object(object_id) {
    return $(object_id);
}

function hideAll(objectArray) {
	for(var i=0; i<objectArray.length; i++) {
		if(object = $(objectArray[i])) {
			if(object.style) {
				object.hide();
			}
		}
	}
}

function showObject(object, display) {
	//Default to "block" for display arg
	display = typeof(display) != "undefined" ? display : "block";
	
	if(object = getObject(object)) {
		object.style.display = display;
	}
}

function viewObject(view_link_object, close_link_object, object_id, force) {
	return view_object(view_link_object, close_link_object, object_id, force);
}

function view_object(view_link_object, close_link_object, object_id, force) {	
	if(!force) {
		force = false;
	}
	
	
	if(force) {
		$(object_id).show();
	} else {
		Effect.BlindDown(object_id);
	}
	
	if(view_link_object && $(view_link_object)) {
		$(view_link_object).hide();
	}
	$(close_link_object).show();
}

function closeObject(close_link_object, view_link_object, object_id) {
	return close_object(close_link_object, view_link_object, object_id);
}


function close_object(close_link_object, view_link_object, object_id) {
	if(view_link_object && $(view_link_object)) {
		$(view_link_object).show();
	}
	Effect.BlindUp(object_id);
	$(close_link_object).hide();
}

//Inspired from PHPs array_search function
/*Object.prototype.search = function(needle, strict) {
	//Inspired by a function from PHP
	if(typeof(strict) == "undefined" || typeof(strict) == "null") {
		strict = false;
	}
	
	var i;
	for(var i in this) {
		if((this[i] == needle && strict == false) || (this[i] === needle && strict == true)) {
			return i;
		}
	}
	return false;
}*/


Array.prototype.array_search = function(needle, strict) {

	//Inspired by a function from PHP
	if(typeof(strict) == 'undefined' || typeof(strict) == 'null') {
		strict = false;
	}
	
	if(typeof(needle) == 'array') {
	
		for(var i in needle) {
			index = this.array_search(needle[i], strict);
			
			if(index !== false) {
				return new Array(i, index);
			}
			
		}
		
		return false;
		
	} else {
	
		for(var i in this) {
		
			if((this[i] == needle && strict == false) || (this[i] === needle && strict == true)) {
				return i;
			}
			
		}
		
		return false;
	}
}

function array_search(needle, array) {//Search an array for a specific value
    value = false;
    for(i=0; i<array.length; i++) {
        if(array[i] == needle) {
            value = i;
            break;
        }
    }
    //alert(value);
    return value;
}

function return_regex(regex) {
    if(regex == "email") {
        return /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i;
    }
    if(regex == "phone") {
        return /^[2-9][0-9]{2}[2-9][0-9]{2}[0-9]{4}$/i;
    }
    if(regex == "poBox") {
        return /P[\. ]*(O|0)\.* +Box/i;
    }
} 

//Inspired from an article on phpied.com/javascript-include
var included_files = new Array();

function include(filename) {
	var head = document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', filename);
	head.appendChild(js);
	return true;
}

function include_once(filename) {
	if(array_search(filename, included_files) === false) {
		if(include(filename)) {
			included_files.push(filename);
			return true;
		} else {
			return false;
		}
	} else {
			return true;
	}
} 

//Inspired from a FAQ at codingforums.com
String.prototype.trim = function(charlist) {
	var string = this.replace(/^\s*|\s*$/g, '');
	/*if (typeof charlist == "string") {
		charlist_array = charlist.split(',');
		for (var i in charlist_array) {
			charlist_array[i].trim();
			var regex = new RegExp('/' + charlist_array[i] + '/', 'gi');
			string = string.replace(regex, '');
		}
	}*/
	return string;
}

String.prototype.ltrim = function(charlist) {
	var string = this.replace(/^\s*/g, '');
	return string;
}

String.prototype.rtrim = function(charlist) {
	var string = this.replace(/\s*$/g, '');
	return string;
}

//Originally was Object.prototype.merge, but was causing issues for SWFObject
function AssociativeArray() {
	this.merge = function() {
		//Any keys in the called objects that exist in the calling objects will be discarded
		//If it's not an object, just return it
		if(typeof this != "object") {
			return this;
		}
	
		for(i=0; i<arguments.length; i++) {
			if (typeof arguments[i] == "object") {
				for(var j in arguments[i]) {
					if (this[j] == "undefined") {
						this[j] = arguments[i][j];
					}
				}
			}
		}
	
		return this;
	}
}

function open_window(object, title, width_height_array, location_array, options_object) {
	
	//If options_object is just a string, convert it into an object
	if (typeof options_object == "string") {
		options_array = options_object.split(',');
		
		options_object = new AssociativeArray;
		
		for(i=0; i<options_array.length; i++) {
			option_value_array = options_array[i].split('=');
			option_key = option_value_array[0].trim();
			option_value = option_value_array[1].trim();
			
			options_object[option_key] = option_value;
		}
	}
	
	//Set default values
	var canonical_object = new AssociativeArray;
	canonical_object['status'] = 'no';
	canonical_object['toolbar'] = 'no';
	canonical_object['location'] = 'no';
	canonical_object['menubar'] = 'no';
	canonical_object['directories'] = 'no';
	canonical_object['resizable'] = 'yes';
	canonical_object['scrollbars'] = 'no';
	
	options_object.merge(canonical_object);
	
	//If location array is just a string, convert it into an array
	if (typeof location_array == "string") {
		location_array = location_array.split(',');
	}
	
	location_array[0] = location_array[0].trim();
	
	if (location_array.length == 1) {
		//If only one value is sent, just use it for both x and y
		location_array[1] = location_array[0];
	} else {
		location_array[1] = location_array[1].trim();
	}
	
	//If width_height array is just a string, convert it into an array
	if (typeof width_height_array == "string") {
		width_height_array = width_height_array.split(',');
	}
	
	if (width_height_array.length == 1) {
		//If only one value is sent, just use it for both x and y
		width_height_array[1] = width_height_array[0];
	}
	
	var width = width_height_array[0];
	var height = width_height_array[1];
		
	var url=object.href.toString();
	if (typeof title != "string" || title == '') {
		var title=object.target.toString();
	}
	
	//Generate option string
	var options = '';
	for (var i in options_object) {
		options += i + '=' + options_object[i] + ',';
	}
	
	var new_window = window.open(url, title, options + ',width=' + width + ',height=' + height);
	
	if (location_array.length >= 2) {
		//new_window.moveTo(location_array[0], location_array[1]);
	}
	
}

//From quirksmode:
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();