/**
 * Prototype for sMenu and sSubMenu
 */
function sBaseMenu(){
  this.normalItemClassName 		= ''; 
  this.selectedItemClassName 	= '';
  this.currentItemClassName 	= '';  
  this.separatorClassName		= 'mmi_separator'; 
  this.htmlElement 				= null;
  this.selected_item			= null;
  this.default_item				= null;
  
  this.unselect_timeout 		= 100;
  this.unselect_timer 			= null;    
  
  this.item_constructor = sMenuItem;
}


sBaseMenu.prototype.init = function(){
  var item = this.htmlElement.firstChild;
  var menuitem = null;
  var menuitem_separator = null;
  while(item!=null){
	  if (item.nodeType==1) {
	    if (item.className.indexOf(this.normalItemClassName)!=-1){
		  menuitem = new this.item_constructor();
		  if (item.className.indexOf(this.currentItemClassName)!=-1){
			this.default_item = menuitem;
		  }
		  menuitem.init(item,this);		  
		  this.subitems.push(menuitem);
		  if (menuitem_separator!=null){
			menuitem.setLeftSeparator(menuitem_separator);
			menuitem_separator = null;
		  }
	    }
		if (item.className.indexOf(this.separatorClassName)!=-1){
		  menuitem_separator = item;
		  if (menuitem!=null){
			menuitem.setRightSeparator(item);
			menuitem = null;
		  }
		}
	  }
	  item = item.nextSibling;
  }
  var this2 = this;
}


sBaseMenu.prototype.$ = function(id){
  return document.getElementById(id);
} 


sBaseMenu.prototype.isIE = function(){
  return /msie/i.test(navigator.userAgent);
  //return typeof(this.htmlElement.filters)=='object';
}
/**
 * Safari, Opera and Mozilla
 */
sBaseMenu.prototype.isSOM = function(){
  return typeof(this.htmlElement.style.opacity)=='string';
}

sBaseMenu.prototype.preloadImage = function(name){
  var v = new Image();
  v.src = name;
  this.images_preloaded.push(v);
}

sBaseMenu.prototype.cancelUnselectTimer = function(){
  if (this.unselect_timer!=null){
	window.clearTimeout(this.unselect_timer);
	this.unselect_timer = null;
  }  
}

sBaseMenu.prototype.unselectItemDelayed = function(){ 
  this.cancelUnselectTimer();
  if (this.selected_item!=null){
    var this1 = this;
	var f = function(){
	  this1.hideByTimeout();
      this1.unselect_timer = null;
	}
    this.unselect_timer = window.setTimeout(f,this.unselect_timeout);
  }  
} 

sBaseMenu.prototype.hideByTimeout = function(){ 
  this.selected_item.unselectItem();
}



/**
 * Main menu
 */
function sMenu(htmlElementID,containerID){
  this.htmlElement = this.$(htmlElementID);
  this.container   = this.$(containerID);
  this.selectedItemClassName = "menuitem_selected";
  this.normalItemClassName = 'menuitem';
  this.subitems = new Array();
  var this1 = this;
  this.preloadImages();  
    this.init(); 
	this.placeEvently();
}
sMenu.prototype = new sBaseMenu();
sMenu.prototype.preloadImages = function(){
  this.images_preloaded = new Array();
  this.preloadImage('/css/cobrands/realestateinvestor/menuitem_selected_l.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menuitem_selected_r.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_bg.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_current.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_normal.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_point.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_selected.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_separator_current.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_separator_lsel.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_separator_normal.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_separator_rsel.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_opened.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_separator_ropen.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/menu_separator_lopen.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/submenu_selected.gif');
}
sMenu.prototype.placeEvently = function(){
}

/**
 * Second menu
 */
