if (typeof window.onload != 'function')
{
	window.onload = initMainMenu;
}else if (window.attachEvent){
	window.attachEvent('onload',initMainMenu);
}else if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", initMainMenu, false);
}

function initMainMenu()
{
	var el = document.getElementById('MainMenu');

	if (el)
	{
		var tds = el.getElementsByTagName('td');

		for(var i = 0, j=0; i < tds.length; i++)
		{
			if (tds[i].className != 'Splitter' && tds[i].className != 'SplitterSel')
			{
				tds[i].index = j;j++;
				if(tds[i].className != 'Sel')
				{
					tds[i].onmouseover = function (event) {SubMenu.handleOver(event, this);this.style.backgroundPosition = 'left -62px';}
					tds[i].onmouseout = function (event)  {SubMenu.handleOut(event, this);this.style.backgroundPosition = 'left 0px';}
				}
			}
		}

		SubMenu.init();
	}
}

/* Find A Word Game Scripts*/

var lblCheck = '';
var gameBasePath = '';

function clickEvent (object, action)
{
	if (object)
	{
		if (typeof object.onclick != 'function')
			object.onclick = action;
		else if (object.attachEvent)
			object.attachEvent('onclick', action);
		else if (object.addEventListener)
			object.addEventListener('click', action, false);
	}
}

function setOpacity (object, opacity)
{
    if (object && object.style)
    {
	    if (opacity < 0)
	    	opacity = 0;

	    if (opacity > 1)
	    	opacity = 1;

	    object.style.opacity = (opacity);
	    object.style.MozOpacity = (opacity);
	    object.style.KhtmlOpacity = (opacity);
	    object.style.filter="alpha(opacity="+opacity+")";
	 }
}

function findAWordPopup_show(e)
{
	var el = document.getElementById('findAWordPopup');
	var el_cont = document.getElementById('findAWordPopup_cont');

	el.style.display = "block";

	if (!e) var e = window.event;

	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}

	el.style.left = (3 + posx) + 'px';
	el.style.top  = (10 + posy) + 'px';


	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	if (targ)
	{
		var word = targ.innerHTML;
		var setid = targ.getAttribute('setid');
		var refid = targ.getAttribute('refid');
		var taskid = targ.getAttribute('taskid');

		el_cont.innerHTML = word + '<br /> <a href="' + gameBasePath + '&set=' + setid + '&ref=' + refid + '&task=' + taskid + '">' + lblCheck + '</a>';
	}

	if (e.stopPropagation)
	{
		e.stopPropagation()
	}else{
		window.event.cancelBubble = true;
	}

	return false;
}

function initWord( labelCheck, basePath )
{
	lblCheck = labelCheck;
	gameBasePath = basePath;

	var el_list = [];
	var el_popup = document.getElementById('findAWordPopup');
	var has_found = true;
	var i = 0;
	while(has_found)
	{
		var el = document.getElementById('findAWord_word_' + i);
		if (el)
		{
			el_list[el_list.length] = el;
		}else{
			has_found = false;
		}
		i++;
	}

	if (el_popup)
		setOpacity(el_popup, 0.8);

	clickEvent(el_popup, function ( e ) {
		if (!e) var e = window.event;

		if (e.stopPropagation)
		{
			e.stopPropagation()
		}else{
			window.event.cancelBubble = true;
		}
	});

	clickEvent(document, function () {
		document.getElementById('findAWordPopup').style.display = "none";
	});
}

