/*
 *	Module containing functions for showing and hiding navs
 *
 *-------------------------------------------------------------
 * d1	05-Dec-2005		Matt Stevenson		Created
 * d2	06-Dec-2005		Matt Stevenson		Modified hide_menu to check for correct event
 *											Added the contains method to do this
 * d3	08-Dec-2005		Matt Stevenson		Fixed problem with pageHeight changing
 * d4	21-Dec-2005		Matt Stevenson		Added method to set section on page load
 * d5	29-Dec-2005		Matt Stevenson		Added prepPage method
 * d6	16-Jan-2006		Matt Stevenson		Modified show_menu to check for height past bottom of the page
 * d7	17-Jan-2006		Matt Stevenson		Fixed pageHeight problem with small pages
 * d8	18-Jan-2006		Matt Stevenson		Incorporated opacity functionality
 *-------------------------------------------------------------
 */

var product = false;
var detect = navigator.userAgent.toLowerCase();


function show_menu(id)
{
	//ignore safari for the time being...
	if (!checkIt('safari'))
	{
		var pageHeight = document.body.clientHeight;

		document.getElementById('a_'+id).className = "on";
		document.getElementById('li_'+id).className = "on";

		//make sure opacity is 0 to start with...
		document.getElementById("menu_"+id).style.opacity = 0
		document.getElementById("menu_"+id).style.filter = "alpha(opacity=0)";

		//display the menu...
		document.getElementById("menu_"+id).style.display = 'block';
	
		//fade it in...
		shiftOpacity("menu_"+id, 300);
	
		//see if we need to show the menu above the menu link (stops page extending problem)
		var testHeight = getElementPosition("menu_"+id).top + document.getElementById("menu_"+id).offsetHeight;
	
		if (testHeight > document.body.clientHeight)
		{
			var newPos = getElementPosition("menu_"+id).top - testHeight;
			document.getElementById("menu_"+id).style.marginTop = newPos + "px";
		}

		//ie messes around with the page height unless we do this...
		document.getElementById('pageHeight').style.height = pageHeight;
	}
}


function getElementPosition(elemID)
{ 
	//returns position object which has fields top and left...
	var offsetTrail = document.getElementById(elemID); 
	var offsetLeft = 0; 
	var offsetTop = 0; 
 
	while (offsetTrail)
	{ 
		offsetLeft += offsetTrail.offsetLeft; 
		offsetTop += offsetTrail.offsetTop; 
		offsetTrail = offsetTrail.offsetParent; 
	}	 
 
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined')
	{ 
		offsetLeft += document.body.leftMargin; 
		offsetTop += document.body.topMargin; 
	} 
 
	return {left:offsetLeft,top:offsetTop}; 
} 


function hide_menu(name, event)
{
	//ignore safari for the time being...
	if (!checkIt('safari'))
	{
		var menu = document.getElementById('menu_'+name);
		var pageHeight = document.body.clientHeight;
	
		//check if the contains method exists...
		if (menu.contains)
		{
			//if so - use that to determine whether we are
			//reacting to an event from the div or one of its
			//children
			if (!menu.contains(event.toElement))
			{
				menu.style.display = 'none';

				if (section != name)
				{
					document.getElementById('a_'+name).className = "";
					document.getElementById('li_'+name).className = "";
				}
				else
				{
					//ie messes around with the page height unless we do this...
					document.getElementById('pageHeight').style.height = pageHeight;
				}
			}
		}
		else
		{
			//alternatively use the mozilla contains method...
			if (!contains(menu, event.relatedTarget))
			{
				if (section != name)
				{
					document.getElementById('a_'+name).className = "";
					document.getElementById('li_'+name).className = "";
				}

				menu.style.display = 'none';
			}
		}
	}
}

function contains (container, containee) 
{ 
    var contains = false; 
    
	while (containee) 
	{ 
      if ((contains = container == containee))
	  {
		break;
	  }
      containee = containee.parentNode; 
    } 
    return contains; 
} 


function setSection ()
{
	//make sure we have been given a section
	if (section)
	{
		//check if we have the elements that need to be set...
		if (document.getElementById('a_'+section))
		{
			document.getElementById('a_'+section).className = "on";
		}

		if (document.getElementById('li_'+section))
		{
			document.getElementById('li_'+section).className = "on";
		}
	}
}


function prepPage ()
{
	setSection();
	
	//initialise the hidden fields on the basket (if this is the basket page!)
	if (section == 'shoppingbasket')
	{
		initHiddens();
	}

	if (window.isProduct)
	{
		if (isProduct == true)
		{
			insertVals();
		}
	}
}


function opacity(id, opacStart, opacEnd, millisec) 
{ 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) 
	{ 
        for(i = opacStart; i >= opacEnd; i--) 
		{ 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
	else if(opacStart < opacEnd) 
	{ 
        for(i = opacStart; i <= opacEnd; i++) 
        { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) 
{ 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function shiftOpacity(id, millisec) 
{ 
    //if an element is invisible, make it visible, else make it ivisible 
    if(document.getElementById(id).style.opacity == 0) 
	{ 
        opacity(id, 0, 95, millisec); 
    } 
	else 
	{ 
        opacity(id, 95, 0, millisec); 
    } 
} 

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

