/* Returns a cross browser compatible height or width value for the window */
function getDimension(dim){
    if (dim === "height"){
       if (window.innerHeight){
          return window.innerHeight;
       }
       else if (document.documentElement.clientHeight){
          return document.documentElement.clientHeight;
       }
       else if (document.body.clientHeight){
          return document.body.clientHeight;
       }
    }
    else if (dim === "width"){
       if (window.innerWidth){
          return window.innerWidth;
       }
       else if (document.documentElement.clientWidth){
          return document.documentElement.clientWidth;
       }
       else if (document.body.clientWidth){
          return document.body.clientWidth;
       }
    }
    return undefined;
}

function SlideSorter(pId, styleStr, cId){
    
    // private instance variables
    var objRef = this;
    var parentId = pId;
    var contentMarginStyle = styleStr;
    var contentId = cId;
    var fullScreenMargins = "0.5em 0 0.5em 0.5em";
    var slides = undefined;
    var modSlides = [];
    var bodyContent = $("#bodyContent *");
    var pickedupId = "";
    var mouseOffsetX = 0;
    var mouseOffsetY = 0;
    var panelKeys = {"slidePanel" : "divEditPanel"};
    var orientation = "vertical";
    var locked = false;
    var panelId = "divEditPanel";
    var slideIndex = 0;
    var panelDivisions = [];
    var moveposition = 0;
    var slidePickedUp = 0;
    var prevOrientation = orientation;
    
    this.setParentId = function(pid){
       parentId = pid;
    };
    this.setSlides = function(s){
       slides = s;
       modSlides = s;
    };
    
    this.getParentId = function(){
       return parentId;
    };
    this.getSlides = function(){
       return slides;
    };
    
    // Window resize handler
    var windResize = function(ev){
        $("#" + contentId).css("height", getDimension("height") - 100);
        //$("#divEditPanel").css("top", window.innerHeight - 175).css("width", window.innerWidth - 350);
    };
    
    // 
    var enlargeSlidePic = function(e){
        $("#slidePanelTable td .rew").css("border-width", "2px");
        var ta = "rq" + e.target.id.substring(6).toString();
        document.getElementById(ta).style.borderWidth = "5px";
    };
    
    // 
    var gotoSlide = function(event){

        if (orientation === "grid"){
            orientation = "vertical";
            createSorter(null);
        }

        $("#slidePanelTable td .rew").css("border-color", "#000000").css("background-color", "#FFFFFF");
        slideIndex = event.target.id.substring(6) - 1;
        $("#rq" + (slideIndex + 1).toString()).css("border-color", "blue").css("background-color", "#FFFF70");
        //var ta = "rq" + (slideIndex + 1).toString();
        //document.getElementById(ta).style.borderColor = "blue";
        //document.getElementById(ta).style.backgroundColor = "#FFFF70";
        $("#bodyContent *").hide();
        slides[slideIndex][4].show();
    };

    var remove = function(){
        $("#slidePanel, #" + panelId).remove();
        $("#" + contentId).css("margin", contentMarginStyle).css("position", "relative").css("top", "auto").css("right", "auto").css("left", "auto").css("overflow-y", "auto").css("height", "auto");
        $("#bodyContent *, #col1hidelink, div#column-one, div#p-personal, div#p-cactions, div#footer").show();
    };

    // These methods allow the slide sorter to be picked up and moved around
    var move = function(e){
        //alert(e.screenY - mouseOffsetY);
        $("#" + pickedupId).css("left", (e.screenX - mouseOffsetX) + "px");
        $("#" + pickedupId).css("top", (e.screenY - mouseOffsetY) + "px");
    };

    var moveP = function(e){
        var val;

        if (orientation === "vertical"){
           $("#" + pickedupId).css("left", (150) + "px");
           $("#" + pickedupId).css("top", (e.screenY - 200) + "px");
        }
        else{
           $("#" + pickedupId).css("left", (e.screenX - parseInt($("#divEditPanel").css("left"), 10)) + "px");
           $("#" + pickedupId).css("top", (0) + "px");
        }
        $(".pods").hide();

        if (orientation === "vertical"){
           val = e.screenY + $("#sidp").get(0).scrollTop;
        }
        else{
           val = e.screenX + $("#sidp").get(0).scrollLeft;
        }
        for (var i = 0; i < panelDivisions.length; i++){
           if (val > panelDivisions[i].top && val < panelDivisions[i].bottom){
               $("#bb" + i).show();
               moveposition = i - 1;
               break;
           }
        }
    };
    var pickup = function(e){
        pickedupId = panelKeys[e.target.id];
        $("#" + e.target.id).unbind("mousedown", pickup);
        slidePickedUp = parseInt(e.target.id.substring(4), 10) - 1;
        if (e.target.id.substring(0, 4) === "hand"){
           $(document).bind("mousemove", moveP);
           $(document).bind("keydown", maindrop);
           $("#" + pickedupId).css("position", "absolute").css("width", "175px");
        }
        else{
           //$("#" + e.target.id).bind("mouseup", drop);
           $(document).bind("mouseup", drop);
           $(document).bind("mousemove", move);
           $("#" + pickedupId).unbind("mousedown", pickup);
        }
        if (!$("#" + "divEditPanel").css("left")){
            $("#" + pickedupId).css("left", 105);
            $("#" + pickedupId).css("top", 105);
        }
        mouseOffsetX = e.screenX - parseInt($("#" + pickedupId).css("left"));
        mouseOffsetY = e.screenY - parseInt($("#" + pickedupId).css("top"));
        if (!mouseOffsetX || !mouseOffsetY){
            mouseOffsetX = 185;
            mouseOffsetY = 105;
        }
    };
    var maindrop = function(e){
        $(document).unbind("keydown", maindrop);
        $(document).unbind("mousemove", moveP);
        $(".pods").hide();
        
        //$("#" + pickedupId).unbind("mouseup", dropIN);
        $("#" + pickedupId).bind("mousedown", pickup);
        moveSlide(moveposition, pickedupId);
        
        $("#slidePanelTable td .rew").css("border-color", "#000000").css("background-color", "#FFFFFF");
        $("#rq" + (slideIndex + 1).toString()).css("border-color", "blue").css("background-color", "#FFFF70");
        //var ta = "rq" + (slideIndex + 1).toString();
        //document.getElementById(ta).style.borderColor = "blue";
        //document.getElementById(ta).style.backgroundColor = "#FFFF70";
        //$("#" + pickedupId).css("position", "relative").css("left", 0).css("top", 0).css("width", "auto");
    };
    var drop = function(e){
        //pickedupId = panelKeys[e.target.id];
        pickedupId = "divEditPanel";
        $(document).unbind("mousemove", move);
        //$("#" + pickedupId).unbind("mouseup", drop);
        $(document).unbind("mouseup", drop);
        $("#" + pickedupId).bind("mousedown", pickup);
    };

    var moveSlide = function(position, id){
        var moveIndex = parseInt(id.substring(2), 10) - 1, temp, ta;
        
        //if (Math.abs((moveIndex + 1) - (position + 1)) < 2) position = moveIndex;
        //alert((moveIndex + 1) + " " + (position + 1));
        
        if (moveIndex > position) position++;
        
        if (position < 0) position = 0;
        if (position >= modSlides.length) position = (modSlides.length - 1);
        if (slideIndex === moveIndex){
           slideIndex = position;
        }

        $("#" + pickedupId).css("position", "relative").css("left", 0).css("top", 0).css("width", "auto");
        $(".hyts").attr("innerHTML", "");
        temp = modSlides[moveIndex];
        modSlides.splice(moveIndex, 1);
        modSlides.splice(position, 0, temp);
        attachClones(modSlides);
    };

    var attachClones = function(sli){
        var a, b, clon, par, arr = [], oarr = [], rset = [], i;

        // Add the mini slide previews to the page
        for (i = 0; i < sli.length; i++){
            clon = sli[i][4].clone(false);
            clon.appendTo("#slideP" + (i + 1).toString());
            clon.show().css("cursor", "pointer").bind("mouseover", enlargeSlidePic).bind("click", gotoSlide).attr("id", "slideP" + (i + 1).toString());
            clon.find("*").show().attr("id", "slideP" + (i + 1).toString()).css("cursor", "pointer").bind("mouseover", enlargeSlidePic).bind("click", gotoSlide);
            clon.find("img").attr("width", "35").attr("height", "30");
        }
    };

    var createGridView = function(){
        
        var slidearea = "", i = 0, y = 1;
        
        slidearea += '<tr>';
        
        for (i = 1; i < slides.length + 1; i++){
           if ((y % 7) === 0) slidearea += '</tr><tr>';
           
           if (i != 1) y++;
           
           slidearea +=   '<td>' +
                            '<div id="rq' + i + '" class="rew" style="border-style:solid;border-width:3px;border-color:#000000;">' +
                              '<div class="handles" id="hand' + i + '" style="cursor:pointer;width:100%;height:15px;background-color:#484848;">' +
                              '</div>' +
                              '<div class="hyts" id = "slideP' + i + '" style = "overflow: hidden;font-size: 0.4em;text-align: center;width: 100%; height: 100px;margin-bottom:2em;">' +
                              '</div>' +
                            '</div>' +
                          '</td>';
        }
        slidearea += '</tr>';
        
        return slidearea;
    };
    
    var createMiniSlides = function(){

        var slidearea = "", topC = 0, bottomC = 100, i = 0;
        panelDivisions = [];

        if (orientation === "vertical"){
            slidearea += '<tr>' +
                           '<td class="pods" id="bb' + i + '" style="display:none;height:1px;width:100%;background-color:#000000;">' +
                           '</td>' +
                         '</tr>';
            panelDivisions.push({top:topC, bottom:bottomC});
            topC += 135;
            bottomC += 135;
        }
        else{
            slidearea += '<td>' +
                           '<td class="pods" id="bb' + i + '" style="display:none;height:100%;width:1px;background-color:#000000;">' +
                           '</td>' +
                         '</td>';
            panelDivisions.push({top:topC, bottom:bottomC});
            topC += 180;
            bottomC += 180;
        }

        for (i = 1; i < slides.length + 1; i++){
            if (orientation === "vertical"){
                   slidearea += '<tr id="sp' + i + '">' +
                                  '<td>' +
                                    '<div id="rq' + i + '" class="rew" style="border-style:solid;border-width:3px;border-color:#000000;">' +
                                      '<div class="handles" id="hand' + i + '" style="cursor:pointer;width:100%;height:15px;background-color:#484848;">' +
                                      '</div>' +
                                      '<div class="hyts" id = "slideP' + i + '" style = "overflow: hidden;font-size: 0.4em;text-align: center;width: 100%; height: 100px;margin-bottom:2em;">' +
                                      '</div>' +
                                    '</div>' +
                                  '</td>' +
                                '</tr>' +
                                '<tr>' +
                                  '<td class="pods" id="bb' + i + '" style="display:none;height:1px;width:100%;background-color:#000000;">' +
                                  '</td>' +
                                '</tr>';
                   panelDivisions.push({top:topC, bottom:bottomC});
                   topC += 135;
                   bottomC += 135;
             }
             else{
                   slidearea += '<td><div id="rq' + i + '" class="rew" style="border-style:solid;border-width:3px;border-color:#000000;"><div class="handles" id="hand' + i + '" style="cursor:pointer;width:100%;height:15px;background-color:#484848;"></div><div class="hyts" id = "slideP' + i + '"' + 
                   'style = "overflow: hidden;font-size: 0.4em;text-align: center;width: 175px; height: 100px;margin-bottom:2em;">' +
                   '</div></div></td>' +
                   '<td>' +
                   '<td class="pods" id="bb' + i + '" style="display:none;height:100%;width:1px;background-color:#000000;">' +
                   '</td>' +
                   '</td>';
                   panelDivisions.push({top:topC, bottom:bottomC});
                   topC += 180;
                   bottomC += 180;
             }
             panelKeys["hand" + i] = ("rq" + i);
        }
        
        if (orientation === "horizontal") slidearea += "</tr>";
        
        return slidearea;
    };
    
    var createSorter = function(e){
        
        var id = "slidePanel", slidePane = "", i = 1;
        
        if ($("#globalWrapper *").is("#" + panelId)){
            $("#" + panelId).remove();

            if (!e){
                orientation = orientation;
            }
            else if (e.target.id === "con86"){
               if (orientation === "vertical"){
                  orientation = "horizontal";
               }
               else if (orientation === "horizontal"){
                  orientation = "vertical";
               }
               else if (orientation === "grid"){
                  orientation = prevOrientation;
               }
            }
            else if (e.target.id === "con85"){
               if (orientation === "grid"){
                  orientation = prevOrientation;
               }
               else{
                  prevOrientation = orientation;
                  orientation = "grid";
               }
            }
        }
        
        if (orientation === "vertical"){
           slidePane = '<div id = "' + panelId + '" style="z-index:200;position: fixed;left: 2.0em;top:5px;height: 185px;width:225px;">' +
                       '<div id="' + id + '" style="border: solid 0.2em #787878;cursor:pointer;background-color:#000045;height:20px;width:98%;"><div id="con86" style="margin:0.15em;float:right;background-color:red;width:20px;height:85%;"></div><div id="con85" style="margin:0.15em;float:right;background-color:green;width:20px;height:85%;"></div></div>' +
                       '<div id="sidp" style = "border: solid 0.3em #999999;padding: 0.3em;background-color: #BBBBBB;overflow-y: scroll;overflow-x:hidden;height:' + (getDimension("height") - 65) + 'px;">' +
                       '<table id = "slidePanelTable" style = "width: 98%;" align = "center" cellpadding = "3px" cellspacing = "3px">';
           
           $("#" + contentId).show().css("margin", contentMarginStyle).css("height", getDimension("height") - 100).css("overflow-y", "auto").css("position", "fixed").css("top", "0.25em").css("left", "5em").css("right", "3em");
        }
        else if (orientation === "horizontal"){
           slidePane = '<div id = "' + panelId + '" style="z-index:200;position: fixed;bottom:3em;left:3em;height: 185px;width:' + (getDimension("width") - 75) + 'px;">' +
                       '<div id="' + id + '" style="border: solid 0.2em #787878;cursor:pointer;background-color:#000045;height:20px;width:99.65%;"><div id="con86" style="margin:0.15em;float:right;background-color:red;width:20px;height:85%;"></div><div id="con85" style="margin:0.15em;float:right;background-color:green;width:20px;height:85%;"></div></div>' +
                       '<div id="sidp" style = "border: solid 0.3em #999999;padding: 0.3em;background-color: #BBBBBB;overflow-x: scroll;overflow-y:hidden;">' +
                       '<table id = "slidePanelTable" style = "width: 98%;" align = "center" cellpadding = "3px" cellspacing = "3px"><tr>';
           
           $("#" + contentId).show().css("margin", fullScreenMargins).css("height", getDimension("height") - 300).css("overflow-y", "auto").css("position", "fixed").css("top", "0.25em").css("left", "1em").css("right", "1em");
        }
        else if (orientation === "grid"){
        	slidePane = '<div id = "' + panelId + '" style="z-index:200;position: fixed;left: 2.0em;top:5px;height: 185px;width:' + (getDimension("width") - 75) + 'px">' +
            '<div id="' + id + '" style="border: solid 0.2em #787878;cursor:pointer;background-color:#000045;height:20px;width:98%;"><div id="con86" style="margin:0.15em;float:right;background-color:red;width:20px;height:85%;"></div><div id="con85" style="margin:0.15em;float:right;background-color:green;width:20px;height:85%;"></div></div>' +
            '<div id="sidp" style = "border: solid 0.3em #999999;padding: 0.3em;background-color: #BBBBBB;overflow-y: scroll;overflow-x:scroll;height:' + (getDimension("height") - 65) + 'px;">' +
            '<table id = "slidePanelTable" style = "width: 98%;" align = "center" cellpadding = "3px" cellspacing = "3px">';

            $("#" + contentId).hide();
        }
        
        if (orientation === "vertical" || orientation === "horizontal"){
            slidePane += createMiniSlides();
        }
        else{
            slidePane += createGridView();
        }
        
        slidePane += '</table></div></div>';
        
        // Attach the panel that will hold the slides list
        $("#" + parentId).prepend(slidePane);
        
        attachClones(slides);
        
        $(".handles").bind("mousedown", pickup);
        $("#" + id).bind("mousedown", pickup);
        $("#slidePanelTable td .hyts").bind("mouseover", enlargeSlidePic);
        $("#slidePanelTable td .hyts").bind("click", gotoSlide);
        $("#con86").bind("click", createSorter);
        $("#con85").bind("click", createSorter);
        
        $("#rq" + (slideIndex + 1).toString()).css("border-color", "blue").css("background-color", "#FFFF70");
        //var ta = "rq" + (slideIndex + 1).toString();
        //document.getElementById(ta).style.borderColor = "blue";
        //document.getElementById(ta).style.backgroundColor = "#FFFF70";
    };
    
    this.setup = function(){
        var xcount = 0, i = 0, y = 0, val, exp;
        
        if (!$("#globalWrapper *").is("#" + panelId)){
           $("#col1hidelink, div#column-one, div#p-personal, div#p-cactions, div#footer").hide();

           if (slides.length > 0){
              createSorter(new Object());
              $("#bodyContent *").hide();
              slides[slideIndex][4].show();
              $(window).bind("resize", windResize);
           }
        }
        else{
           remove();
        }
    };
}

