// DRAG N DROP ----------------------------------------------------------------

var ie = document.all;
var nn6 = document.getElementById && !document.all;
var isdrag=false;
var x,y;
var dobj,fobj;
//var scrollwindowHeight = parseInt(document.getElementById("menuContentHolder").style.width);
//alert(document.getElementById("menuContentHolder"));

function movemousePreviewWindow(e) {
   
  if (isdrag)
  {
    // tx and ty is pickup position in relation to parent element.
    // x and y is pickup position in relation to the browser window.
    // cX and cY is mouse pointer current position in browser window.

    var cX = nn6 ? e.clientX : event.clientX;
    var cY = nn6 ? e.clientY : event.clientY;

    // curX and curY is current position in relation to parent element;

    var curX = (tx + cX - x);
    var curY = (ty + cY - y);

    dobj.style.left = curX;
    dobj.style.top = curY;

    return false;
  }
}
// MOVE EDIT WINDOW -----------------------------------------------------------

function movemouseEditWindow(e) {
  if (isdrag)
  {
    // tx and ty is pickup position in relation to parent element.
    // x and y is pickup position in relation to the browser window.
    // cX and cY is mouse pointer current position in browser window.

    var cX = nn6 ? e.clientX : event.clientX;
    var cY = nn6 ? e.clientY : event.clientY;

    // curX and curY is current position in relation to parent element;

    var curX = (tx + cX - x);
    var curY = (ty + cY - y);

    dobj.style.left = curX;
    dobj.style.top = curY;

    return false;
  }
}

// MOVE MOUSE SCROLL ----------------------------------------------------------

 var getGotoDisp = true;
 var gotoDispHolder = "";

