﻿// JScript File
var _isDragging = false;
var oldOffsetLeft;
var IE_noCss3 = 0;
function initializeDraggables(dragClassName)
 {
    $(dragClassName).draggable(
        {
            cursor : 'move',
            handle : ".draggable_window_top",
            start : function()
                {
                    oldOffsetLeft = this.style.left;

                    _isDragging = true;
                    
                    var tabId = $(this).parents(".tabContainer")[0].id.replace("fragment-","");
                    SetZindexUpdateMax($(this),tabId);
                    
                    dragged_element=this;
                    dragged_element_top=this.style.top;
                    dragged_element_left=this.style.left;
                    dragged_element_height=$(this).height();
                    dragged_element_width=$(this).width();

                    //registerEventName(event,'start');
                    var frameID = this.id.replace("win","");
                    var cell = $("#tdContentWin"+frameID);
                    SetFrameSize(cell);
                    minimizeElements($(this));
                    
                    // for dropping - set opacity to 0.5 and background transparent
                    $("#win"+frameID).css("background-color","Transparent");
                    $("#win"+frameID+"_resizeSheet").css("background-color","Transparent");
                    $("#outerDivWin"+frameID).css("background-color","Transparent");
                    $("#tableHeaderWin"+frameID).css("opacity","0.5");
                    $(this).find(".WindowRoundEdge").css("opacity","0.5");
                    //alert($(this).find(".WindowRoundEdge").length);
                },
            stop : function()
                {
                    var theEvent = window.event || arguments.callee.caller.arguments[0];
                    var frameID = this.id.replace("win","");
                    
                    // NOTE: i changed the bg-color of the 'sheet' to Trasparent
                    // this is only temporary untill we find what to do with the resizing.
                    
                    // restore opacity and backgrounds
                    $("#win"+frameID).css("background-color","");
                    //$("#win"+frameID+"_resizeSheet").css("background-color","#ffffff");
                    $("#win"+frameID+"_resizeSheet").css("background-color","Transparent");
                    $("#outerDivWin"+frameID).css("background-color","");
                    $("#tableHeaderWin"+frameID).css("opacity","");
                    $(this).find(".WindowRoundEdge").css("opacity","");
                    
                    var cell = $("#tdContentWin"+frameID);
                    SetFrameSize(cell);
                    //checkPosition(this,topBorder);
                    checkPosition(this,116);
                    if (!editable && !genericOverDroppable) 
                    {
                        this.style.left = dragged_element_left;
                        this.style.top = dragged_element_top;
                       openSystemDiv("SysWinEditInfo","Edit Tab",100,460,0,0,'');

                    }

                    //registerEventName(event,'stop');
                    initializeResizable(".resizeSheet");
                    
                    _isDragging = false;
                    ajaxUpdateCall(
                        {
                            type   : "drag-stop",
                            id     : this.id.replace("win",""),
                            frTop  : this.style.top,
                            frLeft : this.style.left
                        });
                }
            }).bind("click",function()
                {
                    var tabId = $(this).parents(".tabContainer")[0].id.replace("fragment-","");
                    SetZindexUpdateMax($(this),tabId);
                    // BUG: when moving your cursor above a flash item while dragging
                    //      the frame would stick to you mouse pointer.
                    // FIX: stop dragging by clicking.
                    if (_isDragging) {
                        _isDragging = false;
                        $(this).triggerHandler("dragstop");
                    }
                });

        $(".win_iframe").bind("click",function()
        {
            var tabId = $(this).parents(".tabContainer")[0].id.replace("fragment-","");
            SetZindexUpdateMax($(this).parents(".dragWin"),tabId);
        });
 }
 function initializeResizable(resizableClassName)
 {
    $(resizableClassName).resizable(
        {
            minHeight : 65,
            minWidth : 100,
            start : function(ev,ui)
            {
                //registerEventName(event,"resize_start");
                var theDiv = $(this).parents(".dragWin");
                
                var tabId = theDiv.parents(".tabContainer")[0].id.replace("fragment-","");
                SetZindexUpdateMax(theDiv,tabId);
                
                $(this).css("z-index",200000000);
                $(this).css("opacity",0.6);
            },
            resize : function(ev,ui)
            {
                //registerEventName(event,"resize");
            },
            stop : function(ev,ui)
            {
                //registerEventName(event,"resize-stop");
                var height=$(this).height();
                var width=$(this).width();
                $(this).css("z-index",-1);
                $(this).css("opacity",1);
                
                var sheet = $(this);
                var theDiv = $(this).parents(".dragWin");
                var frameID = theDiv[0].id.replace("win","");
                var cell = $("#tdContentWin"+frameID);
                var row = $("#trContentWin"+frameID);
                
                minimizeElements(theDiv);
                SetSize(theDiv,width-2,height-2);
                FixTable(theDiv.children(":first"),$(this));
                SetFrameSize(cell);
                SetBannersSize(row,sheet);
                
                ajaxUpdateCall(
                    {
                        type      : "resize-stop",
                        id        : theDiv[0].id.replace("win",""),
                        frHeight  : $("#frame"+frameID).height(),
                        frWidth   : $("#frame"+frameID).width()
//                        frHeight  : theDiv.children("div.draggable_window").height(),
//                        frWidth   : theDiv.children("div.draggable_window").width()
                    });
                
                //TODO:think of another way to make sure the sheet isnt getting smaller then the table.
                //CheckSheetRelativeSize($(this),theDiv("table.draggable_window"));
                //=================================
            }
        });
 }


