﻿function cursor(type){
  if(type != 'default'){
    document.getElementById('window').style.cursor = 'url(grabbing.cur), e-resize';
    document.body.style.cursor = 'url(grabbing.cur), e-resize';
  }
  else
    document.body.style.cursor = 'default';
}

function centerIt(elementToCenter, event){
  closeAllPop();
  var center = document.getElementById('window');
  var clickPoint = event.clientX;
  var windowOffset = (center.parentNode.parentNode.offsetLeft + center.parentNode.offsetLeft);
  var moveItWidth = center.parentNode.style.width;
  var moveItLeft = center.style.left;
  var moveItCenter = Math.round((moveItWidth.substring(0, moveItWidth.length -2) - moveItLeft.substring(0, moveItLeft.length -2)) / 2);
  var windowCenter = (moveItWidth.substring(0, moveItWidth.length -2) / 2) + windowOffset;
  var windowScrollDelta = windowCenter - moveItCenter;
  var scrollCenter = clickPoint - windowScrollDelta;
  var moveItLeftDelta = scrollCenter - moveItCenter;
  var endCenterPoint = (center.style.width.substring(0, center.style.width.length -2) - moveItWidth.substring(0, moveItWidth.length -2));
 // window.alert(center.style.width.substring(0, center.style.width.length -2) + ' - ' + moveItWidth.substring(0, moveItWidth.length -2) + ' = ' + endCenterPoint);
  center.style.left = brakeCheck(moveItLeft.substring(0, moveItLeft.length -2) - moveItLeftDelta, endCenterPoint) + 'px';
  //window.alert('Click x: ' + clickPoint + ' Element Lext X: ' + windowOffset + '\n Element Width: ' + moveItWidth + ' Scroll Offset: ' + moveItLeft + '\n Scroll Center: ' + moveItCenter + '\n Window Center: ' + windowCenter + '\n New Scroll Center: ' + scrollCenter );
}

function brakeCheck(pos, highPos){
  if(pos > 0){ return 0;}
  else if(pos < (-1 * highPos)){ return -1 * highPos;}
  return pos;
}

function moveIt(elementToDrag, event){
  closeAllPop();
  var move = document.getElementById('window');
  var currentX = elementToDrag.style.left;
//  window.alert(currentX);
 // document.getElementById('status').innerHTML = currentX; //elementToDrag.style.left;
  if (document.addEventListener) {
    document.addEventListener("mousemove", moveHandler, true);
    document.addEventListener("mouseup", upHandler, true);
  }
  else if (document.attachEvent) { 
    elementToDrag.setCapture();
    elementToDrag.attachEvent("onmousemove", moveHandler);
    elementToDrag.attachEvent("onmouseup", upHandler);
        // Treat loss of mouse capture as a mouseup event
    elementToDrag.attachEvent("onlosecapture", upHandler);
  }
  var initCursor = event.clientX; // + elementToDrag.offsetLeft;
   document.body.style.cursor = 'default';//elementToDrag.style.curor =  'pointer';
    //var deltaX = elementToDrag.offsetLeft;
   // window.alert(elementToDrag.offsetLeft); 


  function moveHandler(e) {
    if (!e) e = window.event;  // IE Event Model
    var deltaX = initCursor - e.clientX;
   
    // Move the element to the current mouse position, adjusted as
    // necessary by the offset of the initial mouse-click.
    if(deltaX > 0){
      elementToDrag.style.left =  Math.max(-1 * ((((elementToDrag.style.width.substring(0, elementToDrag.style.width.length - 2)) - 2)) - elementToDrag.parentNode.style.width.substring(0, elementToDrag.parentNode.style.width.length - 2)),(currentX.substring(0, currentX.length -2) - (deltaX))) + "px";
      
    }
    else{
      elementToDrag.style.left =  Math.min(0,(currentX.substring(0, currentX.length -2) - (deltaX))) + "px";
      
    } 
        if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
        else e.cancelBubble = true; 
  }
  
  function upHandler(e) {
      //window.alert(document.getElementById('window').style.left);
        if (!e) e = window.event;  // IE Event Model

        // Unregister the capturing event handlers.
        if (document.removeEventListener) {  // DOM event model
            document.removeEventListener("mouseup", upHandler, true);
            document.removeEventListener("mousemove", moveHandler, true);
        }
        else if (document.detachEvent) {  // IE 5+ Event Model
            elementToDrag.detachEvent("onlosecapture", upHandler);
            elementToDrag.detachEvent("onmouseup", upHandler);
            elementToDrag.detachEvent("onmousemove", moveHandler);
            elementToDrag.releaseCapture();
        }
        else {  // IE 4 Event Model
            // Restore the original handlers, if any
            document.onmouseup = olduphandler;
            document.onmousemove = oldmovehandler;
        }

        // And don't let the event propagate any further.
        if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
        else e.cancelBubble = true;                  // IE
        
    document.getElementById('window').style.cursor = 'url(grab.cur), e-resize';
    document.body.style.cursor = 'default';
    }
  //  document.getElementById('status').innerHTML =  Math.max(-802,( elementToDrag.style.left.substr(0, -2) + (initCursor - e.clientX))) + "px";

}

function event2(){
  window.alert('event2');
}

function showPop(id){
  var tWindow = document.getElementById('window');
  var anchor = document.getElementById(id);
  var tWindowOffset = (tWindow.parentNode.parentNode.offsetLeft + tWindow.parentNode.offsetLeft);
  var windowLength = tWindow.parentNode.style.width;
  var windowOffset = tWindow.style.left.substring(0, tWindow.style.left.length - 2);
 // window.alert( (parseInt(windowOffset) + Math.round(anchor.style.left.substring(0, anchor.style.left.length -2))) + 'px');
  var show = document.getElementById('tlep' + id.substring(3));
  show.style.display = 'block';
  show.style.visibility = 'visible';
  show.style.zIndex = '10';
  show.style.position = 'absolute';
  show.style.top = (parseInt(anchor.style.top.substring(0, anchor.style.top.length - 2)) + tWindow.parentNode.offsetTop + tWindow.parentNode.parentNode.offsetTop - 250) + 'px';
  show.style.left = (parseInt(tWindowOffset) + parseInt(windowOffset) + Math.round(anchor.style.left.substring(0, anchor.style.left.length -2)) - 100) + 'px';
}

function closePop(id){
  var close = document.getElementById(id);
  close.style.display = 'none';
  close.style.visibility = 'hidden';
}

function closeAllPop(){
  var close;
  for(var i = 0; i < 100; i++){
    if(close = document.getElementById('tlep' + i)){
      close.style.display = 'none';
      close.style.visibility = 'hidden';
    }
  }
}

function showHelp(){
  var tt = document.getElementById('tlHelp');
  tt.style.visibility = 'visible';
  var interval = 2000;
  var timer = window.setTimeout(clearHelp, 5000);
}

function clearHelp(){
  document.getElementById('tlHelp').style.visibility = 'hidden'
}