function sSecondMenu(htmlElementID,containerID){
  this.htmlElement = this.$(htmlElementID);
  this.container   = this.$(containerID);
  this.selectedItemClassName = "m2_tab_sel";
  this.currentItemClassName = "m2_tab_cur";  
  this.normalItemClassName = 'm2_tab';
  this.subitems = new Array();
  this.separatorClassName		= 'm2_separator'; 
  this.item_constructor = sSecondMenuItem;
  this.preloadImages();
  this.init();
}
sSecondMenu.prototype = new sBaseMenu();

sSecondMenu.prototype.preloadImages = function(){
  this.images_preloaded = new Array();
  this.preloadImage('/css/cobrands/realestateinvestor/m2_g.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_lg.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_rg.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_b.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_lb.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_rb.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_sel_b.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_sel_lb.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_sel_rb.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_cur_b.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_cur_lb.gif');
  this.preloadImage('/css/cobrands/realestateinvestor/m2_tab_cur_rb.gif');
}

sSecondMenu.prototype.showTip = function(tip){
  if (tip!=null)
    this.$('m2_status2').innerHTML = tip+"&nbsp;";
  else
    this.$('m2_status2').innerHTML = '&nbsp;&nbsp;&nbsp;';
}

sSecondMenu.prototype.hideByTimeout = function(){ 
  this.selected_item.unselectItem();
  this.showTip();  
}

/**
 *
 *
 */
function sSubMenu(e,owner){
  this.htmlElement = e;
  this.container = owner.container;
  this.subitems = new Array();
  this.normalItemClassName = 'submenuitem';
  this.selectedItemClassName = 'submenuitem_selected';

  this.visible = false;
  this.owner = owner; //MenuItem
  
  this.item_constructor = sSubMenuItem;
  
  //used in IE to store select object which should be hidden
  //when popup happening
  this.hidden_selects = new Array(); 
  this.init();
  var this1 = this;
  e.onmouseover = function(){
    this1.cancelUnselectTimer();
	this1.owner.owner.cancelUnselectTimer();
  }
  e.onmouseout = function(){
    this1.unselectItemDelayed();
  }  
}
sSubMenu.prototype = new sBaseMenu();

sSubMenu.prototype.hideByTimeout = function(){ 
  this.Hide();
  this.owner.unselectItem();
}

sSubMenu.prototype.isSupportsOpacity = function(){
   return (this.isIE() || this.isSOM());
}

sSubMenu.prototype.isSupportsOpacity = function(){
   return (this.isIE() || this.isSOM());
}

/**
 * in IE selects are always on top so we should hide
 * selects inderneath option box
 */
sSubMenu.prototype.hideSelectsInIE = function(){
  if (this.isIE()){	
	var selects = document.getElementsByTagName("select");
	var rect = this.htmlElement.getBoundingClientRect();
	for(i=0;i<selects.length;i++){
		if (this.selectIntersects(selects[i],rect)){
			var sel = selects[i];
			sel.style.visibility = 'hidden'; 
		    this.hidden_selects.push(sel);
		}
	}
  }
}

sSubMenu.prototype.showSelectsInIE = function(){
  if (this.isIE()){
	  while(this.hidden_selects.length>0){
	    var sel = this.hidden_selects.pop();
		sel.style.visibility = 'visible'; 
	  }
  }
}

sSubMenu.prototype.selectIntersects = function(sel,rect){
	  var rect1 = sel.getBoundingClientRect();
	  if ((rect1.left>rect.left) 
	  && (rect1.left<rect.right) 
	  && (rect1.top>rect.top) 
	  &&(rect1.top<rect.bottom)) return true;
	  
	  if ((rect1.right>rect.left) 
	  && (rect1.right<rect.right) 
	  && (rect1.top>rect.top) 
	  &&(rect1.top<rect.bottom)) return true;
	  
	  if ((rect1.right>rect.left) 
	  && (rect1.right<rect.right) 
	  && (rect1.bottom>rect.top) 
	  &&(rect1.bottom<rect.bottom)) return true;
	  
	  if ((rect1.left>rect.left) 
	  && (rect1.left<rect.right) 
	  && (rect1.bottom>rect.top) 
	  &&(rect1.bottom<rect.bottom)) return true;
	  return false;
}

