// main nav functions
var menutimeout = [];
var menu_array = ['sol','pro','sup','res','com'];

if(!Array.indexOf){
	 Array.prototype.indexOf = function(obj){
		  for(var i=0; i<this.length; i++){
				if(this[i]==obj){
					 return i;
				}
		  }
		  return -1;
	 }
}

// main function
function menu_toggle(menu,direction,repeat,togglewhat){
	// detect over/out timeout
	var menu_index = menu_array.indexOf(menu);
	if(direction == 'out' && repeat == 1){
		if(togglewhat != 'image'){
			menutimeout[menu_index] = setTimeout("menu_toggle('" + menu + "','out','','" + togglewhat + "')", 500);
		} else {
			menu_toggle(menu,'out','',togglewhat);
		}
	} else {
		clearTimeout(menutimeout[menu_index]);
		
		// allow only 1 open menu
		if(direction != 'out'){
			for(var m in menu_array){
				if(menu_array[m] != menu){
					if(menutimeout[menu_array.indexOf(menu_array[m])] != undefined){
						clearTimeout(menutimeout[menu_array.indexOf(menu_array[m])]);
						menutimeout.splice(menu_array.indexOf(menu_array[m]),1);
						image_toggle(menu_array[m],'out');
						
						if(document.getElementById(menu_array[m] + '_menu') != null){
							var the_menu = document.getElementById(menu_array[m] + '_menu');
							if(the_menu.style.display == 'block'){
								div_toggle(menu_array[m],'out');
							}
						}
					}
				}
			}
		}
		
		image_toggle(menu,direction);
		if(togglewhat != 'image'){
			div_toggle(menu,direction);
		}
	}
} // end nav toggle

// toggle main nav image
function image_toggle(menu,direction){
	var this_anchor = document.getElementById(menu + '_anchor');
	var anchor_src = this_anchor.src;
	
	// check if link is active
	if(this_anchor.parentNode.className != "active"){
		var split_them = anchor_src.split('/');
		var image_filename = '';
		if(split_them[split_them.length - 1].match("2") && direction == 'out'){
			var images_split = split_them[split_them.length - 1].split('2');
			image_filename = images_split[0] + images_split[1];
		} else if(!split_them[split_them.length - 1].match("2") && direction == ''){
			var images_split = split_them[split_them.length - 1].split('.');
			image_filename = images_split[0] + "2." + images_split[1];
		}
		var built_src = '';
		if(image_filename != ''){
			for(var i in split_them){
				if(i < split_them.length - 1){
					built_src += split_them[i] + "/";
				}
			}
			built_src += image_filename;
			this_anchor.src = built_src;
		}
	}
} // end image_toggle

// toggle drop menu
function div_toggle(menu,direction){
	var the_menu = document.getElementById(menu + '_menu');
	// show/hide the menu
	if((the_menu.style.display == 'none' || the_menu.style.display == '') && direction == ''){
		//the_menu.style.display = 'block';
		animatedcollapse.show(menu + '_menu');
	} else if(the_menu.style.display == 'block' && direction == 'out'){
		//the_menu.style.display = 'none';
		animatedcollapse.hide(menu + '_menu');
	}
} // end div_toggle