﻿var iMousePosX = 0;
var iMaxLeft = 150;
var iMaxRight = 700;
var oDragObj = null;
var iDocWidth;
var iBottomSpace = 305;
var _controlPrefix = 'ctl00_ctl00_Innehall_Innehall_';
var _RightPaneWidth = 947;

function InitializeTree(){

    iDocWidth = $(document).width();

    var ul = document.getElementById(_controlPrefix + 'trvCatalog_UL');
    
    // Get all <tr> elements
    var myLis = ul.getElementsByTagName("li");

    // Loop all <li>
    for(var i=0;i<myLis.length;i++){

        if(myLis[i].className == 'TrV-R TrV-CS' || myLis[i].className == 'TrV-R TrV-S'){
            myLis[i].scrollIntoView(true);
            break;
        }

    }
}

function ResizeContent(){

    var iHeight;

    // TODO Sätt rätt höjd när sidfoten är på plats
    iHeight = document.documentElement.clientHeight - iBottomSpace;

    // Do not shrink if too small
    if(iHeight < 50)
        return;
    
    document.getElementById("CatalogCont").style.height = iHeight + 'px';
    document.getElementById("SplitterShadow").style.height = iHeight + 'px';
    
    //if(document.getElementById(_controlPrefix + "FlikarTabContainer_body"))
    //    document.getElementById(_controlPrefix + "FlikarTabContainer_body").style.height = (iHeight - 40) + 'px';
   
    var iWidth;
    iWidth = document.getElementById(_controlPrefix + "hdnSplitterPos").value;
    if(iWidth == ''){
        iWidth = 200;
    }
    else
        iWidth = Math.abs(iWidth) - 12;

    document.getElementById("LeftPane").style.width = iWidth + "px";
    document.getElementById("RightPane").style.width = (_RightPaneWidth - iWidth - 5) + "px";
    
    // Position load indicators
    PositionsLoadIndicators()

}

// Positions load indicators in center of their containers
function PositionsLoadIndicators(){

    CenterElement(_controlPrefix + 'updProgPanTree','LeftPane');
    CenterElement(_controlPrefix + 'updProgPanMain','RightPane');

}

function CenterElement(loadIndicator,container){

    // get the update progress div
    var pnlPopup1 = $get(loadIndicator); 

    //  get the container element
    var pane1 = $get(container);
    
    // get the bounds of both the container and the progress div
    var pane1Bounds = Sys.UI.DomElement.getBounds(pane1);
    var pnlPopup1Bounds = Sys.UI.DomElement.getBounds(pnlPopup1);
    
    // center of container
    var x = pane1Bounds.x + Math.round(pane1Bounds.width / 2) - Math.round(pnlPopup1Bounds.width / 2) - 40;
    var y = pane1Bounds.y + Math.round(pane1Bounds.height / 2) - Math.round(pnlPopup1Bounds.height / 2) - 30;        

    // set the progress element to this position
    Sys.UI.DomElement.setLocation(pnlPopup1, x, y);  

}

// Start
function Drag(ref) {
    oDragObj = ref;
}

// Stop
function Drop() {
    //oDragObj.className = "tagnormal";
    oDragObj = null;
    
    var splitterpos;
    splitterpos = document.getElementById(_controlPrefix + "hdnSplitterPos").value;

    document.getElementById("LeftPane").style.width = (splitterpos - 12) + "px";
    document.getElementById("RightPane").style.width = (_RightPaneWidth + 12 - splitterpos) + "px";        
    
    // Position load indicators
    PositionsLoadIndicators()
}

// Handle mouse movement
function getMouseXY(posX) {
    iMousePosX = (posX - Math.abs((iDocWidth/2 - 495)));
    // Move object, if dragging
    if (oDragObj != null) {
        if (iMousePosX > iMaxLeft && iMousePosX < iMaxRight) {

            document.getElementById("SplitterShadow").style.left = (posX - 3) + "px";
            document.getElementById(_controlPrefix + "hdnSplitterPos").value = iMousePosX;
        }else{
            Drop();
        }
    }
}

// Mouse movement event handler
function getMouseXYPos(e) {
    if (!e) var e = window.event;
    getMouseXY(e.clientX);
    return true;
}

function HookDivScroll(){
    document.getElementById("LeftPane").onmouseup = TrackScroll;
}

function TrackScroll(){
    var div;
    div = document.getElementById("LeftPane");
    if(div){
        document.getElementById(_controlPrefix + "hdnScrollY").value = div.scrollTop;
        document.getElementById(_controlPrefix + "hdnScrollX").value = div.scrollLeft;
    }
}