/*
JS: MainMenu logic
Constants:
	MainMenuConf (Object)
		classname_item             - parent clasname
		classname_item_selected    - parent clasname SELECTED
		classname_item_current     - parent clasname CURRENT (On what page are you)
		classname_subitem          - child clasname
		classname_subitem_selected - child clasname SELECTED
		classname_subitem_current  - child clasname CURRENT (On what page are you)
		hidetimeout                - hide in miliseconds
		simple_hidetimeout         - mouseout hide in miliseconds
		leftoffset                 - child left offset
		topoffset                  - child top offset
		
Functions:
	function mainmenu_attach - array child to parent
		parent    - parentid
		child     - childid - dropdownid
		showtype  - "click" = you should click the parent to show/hide the child
		            "over" = you should place the mouse over the parent to show the child		                     
		position  - "x" = the child is displayed to the right of the parent
		            "y" = the child is displayed below the parent
		isCurrent - on hide use current class not default (look mainmenu_hide)
	function mainmenu_show - display on mouse over
	function mainmenu_click  - display on mouse click
	function mainmenu_show_aux(parent, child)  - generic function for show, clicl
	function mainmenu_hide() - hide on hidetimeout
	
Functions - simple mouseover logic - NO BLINKING!!!:
	function mainmenu_select_attach(object, className, classNameSelected) - attach logic
	function mainmenu_select_show()	- mouseover
	function mainmenu_select_hide() - mouseout
*/

var MainMenuConf = { 
	classname_item : "MainMenu_Item", 
	classname_item_selected : "MainMenu_Item_Selected", 
	classname_item_current : "MainMenu_Item_Current", 
	classname_subitem : "MainMenu_SubItem", 
	classname_subitem_selected : "MainMenu_SubItem_Selected", 
	classname_subitem_current : "MainMenu_SubItem_Current", 
	hidetimeout: 100,
	simple_hidetimeout: 2,
	leftoffset: 4,
	topoffset: -10
}



function mainmenu_attach(parent, child, showtype, position, isCurrent, bDisplayChild, className) {
    p = $(parent);
    c = $(child);
    
    p["at_parent"] = p.id;
    p["at_child"] = c.id;
    p["at_position"] = position;
    p["isCurrent"] = isCurrent;
    p["displayChild"] = bDisplayChild;
    p["at_classname"] = className;
    p["at_classname_selected"] = className + "_Selected";

    c["at_parent"] = p.id;
    c["at_child"] = c.id;
    c["at_position"] = position;
    c["isCurrent"] = isCurrent;
    c["displayChild"] = bDisplayChild;
    c["at_classname"] = className;
    c["at_classname_selected"] = className + "_Selected";
    c.style.position = "absolute";
    c.style.visibility = "hidden";

    switch (showtype) {
        case "click":
            p.onclick = mainmenu_click;
            p.onmouseout = mainmenu_hide;
            c.onmouseover = mainmenu_show;
            c.onmouseout = mainmenu_hide;
            break;
        case "over":
            p.onmouseover = mainmenu_show;
            p.onmouseout = mainmenu_hide;
            c.onmouseover = mainmenu_show;
            c.onmouseout = mainmenu_hide;
            break;
    }
}



//function mainmenu_attach2(parent, child, showtype, position, isCurrent, bDisplayChild, className)
//{
//    
//  p = $(parent);
//  c = $(child);

//  p["at_parent"]     = p.id;  
//  p["at_child"]      = c.id;
//  p["at_position"]   = position; 
//  p["isCurrent"]     = isCurrent;
//  p["displayChild"]  = bDisplayChild;  
//  p["at_classname"]   = className;  
//  p["at_classname_selected"]   = className + "_Selected";
//  
//  c["at_parent"]     = p.id;
//  c["at_child"]      = c.id;  
//  c["at_position"]   = position;  
//  c["isCurrent"]     = isCurrent;
//  c["displayChild"]  = bDisplayChild;
//  c["at_classname"]   = className;  
//  c["at_classname_selected"]   = className + "_Selected";
//  c.style.position   = "absolute";
//  c.style.visibility = "hidden";

//  switch (showtype)
//  {
//    case "click":
//      p.onclick     = mainmenu_click;
//      p.onmouseout  = mainmenu_hide;
//      c.onmouseover = mainmenu_show;
//      c.onmouseout  = mainmenu_hide;
//      break;
//  case "over":
//      p.onmouseover = mainmenu_show2;
//      p.onmouseout = mainmenu_hide2;
//      c.onmouseover = mainmenu_show;
//      c.onmouseout = mainmenu_hide;
//      break;
//  }
//}

//function mainmenu_attach3(parent, child, showtype, position, isCurrent, bDisplayChild, className) {

//    
//    p = $(parent);
//    c = $(child);

//    p["at_parent"] = p.id;
//    p["at_child"] = c.id;
//    p["at_position"] = position;
//    p["isCurrent"] = isCurrent;
//    p["displayChild"] = bDisplayChild;
//    p["at_classname"] = className;
//    p["at_classname_selected"] = className + "_Selected";