var presentationEditor = {};
presentationEditor.move = function(e){
    $("#" + presentationEditor.pickedupId).css("left", (e.screenX - presentationEditor.mouseOffsetX) + "px");
    $("#" + presentationEditor.pickedupId).css("top", (e.screenY - presentationEditor.mouseOffsetY) + "px");
};

presentationEditor.bindToDrop = function(){
    $("#" + presentationEditor.pickedupId).bind("click", presentationEditor.drop);
};

presentationEditor.bindToPickup = function(){
    $("#" + presentationEditor.pickedupId).bind("click", presentationEditor.pickup);
};

presentationEditor.pickup = function(e){
    presentationEditor.pickedupId = presentationEditor.panelKeys[e.target.id];
    $("#" + presentationEditor.pickedupId).unbind("click", presentationEditor.pickup);
    $(document).bind("mousemove", presentationEditor.move);
    presentationEditor.mouseOffsetX = e.screenX - parseInt($("#" + presentationEditor.pickedupId).css("left"));
    presentationEditor.mouseOffsetY = e.screenY - parseInt($("#" + presentationEditor.pickedupId).css("top"));
    setTimeout("presentationEditor.bindToDrop()", 100);
};

presentationEditor.drop = function(){
    $(document).unbind("mousemove", presentationEditor.move);
    $("#" + presentationEditor.pickedupId).unbind("click", presentationEditor.drop);
    //window.alert(presentationEditor.pickedupId);
    setTimeout("presentationEditor.bindToPickup()", 200);
};

