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

function createElement(element)
{
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

function externalLinks()
{
	if (!document.getElementsByTagName) return;

	var anchors = document.getElementsByTagName("a");

	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && 	anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}

function removeRectangle ()
{
    var lnks = document.links;
    if (!lnks) { return false; }

	var lg = lnks.length;

	for (var i = 0; i < lg; i++)
	{
        lnks[i].onmousedown = function()
		{
        	this.blur();
        	return false;
        };
    }
}

function tableRuler()
{
    if (!document.getElementById || !document.getElementsByTagName) { return false; }

    var tables = document.getElementsByTagName ('table');

    for (var i=0;i<tables.length;i++)
    {
        if (tables[i].className.match("ruler"))
        {
			var trs = tables[i].getElementsByTagName('tr');
            for (var j=0; j<trs.length; j++)
            {
                if (trs[j].parentNode.nodeName == 'TBODY' && trs[j].parentNode.nodeName != 'TFOOT')
                {
					trs[j].onmouseover = function() { original_class = this.className; this.className='ruled'; return false }
                    trs[j].onmouseout = function() { this.className = original_class; return false }
                }
            }
        }
    }
}

/**
 * Adds a javascript file link to the head of the page.
 * -------------------------------------------------------------------------- */
function include_dom(script_filename) 
{
    if (typeof document.getElementsByTagName == 'undefined') { return false; }
	
	var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

included_files = new Array();

/**
 * Adds/Includes a javascript file "once"
 * @see {in_array, include_dom}
 * @param {String} script_filename
 * -------------------------------------------------------------------------- */
function include_once(script_filename) 
{
    if (!in_array(script_filename, included_files)) 
	{
        included_files[included_files.length] = script_filename;
        include_dom(script_filename);
    }
}

/**
 * @param {Object} needle
 * @param {Object} haystack
 * @return {Boolean}
 * -------------------------------------------------------------------------- */
function in_array(needle, haystack) 
{
    for (var i = 0; i < haystack.length; i++) 
	{
        if (haystack[i] == needle) { return true; }
    }
    return false;
}

// -- set global variables -- //
DIR_ICONS_URI	= '/assets/icons/';
DIR_JS_URI		= '/assets/js/';
DIR_PHP_URI 	= '/assets/scripts/';

// -- inlcude global scripts -- //
include_once(DIR_JS_URI + 'common.js');
include_once(DIR_JS_URI + 'dialog.js');
include_once(DIR_JS_URI + 'locations.js');
include_once(DIR_JS_URI + 'menu.js');

Array.prototype.exists = function (x) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == x) return true;
    }
    return false;
}