function movemouseScroll(e) {
  if (isdrag && (dobj.id == "dragbutton"))
  {
    // tx and ty is pickup position in relation to parent element.
    // x and y is pickup position in relation to the browser window.
    // cX and cY is mouse pointer current position in browser window.

    var cX = nn6 ? e.clientX : event.clientX;
    var cY = nn6 ? e.clientY : event.clientY;

    // curX and curY is current position in relation to parent element;

    var curX = (tx + cX - x);
    var curY = (ty + cY - y);

    if ( (curY >= 0) && (curY <= (scrollwindowHeight - 16)) ) {
      dobj.style.top = curY + "px";
    } else if ( curY < 0 ) {
      curY = 0;
      dobj.style.top = curY + "px";
    } else if ( curY > (scrollwindowHeight - 16) ) {
      curY = (scrollwindowHeight - 16);
      dobj.style.top = curY + "px";
    }

    // Move content in scroll window.

    var contentObj = document.getElementById("menuContent");
    var factor = parseInt(dobj.style.top) / (scrollwindowHeight - 19);

    contentObj.style.top = -((contentObj.offsetHeight - scrollwindowHeight) * factor) + "px";

    // Debug.
/*
    var output = "<pre>tx: "+ tx + "  ty: " + ty + "<br />x: " + x + "  y: " + y + "<br /><br />";
    output += "(tx + cX - x): "+ (tx + cX - x) + "  (ty + cY - y): " + (ty + cY - y) + "<br>";
    output += "\n\n" + dobj.id + dobj.id + ": " + dobj.style.top + "\n";
    output += contentObj.id + " height: " + contentObj.offsetHeight + " contentObj.style.top: " + contentObj.style.top + "\n";
    output += "factor: " + factor + "\n";
    output += "curX: "+ curX + "  curY: " + curY + "</pre>";
    document.getElementById("coords").innerHTML= output;
*/


    return false;
  } else if (isdrag && (dobj.id == "dragbutton2")) {


    var dragback2Width = parseInt(document.getElementById("dragback2").style.width);
    // tx and ty is pickup position in relation to parent element.
    // x and y is pickup position in relation to the browser window.
    // cX and cY is mouse pointer current position in browser window.

    var cX = nn6 ? e.clientX : event.clientX;
    var cY = nn6 ? e.clientY : event.clientY;

    // curX and curY is current position in relation to parent element;

    var curX = (tx + cX - x);
    var curY = (ty + cY - y);

    if ( (curX >= 0) && (curX <= (dragback2Width - 16)) ) {
      dobj.style.left = curX  + "px";
      
    } else if ( curX < 0 ) {
      //curX = 0;
      curX = 0;
      dobj.style.left = curX + "px";
      
    } else if ( curX < (dragback2Width - 16) ) {
      curX = (dragback2Width - 16);
      dobj.style.left = curX + "px";
    }


        contentObj = document.getElementById("hitsDisplay");
        factor = parseInt(dobj.style.width) / (dragback2Width);


// calc and display imageId to Show
        var cpos = calcDragPos();
        if (cpos < 1) cpos = 1;
        if (!getGotoDisp){
            gotoDispHolder = document.getElementById("gotoDisp").innerHTML;
            getGotoDisp = false;
        }
        
        document.getElementById("gotoDisp").innerHTML = gw('gå till', 1)+ " " + gw('bild', 1) + " " + cpos;
        //<span id="firstimgdisp">
/*
        var inPos = parseInt(dobj.style.left);

//          dobj = null;
          if (inPos > 205) inPos = 205;

          if (ndc.hits > 0) {
          //    alert("inPos : " + inPos);
              if (inPos > 100) inPos += 16;

              var nI = (inPos / 2);
              ni2 = (nI /100);
              var dVal = (ni2 * ndc.hits);
              if (dVal > ndc.hits) dVal = ndc.hits;
              if (dVal < 1) dVal = 1;
        //    alert("goTo" + Math.round(nId));
            document.getElementById("imCHolder").innerHTML = Math.round(dVal);
          }
*/
// calc and display imageId to Show

/*
    var output = "<pre>tx: "+ tx + "  ty: " + ty + "<br />x: " + x + "  y: " + y + "<br /><br />";
    output += "(tx + cX - x): "+ (tx + cX - x) + "<br />(ty + cY - y): " + (ty + cY - y) + "<br>";
    output += "\n\n" + dobj.id + ":<br />" + dobj.style.top + "\n";
    output += contentObj.id + " height:<br />" + contentObj.offsetHeight + " c.o.s.top: " + contentObj.style.top + "\n";
    output += "\n\n" + dobj.id + "w:<br />" + dobj.style.width + "\n";
    output += contentObj.id + " width:<br />" + contentObj.offsetWidth + " c.o.s.width: " + contentObj.style.width + "\n";
    output += "factor:<br />" + factor + "\n";
    output += "scrollwindowwidth: " + parseInt(document.getElementById("hitsDisplay").style.width) + "\n";
    output += "jem" + (curX + 16) + "px";
    output += "<br />jem2:" + scrollwindowWidth;
    output += "curX: "+ curX + "  curY: " + curY + "</pre>";



    document.getElementById("coords").innerHTML= output;
*/

    return false;
  }

  return false;
}

// SELECT MOUSE -------------------------------------------------------------

function selectmouse(e) {
  fobj = nn6 ? e.target : event.srcElement;
  var topelement = nn6 ? "HTML" : "BODY";


  while (fobj.tagName != topelement && fobj.id != "dragbutton" && fobj.id != "dragbutton2" && fobj.className != "dragmeEditWindow") {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
    //if (fobj.className =! "true") {
    //alert("skum: " +  fobj.className);
    //}
  }




  // Scrolldragger.

  if (fobj.id == "dragbutton") {
    isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    document.onmousemove=movemouseScroll;
    return false;
  }

  if (fobj.id == "dragbutton2") {
    isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;

    //document.getElementById("jemx").innerHTML = x;
    //document.getElementById("jemy").innerHTML = y;

    document.onmousemove=movemouseScroll;
    
    return false;
  }

//if (fobj.className != ""){
//alert("jernis ; " + fobj.classNames);
//}

  if (fobj.className=="dragmeEditWindow") {
      
    isdrag = true;
    dobj = document.getElementById("ew");
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
     
    document.onmousemove=movemousePreviewWindow;
    return false;
  }
  //return false;
}

