/* ----------------------------------------------------------------------------
Title:				Global Javascript Functions - www.ark.info
File:				/assets/js/global.js
Author:				George Komianos
Copyright:			2008 gkmedia.gr - Unless otherwise noted.
					No portion of this document may be reproduced, unless otherwise stated.
					Any GNL code is noted accordingly.

Created:
Modified:

TODO:
BUG/FIX:
---------------------------------------------------------------------------- */

function addEvent(obj, evType, fn, useCapture)
{
    if (obj.addEventListener) 
	{
        obj.addEventListener(evType, fn, useCapture);
        return true;
    }
    else if (obj.attachEvent) 
	{
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    }
    else {
       obj["on"+evType] = fn;
  }
}


function attachEventListener(target, eventType, functionRef, capture)
{
	if (typeof target.addEventListener != "undefined")
	{
		target.addEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.attachEvent != "undefined")
	{
		target.attachEvent("on" + eventType, functionRef);
	}
	else 
	{
		eventType = "on" + eventType;
	
		if (typeof target[eventType] == "function")
		{
			var oldListener = target[eventType];
		
			target[eventType] = function()
			{
				oldListener();
				return  functionRef();
			};
		}
		else 
		{
			target[eventType] = functionRef;
		}
	}
	return true; 
}

function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		var oldfn = window.onload;
		if (typeof window.onload != 'function')
		{
		  window.onload = fn;
		}
		else
		{
			window.onload = function()	
			{
				oldfn();
				fn();
			};
		}
	}
}


/**
 * Obfuscates provided string.
 * @param {String} text
 * @return {String} 
 * -------------------------------------------------------------------------- */
function obfuscate (text)
{
    var obfuscated = '';

    for (i = 0; i < text.length; i++) 
	{
		obfuscated += "&#" + text.charCodeAt(i);
	}
	return (obfuscated);
}

/**
 * Prints an obfuscated mailto link.
 * If link_text is not provided the email address is used instead.
 * @param {String} mailbox - Mailbox user name
 * @param {String} link_text -  String with the link text (optional)
 * @return {html}
 * -------------------------------------------------------------------------- */
function show_address (mailbox, link_text)
{
	if (mailbox == '') {
		mailbox = 'mail';
	}

	var domain_name	= 'animinds.gr';
	var email	 	= mailbox + '@' + domain_name;
	var email_obf 	= obfuscate (email);

	// use the link_text provided or the address if it's not.
	if (link_text != '') {
		var link_text_obf = obfuscate (link_text);
	}
	else {
		var link_text_obf = email_obf;
	}
	document.write('<a href="mailto:' + email_obf + '">' + link_text_obf + '</a>');
}

// -- addEvent Statements -- //
// addEvent(window,"load", function_name);


