
//Show or Hide the Album Viewer

var album = new Array();
var pageNumber = 0; //global page number
var currentPhoto = 0;
var timerID = 0;
var loadPicture = null;
var lastLoaded = null;

/****Force Scroll Up****/
/*
 Unfortunately changing the overflow of a document causes Firefox to
 refresh the page, which in turn restarts the MP3 player. Also changing it in IE
 causes two scroll bars to show up. So I have to force the view to the top
 of the page at all times when the photoviewer is displayed. Not the best work around
 but whatcha gonna do?
*/

function doScrollUp() {
        nowScroll = getTop();
        if (nowScroll > 0) {
            //self.scrollTo(0, Math.max(nowScroll-500,0));   //Smooth Scroll
            self.scrollTo(0, Math.max(nowScroll*0.00,1)-1);   //Decelerated Scroll
        }
        timerID = setTimeout("doScrollUp()",25);
}

function getTop() {
	return filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


/******Album Viewer*****/

//Show album and start loading
function showAlbumViewer(albumId, loadThis){
    loadAlbum(albumId);
    loadPicture = loadThis; //store it globally
    
    //this will manually assign the background picture (must pass full name of photo ie: 5.jpg)
       
    //Let's wait a bit for the album to load.
    setTimeout('document.getElementById("bodyCover").style.visibility = "visible"', 500);
    setTimeout('doScrollUp();', 500);
    return;
}

function restoreAlbumViewer(){
    //Bring it back up without changing what was previously loaded
    //Users can come back after a comment, without interuption
    document.getElementById("bodyCover").style.visibility = "visible";
    doScrollUp();
}

//hide album
function hideAlbumViewer(){
    clearTimeout(timerID);
    document.getElementById("bodyCover").style.visibility = "hidden";
    return;
}

function comment(){
    var file = lastLoaded.split('.');
    //alert('type=photo&id=' + file[0]);
    post = 'type=photo&id=' + file[0]
    ajax('comment.php?ajax=1', post, 'innerHTML', 'content', null);
}

function loadAlbum(albumId){
    
    //clear the album (if there is one) since we're loading a new one
    var length = album.length;
    if(length > 0){
        for(var j=0; j < length; j++){
            album.pop();
        }
    }
    
    //Make ajax calls to get the album info
    var post = "function=getAlbum&album=" + albumId;
    ajax("photoviewer/request.php", post, "call", "sortAlbum", null);
    //This will call 'sortAlbum' function below
}

function sortAlbum(response){
    //sortAlbum will parse string into a 2d array and store into the album Var
    var photos = response.split("~|||~");
    for(x = 0; x < photos.length; x++ ){
        photo = photos[x].split("~|~");
        album.push(photo);
    }
    
    loadPage(0); //load first page
    loadImage(0); //load first image on page
    return;
}

function loadPage(num){
    pageNumber = num; //keep it global

    //clear the cells background images
    for(x=0; x < 8; x++){
        if(document.getElementById('c'+x)){
            document.getElementById('c'+x).style.backgroundImage = "none";    
        }
    }
    
    //clear cell borders too
    selectCell('none');
    
    var start = num * 8;
    var end = start + 7;
    
    var c = 0; //ranges from 0-7 for the display cells
    for(x = start; x <= end; x++){
        if(x < album.length){
            photo = album[x];
            
            if(document.getElementById('c' + c)){
                //change background of photo
                document.getElementById('c'+c).style.background = "url(photos/thumb/" + photo[0] + ")";
            }
        }
        c++;
    }
    
    //The following allows user to go to a next page, or previous page
    
    //Is there a previous page?
    if(start == 0){ //We're at the beginning, there is no previous
        prevPage("off");
    }else{
        //Whatever page we just loaded, minus 1 to go back a page
        prevPage(num-1);
    }
        
    if(end >= album.length -1){
        //If we've hit the end of the album, there is no next page
        nextPage("off");
    }else{
        //if the album still keeps going, allow user to see another page
        nextPage(num+1);
    }
    return;
}

function loadImage(x){
    //alert('before:' + x);
    
    var id = (pageNumber * 8) + x; //the true image id
    //for blank photos we ignore the call
    if(id >= album.length){ return false; }
    
    //it's valid, so let's select
    selectCell(x); //put a border around the display cell
    currentPhoto = x; //keep it global
    
    //alert('after:' + id);
    if(document.getElementById('bodyCover_photo')){
        if(loadPicture != null){
            //We're supposed to load this one instead (override mode)
            selectCell('none'); //Turn off selector
            document.getElementById('bodyCover_photo').style.backgroundImage = "url(photos/medium/" + loadPicture + ")";
            lastLoaded = loadPicture;
            loadPicture = null; 
        }else{
            //normal use
            document.getElementById('bodyCover_photo').style.backgroundImage = "url(photos/medium/" + album[id][0] + ")";
            lastLoaded = album[id][0];
        }
        
        return true;
    }else{
        return false;   
    }
}

function selectCell(cell){
    
    if(cell == "none"){ cell = 9}; //purposely out of range, don't select anything but clears all cells anyways
    
    //clear any previously selected cell
    for(c=0; c < 8; c++){
        if(document.getElementById('c'+c)){
            if(c == cell){ //if it's the selected cell
               document.getElementById('c'+c).style.border = 'solid 1px #999';
            }else{
                //not a selected cell, let's get the id and see if there's a picture there
                id = (pageNumber * 8) + c; //the true image id
                
                if(id < album.length){
                    //there's an image
                    document.getElementById('c'+c).style.border = 'solid 1px #444';
                }else{
                    //it's a blank, hide the border
                    document.getElementById('c'+c).style.border = 'solid 0px #444';
                }
            }
        }
    }
    return true;
}

function nextPage(status){
    //status is either off, or the next page number
    if(document.getElementById('nextPage')){
        next = document.getElementById('nextPage');
        if(status == "off"){
            next.innerHTML = '';
            next.onclick = function() { return;}
        }else{
            next.innerHTML = '[Next]';
            next.onclick = function(){
                loadPage(status);
                return;
            }
        }   
    }
    return;
}

function prevPage(status){
    //status is either off, or the previous page number
    if (document.getElementById('prevPage')){
        prev = document.getElementById('prevPage');
        if(status == "off"){
            prev.innerHTML = '';
            prev.onclick = function() { return; }
        }else{
            prev.innerHTML = '[Previous]';
            prev.onclick = function(){
                loadPage(status);
            }
        }
    }
    return;
}

function nextPhoto(){
    //new picture Id if we go ahead
    var pictureID = currentPhoto + (pageNumber * 8) + 1;
    
    //back to the start if we've reached the end
    if(pictureID == album.length){
        currentPhoto = 0;
        pageNumber = 0;
        loadPage(0);
        loadImage(currentPhoto);
        return;
    }
    
    //if we got to this point we can actually increase it
    currentPhoto++; //increase next
    
    //if we've reached the last photo of this page, then go to next page
    if(currentPhoto == 8){
        currentPhoto = 0;
        //load the next page
        loadPage(pageNumber + 1);
    }
    
    loadImage(currentPhoto);
    return;
}

function prevPhoto(){
    //new picture Id if we go ahead
    var pictureID = currentPhoto + (pageNumber * 8) - 1;
        
    //back to the end if we've reached the start
    if(pictureID < 0){
        pageNumber = Math.floor( (album.length) / 8);
        currentPhoto = (album.length -1) - (pageNumber * 8);
        loadPage(pageNumber);
        loadImage(currentPhoto);
        return;
    }
    
    //if we got to this point we can actually decrease it
    currentPhoto--; //increase next
    
    //if we've reached the first photo of this page, then go to previous page
    //and load the last cell
    if(currentPhoto < 0){
        currentPhoto = 7;
        //load the prev page
        loadPage(pageNumber - 1);
    }
    
    loadImage(currentPhoto);
    return;
}