var path_to_images = "/wp-content/themes/shopbuild/images/menu/";

function loadmenu() {
	if (document.getElementById('menu')) {
		var links = document.getElementById('menu').getElementsByTagName('a');
		for (var i=0; i < links.length; i++) {
			if (links[i].id.indexOf('mainnavitem') != -1) {
				links[i].onmouseover = highlightmenu;
				links[i].onmouseout = unhighlightmenu;
			}
			else if (links[i].id.indexOf('subnavitem') != -1) {
				links[i].onmouseover = highlightsubmenu;
				links[i].onmouseout = unhighlightsubmenu;
			}
		}
	}
}

var timeout	= 200;
var closetimer	= 0;
var ddmenu	= 0;
var ddmenuitem = 0;

// open hidden layer
function mopen(id)
{	
	// cancel close timer
	mcancelclosetime();

	// don't re-open a menu that's already open
	if (ddmenu != document.getElementById(id)) {
		
		// close old layer
		if (ddmenu != 0) {
			ddmenu.style.display = 'none';
		}
		// and remove the highlight from the main menu item to which it belongs
		if (ddmenuitem != 0) {
			togglemenuitem(ddmenuitem, false);
			ddmenuitem = 0;
		}
		
		// get new layer and show it
		ddmenu = document.getElementById(id);
		ddmenu.style.display = 'block';
	}

}
// close showed layer
function mclose()
{
	// hide the menu
	if (ddmenu) {
		ddmenu.style.display = 'none';
		ddmenu = 0;
	}
	// and remove the highlight from the main menu item to which it belongs
	togglemenuitem(ddmenuitem, false);
	ddmenuitem = 0;
}

// go close timer
function mclosetime()
{
	// request the menu be closed shortly
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime()
{
	if(closetimer)
	{
		// keep the menu open
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

// insert the correct suffix into the image name
function getactivetoken(on) {
	if (on) {
		return "_on";
	} else {
		return "";
	}
}

// highlight/unhighlight a main menu item
function togglemenuitem(link, on) {
	var id = link.id;
	id = id.replace('mainnavitem_', '');
		
	var img = link.getElementsByTagName('img');
	if (img) {
		img[0].src = path_to_images + "main_" + id + getactivetoken(on) + ".jpg";
	}
}

// show/hide a submenu
function togglesubmenu(link, on){
	var id = link.id;
	id = id.replace('mainnavitem_', '');
	
	if (document.getElementById('subnav_' + id)) {
		if (on) {
			// open the menu
			mopen('subnav_'+id);

			// remember which item in the main nav it belonged to 
			ddmenuitem = link;
		}
		else {
			mclosetime();
		}
	}
}

// highlight/unhighlight a submenu item
function togglesubmenuitem(link, on) {
	var id = link.id;
	id = id.replace('subnavitem_', '');
	
	var img = link.getElementsByTagName('img');
	if (img) {
		img[0].src = path_to_images + "sub_" + id + getactivetoken(on) + ".jpg";
	}
}

function highlightmenu() {
	// highlight the item
	togglemenuitem(this, true);
	// show the submenu (if there is one)
	togglesubmenu(this, true);
}

function unhighlightmenu() {
	// unhighlight the item
	togglemenuitem(this, false);
	// hide the submenu (if there is one)
	togglesubmenu(this, false);
}

function highlightsubmenu() {
	// retain the highlight on the item to which the menu belongs
	togglemenuitem(ddmenuitem, true);
	// highlight this item
	togglesubmenuitem(this, true);
	// stop the menu from being closed
	mcancelclosetime();
}

function unhighlightsubmenu() {
	// unhighlight this item
	togglesubmenuitem(this, false);
	// request that the menu is closed (this will be overridden if they've just moved to another item in the same menu)
	mclosetime();
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function getHeight(Elem) {
	if (ns4) {
		var elem = getObjNN4(document, Elem);
		return elem.clip.height;
	} else {
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (op5) { 
			xPos = elem.style.pixelHeight;
		} else {
			xPos = elem.offsetHeight;
		}
		return xPos;
	} 
}  
  function scrollPosition() {
	var ScrollTop = document.body.scrollTop;
 
	if (ScrollTop == 0)
	{
    	if (window.pageYOffset)
        	ScrollTop = window.pageYOffset;
	    else
    	    ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}  	
  
  	return ScrollTop;
  }
  
function checkCartHeight() {
	if ((document.getElementById('sidebar')) && (document.getElementById('sideshoppingcart')) && (document.getElementById('content'))) {
		// get the height of the shopping cart
		var sidebar_height = document.getElementById('sidebar').offsetHeight + 20;
		// use it as the min height of the content area
		document.getElementById("content").style.minHeight = sidebar_height + "px";
	}	
}

function moveCart() {
	if ((document.getElementById('sidebar')) && (document.getElementById('sideshoppingcart'))) {
		var pos_y = findPosY(document.getElementById('sidebar'));
		var pos_scroll = scrollPosition();
		
		if (pos_scroll > 383) {
			document.getElementById('sidebar').style.top = pos_scroll + "px";
		} else {
			document.getElementById('sidebar').style.top = "383px";
		}
	}
	
	checkCartHeight();
}

var carttimer;
function checkCartPosition() {
	if(carttimer)
	{
		// clear the timer
		window.clearTimeout(carttimer);
		carttimer = null;
	}
	// set a timer - this means that we only move the cart when scrolling has finished
	// and that prevents repainting problems in Safari
	carttimer = window.setTimeout(moveCart, 100);
}
window.onscroll = checkCartPosition;

// ikg - load into the top window if we're not there already - this sorts out problems with links 
// from the flash in the iframe header area, which would otherwise open into the iframe itself
if (self.location.href != top.window.location.href) {
	 top.window.location.href = self.location.href;
}
