if (window.addEventListener)
{
    window.addEventListener('mousemove', hideMenu, true);
    window.addEventListener('click', hideSpecDescription, true);
    window.addEventListener('resize', hideSpecDescription, true);
}
else
{
    window.document.attachEvent('onmousemove', hideMenu);
    window.document.attachEvent('onclick', hideSpecDescription);
    window.document.attachEvent('onresize', hideSpecDescription);
}

var menuTimer = new Array();
var skipTimer = false;
var enableMenuTimerTimer = new Array();

function changeTab(id)
{
    var tabs         = document.getElementById('itempagetabcontainertabs').getElementsByTagName('a');
    var contentBoxes = document.getElementById('itempagetabcontainercontents').getElementsByTagName('a');
    
    for (var i in tabs)
    {
        var tab = tabs[i];
        if (tab.id != undefined)
        {
            var tabID = tab.id.split('_')[1];
            var contentsBox = document.getElementById('itempagetabcontents_' + tabID);
            
            if (contentsBox != null)
            {
                if (id == tabID)
                {
                    tab.className = 'selected';
                    contentsBox.style.display = 'inherit';
                }
                else
                {
                    tab.className = '';
                    contentsBox.style.display = 'none';
                }
            }
        }
    }
    
    return false;
}

function enableMenuTimer()
{
    window.skipTimer = false;
    //document.getElementById('debug').innerHTML += "Enabled<br>";
}

function disableMenuTimer()
{
    window.skipTimer = true;
    //document.getElementById('debug').innerHTML += "Disabled<br>";
}

function startMenuTimer(id)
{
    if (window.skipTimer)
    {
        showMenu(id);
    }
    else
    {
        clearTimeout(window.menuTimer);
        window.menuTimer.push(setTimeout('showMenu(' + id + ');', 200));
    }
}

function showMenu(id)
{
    hideMenu();
    
    disableMenuTimer();
    
    for (var i in window.enableMenuTimerTimer)
        clearTimeout(window.enableMenuTimerTimer[i]);
    
    var button = document.getElementById('menuitem_' + id);
    
    var dropdown = document.getElementById('menudropdown_' + id);
    
    button.className       = 'hovered';
    dropdown.style.display = 'inherit';
    
    if (button.offsetWidth > dropdown.offsetWidth)
        dropdown.style.width = (button.offsetWidth - 2) + 'px';
    
    dropdown.style.top     = (button.offsetTop + button.offsetHeight) + 'px';
    dropdown.style.left    = button.offsetLeft + 'px';
}

function hideMenu(e)
{
    var hide = true;
    
    if (e)
    {
        var currentNode = e.target || e.srcElement;
        
        while (true)
        {
            if (currentNode != null && currentNode.id != undefined && (currentNode.id.split('_')[0] == 'menuitem' || currentNode.id.split('_')[0] == 'menudropdown'))
            {
                hide = false;
                break;
            }
            
            if (currentNode == undefined || (currentNode != undefined && currentNode.tagName == 'BODY'))
                break;
            
            currentNode = currentNode.parentNode;
        }
        
        if (hide)
            window.enableMenuTimerTimer.push(setTimeout('enableMenuTimer();', 1000));
    }
    
    if (hide)
    {
        var topLinks = document.getElementById('toplinks');
        
        for (var i in topLinks.children)
        {
            if (topLinks.nodeType == 1)
            {
                if (topLinks.children[i].tagName == 'DIV')
                    topLinks.children[i].style.display = 'none';
                else if (topLinks.children[i].tagName == 'A')
                    topLinks.children[i].className = '';
            }
        }
        
        for (var i in window.menuTimer)
            clearTimeout(window.menuTimer[i]);
            
        window.menuTimer = new Array();
    }
}

function setRating(id, rating, save)
{
    if (save)
        document.getElementById('currentrating').value = rating;
        
    if (rating == undefined)
        rating = document.getElementById('currentrating').value;
    
    var images = document.getElementById(id).getElementsByTagName('IMG');
    
    for (var i in images)
    {
        var image = images[i];
        
        if (i < rating)
            image.src = 'images/fullstarbig.png';
        else
            image.src = 'images/emptystarbig.png';
    }
}