presentationEditor.sidePanel = function(){
    
    var id = "slidePanel",
    slidePane = '<div style = "padding: 0.5em;background-color: #BBBBBB;overflow-y: scroll;overflow-x:hidden;position: fixed;left: 2.0em;top:5;width: 225px;height:' + (window.innerHeight - 50) + 'px;" id = "' + id + '"><table id = "slidePanelTable" style = "width: 85%" align = "center" cellpadding = "3px" cellspacing = "3px">',
    i = 1,
    rootElement = {};
    
    if (presentation.slides.length > 0){
        for (i = 1; i < presentation.slides.length + 1; i++){
        	//slidePane += '<div id="sp' + i + '" style="width:75%;height:100px;border-style:solid;border-width:2px;border-color:#000000;">' +
        	//i + '</div>';
            slidePane += '<tr id="sp' + i + '"><td id = "slideP' + i + '"' + 
            'style = "overflow-x: hidden;font-size: 0.8em;text-align: center;width: 75px; height: 100px;border-style:solid;border-width:2px;border-color:#000000;">' + presentation.slides[i-1][4].html();
            '</td></tr>';
            //alert(bodyContent.slice(presentation.slides[i-1][0], presentation.slides[i-1][1]).clone().html());
            //$("#slideP" + i.toString()).prepend(presentation.slides[i-1][4]);
            //$(presentation.slides[i-1][4].clone()).prependTo($("#slideP" + i.toString()));
        }
        
        slidePane += '</table></div>';
    }
    $("#" + presentation.parentId).prepend(slidePane);
    rootElement = document.getElementById(id);
    $(rootElement).bind("click", presentationEditor.pickup);
    $("#slidePanelTable td p").bind("mouseover", presentationEditor.enlargeSlidePic);
    $("#slidePanelTable td p").bind("click", presentationEditor.gotoSlide);
};

