var X;
var Y;
  
var amb_layers = document.layers ? 1 : 0;
var opera = navigator.userAgent.indexOf('Opera') > 0;
var konky = navigator.userAgent.indexOf('Konqueror') > 0;

var ult_L1 = 0;
var ult_L2 = 0;
var ult_L3 = 0;
var ult_X1 = 0;
var ult_X2 = 0;

var ample = 640;
var altura = 400;

var oldcolor = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var oldbg;
var oldfg;

function setbg(tag, bg, fg) {

    if (typeof(tag.style) == 'undefined') { return false; }
    if (typeof(bg) == 'undefined') { bg = '#6666cc'; }
    if (typeof(fg) == 'undefined') { fg = '#ffffff'; }
    oldbg = tag.style.backgroundColor;
    oldfg = tag.style.color;
    tag.style.backgroundColor = bg;
    tag.style.color = fg;
    return true;
}

function resetbg(tag) {

    if (typeof(tag.style) == 'undefined') { return false; }
    tag.style.backgroundColor = oldbg;
    tag.style.color = oldfg;
    return true;
}

function setlinia(linia) {
    
    if (typeof(linia.style) == 'undefined' || typeof(linia.cells) == 'undefined') {
        return false;
    }
    
    var celles = linia.cells.length;

    for (var c = 0; c < celles; c++) {
        oldcolor[c] = linia.cells[c].bgColor;
        linia.cells[c].bgColor = '#ccdccc';
        linia.cells[c].style.backgroundColor = '#ccdccc';
    }

    return true;
}

function resetlinia(linia) {

    if (typeof(linia.style) == 'undefined' || typeof(linia.cells) == 'undefined') {
        return false;
    }
    
    var celles = linia.cells.length;

    for (var c = 0; c < celles; c++) {
        linia.cells[c].bgColor = oldcolor[c];
        linia.cells[c].style.backgroundColor = oldcolor[c];
    }

    return true;
}

function getwindowsize() {
   
   if (window.outerWidth) {
      
      ample = window.outerWidth;
      altura = window.outerHeight;
   
   } else {
   
      ample = document.body.clientWidth;
      altura = document.body.clientHeight;
   
   }
   
   return;

}

var comodi = '';

function pop(ev, nota, tipus, amplada, fokus, nou_comodi) {
   
   comodi = nou_comodi;

   if (! amplada) amplada = 200;
   
   getwindowsize();
   
   X = gX(ev);
   Y = gY(ev);
   
   /*
   if (ev.pageY) {
     
      X = ev.pageX;
      Y = ev.pageY;
      
   } else { 
   
      X = ev.clientX;
      Y = ev.clientY;
      
      if (! opera && ! amb_layers && ! konky) {
         if (document.body.scrollTop) Y += document.body.scrollTop;
         if (document.body.scrollLeft) X += document.body.scrollLeft;
      }
   }
   */

   if (tipus == 0) {   
      Y = Y + 15;
      X = (X - amplada) + 10 ;
      if (X < 0) X = 0;
      ult_X1 = X;
      if (ult_L1) amaga(ult_L1, 0);
      if (ult_L2) amaga(ult_L2, 0);
      if (ult_L3) amaga(ult_L3, 0);
      ult_L1 = nota;
   }else if (tipus == 1) {   
      Y = Y + 10;
      X = X - (amplada / 2)
      if (X < 0) X = 0;
      ult_X1 = X;
      if (ult_L1) amaga(ult_L1, 0);
      if (ult_L2) amaga(ult_L2, 0);
      if (ult_L3) amaga(ult_L3, 0);
      ult_L1 = nota;
   }else if (tipus == 2) {
      Y = Y - 5;
      X = ult_X1 + 80;
      if (X < 0) X = 0;
      ult_X2 = X;
      if (ult_L2) amaga(ult_L2, 0);
      if (ult_L3) amaga(ult_L3, 0);
      ult_L2 = nota;
   }else{
      Y = Y - 5;
      X = ult_X2 + 50;
      if (X < 0) X = 0;
      if (ult_L3) amaga(ult_L3, 0);
      ult_L3 = nota;
   }
      
   sX(nota, X);
   sY(nota, Y);
   sE(nota);

   if (fokus) {
      if (document.js_anar.anar) {
         document.js_anar.anar.focus();
      } else {
         document.eval(nota).document.js_anar.anar.focus();
      }
   }

   return true;

}

function mostrar_nota() { return void(0); }
function tancar_nota() { return void(0); }

function amaga(nota, tipus){
   if (tipus == 1) {   
      if (ult_L2) amaga(ult_L2, 0);
      if (ult_L3) amaga(ult_L3, 0);
   } else if (tipus == 2) {
      if (ult_L3) amaga(ult_L3, 0);
   }
   
   hE(nota);
}

function wst(st) {
   if (st) {
      window.status = st;
   } else { 
      window.status = 'Tancar';
   }
   return true;
}   


function gE(e) {
   if (amb_layers) return document.layers[e];
   if (document.all) return document.all[e];
   return document.getElementById(e);
}
  
function gX(ev) {
   
   if (ev.pageX) {
     
      X = ev.pageX;
      
   } else { 
   
      X = ev.clientX;
      
      var scroll = 0;
      if( document.body && document.body.scrollLeft ) {
        scroll = document.body.scrollLeft;
      } else if( document.documentElement && document.documentElement.scrollLeft ) {
        scroll = document.documentElement.scrollLeft;
      }

      X += scroll;
   
   }
   
   return X;
}   
   
function gY(ev) {
   
   if (ev.pageY) {
     
      Y = ev.pageY;
      
   } else { 
   
      Y = ev.clientY;
  
     var scroll = 0;
     if( document.body && document.body.scrollTop ) {
       scroll = document.body.scrollTop;
     } else if( document.documentElement && document.documentElement.scrollTop ) {
       scroll = document.documentElement.scrollTop;
     }

     Y += scroll;
   
   }

   return Y;
}
   


function sX(e, x) {
   e = gE(e); 
   if (amb_layers) {
      e.left = x; 
   } else { 
      e.style.left = x + 'px';
   }
}
  
function sY(e, y) {
   e = gE(e); 
   if (amb_layers) {
      e.top = y; 
   } else { 
      e.style.top = y + 'px';
   }
}

function sE(e) { 
   e = gE(e); 
   if(amb_layers) {
      e.visibility = 'show';
   } else {
      e.style.visibility='visible';
   }
}
      
function hE(e) {
   e = gE(e); 
   if (amb_layers) {
      e.visibility = 'hide';
   } else {
      e.style.visibility = 'hidden';
   }
}
  



























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;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
  if (! this.isIE) {
    this.isNS = true;
  } else if (! this.isNS) {
    this.isIE = true;
  }

  if (amb_layers) {
    this.isNS = true;
    this.isIE = false;
  } else {
    this.isNS = false;
    this.isIE = true;
  }
   
  this.isNS = true;
  this.isIE = true;

}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // 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;
  }

  dragObj.elNode = gE(id);

  // 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;
  }

  //x = gX(id);
  //y = gY(id);

  // 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;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  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 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) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",	dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,	true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}