function navNestNew ()
{
	if (!document.getElementById && !document.createTextNode) {
		return;
	}
    if (document.getElementById('nav-side')) {
		var container = document.getElementById('nav-side');	// the div container
	}
	else {
		return;
	}

	var menu = container.childNodes[1];
    //var menu = container;

	var activeSubClass = 'activesub';			// class used to for the active (open) sublist
	var inactiveSubClass = 'notactivesub';		// class used to for the inactive (hidden) sublists

	var parentClass = 'isParent';				// class assigned to all LIs with a sublist
	var parentOpen = 'parentOpen';				// class assigned to all LIs with a sublist
	var activeParentClass = 'isActive';			// class assigned to the parent LI of the active sublist
	var activeOpen = 'activeOpen';

	var toHideClass = 'hideChild';				// class used to hide an element
	var toShowClass = 'showChild';				// class used to show an element
	var currentClass = 'active';				// class of the current link

	// only proceed if the navigation element exists
	if (menu) {

		// set the variables to be used
		var lis, i, firstUL, j, apply;

		lis = menu.getElementsByTagName('li');		// get all the LIs in the menu

		// loop through all LIs and check which ones have a nested UL
		for (i=0; i<lis.length; i++)
		{
			firstUL = lis[i].getElementsByTagName('ul')[0]; // since we're looping there is only one sublist at a time, thus [0]
            //
			// if there is a nested UL, deactivate the first nested link and apply the parentClass
			if (firstUL) {
				lis[i].childNodes[0].onclick = function() { return false; } 				// deactivate
				lis[i].className += lis[i].className == ''?parentClass:' '+parentClass; 	// add the parentClass

                if (lis[i].className != 'undefined') {
                    //alert(lis[i].className);
                }
                    
				//var sublist = lis[i].getElementsByTagName('ul');
				var sublinks = lis[i].getElementsByTagName('li');

				for (a=0; a<sublinks.length; a++) {
					var parentUL = firstUL;			// the sublist

					// if an active LI is found, apply the activeSubClass to its UL and stop the loop,
					// otherwise apply the inactiveSubClass.
                    var classes = sublinks[a].className.split(" ");

                    if (classes.exists('active')) {
						//alert('found active');
                        firstUL.setAttribute ('class', activeSubClass);
						firstUL.setAttribute ('className', activeSubClass);
						lis[i].setAttribute ('class', activeOpen);
						lis[i].setAttribute ('className', activeOpen);

						//firstUL.className += lis[i].className == ''?activeSubClass:' '+activeSubClass;
						//lis[i].className += lis[i].className == ''?activeParentClass:' '+activeParentClass;
						break
					} else {
						//firstUL.className += lis[i].className == ''?inactiveSubClass:' '+inactiveSubClass;
						firstUL.setAttribute ('class', toHideClass);
						firstUL.setAttribute ('className', toHideClass);
					}
				} // loop of sub LIs
			} // loop of LIs with sublists
		} // loop of all menu LIs


		// apply the onclick event to the Parent LIs
		// find all the subULs and apply the onclick event to their Parent LI
		lis = menu.getElementsByTagName('li');
		for (i=0; i<lis.length; i++)
		{
			if ( (lis[i].className.match(parentClass)) || (lis[i].className.match(activeParentClass)) || (lis[i].className.match(activeOpen))  )
			{
				lis[i].onclick = function() { doMenuToggle (this); }
			}
		}

	} // end if menu


	function doMenuToggleNew (o)
	{

		lis = menu.getElementsByTagName('li');
		targetUL = o.childNodes[2];

		for (i=0; i<lis.length; i++)
		{
			var subUL = lis[i].getElementsByTagName('ul')[0];
			//var subUL = lis[i].getElementsByTagName('ul');

			/*
				if it's an active UL and it's the target UL, toggle it and hide the others
				if it's an active UL and it's not the target UL, toggle it and hide the others

				if it's an inactive and it's the target UL, toggle it and hide the others including the active one.
				if it's an inactive and it's not the target UL, toggle it and hide the others including the active one.

			*/

			if (subUL)
			{
				for (j=0; j<subUL.length; j++)
				{
					var parentLI = subUL[j].parentNode;
					//alert (parentLI.className);

                    var classes = subUL[j].className.split(" ");

                    if (classes.exists('active')) {

                    }
					if ( (classes.exists(activeSubClass)) || (classes.exists(inactiveSubClass)) &&  (targetUL == subUL[j]) )
                    //if ( ((subUL[j].className.match (activeSubClass)) || (subUL[j].className.match (inactiveSubClass))) &&  (targetUL == subUL[j]) )
					{
						if ( classes.exists(activeSubClass) || classes.exists(inactiveSubClass) )
						//if ( (subUL[j].className.match (activeSubClass)) || (subUL[j].className.match (inactiveSubClass)) )
						{ //alert ('found active target');
							if (subUL[j].style.display == "none")
							{
								subUL[j].setAttribute ('class', activeSubClass);
								subUL[j].setAttribute ('className', activeSubClass);
								subUL[j].style.display = "block";
								parentLI.setAttribute ('class', activeOpen);
								parentLI.setAttribute ('className', activeOpen);
							} else {
								subUL[j].setAttribute ('class', inactiveSubClass);
								subUL[j].setAttribute ('className', inactiveSubClass);
								subUL[j].style.display = "none";
								parentLI.setAttribute ('class', activeParentClass);
								parentLI.setAttribute ('className', activeParentClass);
							}
						}


					}
                    // if ( ((subUL[j].className.match (activeSubClass)) || (subUL[j].className.match (inactiveSubClass))) && (targetUL != subUL[j]) )
					if ( classes.exists(activeSubClass) ||
                        classes.exists(inactiveSubClass) &&
                         (targetUL != subUL[j]) ) { //alert ('found active but NOT target');
								subUL[j].setAttribute ('class', inactiveSubClass);
								subUL[j].setAttribute ('className', inactiveSubClass);
								subUL[j].style.display = "none";
								parentLI.setAttribute ('class', activeParentClass);
								parentLI.setAttribute ('className', activeParentClass);
					}

					if ( classes.exists(toHideClass) ||
                         classes.exists(toShowClass) &&
                         (targetUL == subUL[j]) ) {
						//alert ('found inactive target');
						if ( classes.exists(toShowClass) ) {
							subUL[j].setAttribute ('class', toShowClass);
							subUL[j].setAttribute ('className', toShowClass); 	// IE understands this
							o.setAttribute ('class', parentOpen);
							o.setAttribute ('className', parentOpen);
						}
						else if ( targetUL.className.match (toShowClass) ) {
							subUL[j].setAttribute ('class', toHideClass);
							subUL[j].setAttribute ('className', toHideClass); 	// IE understands this
							parentLI.setAttribute ('class', parentClass);
							parentLI.setAttribute ('className', parentClass);
						}
					}

					if ( classes.exists(toHideClass) ||
                         (classes.exists(toShowClass)) &&
                         (targetUL != subUL[j]) ) { //alert ('found inactive - NOT target');
							subUL[j].setAttribute ('class', toHideClass);
							subUL[j].setAttribute ('className', toHideClass); 	// IE understands this
							parentLI.setAttribute ('class', parentClass);
							parentLI.setAttribute ('className', parentClass);

					} //else return false;
					//alert (parentLI.className);
				} // end subULs loop
			} // end subUL conditional
		} // end all LIs loop

	} // doMenuToggle
    return;
}

function show_address (mailbox, link_text)
{
	if (mailbox == '') {
		mailbox = 'info';
	}

	var domain_name	= 'corfuxenos.gr';
	var email	 	= mailbox + '@' + domain_name;
	//var email_obf 	= obfuscate (email);
	var email_obf = '';
	for (i = 0; i < email.length; i++) 
	{
		email_obf += "&#" + email.charCodeAt(i);
	}
	

	// 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 + '" title="start your email client to send message to this address">' + link_text_obf + '</a>');
}

function addFormToggle () 
{
	if (typeof document.getElementById == 'undefined') { return false; }
	
	var elm = document.getElementById('toggle-form');
	if (!elm) return false;
}


if(document.getElementById && document.createTextNode)
{
	addEvent(window, 'load', externalLinks);
	addEvent(window, 'load', removeRectangle);
	addEvent(window, 'load', tableRuler);
    //addEvent(window, 'load', navNest);
}

