/**
 * Link menu  v 1.1
 * copyright yippykya.com Dec 2006
 */

var menu_idx = 0;
var active_menu;
var active_link;

/*
// Sampe menu data
var menu1 = new Array();
menu1[0] = '<a href="" id="menu_content" class="link_menu" onmouseover="link_menu(this,event,menu1_1,120)">Content</a>';
menu1[1] = '<a href="" id="menu_style" class="link_menu" onmouseover="link_menu(this,event,menu1_1,120)">Style</a>';

var menu1_1 = new Array()
menu1_1[0] = '<a href="" id="menu_fontsize" class="link_menu" onmouseover="link_menu(this,event,menu_fontsize,70)">Font Size</a>';
menu1_1[1] = '<a href="" id="menu_bgcolor" class="link_menu" onmouseover="link_menu(this,event,menu1,70)">Background Color</a>';
menu1_1[2] = '<a href="" id="menu_fontColor" class="link_menu" onmouseover="link_menu(this,event,menu_color,200)">Fore Color</a>';

var menu_fontsize = new Array();
for(i = 1; i<15; i++){
  menu_fontsize[i] = '<a href="">'+ (i + 7) +' pt</a>';
}

var menu_color = new Array();
menu_color[0] = '<a href="">asdasd</a>';
*/

var op5 = false;

function link_menu(ob,val, menuitems, width, bellow, classname,obout){
  if(classname==undefined)classname = 'menu_set';
  if(bellow == undefined) bellow = false;
 
  /*  
  if(menuitems.tagName=='DIV'){
    mi = new Array();
    mi[0]= menuitems.innerHTML;
    menuitems = mi;
  }
    */
    
  if(typeof(menuitems) == 'function'){
    menuitems();    
  }
  
  if(ob.sub_menu == undefined){

    ob.sub_menu = generate_menu(ob, menuitems, width, classname,obout);
    if(!ob.sub_menu) {
      ob.sub_menu = undefined;
      return;  
    }
    ob.sub_menu.def_classname = classname;
    if(val!=undefined){
      ob.sub_menu.value = val;
    }    
  }
  
  active_link =ob;
  
  reposition_menu(ob.sub_menu, ob, bellow);
  

  if(ob.sub_menu.hide_timer != undefined){
    clearTimeout(ob.sub_menu.hide_timer);
    ob.sub_menu.hide_timer = undefined;
  }

  
}
  
  
function reposition_menu(menu, parent, bellow){
  parent_x =  pos_x(parent);
  parent_width =  element_width(parent);

  if(bellow){
    parent_height = element_height(parent);
    menu_x = parent_x;
    menu_y = pos_y(parent)+ parent_height; 
  }else{
    menu_x =  parent_x + parent_width +4;
    menu_y =  pos_y(parent);
  }
  

  menu.style.left = menu_x + 'px';
  menu.style.top  = menu_y + 'px';
  menu.style.display = '';
    
  menu_width = element_width(menu);
  menu_right = menu_width + menu_x;

  if(document.body.offsetWidth < menu_right){
    menu.className = menu.def_classname + '_left';
    menu.style.left = parent_x - menu_width - 4;
  }else{
    menu.className = menu.def_classname ;
  };
  
  
}  
function generate_menu(parent, menuitems, width,classname, parent_out){
  o_sm = document.createElement('DIV');
  o_sm.parent = parent;
  o_sm.id = 'ypmenu_' + menu_idx;
  menu_idx++;
 
  if(!body_loaded)return;
 
  if(document.all){
    if(width!= undefined){
      o_sm.style.width = width + 'px';
    }
  }

  if(parent_out==undefined){

    parent.sub_menu = o_sm;
    parent.onmouseout = set_out_submenu;
  }else{

    parent_out.sub_menu = o_sm;
    parent_out.onmouseout = set_out_submenu;
  }
  
  o_sm.onmouseout = set_out;  
  o_sm.onmouseover = set_over;

  
  o_sm.innerHTML = '<div class="menu_arrow"></div>'+menuitems.join("");
  o_sm.className = classname;
  
  
  o_sm.style.zIndex = 1000;
  document.body.appendChild(o_sm);
  //document.body.insertBefore(o_sm, document.body.firstChild);
  
  o_sm_width = element_width(o_sm);  
  for(i = 0; i<o_sm.childNodes.length; i++){
    if(o_sm.childNodes[i].tagName =='A'){
      o_sm.childNodes[i].style.width = o_sm_width +'px';
      
    }
    
  }
    

  return o_sm;
}  


function set_over(){
  active_menu = this;

  this.on_over = true;
  if(this.hide_timer != undefined){

    clearTimeout(this.hide_timer);
    this.hide_timer = undefined;
  }
  
  
  /*Clear parents autohide to keep the menu showing*/
  ob = this.parent
  
  
 var str_clear='';
  
  while(ob.hide_timer!= undefined){
   // $('ca__street_name__0').value = "HERE " + $('ca__street_name__0').value;
    
    if(ob.hide_timer!= undefined){
      str_clear += ','+ ob.hide_timer ;
      clearTimeout(ob.hide_timer);
      $(ob).addClassName('active_menu');
      ob.hide_timer = undefined;
    }
    ob = ob.parentNode;
    if(ob.hide_timer==undefined) ob = ob.parentNode;

  }
  
  // alert(str_clear);
}

               