//    c["at_parent"] = p.id;
//    c["at_child"] = c.id;
//    c["at_position"] = position;
//    c["isCurrent"] = isCurrent;
//    c["displayChild"] = bDisplayChild;
//    c["at_classname"] = className;
//    c["at_classname_selected"] = className + "_Selected";
//    c.style.position = "absolute";
//    c.style.visibility = "hidden";

//    switch (showtype) {
//        case "click":
//            p.onclick = mainmenu_click;
//            p.onmouseout = mainmenu_hide;
//            c.onmouseover = mainmenu_show;
//            c.onmouseout = mainmenu_hide;
//            break;
//        case "over":
//            p.onmouseover = mainmenu_show3;
//            p.onmouseout = mainmenu_hide3;
//            c.onmouseover = mainmenu_show;
//            c.onmouseout = mainmenu_hide;
//            break;
//    }
//}



function mainmenu_show() {
    
    p = $(this["at_parent"]);
    c = $(this["at_child"]);
    bDisplayChild = p["displayChild"];

    p.className = p["at_classname_selected"];

    if (bDisplayChild) {
        c.className = 'MainMenu_Item_Sub';
        mainmenu_show_aux(p.id, c.id);
    }
    clearTimeout(c["at_timeout"]);
}


function mainmenu_click()
{
  p = $(this["at_parent"]);
  c = $(this["at_child" ]);
  bDisplayChild =  p["displayChild"];  
  if(bDisplayChild){
	  if (c.style.visibility != "visible") mainmenu_show_aux(p.id, c.id);
	  else c.style.visibility = "hidden";
  }
  return false;
}

function mainmenu_show_aux(parent, child)
{
  var p = $(parent);
  var c = $(child);
  var top = (c["at_position"] == "y") ? p.offsetHeight + 2 : 0;
  
  var left = (c["at_position"] == "x") ? p.offsetWidth + 2 : 0;
  
  for (; p; p = p.offsetParent)
  {
    top  += p.offsetTop;
    left += p.offsetLeft;    
  }
  left = left + MainMenuConf.leftoffset;
  
  top = top + MainMenuConf.topoffset;
  

  var ver = getInternetExplorerVersion(); //zaznaj brskalnik
  if (ver > -1) {
  //koda za IE 8:
      if (ver > 7) {
          c.style.position = "absolute";
          top = top - getOffset(document.getElementById('glavni_meni')).top; 
          c.style.top = top + 'px';
          left = left - getOffset(document.getElementById('glavni_meni')).left;
          c.style.left = left + 'px';
      }
      else {
          //koda za IE 7 ali manjši:
          c.style.position = "fixed";
          top = top + getOffset(document.getElementById('glavni_meni')).top - 30;
          //alert(top);
          c.style.top = top + 'px';
          //left = left - getOffset(document.getElementById('glavni_meni')).left;
          left = left - 1;
         
          c.style.left = left + 'px';          
      }
  }
  else {
      //koda za FF in ostale:
      c.style.position = "absolute";
      c.style.top = top + 'px';           
      c.style.left = left + 'px';  
  }
  c.style.visibility = "visible"; 
 
}

function getOffset(el) {
    var _x = 0;
    var _y = 0;
    while (el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.offsetParent;
    }
    return { top: _y, left: _x };
}


function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function pageWidth() { return window.innerWidth != null ? window.innerWidth : document.body != null ? document.body.clientWidth : null; }
function pageHeight() { return window.innerHeight != null ? window.innerHeight : document.body != null ? document.body.clientHeight : null; } 



function mainmenu_hide()
{
  p = $(this["at_parent"]);
  c = $(this["at_child"]);
  bDisplayChild =  p["displayChild"];  
  
  if( this["isCurrent"] == true){
  	className = p["at_classname_selected"];
  } else {
	className = p["at_classname"];
  }  
  c["at_timeout"] = setTimeout("$('"+c.id+"').style.visibility = 'hidden';$('"+p.id+"').className='"+className+"';", MainMenuConf.hidetimeout);
}






// ----- Simple mouseover logic----- NO BLINKING!!!!

function mainmenu_select_attach(object, className, isCurrent)
{  
  p = $(object);
  p["at_id"]     = p.id;  
  p["at_classname"]   = className;  
  p["at_classname_selected"]   = className + "_Selected";  
  p["isCurrent"]     = isCurrent;
  
  p.onmouseover = mainmenu_select_show;
  p.onmouseout  = mainmenu_select_hide;
}


function mainmenu_select_show() {
    
  p = $(this["at_id"]);
  p.className = this["at_classname_selected"];
  
  clearTimeout(p["at_timeout"]);
}

function mainmenu_select_hide()
{
  p = $(this["at_id"]);
  
  if( p["isCurrent"] == true){
  	className = p["at_classname_selected"];
  } else {
	className = p["at_classname"];
  } 
  
  p["at_timeout"] = setTimeout("$('"+p.id+"').className='"+className+"';", MainMenuConf.simple_hidetimeout);
}
