/**
 * Utilities library
 *
 * @author Vali Dumitru
 */
var Utils = window.Utils || {};

/**
 * Redirects the user to a given URL
 *
 * @param {String} url
 */
Utils.goToURL = function(url) {
	if(url == undefined || url == null) {
		alert('Invalid URL passed in (Utils::goToURL)!');
		return false;
	}

	window.location.href = url;
}

/**
 * Fixes the links that need to be opened in another window/parent window, etc.
 * Using the target attribute on links in XHTML Strict breaks the markup validation
 */
Utils.parseLinks = function() {
	var allLinks = $$('body a[rel=external]');
	var totalLinks = allLinks.length;

	if (totalLinks > 0) {
		for (var i = 0; i < totalLinks; i++) {
			allLinks[i].target = '_blank';
		}
	}
}

Utils.hoverMenuItem = function(item) {
	if(!item.className.match('hovered')) item.className += ' hovered';
}

Utils.moveoutMenuItem = function(item) {
	item.className = item.className.replace('hovered', '');
}

/**
 * Embeds a SWF into a page using the given options
 *
 * @param {Object} options
 */
Utils.embedSWF = function(options)
{
	var defaults = {
		url: '',
		container: 'elementID',
		width: 500,
		height: 350,
		flashVars: {},
		parameters: {
			quality: 'high',
			menu: false,
			wmode: 'transparent'
		},
		attributes: {}
	};

	if(options == undefined || options == null)
	{
		options = defaults;
	}
	else
	{
		// Allow skipping certain options to be set to the default values
		// You can pass in only the options you want to override
		for(var x in defaults)
		{
			eval('if(!options.' + x + ') options.' + x + ' = defaults.' + x + ';');
		}
	}

	swfobject.embedSWF(options.url, options.container, options.width, options.height, '9.0.0', '', options.flashVars, options.parameters, options.attributes);
}

/**
 * Reverses the given string and returns it. If other data type than a string is passed in, an empty string is returned
 *
 * @param string {String}
 *
 * @returns {String} The reversed string
 */
Utils.reverseString = function(string)
{
	if(string == undefined || string == null || typeof string != 'string') return '';

	var reversed = '';

	for(var i = (string.length - 1); i >= 0; i--)
	{
		reversed += string.charAt(i);
	}

	return reversed;
}

/**
 * Decodes an obfuscated e-mail anchor, setting its target correctly and forwarding the user to that e-mail address.
 * Useful against crawlers and/or other types of robots
 *
 * @param sourceElement {DOMElement}
 *
 * @returns {Boolean} The status of the decoding operation
 */
Utils.sendEmail = function(sourceElement)
{
	if(sourceElement != undefined && sourceElement != null)
	{
		sourceElement.href = 'mailto:' + Utils.reverseString(sourceElement.innerHTML);

		return true;
	}

	return false;
}

