var menuheight;
var lightboxesInEditMode = 0;
var activeLightboxId = 0;
var lightboxCount = 0;
var dissapearTimer;
var dissapearTimeoutMs = 100;

function Lightbox_PanelExpand(){
//    // make sure we have a good lightbox count
//    if(initialLightboxCount>0){
//        lightboxCount += initialLightboxCount;
//        initialLightboxCount = 0;
//    }        
//    
//    // only open it if we actually have at least one lightbox
//    if(lightboxCount>0){
//        var lbdivs = document.getElementById('lightboxesDiv');
//        
//        //lbdivs.style.height = '';
//        lbdivs.className = 'lightboxesPanel_Open';
//        
//        if(lbPanelWidth_Open)
//                lbdivs.style.width = lbPanelWidth_Open;
//        
//        clearTimeout(dissapearTimer);
//        
//        createCookie('lightboxPanelIsOpen',true,14); 
//    }
}

function Lightbox_PanelCollapse(){
    //dissapearTimer = setTimeout('Lightbox_PanelCollapseDo()',dissapearTimeoutMs);
    //Lightbox_PanelCollapseDo();
}

function Lightbox_PanelCollapseDo(){
//    var lbdivs = document.getElementById('lightboxesDiv');
//    
//    if(lightboxesInEditMode==0){
//        lbdivs.className = 'lightboxesPanel_Closed';
////        if(lbPanelHeight_Closed)
////            lbdivs.style.height = lbPanelHeight_Closed;
////        else
////            lbdivs.style.height = '110px';
//            
//        if(lbPanelWidth_Closed)
//            lbdivs.style.width = lbPanelWidth_Closed;
//            
//        createCookie('lightboxPanelIsOpen',false,14);
//    }
}

function Lightbox_MakeActive(lightboxId){
    // clear the old one
    if(activeLightboxId>0){
        var lb = document.getElementById('lightbox' + activeLightboxId);
        if(lb)
            lb.className = 'lightboxItem_Unselected';
        
        var lbmsg = document.getElementById('defaultmsg' + activeLightboxId);
        if(lbmsg)
            lbmsg.style.display = 'none';
            
        var lblink = document.getElementById('defaultlink' + activeLightboxId);
        if(lblink)
            lblink.style.display = 'inline';
    }        

    // the important part
    activeLightboxId = lightboxId;
    createCookie('activeLightboxId',activeLightboxId,14);    
    
    // do the visual stuffs to the new one
    if(activeLightboxId>0){
        var lb = document.getElementById('lightbox' + activeLightboxId);
        if(lb)
            lb.className = 'lightboxItem_Selected';
            
        var lbmsg = document.getElementById('defaultmsg' + activeLightboxId);
        if(lbmsg){
            lbmsg.style.display = 'block';
            setTimeout('document.getElementById(\'defaultmsg' + activeLightboxId + '\').style.display = \'none\'',3000);
        }        
            
        var lblink = document.getElementById('defaultlink' + activeLightboxId);
        if(lblink)
            lblink.style.display = 'none';
    }
    
}

function Lightbox_LoadActiveFromCookie(){
    activeLightboxId = readCookie('activeLightboxId');
    
    // do the visual stuffs to the new one
    if(activeLightboxId>0){
        var lb = document.getElementById('lightbox' + activeLightboxId);
        if(lb)
            lb.className = 'lightboxItem_Selected';
            
        var lblink = document.getElementById('defaultlink' + activeLightboxId);
        if(lblink)
            lblink.style.display = 'none';
    }
    
    // check to see if we should expand the lightbox panel
    
    var lightboxPanelIsOpen = readCookie('lightboxPanelIsOpen');
    if(lightboxPanelIsOpen=='true'){
        Lightbox_PanelExpand();
        dissapearTimer = setTimeout('Lightbox_PanelCollapseDo()',dissapearTimeoutMs);
    }
}

function Lightbox_ToggleRenameMode(id){
    var ediv = document.getElementById('nameEdit' + id);
    var ldiv = document.getElementById('nameLabel' + id);
    var textbox = document.getElementById('nameEditTextbox' + id);
    
    if(ediv && ldiv && textbox){
        if(ediv.style.display == 'inline'){
            ediv.style.display = 'none';
            lightboxesInEditMode--;
            ldiv.style.display = 'inline';            
            
        }else{
            ediv.style.display = 'inline';
            lightboxesInEditMode++;
            textbox.select();
            textbox.focus();
            ldiv.style.display = 'none';                              
        }    
    }
}

function Lightbox_RenameChanged(textbox, lightboxid, labelname){

    //alert('new name: ' + textbox.value);
    
    var label = document.getElementById(labelname);
    if(label)
        label.innerHTML = textbox.value;
        
    // save to server
    Photocore.Modules.WebApi.Lightbox_Rename(lightboxid,textbox.value);
        
    Lightbox_ToggleRenameMode(lightboxid)
}

function Lightbox_RenameKeydown(textbox, lightboxid, labelname, oEvent){
    //alert(event);
    if (oEvent.keyCode && oEvent.keyCode == 13)
    {
        //alert('you hit enter!');
        Lightbox_RenameChanged(textbox, lightboxid, labelname);
        textbox.style.display = 'none';
        return false;
    } 
    else
        return true;
}