presentationEditor.enlargeSlidePic = function(e){
    $("#slidePanelTable td").css("border-width", "2px");
    for (i = 0; i < $("#slidePanelTable td").length; i++){
        if ($("#slidePanelTable td p").get(i) === e.target){
            $("#slidePanelTable td").get(i).style.borderWidth = "5px";
        }
    }
};

presentationEditor.panelKeys = {"grabEditPanel" : "divEditPanel"
		                        };
presentationEditor.mouseOffsetX = 0;
presentationEditor.mouseOffsetY = 0;

presentationEditor.slideTimeLine = function(){

    var id = "divEditPanel",
    bottomPanel = '<div id = "' + id + '" style = "width: ' + (window.innerWidth - 475) + 'px;opacity:0.95;border-style:solid;border-width: 0.3em;border-color:#DDDDDD;background-color:#999999;position: fixed; left:300px;top: ' + (window.innerHeight - 175) +'px;">' +
    '<div id = "grabEditPanel" style = "cursor: pointer;background-color: #BBBBBB;width: 100%;height: 20px;"></div>' +
    '<div style = "overflow-x: scroll;">' +
    '<form style = "padding: 0.5em;width: 75%" id = "editFormPresent">' +
    '<table>' +
       '<tr id = "powert">',
    i = 1,
    rootElement = {};
    
    if (presentation.slides.length > 0){
        for (i = 1; i < presentation.slides.length + 1; i++){
            bottomPanel += "<td><table class = 'botslides'>" +
            '<tr><td style = "text-align: center; font-size: 1.0em;font-weight: bold;">Slide ' + i +'</td></tr>' + 
            '<tr id = "bar1"><td><div id = "push' + i + '" class = "pushout" style = "text-align: center;cursor: e-resize;border-style: solid;border-width: 0.2em;border-color: #000000;height: 20px;width:' + (presentation.slides[i - 1][5]*presentationEditor.factor) + 'px;background-color: #CCCCCC">' + (presentation.slides[i - 1][5]) + '</div></td></tr>' + 
            '<tr id = "bar3"><td align = "right"><div style = "border-style: solid;border-width: 0.1em;border-color:#BBBBBB;background-color: #DDDDDD;text-align: center;position: relative;left: 1.9em;width: 3em;" id = "outtot' + i + '" class = "outtotal" type = "text">' + (presentation.slides[i - 1][5]) + '</div></td></tr>' +
            '</td></table>';
        }
        
        bottomPanel += '</tr>' +
        '</table>' +
        '</form>' +
        '</div>' +
        '</div>';
        
        $("#" + presentation.parentId).prepend(bottomPanel);
        rootElement = document.getElementById(id);
        $(rootElement).bind("click", presentationEditor.pickup);
        $(".pushout").bind("click", presentationEditor.alterTime);
        presentationEditor.calculateTimeTotal();
    }
};

