﻿var _thumbnail_url = 'images/gallery/thumbs/';
var _large_url = 'images/gallery/';
var _dataSource = null;
var _type = null;
var _carousel = null;

function getThumbnailHTML(photo) {
	var onclick = "";
	var image = "";
	if (_type == 'photo'){
    	onclick = "displayImage('" + photo.image + "','" + photo.title + "','" + photo.description + "')";
		image = '<a href="#" onclick="' + onclick + '; return false;"><img width="100px" src="' + _thumbnail_url + photo.image + '" alt="' + photo.title + '"/></a>';
	}
	else{
		onclick = "displayEmbeded('" + photo.youtuberef + "')";
    	image = '<a href="#" onclick="' + onclick + '; return false;"><img width="100px" src="http://i3.ytimg.com/vi/' + photo.youtuberef + '/default.jpg"/></a>';
	}
    return image;
}

function displayEmbeded(utuberef) {
	
	$('#main-image-placeholder *').remove();
	$('#main-image-placeholder')
		.append('<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/' + utuberef + '&hl=en_US&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + utuberef + '&hl=en_US&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>');
}

function displayImage(image, title, description) {

    // dunno how big the image is gonna be... tell the users we haven't hung!
    $('#main-image-placeholder').addClass('loading');
    $('#main-image-placeholder *').remove();

    var img = new Image();
    $(img).load(function() {
        $(this).hide();
        $('#main-image-placeholder').removeClass('loading')
                    .append(this)
                    .append('<h3>' + title + '</h3>')
                    .append('<p>' + description + '</p>');

        $(this).fadeIn(1000);
    }).attr('src', _large_url + image).attr('alt', title);
    
}

function carousel_OnInit(carousel, state) {
    if (state == 'init')
        _carousel = carousel;
}

function carousel_ItemLoadCallback(carousel, state) {

    if (state != 'init')
        return;
    $.ajax({
        type: 'GET',
        url: _dataSource,
        contentType: 'application/json;charset=utf-8',
        scriptCharset: 'utf-8',
        dataType: 'json',
        success: function(data) {
            var photos = data.items;
            for (i = 0, j = photos.length; i < j; i++) {
                carousel.add(i, getThumbnailHTML(photos[i]));
            }
            carousel.size(photos.length);

            $('a', carousel.get(0)).click();
        },
        error: function(xhr, textStatus, errorThrown) {
            $('#error').html(xhr.responseText);
        }
    });
}

function resetThumbnails(category, type) {
	_type = type;
    _dataSource = 'json/' + category + '.js';
    if (_carousel == null) {
        $('#thumbnails').jcarousel({
            initCallback: carousel_OnInit,
            itemLoadCallback: { onBeforeAnimation: carousel_ItemLoadCallback }
        });
    }
    else {
        _carousel.reset();
    }
    return false;
}


function BuildMenu() {

    // Hide all the sub-menus.
    $('#accordion-menu ul').hide();

    /* 
    IE6 can't select '#accordion-menu>li>a' in CSS
    so let jquery come to the rescue and add a class for us
    so that we can do some styling.
    */
    $('#accordion-menu>li>a').addClass('toplevel-item')
                .click(function() {

                    var childList = $(this).next();
                    if (childList.is('ul')) {

                        if (childList.is(':visible')) {
                            childList.slideUp('normal');
                        }
                        else {
                            childList.slideDown('normal');
                            $('#accordion-menu ul').not(childList).slideUp('slow');
                        }
                        return false;
                    }
                    else {
                        $('#accordion-menu ul:visible').slideUp('slow');
                    }
                });

}