sSubMenu.prototype.setPosition = function(){
	var left = getPageOffsetLeft(this.owner.htmlElement)-4; //separator width
	var scrX = document.body.parentNode.scrollLeft;
	var winW = document.body.parentNode.clientWidth;
    if (left<scrX){
	  left = scrX;
	}
	if((left+this.htmlElement.offsetWidth)>(scrX + winW)){
	  left = scrX + winW - this.htmlElement.offsetWidth; 
	}	
    this.htmlElement.style.left = left + 'px';
	this.htmlElement.style.right = '';			
	this.htmlElement.style.top  = getPageOffsetTop(this.owner.htmlElement)-10+this.owner.htmlElement.offsetHeight+'px';
}

sSubMenu.prototype.Show = function(){
	this.setPosition();
	this.htmlElement.style.visibility = "visible";
   	this.visible = true;
	this.hideSelectsInIE();
}


sSubMenu.prototype.Hide = function(){
	this.cancelUnselectTimer();
	this.visible = false;	
    this.htmlElement.style.visibility = "hidden";

}

/**
 * Prototype of menuitem
 */
function sBaseMenuItem(){

  this.external_link       = false;
  this.selected			   = false;

  this.separator_left      = null;
  this.separator_left_cl   = null;
  this.separator_right	   = null;
  this.separator_right_cl  = null;
    
  this.normalItemClassName = null;
  this.openedItemClassName   = 'menuitem_opened';		//when submenu opened but not selected
  
  this.separator_left_cl_sel  = 'mmi_separator_lsel';
  this.separator_right_cl_sel = 'mmi_separator_rsel';  

  this.separator_left_cl_opened  = 'mmi_separator_lopen';
  this.separator_right_cl_opened = 'mmi_separator_ropen';  

  
  this.tip				  = null;
  
}

sBaseMenuItem.prototype.init = function(htmlElement,owner){
  this.htmlElement         = htmlElement;
  if (this.normalItemClassName==null){
    this.normalItemClassName = htmlElement.className;
  }
  this.owner               = owner; //sMenu
  this.submenu_id	= null;
  var this1 = this;

  this.replaceHyperlinks(this.htmlElement);
  this.findSubmenu(this.htmlElement);

  
  this.htmlElement.onmouseover = function(){
	this1.selectItem();
  }
	
  this.htmlElement.onmouseout = function(){
	this1.owner.unselectItemDelayed();  
  }
	
  this.htmlElement.onclick = function(){
	  if (this1.external_link)
	    window.open(this1.url,'_blank');
	  else
	    window.location = this1.url;
  }	
}	

sBaseMenuItem.prototype.findSubmenu = function(e){
}

sBaseMenuItem.prototype.replaceHyperlinks = function(e){   
  var item = e.firstChild;
  while(item){
    if (item.nodeType==1) {
	  if (item.nodeName.toUpperCase()=='A'){
	   this.url = item.getAttribute('href');
	   if (item.target=='_blank'){
	     this.external_link = true;
	   }
	   this.title_width = item.offsetWidth;
	   var text = this.title = getNodeText(item);
	   e.removeChild(item);
	   var text_node = document.createTextNode(text);
	   e.appendChild(text_node);
	  }
	  else if (item.nodeName.toUpperCase()=='DIV') {
	    this.replaceHyperlinks(item);
	  }
	}
    item = item.nextSibling;
  }
}

sBaseMenuItem.prototype.setLeftSeparator = function(separator){
  this.separator_left = separator;
  if (this.separator_left_cl==null){
    this.separator_left_cl = separator.className;
  }
}