// RELEASE MOUSE -------------------------------------------------------------

function calcDragPos(){
    if (dobj != null) {
      if (dobj.id == "dragbutton2") {
          var inPos = parseInt(dobj.style.left);
          var dobjLength = 195;//parseInt(dobj.style.width);
          if (inPos > dobjLength) inPos = dobjLength;

          if (ndc.hits > 0) {
              //alert("inPos : " + inPos);
              if (inPos > dobjLength) inPos += 16;
              var tmpi = (dobjLength/100);
              var nI = (inPos / Math.round(tmpi));
              ni2 = (nI /91);
              var dVal = (ni2 * ndc.hits)-1;
              if (dVal > ndc.hits) dVal = ndc.hits;
            //alert("goTo" + Math.round(nId));
            //document.getElementById("imCHolder").innerHtml = Math.round(dVal);
            //Jemplay(Math.round(dVal));
            if (dVal < 0) dVal = 0;
            return Math.round(dVal);
            }
        }
    }
    return false;
}

function releasemouse() {
  isdrag = false;
if (fobj.className !="dragmeEditWindow") {
  if (dobj != null) {
      var hotId = (calcDragPos() -1);
      if (hotId < 0) hotId = 0;
      if (hotId >= ndc.hits) hotId = (ndc.hits - 1);
      //alert(hotId + " " + ndc.hits);
      
        dobj = null;
        getGotoDisp = true;
        
        document.getElementById("gotoDisp").innerHTML = gotoDispHolder;
        gotoHotId(hotId);
      /*
      if (dobj.id == "dragbutton2") {
          var inPos = parseInt(dobj.style.left);
          
          dobj = null;
          if (inPos > 205) inPos = 205;

          if (ndc.hits > 0) {
              alert("inPos : " + inPos);
              if (inPos > 100) inPos += 16;

              var nI = (inPos / 2);
              ni2 = (nI /100);
              var dVal = (ni2 * ndc.hits);
              if (dVal > ndc.hits) dVal = ndc.hits;
            alert("goTo" + Math.round(nId));
            document.getElementById("imCHolder").innerHtml = Math.round(dVal);
            //Jemplay(Math.round(dVal));
            }
            */
        }

    
 

  if (fobj.className=="dragbutton") {
    dobj = null;
  }

  if (fobj.className=="dragbutton2") {
    dobj = null;
  }
}
   return false;
}

// ADJUSTING SCROLL WINDOW ---------------------------------------------------


function scrollRecalc() {
  var contentObj = document.getElementById("menuContent");
  var dragObj = document.getElementById("dragbutton");
  var contentFactor = (-parseInt(contentObj.style.top)) / (contentObj.offsetHeight - scrollwindowHeight);


  dragObj.style.top = (contentFactor * (scrollwindowHeight - 19)) + "px";

//  alert("offset: " + parseInt(contentObj.offsetHeight));
//  alert("scrollwindow Height: " + scrollwindowHeight);

  if ( parseInt(contentObj.offsetHeight) < scrollwindowHeight ) {
    document.getElementById("dragback").style.display = "none";
    document.getElementById("dragbutton").style.display = "none";
    var contentPosition = (scrollwindowHeight - contentObj.offsetHeight);

    document.getElementById("menuContent").style.top = contentPosition + "px";
    document.getElementById("dragbutton").style.top = (scrollwindowHeight - 16) + "px";

//alert(document.getElementById("menuContentHolder").style.height);
//alert(document.getElementById("menuContent").style.top);
//alert("top: " + document.getElementById("menuContent").style.top + " height: " + document.getElementById("menuContent").style.height);
  } else {
    if ( parseInt(contentObj.style.top) > 0 ) {
      contentObj.style.top = "-20px";
      dragObj.style.top = "20px";
    }
    document.getElementById("dragback").style.display = "";
    document.getElementById("dragbutton").style.display = "";
  }

}


// SETTING EVENTS ------------------------------------------------------------

document.onmousedown=selectmouse;
document.onmouseup=releasemouse;





