/***
Filename:		imagelibrary.js
Author:			Andrei Vais
Date:			27/10/2009
Description:	Javascript functionality for image library page
***/


function initialiseImageLibrary()
{
    //console.log('initialiseColours');
    if (!document.getElementById) return false;
	if (!document.getElementById("content")) return false;
	if (!document.getElementById("previewimage")) return false;
	if (!document.getElementById("libraryshots")) return false;
	
	var ImageLibraryGroupsDescription = getElementsByClassName(document.getElementById("content"), "div", "shotinfo");
    //console.log('ImageLibraryGroupsDescription length: ', ImageLibraryGroupsDescription.length);
    //hide the copy for all image library groups
    for (i = 0; i < ImageLibraryGroupsDescription.length; i++)
    {  
        ImageLibraryGroupsDescription[i].className="shotinfo hide";
    }    
    
    var ImageLibraryGroups = getElementsByClassName(document.getElementById("libraryshots"), "a", "*");
    //console.log('ImageLibraryGroups length: ', ImageLibraryGroups.length);
    
    for (i = 0; i < ImageLibraryGroups.length; i++)
    {  
        //console.log(ImageLibraryGroups[i].getAttribute("href"), " : ", ImageLibraryGroups[i].className, " : ", ImageLibraryGroups[i].getAttribute("rel"));
        //set a default ImageLibraryGroup
        if ((ImageLibraryGroups[i].className != "") && (ImageLibraryGroupsDescription[i].getAttribute("id") == ImageLibraryGroups[i].getAttribute("rel")))
        {
            ImageLibraryGroupsDescription[i].className="shotinfo show";
            document.getElementById("previewimage").setAttribute("src", ImageLibraryGroups[0].getAttribute("href"));
        }
        
        ImageLibraryGroups[i].onclick = function()
	    {
	      return showSelectedLibraryGroup(this);
	    }
    }
}

function showSelectedLibraryGroup(pSelectedLibraryGroup)
{
    //console.log("showSelectedLibraryGroup");
    //console.log(pSelectedLibraryGroup.getAttribute("rel"));
    
    var ImageLibraryGroupsDescription = getElementsByClassName(document.getElementById("content"), "div", "shotinfo");
    var ImageLibraryGroups = getElementsByClassName(document.getElementById("libraryshots"), "a", "*");
    for (i = 0; i < ImageLibraryGroupsDescription.length; i++)
    {  
        ImageLibraryGroupsDescription[i].className="shotinfo hide";
        ImageLibraryGroups[i].className="";
        if(ImageLibraryGroupsDescription[i].getAttribute("id") == pSelectedLibraryGroup.getAttribute("rel"))
        {
            ImageLibraryGroupsDescription[i].className="shotinfo show";
            ImageLibraryGroups[i].className="current";
            document.getElementById("previewimage").setAttribute("src", pSelectedLibraryGroup.getAttribute("href"));
            document.getElementById("previewimage").setAttribute("alt", pSelectedLibraryGroup.getAttribute("title"));
        }
    }    
    return false;
}

//addLoadEvent(initialiseImageLibrary);

jQuery(document).ready(function () {
    // main vertical scroll
    jQuery("#group-images").scrollable({
        // basic settings
        vertical: true,
        // up/down keys will always control this scrollable
        keyboard: 'static',
        // assign left/right keys to the actively viewed scrollable
        onSeek: function (event, i) {
            horizontal.eq(i).data("scrollable").focus();
        }
        // main navigator (thumbnail images)
    }).navigator("#groups");

    // horizontal scrollables. each one is circular and has its own navigator instance
    var horizontal = $(".scrollable").scrollable({ circular: true }).navigator(".navi");
    // when page loads setup keyboard focus on the first horzontal scrollable
    horizontal.eq(0).data("scrollable").focus();

    jQuery('.navi').each(function (i, e) {
        jQuery('a', e).each(function (i2, e2) {
            jQuery(e2).html(i2 + 1);
        });
    });
    
    //if the image library has too many categories then add event to scroll back to the top
    if (jQuery("#groups li").length > 8)
    {
        jQuery("#groups li:gt(7)").click(function (){
            jQuery.scrollTo(jQuery("#pagecontent.imagegallerypagecontent"), 1000);
        });
    }
});