sBaseMenuItem.prototype.setRightSeparator = function(separator){
  this.separator_right = separator;
  if (this.separator_right_cl==null){
    this.separator_right_cl = separator.className;
  }
}

sBaseMenuItem.prototype.setSelectedState = function(){
  this.htmlElement.className = this.owner.selectedItemClassName;
  if (this.separator_left!=null){
	this.separator_left.className = this.separator_left_cl_sel;
  }
  if (this.separator_right!=null){
	this.separator_right.className = this.separator_right_cl_sel;
  }
}

sBaseMenuItem.prototype.setUnselectedState = function(){
  this.htmlElement.className = this.normalItemClassName;
  if (this.separator_left!=null){
     this.separator_left.className = this.separator_left_cl;
  }
  if (this.separator_right!=null){
    this.separator_right.className = this.separator_right_cl;
  }
}

/**
 * Menu item constructor
 */
function sMenuItem(){
  this.submenu              = null;
  this.show_submenu_timeout = 300;
  this.show_submenu_timer   = null;
}
sMenuItem.prototype = new sBaseMenuItem();

sMenuItem.prototype.showSubmenuDelayed = function(){	
 if (this.submenu!=null){
   this.cancelSubmenuShowing();
   var this1 = this;
   var f = function(){
     this1.submenu.Show();
   };
   this.show_submenu_timer = window.setTimeout(f,this.show_submenu_timeout);    
 }
}
sMenuItem.prototype.cancelSubmenuShowing = function(){	
  if (this.show_submenu_timer!=null){
	window.clearTimeout(this.show_submenu_timer);
	this.show_submenu_timer = null;
  }
}
sMenuItem.prototype.selectItem = function(){	
  this.owner.cancelUnselectTimer();
  this.cancelSubmenuShowing();
  if ((this.owner.selected_item!=null) && (this.owner.selected_item!=this)) {
	this.owner.selected_item.unselectItem();
  }
  this.owner.selected_item = this;
  this.selected = true;
  this.setSelectedState();
//  }
  if (this.submenu!=null){
    this.submenu.cancelUnselectTimer();
    if (!this.submenu.visible){
	  this.showSubmenuDelayed();
    }
	if(this.submenu.selected_item){
		this.submenu.selected_item.unselectItem();	
	}
  }

}



sMenuItem.prototype.unselectItem = function(){
  this.owner.cancelUnselectTimer();
  this.cancelSubmenuShowing();
  this.selected = false;	
  
  this.setUnselectedState();
  if (this.submenu!=null){
    this.submenu.Hide();
	if (this.submenu.selected_item!=null){
	  this.submenu.selected_item.unselectItem();
	}
  } 
}

sMenuItem.prototype.setOpenedState = function(){
  this.htmlElement.className = this.openedItemClassName;
  if (this.separator_left!=null){
    this.separator_left.className = this.separator_left_cl_opened;
  }
  if (this.separator_right!=null){
    this.separator_right.className = this.separator_right_cl_opened;
  }
}
/**
 * added submenu_id
 */
sMenuItem.prototype.replaceHyperlinks = function(e){   
  var item = e.firstChild;
  while(item){
    if (item.nodeType==1) {
	  if (item.nodeName.toUpperCase()=='A'){
	   this.url = item.getAttribute('href');
	   this.submenu_id = item.getAttribute('rel');
	   if (item.target=='_blank'){
	     this.external_link = true;
	   }
	   this.title_width = item.offsetWidth;
	   var text = this.title = getNodeText(item);
	   e.removeChild(item);
	   var text_node = document.createTextNode(text);
	   e.appendChild(text_node);
	  }
	  else if (item.nodeName.toUpperCase()=='DIV') {
	    this.replaceHyperlinks(item);
	  }
	}
    item = item.nextSibling;
  }
}

sMenuItem.prototype.findSubmenu = function(e){
	if (this.submenu_id!=null){
	  var item = $(this.submenu_id);
	  this.submenu = new sSubMenu(item,this);
	  item.style.top = '0px'; //
	}
}

