/*
  Copyright (c) 2009, Pilot Interactive Inc. (www.pilotinteractive.ca)
  All rights reserved.

  DEVELOPED BY: David Di Biase (david@pilotinteractive.ca)
  DESCRIPTION:  Handles the gallery management and visualisation aspects.
  DATE:         May 7, 2009
  UPDATE HIST.: David D. (david@pilotinteractive.ca). v1. May 7, 2009
                - Initial beta approval delivery release
                - Patched for having scroll functionality between pages
                - Removed the width spacing functionality for two spaces
*/

/**
 * Opens the slide details and assigns the arrow and close buttons to
 * their proper positions on the stage.
 */

var page = 0;
var numthumbs = 4;
var totalitems = 0;
var disable_scroll = true;
var columns = 5;
var thumbs_width = 136;
var thumbs_height = 195;
var popup_view = false;

$(document).ready(function() {
	if (disable_scroll) {
    	$("#previous").css("display", "none");
    	$("#next").css("display", "none");
    	$("#thumbs").css("height", "auto");
    	$("#thumbs").css("width", "auto");
	}
});

function openDetails(target) {
    if (popup_view) {
        openPopUp(target);
    } else {
        openGalleryView(target);
    }
}

function openGalleryView(target) {
	$("#gallContent").fadeOut("slow");
	$("#galleryView").fadeIn("slow");
	var item = galleryData[target.id];
	$("#galleryView").html("<a href='#' onClick='javascript: closeDetails();'>< Back to gallery</a><br><br><h2>" + item.title + "</h2>" + item.desc + "<br><br>");
	populateDetails(target.id, item, false);
}

function openPopUp(target) {
	closeDetails();
	var item = galleryData[target.id];
    $("body").append("<img id='ibox_arrow' src='" + dir_path + "images/arrow.gif'><img id='ibox_close' src='" + dir_path + "images/closebutton.gif' onClick='closeDetails()'><div id='ibox'><div id='ibox_thmbs'></div><div style='clear: both'></div><div id='ibox_details'></div>");
    
    var pos = $("#" + target.id).offset();
    var thumbpos = $("#gallery_container").offset();
    
    if (disable_scroll) {
        $("#ibox").css("top", pos.top - 6);
   	 	$("#ibox_arrow").css("top", pos.top + 5);
    	$("#ibox_close").css("top", pos.top + 228);
    } else {
	    $("#ibox").css("top", thumbpos.top - 6);
   	 	$("#ibox_arrow").css("top", thumbpos.top + 5);
    	$("#ibox_close").css("top", thumbpos.top + 228);
    }
    $("#ibox").css("left", pos.left + 130);
    $("#ibox_arrow").css("left", pos.left + 120);
    $("#ibox_close").css("left", pos.left + 130);
    populateDetails(target.id, item, true);
    $("#ibox").fadeIn("slow");
    $("#ibox_arrow").fadeIn("slow");
    $("#ibox_close").fadeIn("slow");
}

/**
 * Closes the opened gallery item window and its arrow/close buttons.
 */
function closeDetails() {
	$("#ibox").remove();
	$("#ibox_arrow").remove();
	$("#ibox_close").remove();
	$("#galleryView").fadeOut("slow");
	$("#gallContent").fadeIn("slow");
}

/**
 * Populates the ibox details with information about the gallery item,
 * which is taken from the gallery detail JSON object.
 */
function populateDetails(name, item, popup) {
    if (popup) {
        if (item.images == 1) {
            $("#ibox_thmbs").remove();
            $("#ibox_details").css("height", "222px");
            $("#ibox_details").css("width", "162px");
        } else {
            for (var i = 1; i < item.images; i++) {
                $("#ibox_thmbs").append(imageItem(dir_path + "thumbs/" + name + "-" + i + "_t.jpg", dir_path + "thumbs/" + name + "-" + i + ".jpg"));
            }
            var width = ((item.images - 1) * 111);
            if (width < 111) {
               width = 111;
            }
            $("#ibox_details").css("width", width + "px");
	    }
	    $("#ibox_details").append(item.desc.replace(/\n/g, "<br>"));
    } else {
        for (var i = 1; i < item.images; i++) {
            $("#galleryView").append(imageItem(dir_path + "thumbs/" + name + "-" + i + "_t.jpg", dir_path + "thumbs/" + name + "-" + i + ".jpg"));
        }
    }
}

/**
 * Generates the highslide image data to be used for the zoom functionality.
 */
function imageItem(thumb, highres) {
	return '<a href="' + highres + '" class="highslide" onclick="return hs.expand(this)"><img class="highslide_thumb" src="' + thumb + '" alt="Highslide JS" title="Click to enlarge" /></a>' +
           '<div class="highslide-caption"></div>';
}

/**
 * Iterates through the gallery data provided and appends the data to the thumbs
 * div which is provided via the function.
 */
function populateGallery() {
    var count = 0;
    if (disable_scroll) {
        var startNum = 0;
        var endNum = 99999999;
    	var displayedItems = 0;
    	var last = "";
    } else {
    	var startNum = page * numthumbs;
    	var endNum = (page + 1) * numthumbs;
    	var displayedItems = 0;
    	var last = "";
    }
    $("#thumbs").html("");
    for (itemId in galleryData) {
        if (count >= startNum && count < endNum) {
            $("#thumbs").append('<div id="' + itemId + '" style="width: ' + thumbs_width + '; height: ' + thumbs_height + '" class="cover" onClick="openDetails(this, \'' + itemId + '\')"><img src="' + dir_path + 'thumbs/' + itemId + '-0_t.jpg"><br>' + galleryData[itemId].title + '</div>');
            last = itemId;
            displayedItems++;
        }
        count++;
	}
	totalitems = count;
	if (displayedItems != numthumbs) {
	    $("#next").css("visibility", "hidden");
	} else {	    $("#next").css("visibility", "visible");
	}
	// A quick hack to remove the right divider line of the poster
	$("#" + last).css("border-right", "none");
	setPages();
}

/**
 * Sets the number of pages currently available.
 */
function setPages() {
    if (! disable_scroll) {
        $("#pages").html("Page <b>" + (page + 1) + "</b> of <b>" + Math.ceil(totalitems / numthumbs) + "</b>");
    }
}

/**
 * Next page to be displayed in the list.
 */
function clickNext() {
    if (! isCorrectPage(page + 1)) { return false; }
    page++;
    populateGallery();
    closeDetails();
}

/**
 * Previous page tp be displayed.
 */
function clickPrevious() {
    if (! isCorrectPage(page - 1)) { return false; }
    page--;
    populateGallery();
    closeDetails();
}

/**
 * Determine if the specified page is correct or not.
 */
function isCorrectPage(currPage) {
    if (currPage < 0 || currPage > Math.ceil(totalitems / numthumbs)) {
        return false;
    }
    return true;
}
