﻿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();
			};
		}
	}
}

/*
 * Most of these fuctions do not check for object existance.
 * There are primarily function that are meant to be called in other function/methods.
 */
function getEventTarget(event)
{
	var targetElement = null;
	
	targetElement = (typeof event.target != "undefined") ? event.target : event.srcElement;
	
	while (targetElement.nodeType == 3 && targetElement.parentNode != null)
	{
		targetElement = targetElement.parentNode;
	}
	
	return targetElement;
}

/**
 * Insert an element after a particular node
 * @param {Object} parent
 * @param {Object} node
 * @param {Object} referenceNode
 */
function insertAfter(parent, node, referenceNode) 
{
	parent.insertBefore(node, referenceNode.nextSibling);
}

/**
 * Opens a new window.
 * @param {String} theURL
 * @param {String} winName
 * @param {String} features
 */
function MM_openBrWindow(theURL,winName,features)
{ //v2.0
    window.open(theURL,winName,features);
}

/**
 * Grab Elements from the DOM by className
 * @param {Object} searchClass
 * @param {Object} node - if provided, only its children are searched
 * @param {Object} tag
 * @return {Array}
 */
function getElementsByClass (searchClass, node, tag) 
{
	var classElements = new Array();
	
	if ( node == null ) { node = document; }
	if ( tag == null ) { tag = '*'; }	
	
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	
	for (i = 0, j = 0; i < elsLen; i++) 
	{
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/**
 * Grab Elements from the DOM by tag name.
 * @param {Object} searchClass
 * @param {Object} node - if provided, only its children are returned
 * @param {Object} tag
 * @return {Array}
 */
function getElements (node, tag)
{
	if ( node == null ) { node = document; }
	if ( tag == null ) { tag = '*'; }
		
	var els = node.getElementsByTagName(tag);
	return els;
}

/** 
 * Returns an element by tag and id (only looks through specified tag)
 * @param tag_name 
 * @param elm_id
 * @return elm/false - the element if it exists, false otherwise.
 */
function getElement (node, tag, elm_id) 
{
	if ( node == null ||  node == '') 
	{
		node = document;
	}
	var elms = node.getElementsByTagName(tag);
	
	if (!elms) { return false; }
	
	var elmsLen = elms.length;
	var elm = false;
	
	for (var i=0; i < elmsLen; i++) 
	{
		var anchor = elms[i];
		if (anchor.id == elm_id) 
		{
			var elm = anchor;
			break;
		}
	}
	
	if (elm) {
		return elm;
	} 
	else {
		return false;	
	}

}

function toggle (targetId)
{
	if (typeof document.getElementById == 'undefined') { return false; }
	
	var elm = document.getElementById(targetId);
	var d = elm.style.display;
	d = (d == 'none') ? 'block' : 'none';
	elm.style.display = d;
}

function show (targetId)
{
	if (typeof document.getElementById == 'undefined') { return false; }
	
	var elm = document.getElementById(targetId);
	var d = elm.style.display;
	
	if (d == "none") 
	{
		elm.style.display = 'block';
	} 
	else return false;
}

function hide (targetId)
{
	if (typeof document.getElementById == 'undefined') { return false; }
	
	var elm = document.getElementById(targetId);
	var d = elm.style.display;
	
	if (d == "block" || d == "") 
	{
		elm.style.display = 'none';
	} 
	
	return true;
}

/**
 * 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);
}

/**
* Checks if some common objects exist
* (getElementById, getElementsByTagName, createTextNode)
* @return {Boolean} true if they do, false otherwise
* --------------------------------------------------------------------------- */
function objectsExist ()
{
	if (typeof document.getElementById == 'undefined') { 
		return false;
	}
	else if (typeof document.getElementsByTagName == 'undefined') {
		return false;
	}
	else if (typeof document.createTextNode == 'undefined') {
		return false;
	}
	else {
		return true;
	}
}

/**
 * Checks for the existance of an object(s)
 * If no object is provided it checks for all common objects.
 * @see {objects_exists}
 * @param {String} obj Determines object to check - accepts id/tag or empty
 * @return {Boolean}
 */
function objectExists (obj)
{
	switch (obj)
	{
		case 'id':
			obj = typeof document.getElementById;
			break;
		
		case 'tag':
			obj = typeof document.getElementsByTagName;
			break;
	}
	
	if (obj != '') 
	{
		if (obj != 'undefined') { return true; }
		else { return false; }
	}
	else { // check common objects
		return objects_exist();
	}
}

/**
 * Removes the rectangle that appears around links in Firefox.
 * -------------------------------------------------------------------------- */
var removeRectangle = function() 
{
    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;
        };
    }
};

/**
 * 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/scripts/';

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


// -- 
var newWindow = null;

function closeWin()
{
	if (newWindow != null) {
		if (!newWindow.closed) {
			newWindow.close();
		}
	}
}
/**
 * Pops up a new window with the parameters given.
 * @param {Object} url
 * @param {Object} type - standard/console/fullscreen
 * @param {Object} strWidth - Window width
 * @param {Object} strHeight - Window height
 */