presentationEditor.calculateTimeTotal = function(){
    var t = $(".outtotal"), z = 0, curVal = 0, formattedVal = "";
    for (z = 0; z < t.length; z++){
       curVal += presentation.slides[z][5];
       formattedVal = new Number(curVal).toPrecision(3);
       $(".outtotal").get(z).textContent = formattedVal;
    }
};

presentationEditor.factor = 50;
presentationEditor.slides = {
    leftPanel: [],
    timeLine: []
};

presentationEditor.pickedupId = {};

presentationEditor.bindings = {
		bar: function(eve){
    var widths = parseInt($(presentation.barThis).css("width").slice(0, -2), 10),
    widthnew = 0;
    if (typeof widths === "number" && widths > 0 && (presentation.origwidth + (eve.screenX - presentation.mousePos)) > 0){
          //$(presentation.thisNum).get(0).value = (presentation.origwidth + (eve.screenX - presentation.mousePos))/presentationEditor.factor;
          presentation.slides[presentation.slideActing][5] = (presentation.origwidth + (eve.screenX - presentation.mousePos))/presentationEditor.factor;
          widthnew = (presentation.origwidth + (eve.screenX - presentation.mousePos)) + "px";
          $(presentation.barThis).css("width", widthnew).text(presentation.slides[presentation.slideActing][5]);
          presentationEditor.calculateTimeTotal();
       }
    },
        undo: function(event){
         $(document).unbind("mousemove", presentationEditor.bindings.bar);
       },
       windResize: function(ev){
           $("#content").css("height", window.innerHeight - 300);
           $("#divEditPanel").css("top", window.innerHeight - 175).css("width", window.innerWidth - 350);
       }
};