/**
 * SubmenuItem
 */
function sSubMenuItem() {
}
sSubMenuItem.prototype = new sBaseMenuItem();

sSubMenuItem.prototype.selectItem = function(){	
  this.owner.cancelUnselectTimer();
  this.owner.owner.owner.cancelUnselectTimer();
  if (this.owner.selected_item!=null) {
	this.owner.selected_item.unselectItem();
  }
  this.owner.selected_item = this;
  this.selected = true;
  this.setSelectedState();
  this.owner.owner.setOpenedState();	  
}

sSubMenuItem.prototype.unselectItem = function(){
  this.selected = false;	
  this.setUnselectedState();
}


/**
 * SecondMenu item constructor
 */
function sSecondMenuItem(){
  this.separator_left_cl_sel = 'm2_separator_sel_l';
  this.separator_right_cl_sel = 'm2_separator_sel_r';
  this.separator_left_cl = 'm2_separator_l';
  this.separator_right_cl = 'm2_separator_r';
  this.normalItemClassName = 'm2_tab';
}
sSecondMenuItem.prototype = new sMenuItem();


sSecondMenuItem.prototype.init = function(htmlElement,owner){
  this.htmlElement         = htmlElement;
  if (this.normalItemClassName==null){
    this.normalItemClassName = htmlElement.className;
  }
  this.owner               = owner; //sMenu
  var this1 = this;
  this.replaceHyperlinks(this.htmlElement);
  
  this.htmlElement.onclick = function(){
	  if (this1.external_link)
	    window.open(this1.url,'_blank');
	  else
	    window.location = this1.url;
  }	
  
  
  if (owner.default_item!=this)  {
    this.htmlElement.onmouseover = function(){
	  this1.selectItem();
    }
    this.htmlElement.onmouseout = function(){
	  this1.owner.unselectItemDelayed();
    }
  }
}	
sSecondMenuItem.prototype.selectItem = function(){	
  if ((this.owner.selected_item!=null) && (this.owner.selected_item!=this)) {
	this.owner.selected_item.unselectItem();
  }
  this.owner.selected_item = this;
  this.selected = true;
  this.setSelectedState();
  this.owner.showTip(this.tip);
}

/**
 * added tip in rel attribute
 */
sSecondMenuItem.prototype.replaceHyperlinks = function(e){   
  var item = e.firstChild;
  var this1 = this;
  while(item){
    if (item.nodeType==1) {
	  if (item.nodeName.toUpperCase()=='A'){
	   this.url = item.getAttribute('href');
	   this.tip = item.getAttribute('rel');
	   if (item.target=='_blank'){
	     this.external_link = true;
	   }
       	   
	   this.title_width = item.offsetWidth;
	   var text = this.title = getNodeText(item);
	   e.removeChild(item);
	   var text_node = document.createTextNode(text);
	   e.appendChild(text_node);
	  }
	  else if (item.nodeName.toUpperCase()=='DIV') {
	    this.replaceHyperlinks(item);
	  }
	}
    item = item.nextSibling;
  }
}


function _(name){
  return document.getElementById(name);
}

function getNodeText(element){
  var res = "";
  var item = element.firstChild;
  while(item){
    if ((item.nodeType==3)){
		res = res + item.nodeValue;
	}
    item = item.nextSibling;
  }
  return res;
}


var debug_console = null;
function debug1(message){
  if (true){
    if (debug_console==null) {
	  debug_console = document.createElement("TEXTAREA");
	  debug_console.style.position = 'absolute';
	  debug_console.style.top = '0px';
	  debug_console.style.left = '0px';
	  debug_console.style.height = '200px';
	  debug_console.style.width = '200px';	  
	  document.body.appendChild(debug_console);
	}
	debug_console.value = message+' '+debug_console.value;
  }
}