function popUpWin(url, type, strWidth, strHeight)
{	
	closeWin();
		
	type = type.toLowerCase();
	// standard   = with toolbar 
	// console    = without toolbar 
	// fullscreen = fullscreen with no toolbar	
	
	if (type == "fullscreen") {
		strWidth  = screen.availWidth;
		strHeight = screen.availHeight;
	}
	var tools="";
	if (type == "standard") {
		tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
	}
		
	if (type == "console" || type == "fullscreen") {
		tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=0,top=0";
	}
	newWindow = window.open(url, 'newWin', tools);
	newWindow.focus();
}

/**
 * Triggers popup - used by addPopUps()
 * @see addPopUps()
 * @param {Object} e
 */
function doPopUp(e)
{
	//set defaults - if nothing in rel attrib, these will be used
	var t = "console"; // for definitions, see popUpWin()
	var w = "780";
	var h = "580";
	
	//look for parameters
	attribs = this.rel.split(" ");
	if (attribs[1]!=null) {t = attribs[1];}
	if (attribs[2]!=null) {w = attribs[2];}
	if (attribs[3]!=null) {h = attribs[3];}
	
	//call the popup script
	popUpWin(this.href,t,w,h);
	
	//cancel the default link action if pop-up activated	
	if (window.event) 
	{
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	} 
	else if (e) 
	{
		e.stopPropagation();
		e.preventDefault();
	}
}
/**
 * Adds onclick event to popup links.
 * Popup links should consist of rel attribute i.e. rel="popup"
 * Additional parameters can be given like so: rel="popup console 800 600"
 * Only the "popup" parameter is mandatory.
 * If the other 3 are not provided the defaults will be used.
 * The defaults are set in doPopUp()
 */
function addPopUpTriggers()
{
	var popups = document.getElementsByTagName("a");
	for (i=0;i<popups.length;i++)
	{
		if (popups[i].rel.indexOf("popup")!=-1)
		{
			// attach popup behaviour
			popups[i].onclick = doPopUp;
			// add popup indicator
			/*
			if (popups[i].rel.indexOf("noicon")==-1)
			{
				popups[i].style.backgroundImage = "url(pop-up.gif)";
				popups[i].style.backgroundPosition = "0 center";
				popups[i].style.backgroundRepeat = "no-repeat";
				popups[i].style.paddingLeft = "15px";
			}
			*/
			// add info to title attribute to alert fact that it's a pop-up window
			popups[i].title = popups[i].title + " [Opens in pop-up window]";
		}
	}
}

/**
 * Adds onmouseover effect on table rows
 * -------------------------------------------------------------------------- */
function tableHover ()
{
    if (typeof document.getElementsByTagName == 'undefined') { return false; }
	
    var tables = document.getElementsByTagName("table");

    for (var i=0; i < tables.length; i++)
    {
    	if (tables[i].className.match("hover"))
    	{ 
    		var rows = tables[i].rows;
			for (var i=0; i < tables.length; i++)
			{
				tables[i].onclick = function() {
					popImage(this.href, this.title);
					return false;
				};
			}
    	}
    }
}
/**
 * Adds odd/even class to table rows
 * -------------------------------------------------------------------------- */
function tableRuler()
{
    //if (!objectsExist) { 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');
            var ruler_class = 'ruled';
			
			for (var j=0; j<trs.length; j++)
            {
                if (trs[j].parentNode.nodeName == 'TBODY' && trs[j].parentNode.nodeName != 'TFOOT')
                {                    
					trs[j].onmouseover = function() 
					{ 
						if (this.className.match("odd")) {
							var after_class = 'odd';
						}
						else {
							var after_class = '';
						}
						
						this.className=ruler_class; 
						
						this.onmouseout = function() { this.className=after_class; return false }
						return false ;
					}
					
                }
            } // row loop
        }
    }// table loop
}

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";
		}
	}
}

/** ----------------------------------------------------------------------------
* Writes the obfuscated mailto link to the page.
* @param {string} mailbox - Mailbox user name.
* @param {string} domain - if not provided defaults to soel.gr
* @param {String} link_text - if not provided, email address is used
* @return {html mailto link}
---------------------------------------------------------------------------- */
function showAddress (mailbox, domain, link_text, link_title)
{
	if (mailbox == '') {
		return false;
	}
	
	if (domain == '') {
		var domain	= 'thelittlebabyshop.gr';
	}
	
	var email	 	= mailbox + '@' + domain;
	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;
	}
	
	if (link_title == '') 
	{
		var link_title = 'write email using your email client';
	}
		
	document.write('<a href="mailto:' + email_obf + '" title="'+ link_title + '">' + link_text_obf + '</a>');
}
// -- addEvent Statements -- //
//addEvent(window,"load",addPopUpTriggers);
//addEvent(window,"load",tableRuler);
addEvent(window,"load",removeRectangle);
addEvent(window,"load",externalLinks);
