function getSize(){
  maxW=parseInt(0);
  maxH=parseInt(0);
  mode=0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
                if(parseInt(myWidth) > maxW){maxW=myWidth;}
                if(parseInt(myHeight) > maxH){maxH=myHeight;}
                mode=1;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
                if(parseInt(myWidth) > maxW){maxW=myWidth;}
                if(parseInt(myHeight) > maxH){maxH=myHeight;}
                mode=2;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
                if(parseInt(myWidth) > maxW){maxW=myWidth;}
                if(parseInt(myHeight) > maxH){maxH=myHeight;}
                mode=3;
	}
  if (parseInt(document.body.clientHeight) > parseInt(maxH)){maxH=document.body.clientHeight;}
  if (parseInt(document.body.clientWidth) > parseInt(maxW)){maxW=document.body.clientWidth ;}
  var popovers = $('body').select('.popover_container');
  if(popovers.length > 0){
    poph = parseInt(popovers[0].offsetTop+popovers[0].scrollHeight+20);
    popw = parseInt(popovers[0].offsetLeft+popovers[0].scrollWidth+20);
    if(poph>maxH){
      maxH = poph;
    }
    if(popw > maxW){
      maxW = popw;
    }
  }
  return Array(myWidth,myHeight,maxW,maxH,mode);
}
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
var curid;
var cselmenu;
var smtimer;
var hmtimer;
var timeout = 500;

showSub = function(mid) {
  navRoot = document.getElementById('submenu');
  for( i = 0; i < navRoot.childNodes.length; ++i ) {
    node = navRoot.childNodes[i];
    if(node.id == 'submenu_'+mid) {// || node.id == 'submenu_'+curid && node.style.display == 'none') {
      //alert(node.id+' hide submenu_'+mid);
      //node.style.display = 'block';
      if(node.style.display == 'none') {
        new Effect.Appear(node,{duration:0.7,queue: 'end'});
      }
    }
    else if(node.style.display == 'block') {
      //alert(node.id+' show submenu_'+mid);
      new Effect.Fade(node,{duration:0.7,queue: 'front'});
    }
    else {
      node.style.display = 'none';
    }
  }
  cselmenu = mid;
  return false;
}

hideSub = function(mid) {
  showSub(curid);
  return false;
}

showTimedSub = function(mid) {
  clearTimeout(hmtimer);
  hmtimer = undefined;
  if(typeof(smtimer) == 'undefined') {
    //clearTimeout(smtimer);   
    smtimer = setTimeout("showSub('"+mid+"')",timeout);
  }
}

hideTimedSub = function(mid) {
  clearTimeout(smtimer);
  smtimer = undefined;
  clearTimeout(hmtimer);
  hmtimer = undefined;
  hmtimer = setTimeout("hideSub('"+mid+"')",timeout);
}

showTimedSubmenu = function(mid) {
  clearTimeout(hmtimer);
  hmtimer = undefined;
  clearTimeout(smtimer);
  smtimer = undefined;
}

hideTimedSubmenu = function(mid) {
  hideTimedSub(mid);
}
function genPopOver(holder){
  if(!$(holder)){
    var text = '<div id=\"'+holder+'\" class=\"popover_container\" style=\"display:none;\"><div class=\"ajax_loader\"><img class=\"ajax_loader\" src=\"\/loader.gif\" alt=\"loading\" \/><\/div><\/div>'; //style=\"display:none;\"
    $('body').insert({Bottom:text});
    showPopOver(holder);
  }
  genPopOverBack();
}
function genPopOverBack(){
  ssize = getSize();
  var divs = $('body').select('div.popover_container');
  var back = '<div id=\"popover_back\" style=\"width:'+ssize[2]+'px;height:'+ssize[3]+'px;\"></div>';
  removePopOverBack();
  if(divs.length > 0){
    divs[0].insert({Before:back});
  }else{
    $('body').insert({Bottom:back});
  }
  resizePopOverBack();
}
function resizePopOverBack(){
 ssize = getSize();
 if($('popover_back')){
  $('popover_back').style.width = ssize[2]+'px';
  $('popover_back').style.height = ssize[3]+'px';
 }
}
function removePopOverBack(){
  if($('popover_back')){
    $('popover_back').remove();
  }
}
function clearPopOvers(){
  var divs = $('body').select('div.popover_container');
  for(var i=0;i<divs.length;i++){
   divs[i].remove();
  }
  removePopOverBack();
}
function closePopOver(holder){
 new Effect.Fade(holder,{duration:0.5,afterFinish:function(){$(holder).remove();removePopOverBack();}});
}
function showPopOver(holder){
 var ssize = getSize(); //Array(myWidth,myHeight,maxW,maxH,mode);
 $(holder).style.display = '';
 var height = $(holder).offsetHeight;
 var width = $(holder).offsetWidth;
 $(holder).style.display = 'none';
 var left = parseInt((ssize[0]-width)/2);
 $(holder).style.left = left+'px';
 var scrolltop = f_scrollTop();
 var top = parseInt(scrolltop)+parseInt(parseInt(ssize[1]-height)/2);
 $(holder).style.top = top+'px'; 
 $(holder).style.display ='';
 new Effect.Appear(holder,{duration:0.5});
}
var defaultheight = 300;
function resizePopOverHeight(id){
  el = $(id);
  var diffspace = 22;
  if(el.scrollHeight <= defaultheight){
    return '';
  }
  else if((el.scrollHeight-diffspace) < el.getHeight()){
    //el.style.height = el.scrollHeight+"px";
    new Effect.Morph(el,{style:'height:'+defaultheight+'px',afterFinish:function(){resizePopOverBack();}});
  }else{
    //el.style.height = el.scrollHeight+"px";
    defaultheight = el.getHeight()-diffspace;
    new Effect.Morph(el,{style:'height:'+(el.scrollHeight-(diffspace-4))+'px',afterFinish:function(){resizePopOverBack();}});
  } 
}
function genPopPage(holder,url){
  //var holder = 'offerpreview_container';
  genPopOver(holder);
  url = escape(encodeURIComponent(url));
  new Ajax.Updater(holder,'/genpoppage',{parameters:'js=1&mode=popover&popholder='+holder+'&url='+url,onComplete:function(){showPopOver(holder);}});
}