function set_out(){
  this.on_over = false;
  active_menu = undefined;  
  this.hide_timer = setTimeout('hide_menu("' + this.id + '");',300);

}

function set_out_submenu(){
  this.hide_timer = setTimeout('hide_menu("' + this.sub_menu.id + '",true);',50);
}


function hide_menu(set_id, by_parent){
  o_set = document.getElementById(set_id);



  if(by_parent){
     if(active_link.sub_menu == o_set){
        o_set.hide_timer = setTimeout('hide_menu("'+set_id +'");', 300);
        return;
     }
  }



  o_set.style.display = 'none';

  ob = o_set.parent;
  //alert("HERE1");
  //ob.extend();
  $(ob).removeClassName('active_menu');
//  alert("HERE");
  
  while(ob.parentNode.parentNode.on_over == false){
    if(active_menu!= undefined){
      if(active_menu.parent.parentNode == ob.parentNode) break;
    }
    if(ob.hide_timer!= undefined){

      return;
    }else{

       ob.parentNode.parentNode.style.display = 'none';
    }
    ob = ob.parentNode.parentNode.parent;

  }
    
  
}




function pos_x(obj){
	actb_toreturn = 0;
	var str ='';
	while(obj){
	  actb_toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return actb_toreturn;
}

function pos_y(obj){
	actb_toreturn = 0;
	while(obj){
		actb_toreturn += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return actb_toreturn;
}


function element_height(elem) {
	if (op5) { 
		return elem.style.pixelHeight;
	} else {
		return elem.offsetHeight;
	}

}

function element_width(elem) {
	if (op5) {
		return elem.style.pixelWidth;
	} else {
		return elem.offsetWidth;
	}
}
	
	
/* Small Top Banner Menu 
***************************************/

var active_menu = undefined;
var document_onmousedown = undefined;

function show_menu(oid,link_id){
  o_menu = g(oid);
  o_menu_link = g(link_id);
  o_menu.parent = o_menu_link;  

  if(active_menu!=undefined){
    if(active_menu != o_menu){
      active_menu.hide();
    }
  }  
  
  o_menu.style.display = '';  
  o_menu.style.left = pos_x(o_menu_link) + 'px';
  o_menu.style.top= pos_y(o_menu_link) + 'px';
  
  o_menu.parent.className = 'my_menu_link_active';
  
 // o_menu_link.appendChild(o_menu);

  active_menu = o_menu;

  if(o_menu.hide== undefined){
    o_menu.hide = function(){
      this.style.display = 'none';
      active_menu = undefined;
      this.parent.className = '';
    }
  }
  document_onmousedown = document.onmousedown;
  
  document.onmousedown = function(e){
    // document.onmousedown = document_onmousedown;         
    if(active_menu!=undefined){
      //if(typeof(event)=='undefined')event = e;
      if(active_menu.id!='search_box'){
        setTimeout('active_menu.hide()',200);
      }
    }
  }
}	

function toogle_search_box(){
  o_sbox = g('search_box');
  if(active_menu!=undefined){
    if(active_menu != o_sbox){
      active_menu.hide();
    }
  }   
 
  if(o_sbox.hide== undefined){
    o_sbox.hide = function(){
      this.style.display = 'none';
      active_menu = undefined;
      this.parent.className = '';
    }      
  }
  if(o_sbox.style.display =='none'){
    o_sbox.style.display='';
    active_menu = o_sbox;
  }else{
    o_sbox.style.display='none';
  }  
}

/* ico_baloon */
function ico_balloon(ob, url, obout){
  majax = new icocrm.ajax();
  if(ob.balloon_html==undefined){
   ob.balloon_html = majax.query(url);
  }
  if(ob.balloon_html!=''){
    link_menu(ob,'',['<div class="ico_balloon_frame"><div style="padding:10px">' + ob.balloon_html + '</div></div>'],undefined,undefined,'ico_balloon',obout);
  }
}

function ico_link_menu(ob,html,obout,classname,bellow){
  if(classname == undefined) classname = 'ico_balloon'; 
  link_menu(ob,'',['<div class="ico_balloon_frame">' + html + '</div>'],undefined,bellow, classname ,obout);
}
function ico_hir_menu(ob, url, obout){
  majax = new icocrm.ajax();
  if(ob.balloon_html==undefined){
   ob.balloon_html = majax.query(url);
  }
  //link_menu(ob,'',['<div class="ico_balloon_frame">' + ob.balloon_html + '</div>'],undefined,undefined,'ico_balloon',obout);
  link_menu(ob,'',['<div class="ico_balloon_frame">' + ob.balloon_html + '</div>'],undefined,undefined,'ico_balloon',obout);

}