function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function setOpacity(value) {
	$("divMenu").style.opacity = value/10;
	$("divMenu").style.filter = 'alpha(opacity=' + value*10 + ')';
}

function initMenuDiv() {
    var coors = findPos($("divMenu"));
    $("divMenu").style.top = coors[1] + 'px';
    $("divMenu").style.left = coors[0] + 'px';
}

function initForDragging() {
    setOpacity(5);
    initMenu();
    $("divMenu").style.position="absolute";
    show($("imgMinimize"));
}

function initForStopping() {
    var kontur = findPos($("divContour"));
    var coors = findPos($("divMenu"));
    if (kontur[1]-coors[1]>-50 && kontur[1]-coors[1]<50 && kontur[0]-coors[0]>-50 && kontur[0]-coors[0]<50) {
        switchMenu();
    }
    setOpacity(10);
}

function dragStart(event, id) {
  var el;
  var x, y;
  
  initForDragging();
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;
  dragObj.elNode.style.zIndex = ++dragObj.zIndex;
  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
  
}

function getWinHeight() {
    var winH = 460;

    if (parseInt(navigator.appVersion)>3) {
     if (navigator.appName=="Netscape") {
      winH = window.innerHeight;
     }
     if (navigator.appName.indexOf("Microsoft")!=-1) {
      winH = document.body.offsetHeight;
     }
    }
    
    return winH;
}

function getWinWidth() {
    var winH = 460;

    if (parseInt(navigator.appVersion)>3) {
     if (navigator.appName=="Netscape") {
      winH = window.innerWidth;
     }
     if (navigator.appName.indexOf("Microsoft")!=-1) {
      winH = document.body.offsetWidth;
     }
    }
    
    return winH;
}



function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {
  initForStopping();
  
  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

function initMenu() {
    show($('divMenu'));
    initMenuDiv();
}

function switchMenuCat(menuCatObj) {
    var menuCatContentObj = $(menuCatObj.id+"Content");
    if (menuCatContentObj) {
        menuCatContentObj.style.display = (menuCatContentObj.style.display == "none" ? "block" : "none");
    }
}

function switchMenu(isCommandFromMenu) {
    $('divMenu').style.position="static";
    initMenuDiv();
    hide($("imgMinimize"));
    moveFly();
}

function relocateMenu() {
    var currentOffset = document.documentElement.scrollTop || document.body.scrollTop; // body for Safari
    $('divMenu').style.position="absolute";
    $('divMenu').style.top = currentOffset +"px";
    show($("imgMinimize"));
}

function moveFly() {
    var header = $('divFly');
	if (!header) return;
	var currentOffset = document.documentElement.scrollTop || document.body.scrollTop; // body for Safari
	
	if (currentOffset != parseInt(header.style.top) && currentOffset>150) 
		header.style.top = currentOffset + 'px';
	else if (currentOffset<=150) {
		header.style.top = '150px';
	}

    var currentY = currentOffset;
    var menuY = parseInt($('divMenu').style.top.substring(0,$('divMenu').style.top.length-2));
    if (menuY+19<currentY || menuY - currentY+20 >getWinHeight()) {
        show($('divFly'));
    } else {
        hide($('divFly'));
    }
}

function debug(str) {
    $("mainPage").innerHTML = str;
}

function menu_call() {
    var currentY = document.documentElement.scrollTop || document.body.scrollTop; // body for Safari
    var menuY = parseInt($('divMenu').style.top.substring(0,$('divMenu').style.top.length-2));
    if (menuY+19<currentY || menuY - currentY+20 >getWinHeight()) {
        relocateMenu();
    }
}

window.onresize = moveFly;
window.onscroll = document.documentElement.onscroll = moveFly;