//globals use to track edge of main nav
var left = null;
var top = null;

var myrules = {
	'.trigger' : function(element){
		element.onmouseover = function(){
			find_main_nav();
			show_menu(document.getElementById(this.id + '_sub'));
			show_image(document.getElementById(this.id + '_image'), "link")
		}
		element.onmouseout = function(){
			hide_menu(document.getElementById(this.id + '_sub'));
			hide_image(document.getElementById(this.id + '_image'), "link")
		}
	},
	'.hidden_sub' : function(element){
		element.onmouseover = function(){
			show_menu(this);
			show_image(this, "subnav");
		}
		element.onmouseout = function(){
			hide_menu(this);
			hide_image(this, "subnav");
		}
		
	}
};

Behaviour.register(myrules);
preload_nav_images();


/* utility functions */

//figure out where our divs are located
function find_x(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 find_y(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 find_main_nav(){
	var home = document.getElementById('home');
	left = find_x(home);
	top = find_y(home) + 24;
  }

//change display properties for us
 function show_menu(e){
	id = e.id.substring(0,e.id.indexOf('_sub'));
	if (id  != "home" && id != "events"){
		e.style.left = left + 'px';
		e.style.top = top + 'px';
		e.style.display = 'inline';
	}
 }

 function hide_menu(e){
 	e.style.display = 'none';
 }
 
 function show_image(e,link_or_subnav){

	if (link_or_subnav == "link")
		e.src = 'images/' + e.id.substring(0,e.id.indexOf('_image')) + "_on.png";
	else if (link_or_subnav == "subnav"){
		i = document.getElementById(e.id.substring(0,e.id.indexOf('_sub')) + "_image");
		i.src = 'images/' + e.id.substring(0,e.id.indexOf('_sub')) + "_on.png";
	}
 }

 function hide_image(e, link_or_subnav){
	
	if (link_or_subnav == "link"){
	  	if (e.className != "active")
			e.src = 'images/' + e.id.substring(0,e.id.indexOf('_image')) + "_off.png";
	}
	else if (link_or_subnav == "subnav"){
		i = document.getElementById(e.id.substring(0,e.id.indexOf('_sub')) + "_image");
		if (i.className != "active")
			i.src = 'images/' + e.id.substring(0,e.id.indexOf('_sub')) + "_off.png";
	}
 }

 function preload_nav_images(){
	
	//preload all the images we need for the nav menu
	
	images = ["home_on.png", "home_off.png", "our_wines_on.png", "our_wines_off.png", 
			  "our_family_on.png", "our_family_off.png", "vineyards_winery_on.png", "vineyards_winery_off.png",
			  "events_on.png", "events_off.png", "tours_on.png", "tours_off.png", "trade_on.png", "trade_off.png"];
	
	for (image in images){
		(new Image()).src = "images/" + image;	
	}
	
 }