/**
 * Common javascript utilities.
 * $Id: utilities.js,v 1.2 2009/11/04 17:39:22 kev Exp $
 */

if (typeof BD == "undefined" || !BD) {
    var BD = {};
}
BD.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=(""+a[i]).split(".");
        o=BD;
        for (j=(d[0] == "BD") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};

BD.namespace("BD.jquery");
BD.jquery.getAnimateOptions = function() {
    return {duration: 500, easing:"easeOutExpo"};
}

/**
 * Validate an email address.
 *
 * @param string email Email address to validate.
 * @return True if the email is valid; false otherwise.
 */
BD.validateEmail = function(email) {
    match = String(email.match(/[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,6}/i));
    if (match != "null" && match != '') 
        return true;
    return false;
}
    

/*
 * Page-based gallery. Requires the following id-based DOM elements to exist:
 * -galleryPrevious - element to select the previous image.
 * -galleryNext - element to select the next image.
 * -galleryCurrentPage - text element indicating the current page number.
 * -galleryTotalPages - text element indicating the number of pages.
 * -galleryDescription - text element for the description of the current image.
 * -galleryImage - img element for the current image.
 *
 * It is expected the current page is already displayed in the above elements.
 * 
 * @param {Array} pages Array of image descriptor objects. Each object must 
 *  have the following properties: 
 *  -'image' specifies the url of the image; 
 *  -'description' specifies the description and alt text for the image.
 */
BD.Gallery = function(pages)
{
    var currentPage = 0;    
    var Pages = pages;
    var that = this;
    var copyVisible = true;
    
    /* private methods */
    
    /* return whether the next pager button is enabled */    
    function nextEnabled()
    {
        return currentPage < Pages.length - 1;
    } 
    
    /* return whether the back pager button is enabled */
    function previousEnabled()
    {
        return currentPage > 0;
    }
    
    /* update the galler ui to the current page */
    function updateContent(direction)
    {
        var myCurrentPage = currentPage;
        
        var img = new Image();
        
        //
        // handler for when image is fully downloaded
        //
        
        img.onload = function() {
            var nextImg;
            
            // ensure this image should still be displayed
            if (myCurrentPage != that.getCurrentPage()) return;
            
            $('#galleryImage').attr('src', Pages[currentPage]['image']);            
            $('#galleryImage').attr('alt', Pages[currentPage]['description']);
            this.onload = null;
            
            //
            // if there's a next image, start downloading it
            //
            
            if (nextEnabled())
            {
                nextImg = new Image();
                nextImg.src = Pages[currentPage+1]['image'];
            }
            
        };
        
        //
        // start downloading the image
        //
        
        img.src = Pages[currentPage]['image'];
        
        //
        // update description + page number
        //
        
        $('#galleryCurrentPage').text(currentPage+1);
        $('#galleryDescription').text(Pages[currentPage]['description']);
        
        //
        // toggle next and previous buttons
        //
        
        if (nextEnabled()) {
            if ($('#galleryNext').hasClass('disabled')) 
                $('#galleryNext').removeClass('disabled');
            $('#galleryNextArrow').attr('src', '/images/arrow_gt.gif');
        } else {
            if (!$('#galleryNext').hasClass('disabled')) 
                $('#galleryNext').addClass('disabled');
            $('#galleryNextArrow').attr('src', '/images/arrow_gt_disabled.gif');
        }   

        if (previousEnabled()) {
            if ($('#galleryPrevious').hasClass('disabled')) 
                $('#galleryPrevious').removeClass('disabled');
            $('#galleryPreviousArrow').attr('src', '/images/arrow_lt.gif');
        } else {
            if (!$('#galleryPrevious').hasClass('disabled')) 
                $('#galleryPrevious').addClass('disabled');
            $('#galleryPreviousArrow').attr('src', '/images/arrow_lt_disabled.gif');
        }
        
        //
        // show/hide copy
        //
        
        if (!previousEnabled() && direction == 'previous' && !copyVisible) {
            copyVisible = true;
            $('#main').css('display', 'block');
            /*
            $('#main').animate({
                height: 'toggle'
            }, BD.jquery.getAnimateOptions());
            */
        }
        
        if (previousEnabled() && direction == 'next' && copyVisible) {
            copyVisible = false;
            $('#main').css('display', 'none');
            /*
            $('#main').animate({
                height: 'toggle'
            }, BD.jquery.getAnimateOptions());
            */
        }
    } 

    /* privileged methods */
    
    /* move to the next page */
    this.nextPage = function()
    {
        if (!nextEnabled()) return;
        currentPage++;
        updateContent('next');
    };
    
    /* move to the previous page */
    this.previousPage = function()
    {
        if (!previousEnabled()) return;        
        currentPage--;
        updateContent('previous');
    };
    
    /* return the current page number, 0-based */
    this.getCurrentPage = function() 
    { 
        return currentPage; 
    };

    //
	// begin preloading images
	//
		
	for (var i = 1; i < pages.length; i++) {
        $.preloadImages(pages[i]['image']);
    }
    $.preloadImages(['/images/arrow_lt.gif', '/images/arrow_gt_disabled.gif']);


};

