/*=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 = file      : whitelabel.js
 = ----------+---------------------------------------------------------------+
 = package   : Whitelabel
 = scope     : global, available to all views.
 = author    : Bastian Kuberek
 = copyright : (c) 2010 The Access Network Company. All rights reserved.
 = ----------+---------------------------------------------------------------+
 = depends   : prototype.js 1.6.x, effects.js, lowpro.js
 =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+*/

Event.addBehavior.reassignAfterAjax = true;
Event.addBehavior({

});

Element.addMethods({
	getInnerText: function(element) {
		element = $(element);
		return element.innerText && !window.opera ? element.innerText : element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
	}
});

/**
 * Loads an external js file
 *
 * @param	{String}	filename	url to js file
 * @return	{void}
 */
function loadjs(filename) {
	var fileref = document.createElement('script');
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", filename);
	if (typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}

/**
 * Loads an external css file
 *
 * @param	{String}	filename	url to css file
 * @return	{void}
 */
function loadcss(filename) {
	var fileref = document.createElement("link");
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", filename);
	if (typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	}
}

/**
 * Scrolls the page to to at speed speed
 *
 * @param	{mixed}		to		an element id or a dom element
 * @param	{Integer}	speed
 * @param	{Object}	options
 * @return	{void}
 */
function scroll_to(to, speed, options) {
	speed = speed ? speed : 250;
	options = Object.extend({axis: 'y'}, options);
	Effect.ScrollTo(to, speed, options || {});
}

/**
 * Toggles true/false all checkboxes that match selector
 * 
 * @param	{String}	selector	css selector
 * @param	{Boolean}	checked		[optional] force value
 * @return	{void}
 */
function toggle_checkbox(selector, checked) {
	$$(selector).each(function(n) {
		if (checked == null) checked = !n.checked;
		n.checked = checked;
	});
}

/**
 * will return the hash portion of the href
 * 
 * @param	{Object}	a	A Dom Element
 * @return	string
 */
function get_anchor_value(a) {
	return a.href.substring(a.href.indexOf('#') + 1);
}

/**
 * Retrieves the max height of all elements that match selector
 *
 * @param	{String}	selector	css selector
 * @return	{Integer}
 */
function get_max_height(selector) {
	var mxh = colh = 0;
	$$(selector).each(function(col) {
		colh = col.getHeight();
		mxh = Math.max(mxh, colh);
	});
	return mxh;
}

/**
 * Matches the height of all elements that match selector
 *
 * @param	{String}	selector	css selector
 * @return	{void}
 */
function match_height(selector) {
	var mxh = get_max_height(selector);
	$$(selector).each(function(col) {
		col.setStyle({height: mxh+'px'});
	});
}

/**
 * Checks for a valoue in an array
 *
 * @param	{mixed}		niddle
 * @param	{Array}		haystack
 * @return	{Boolean}
 */
function in_array(niddle, haystack) {
	var o = {};
	for(var i = 0; i < haystack.length; i++) {
		o[haystack[i]] = '';
	}
	return niddle in o;
}