presentationEditor.setup = function(){
    var xcount = 0, i = 0, y = 0, val, exp;
    
    if (!$("#globalWrapper *").is("#slidePanel")){
    $("div#column-one, div#p-personal, div#p-cactions, div#footer").hide();
    $("#content").css("margin", presentation.style.contentMargin.normalTocShown);
    
    if (presentation.slides.length > 0){

        presentationEditor.sidePanel();
        presentationEditor.slideTimeLine();
    	
        bodyContent.hide();
        $("#" + presentation.contentId).css("height", window.innerHeight - 300).css("overflow-y", "auto").css("position", "fixed").css("top", "0.25em").css("left", "5em").css("right", "3em");
        bodyContent.slice(presentation.slides[0][0], presentation.slides[0][1]).show();
        $(window).bind("resize", presentationEditor.bindings.windResize);
    }
  }
    else{
       presentationEditor.remove();
    }
};

presentationEditor.gotoSlide = function(event){
	var ert = {};
	
    $("#slidePanelTable td").css("border-color", "#000000");
    $(".botslides").css("background-color", "");
    for (y = 0; y < $("#slidePanelTable td p").length; y++){
        if ($("#slidePanelTable td p").get(y) === event.target){
            $("#slidePanelTable td").get(y).style.borderColor = "blue";
            /*
            ert = $("#slidePanelTable td").get(y).cloneNode($("#slidePanelTable td").get(y));
            $(ert).css("position", "fixed").css("left", "20").css("top", "100");
            $(ert).appendTo("#bodyContent");
            */
            $(".botslides").get(y).style.backgroundColor = "#FFFF00";
            bodyContent.hide();
            currentSlide = y;
            presentation.slides[y][4].show();
        }
    }
};

presentationEditor.remove = function(){
    var i = 0;

    $("#slidePanel, #divEditPanel").remove();

    $("#content").css("margin", presentation.style.contentMargin.normalTocShown);
    $("div#column-one, div#p-personal, div#p-cactions, div#footer").hide();
};

presentationEditor.alterTime = function(event){
    $(".pushout").each(function(x){
        if (this === event.target){
          presentation.mousePos = event.screenX;
          presentation.barThis = this;
          presentation.thisNum = $('.pushout').get(x);
          presentation.slideActing = x;
          presentation.origwidth = parseInt($(this).css("width").slice(0, -2), 10);
          $(document).bind("mousemove", presentationEditor.bindings.bar);
          $(document).bind("mouseup", presentationEditor.bindings.undo);
        }
     });
};