// ************************************************************************ //
// ** Note for Round Edges :                                              * //
// * the size and position of all windows will remain the same as before. * //
// * only now we have a 7px edge for evrey window, in each direction.     * //
// ************************************************************************ //



//==================================Win Size Settings============================
//TODO: try to make it less functions

function SetSizeServer(frameID,width,height)
{
   var theDiv =$('#win'+frameID);
   
   var cell = $("#tdContentWin"+frameID);
   var row = $("#trContentWin"+frameID);
   var heightDiff = $("#toolbars"+frameID).height() + $("#tableFooterWin"+frameID).height();
   var widthDiff = $("#tdLeftBannerWin"+frameID).width() + $("#tdRightBannerWin"+frameID).width();
   //set the resizeSheet size.
   var sheet = $("#win"+frameID+"_resizeSheet")
   if (width != null)
   {
         sheet.attr("width",width + widthDiff +2);
         sheet.width(width +widthDiff +2);
   }
   if (height != null)
   {
        sheet.attr("height",height + heightDiff +2);
        sheet.height(height  + heightDiff +2);
   }
   minimizeElements(theDiv);
   SetSize(theDiv,width + widthDiff,height + heightDiff);
   if(IE_noCss3){
        var roundrect = $('#win_vml' + frameID);
        roundrect.width(width+widthDiff + 4);
        roundrect.height(height+heightDiff+12);
        var roundrectTop = $('#win_vmlTOP' + frameID);
        roundrectTop.width(width+widthDiff + 4);

   }
   FixTable(theDiv.children(":first"),sheet);
   SetFrameSize(cell);
   SetBannersSize(row,sheet);
   
   //alert(row.height());
   
   //check the size is ok in sheet and table for visuality.
   CheckSheetRelativeSize(sheet,theDiv.children("div.draggable_window"));
}
 
 function FixTable(tbl,sheet)
 {
    var frameID = sheet[0].id.replace("win","").replace("_resizeSheet","");
    var heightDiff = $("#toolbars"+frameID).height() + $("#tableFooterWin"+frameID).height();
    var widthDiff = $("#tdLeftBannerWin"+frameID).width() + $("#tdRightBannerWin"+frameID).width();
    var tblWidth = sheet.width()-2;
    var rowHeight = sheet.height() - heightDiff - 2;
    var outerDiv = $("#outerDivWin"+frameID);
    //alert(rowHeight);
    //outerDiv.height(rowHeight);
    //fix row height
    var tr = $("#trContentWin"+frameID);
    tr.height(rowHeight);
    //fix container td
    var td = $("#tdContentWin"+frameID); 
    //set to twice the wanted banner width
    var w = tblWidth - widthDiff;
    td.width(w+1);
    td.attr("width", w+1);
    
    // now set the round edges size
    // for now use traversing - make sure to change it to a more stable method in the future

    var roundEdgeTable = sheet.prev();
    if (roundEdgeTable.attr("class") == "WindowRoundEdge") {
        //roundEdgeTable.height(sheet.height());
        //roundEdgeTable.width();
//        var roundEdgeTop = roundEdgeTable.find(".t");
//        var roundEdgeBottom = roundEdgeTable.find(".b");
//        var roundEdgeLeft = roundEdgeTable.find(".l");
//        var roundEdgeRight = roundEdgeTable.find(".r");
        var roundEdgeCenter = roundEdgeTable.find(".frame");
//        roundEdgeTop.width(tblWidth+1);
//        roundEdgeBottom.width(tblWidth+1);
//        roundEdgeLeft.height(rowHeight);
//        roundEdgeRight.height(rowHeight);
        //roundEdgeCenter.height(rowHeight);
    }
}
 
  function FixTableSpecific(tbl,sheet,width,height)
 {
    var frameID = sheet[0].id.replace("win","").replace("_resizeSheet","");
    var heightDiff = $("#toolbars"+frameID).height() + $("#tableFooterWin"+frameID).height();
    var widthDiff = $("#tdLeftBannerWin"+frameID).width() + $("#tdRightBannerWin"+frameID).width();
    var tblWidth = sheet.width()-2;
    var rowHeight = sheet.height() - heightDiff - 2;
    var outerDiv = $("#outerDivWin"+frameID);
    //alert(rowHeight);
    //outerDiv.height(rowHeight);
    //fix row height
    var tr = $("#trContentWin"+frameID);
    tr.height(height);
    //fix container td
    var td = $("#tdContentWin"+frameID); 
    //set to twice the wanted banner width
    var w = width;
    td.width(w+1);
    td.attr("width", w+1);
    
    // now set the round edges size
    // for now use traversing - make sure to change it to a more stable method in the future

    var roundEdgeTable = sheet.prev();
    if (roundEdgeTable.attr("class") == "WindowRoundEdge") {
        //roundEdgeTable.height(sheet.height());
        //roundEdgeTable.width();
//        var roundEdgeTop = roundEdgeTable.find(".t");
//        var roundEdgeBottom = roundEdgeTable.find(".b");
//        var roundEdgeLeft = roundEdgeTable.find(".l");
//        var roundEdgeRight = roundEdgeTable.find(".r");
        var roundEdgeCenter = roundEdgeTable.find(".frame");
//        roundEdgeTop.width(tblWidth+1);
//        roundEdgeBottom.width(tblWidth+1);
//        roundEdgeLeft.height(rowHeight);
//        roundEdgeRight.height(rowHeight);
        //roundEdgeCenter.height(rowHeight);
    }
}