function showSpecDescription(specID, node)
{
    var xmlHttp = newAjax('getspecdescription.php?specid=' + specID, 'GET');
    
    xmlHttp.node = node;
	xmlHttp.onreadystatechange=function()
	{
		if(this.readyState == 4)
		{
            var spec = this.responseText.split('|');
            
            var oldNode = document.getElementById('spec_description_popup');
            
            if (oldNode != undefined)
                document.body.removeChild(oldNode);
            
            var container = document.createElement('DIV');
            container.id = 'spec_description_popup';
            
            var closeNode = document.createElement('SPAN');
            closeNode.setAttribute('onclick', '');
            
            container.appendChild(closeNode);
            
            if (spec[3] != '')
            {
                var imageNode = document.createElement('IMG');
                imageNode.src = 'specimages/' + spec[3];
                imageNode.align = 'left';
                
                container.appendChild(imageNode);
            }
            
            var h4Node = document.createElement('H4');
            h4Node.innerHTML = spec[1];
            
            var pNode = document.createElement('P');
            pNode.innerHTML = spec[2];
            
            container.appendChild(h4Node);
            container.appendChild(pNode);
            
            document.body.appendChild(container);
            
            var coordinates = getCoordinates(this.node);
            
            container.style.left = (coordinates.Left) + 'px';
            container.style.top = (coordinates.Top + 25) + 'px';
        }
    }
}

function newAjax(url, method, params)
{
	var xmlHttp;
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert ("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	xmlHttp.open(method, url, true);
	
	if (params != undefined)
	{
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	}

	xmlHttp.send(params);
	
	return xmlHttp;
}

function getCoordinates(node)
{
    var left = 0;
    var top = 0;

    while (node.tagName != 'BODY')
    {
        left += node.offsetLeft;
        top += node.offsetTop;

        node = node.offsetParent;
    }

    return {Left: left, Top: top};
}

function hideSpecDescription(e)
{
    var node = e.target || e.srcElement;
    
    if (node == undefined || node.tagName == undefined)
        node = document.body;
    
    while (node.tagName != 'BODY' || node.id == 'spec_description_popup')
        node = node.parentNode;
    
    var specPopup = document.getElementById('spec_description_popup');
    
    if (node.tagName == 'BODY')
        if (specPopup != undefined)
            document.body.removeChild(specPopup);
}

function showModule(num)
{
    var thumbnails = document.getElementById('home_spinner_thumbnails').children;
    
    for (var i in thumbnails)
    {
        if (thumbnails[i].className != undefined)
        {
            if (thumbnails[i].id != undefined && thumbnails[i].id.split('_')[3] == num)
                thumbnails[i].className = thumbnails[i].className.replace(' home_spinner_thumbnail_selected', '') + ' home_spinner_thumbnail_selected'; // 'home_spinner_thumbnail home_spinner_thumbnail_selected';
            else
                thumbnails[i].className = thumbnails[i].className.replace(' home_spinner_thumbnail_selected', '');//= 'home_spinner_thumbnail';
        }
    }
    
    var selectedModule = document.getElementById('home_spinner_module_' + num);
    
    //selectedModule.className = 'home_spinner_module home_spinner_module_shown';
    
    document.getElementById('home_spinner_modules').appendChild(selectedModule);
    
    setTimeout(revealModule, 1);
    
    setTimeout(hideModules, 500);
}

function hideModules()
{
    var modules = document.getElementById('home_spinner_modules').children;
    
    var j = 0;
    for (var i in modules)
    {
        if (j < modules.length - 1)
        {
            modules[i].className = 'home_spinner_module';
        }
        
        j++;
    }
}

function revealModule()
{
    var modules = document.getElementById('home_spinner_modules').children;
    
    modules[modules.length - 1].className = 'home_spinner_module home_spinner_module_shown';
}