function Lightbox_Delete(lightboxId){    
    if(confirm('Are you sure you want to delete this lightbox?')){
        var lbdiv = document.getElementById('lightbox' + lightboxId);
        
        // hide the lightbox on the client
        if(lbdiv)
            lbdiv.style.display = 'none';
                    
        // delete on the server
        Photocore.Modules.WebApi.Lightbox_Delete(lightboxId,Lightbox_Delete_callback);
        
        // drop it from the web page (if it's on the page)
        var lbthumb = document.getElementById('LightboxThumbnail' + lightboxId);
        if(lbthumb)
            lbthumb.style.display = 'none';
        
        // deactivate it if necessary
        if(activeLightboxId==lightboxId)
            activeLightboxId = 0;
            
    } else {
    
    }
}

function Lightbox_Delete_callback(val){
    lightboxCount--;
}

function Lightbox_Create(){
    if(Photocore){
        Photocore.Modules.WebApi.Lightbox_Create(Lightbox_Create_callback);
    }else{
        alert('Please wait for the page to fully load');
    }
}

function Lightbox_Create_callback(val){
    var lbdivs = document.getElementById('lightboxesDiv');
    if(lbdivs && val){
    
        lightboxCount++;
        Lightbox_PanelExpand();
    
        if(lightboxCount>1){
            // this is the second or greater lightbox, append
            lbdivs.innerHTML = val.html + lbdivs.innerHTML;
        }else{
            // this is the first lightbox, replace the message that says you don't have a lightbox
            lbdivs.innerHTML = val.html;      
        }
        
        // go into rename mode right away
        Lightbox_ToggleRenameMode(val.lightboxId);
        
        // make it active as well
        Lightbox_MakeActive(val.lightboxId);
    }
}



function Lightbox_SetThumbnail(lightboxId, pictureId){
    // tell server to use this picture from this lightbox
    Photocore.Modules.WebApi.Lightbox_SetThumbnail(lightboxId, pictureId);
    alert('Lightbox thumbnail updated.');
}

function Lightbox_RemovePicture(lightboxId, pictureId){
    // tell server to remove the picture from this lightbox
    Photocore.Modules.WebApi.Lightbox_RemovePicture(lightboxId, pictureId);
    
    // drop it on the client side
    var lbthumb = document.getElementById('PictureThumbnail' + pictureId);
    if(lbthumb)
        lbthumb.style.display = 'none';
}

function Lightbox_AddPicture(link, lightboxId, pictureId){
    Lightbox_AddPictureHeight(link, lightboxId, pictureId, 50);
}

function Lightbox_AddPictureHeight(link, lightboxId, pictureId, height){
    //Lightbox_PanelExpand();
    Photocore.Modules.WebApi.Lightbox_AddPicture(lightboxId, pictureId, height, Lightbox_AddPicture_callback);
    //alert(link.id);
    // highlight the link we just pressed
    var parent = link.parentNode;
    if(parent){
        if(parent.className!='menuRow')
            parent = parent.parentNode;
        
        if(parent.className=='menuRow'){
            var oldcolor = parent.style.backgroundColor;
            parent.style.borderBottom = 'solid thin #ff7f00';
            parent.style.borderTop = 'solid thin #ff7f00';
            parent.style.marginTop = '3px';
            parent.innerHTML = 'Added to Lightbox';
            //setTimeout('document.getElementById(\'' + parent.style.backgroundColor = ' + oldcolor + ';',1000);        
        }
    }
}

function Lightbox_AddPicture_callback(val){
    dissapearTimer = setTimeout('Lightbox_PanelCollapseDo()',dissapearTimeoutMs);
    if(val){
        var imgdiv = document.getElementById('lightboxNewThumbs' + val.lightboxId);
        if(imgdiv){
            imgdiv.innerHTML = val.html + imgdiv.innerHTML;        
        }
    }else{
        //alert('This picture already exists in the selected Lightbox.');
    }
}
function Lightbox_AddPictureToDefault(link, pictureId){
    if(activeLightboxId>0){
        Lightbox_AddPicture(link, activeLightboxId, pictureId);
    }
    else
        alert('Select a default Lightbox first by clicking on the >> next to the Lightbox title.');
}
function Lightbox_AddPictureToDefaultHeight(link, pictureId, height){
    if(activeLightboxId>0){
        Lightbox_AddPictureHeight(link, activeLightboxId, pictureId, height);
    }
    else
        alert('Select a default Lightbox first by clicking on the >> next to the Lightbox title.');
}


/* BEGIN Timer Functions */
//var timerID = 0;
//var tStart  = null;

//function UpdateTimer() {
//   if(timerID) {
//      clearTimeout(timerID);
//      clockID  = 0;
//   }

//   if(!tStart)
//      tStart   = new Date();

//   var   tDate = new Date();
//   var   tDiff = tDate.getTime() - tStart.getTime();

//   tDate.setTime(tDiff);

//   if(tDate.getMilliseconds() >= 100){
//        Stop();
//        eval('Lightbox_PanelCollapseDo()');
//   }
//   
//   timerID = setTimeout("UpdateTimer()", 50);
//}

//function Start() {
//   if(!tStart){
//       tStart   = new Date();

//       timerID  = setTimeout("UpdateTimer()", 50);
//   }
//}

//function Stop() {
//   if(timerID) {
//      clearTimeout(timerID);
//      timerID  = 0;
//   }

//   tStart = null;
//}

//function Reset() {
//   tStart = null;

//}

/* END Timer Functions */