function SetFrameSize(cell)
{
    var frameID = cell[0].id.replace("tdContentWin","");
    var cellH = cell.innerHeight();
    var cellW = cell.innerWidth();
    var frame = $("#frame"+frameID);
    if (cellH != null)
         frame.attr("height",cellH);
    if (cellW != null)
        frame.attr("width",cellW);
}

 function SetBannersSize(_row,sheet)
 {
    var frameID = sheet[0].id.replace("win","").replace("_resizeSheet","");
    var rowHeight = $(sheet).height()- $("#toolbars"+frameID).height() - $("#tableFooterWin"+frameID).height() - 2;
    _row.height(rowHeight);
    var bnrDiv = $("#SideBannerLeft"+frameID);
    bnrDiv.height(rowHeight);
    bnrDiv = $("#SideBannerRightWin"+frameID);
    bnrDiv.height(rowHeight);
 }

function CheckSheetRelativeSize(sheet,tbl)
{
//    var frameID = sheet[0].id.replace("win","").replace("_resizeSheet","");
//    var sheetW,sheetH,tblW,tblH;
//    var theDiv = sheet.parents(".dragWin");
//    var cell = $("#tdContentWin"+frameID);
//   var row = $("#trContentWin"+frameID);
//    sheetW = sheet.width();
//    sheetH = sheet.height();
//    tblW = tbl.width();
//    tblH = tbl.height();
//    
//    if (tblW > sheetW)
//    {
//        sheet.width(tblW+3);
//        sheet.attr("width",tblW+3); 
//    }
//    if (tblH > sheetH)
//    {
//        sheet.height(tblH+3);
//        sheet.attr("height",tblH+3);
//    }
//    
//    minimizeElements(theDiv);
//    SetSize(theDiv,tblW,tblH);
//    FixTable(tbl,sheet);
//    SetFrameSize(cell);
//    SetBannersSize(row,sheet);
}

//====================Win Position settings===================================================

function SetPositionServer(frameID,top,left,_topBorder)
{
    //debugger;
    var theDiv = $("#win"+frameID)[0];
    if (top != null){
        theDiv.style.top = top + 'px';
    }
    if (left != null){
        theDiv.style.left = left + 'px';
    }
    checkPositionServer(theDiv,116);
}

 function checkPosition(win,top)
 {
 //alert(editable);
    var offset = $(win).offset();

    if (offset.top <= top + divAllTop)
    {
        win.style.top = top + 'px';
    }
 }
 function checkPositionServer(win,top)
 {
    var offset = $(win).offset();

    if (offset.top <= top)
    {
        win.style.top = top +'px';
    }
 }
//=========================================Utils=========================================================

 function minimizeElements(_div)
 {
    //minimize iframe
   var frameID = _div[0].id.replace("win","");
   var frame = $("#frame"+frameID);
    if (frame.length)
    {
        frame.attr("width",1);
        frame.attr("height",1);
    }
    
    //minimize Banner divs
     var bnrDiv = $("#SideBannerLeftWin"+frameID);
    bnrDiv.height(1);
    bnrDiv = $("#SideBannerRightWin"+frameID);
    bnrDiv.height(1);
 }