var SubMenu = {
	timer_hide: null,
	timer_show: null,
	visible: false,
	delay: 500,
	browser_class: '',
	repositioned: {},
	main_menu_item_count: null,

	getMainMenuItemCount: function () {
		if (!this.main_menu_item_count) {
			var nodes = document.getElementById('MainMenu').getElementsByTagName('TD');
			var cnt = 0;
			for(var i=0,ii=nodes.length; i<ii; i++) {
				if ((nodes[i].className || '').indexOf('Splitter') == -1) cnt++;
			}
			this.main_menu_item_count = cnt;
		}

		return this.main_menu_item_count;
	},

	getElement: function () {
		return document.getElementById('MainSubMenu');
	},

	show: function (index, delay) {
		if (this.timer_show) clearTimeout(this.timer_show);

		if (typeof index == 'undefined') {
			index = this.index;
		} else {
			this.index = index;
		}

		if (delay && !this.visible) {
			if (this.timer_hide) clearTimeout(this.timer_hide);
			this.timer_show = setTimeout(function () {
				SubMenu.show(index);
			}, this.delay);

			return;
		}

		if (this.timer_hide) clearTimeout(this.timer_hide);

		var last = (index == this.getMainMenuItemCount() - 1);
		var element = this.getElement();
		element.style.display = 'block';

		if (typeof index != 'undefined') {
			element.className = this.browser_class + 'mm mm-' + (index + 1);
			element.parentNode.className = 'MainSubMenu ' + (last ? 'mm-last' : '');
		}

		this.visible = true;
		this.timer_hide = null;

		//Reposition elements, to remove huge whitespaces between elements
		/*
		if (!this.repositioned[index]) {
			this.repositioned[index] = true;

			var nodes = element.getElementsByTagName('DIV');
			var classname = 'cont-' + (index + 1);
			var items = [];

			for(var i=0,ii=nodes.length; i<ii; i++) {
				if (nodes[i].className.indexOf(classname) != -1 && nodes[i].parentNode.className.indexOf('sitemap') != -1) {

					var ch = nodes[i].childNodes;
					for(var i=0,ii=ch.length; i<ii; i++) {
						if (ch[i].nodeType == 1 && ch[i].className == 'list') {
							items.push(ch[i]);
						}
					}

					break;
				}
			}

			var pos = [0,0,0];
			for(var i=0,ii=items.length; i<ii; i++) {
				var item = items[i];
				var offset = [item.offsetLeft, item.offsetTop, item.offsetWidth, item.offsetHeight];
				var col = (offset[0] == 0 ? 0 : (offset[0] == offset[2] ? 1 : 2));

				if (offset[1] > pos[col]) {
					item.style.marginTop = '-' + (offset[1] - pos[col]) + 'px';
				}
				pos[col] += offset[3];
			}
		}
		*/

		//Position menu
		var node = document.getElementById('MainSubMenu');
		var w = document.getElementById('MainMenu').offsetWidth / this.getMainMenuItemCount();
		node.style.width = w + 184 + 'px';
		node.style.left = w * index  + (last ? -184 : 0) + 'px';

		//Position arrow
		var arrow = document.getElementById('MainSubMenuArrow');
		var w = document.getElementById('MainMenu').offsetWidth / this.getMainMenuItemCount();
		arrow.style.width = w - 14 + 'px';
	},

	hide: function (delayed) {
		if (this.timer_show) clearTimeout(this.timer_show);
		if (!this.visible) return;
		if (delayed) {
			if (this.timer_hide) clearTimeout(this.timer_hide);
			this.timer_hide = setTimeout(function () {
				SubMenu.hide();
			}, this.delay);
		} else {
			this.getElement().style.display = 'none';
			this.visible = false;
			this.index = -1;
		}
	},

	isChild: function (parent, child) {
		while(child && child.parentNode) {
			if (child.parentNode === parent) return true;
			child = child.parentNode;
		}
		return false;
	},
	/**
	 * Returns true if mouseover/mouseout event qualifies as mouseenter/mouseleave
	 */
	isMouseEnterLeave: function (event) {
		var event = event || window.event;
		var target = event.currentTarget;
		var related = event.relatedTarget;

		if (!this.isChild(target, related)) {
			return true;
		}
		return false;
	},

	handleOver: function (event, node) {
		if (this.isMouseEnterLeave(event)) {
			this.show(node.index, true);
		}
	},
	handleOut: function (event, node) {
		if (this.isMouseEnterLeave(event)) {
			this.hide(true);
		}
	},

	handleMouseOver: function () {
		this.show();
	},

	handleMouseOut: function () {
		this.hide(true);
	},

	init: function () {
		var element = this.getElement();
		element.onmouseover = function (event) {if (SubMenu.isMouseEnterLeave(event)) SubMenu.handleMouseOver(event);};
		element.onmouseout = function (event) {if (SubMenu.isMouseEnterLeave(event)) SubMenu.handleMouseOut(event);};

		var msie = (navigator.appName=='Microsoft Internet Explorer') ? parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]) : -1;
		if (msie != -1) {
			this.browser_class = 'ie' + msie + ' ';
			this.getElement().className = this.browser_class + this.getElement().className;
		}
	}
};

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) {return s;} : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

jQuery.getUrlParameterByName = function (location, name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(location);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}



/**
 * For tracking hits to external links
 */
$(window).load(function () {

	window.ajaxRequest = function (href, getParameters, new_window) {

		var uri = '/ajax/request.php' + getParameters,

			params = href.replace(/^.*\?/, '');

		$.get(uri, params, function () {
			if (new_window) {
				window.open(href);
			} else {
				document.location = href;
			}
		});
	};

	window.bannerTrack = function(url, banner_id, library_id, new_window) {
		var getParameters = '?action=BannerTrack&country=' + jQuery.cookie('supraLang') + '&banner_id=' + banner_id + '&library_id=' + library_id;
		ajaxRequest(url, getParameters, new_window);
	}

	$('a[href*="/redirect"]').mousedown(function (e) {

		var getParameters = '?action=Promo' + '&country=' + jQuery.cookie('supraLang');
		var href = $(this).attr('href'),
			new_window = (e.button == 3);

		ajaxRequest(href, getParameters, new_window);

		e.preventDefault();
	});
        
        /**
         * For tracking hits to download links
         */
        $('a[href*="/download"]').mousedown(function (e) {
                var href = $(this).attr('href');
                var file = jQuery.getUrlParameterByName(href, 'file');
                if(file) {
                    var dataJSON = {"action": 'FileDownloadTrack', "file": file, "country": jQuery.cookie('supraLang')};
                    jQuery.get("/ajax/request.php", dataJSON );
                    $(this).attr('href', file);
                }
                e.preventDefault();
	});

});

/**
 * For new tracking engine
 */

var dataJSON = {"action": 'SupraTrack', "path": window.location.pathname, "country": jQuery.cookie('supraLang')};

jQuery.get("/ajax/request.php", dataJSON );




