/**
 * @author Kristofer Forsell
 */

function Class() {
    this.me = "gallery";
    this.currentItem;
}

Class.prototype = {

    // INIT
    init: function () {
        this.initialized = false;
		this.isOpen = false;
    },

    loadGallery: function (galleryUrl, startItem) {
		
		blom.home.isOpen = false;
		blom.gallery.isOpen = true;
		
        // Show the loader
        $("#loader").fadeIn(100);
		
        $.ajax({
            type: "GET",
            url: galleryUrl,
            //galleryUrl,
            dataType: "xml",
            success: function (xml) {

                // Hide the loader
                $("#loader").fadeOut(100);

                // Remove Old Gallery
                if ($('#gallery').length > 0) {
                    $("#gallery").remove();
                }

                // Create the gallery
                $("<div id='gallery'></div>").hide().appendTo("#content");
                $("<div id='galleryWrapper'></div>").appendTo("#gallery");
				
				var totalItems, numImages, images, imgWidth;
                //Find gallery -- a little ugly since the xml does not have attributes.
                $(xml).find('blom > gallery').each(function (i) {
					
                    if ( $(this).find('gallery_id').text() === blom.gallery.currentGallery) {
						
						// log('Found gallery', $(this).find('gallery_id').text())
						
                        totalItems = 0;
						images = '';
                        numImages = $(this).find('image_id').size();

                        $(this).find("images").each(function (t) {
							
                            if ($(this).find('image_p_link').text() == 0) {
                                imgWidth = 1000;
								totalItems = totalItems+1;
                            } else if ($(this).find('image_p_link').text() == 1 ) {
								imgWidth = 500;
							} else {
								imgWidth = 500;
								totalItems = totalItems+1;
							}
							
							images += '<div class="gallery-image" style="width: ' + imgWidth + 'px; float: left;">';
                            images = images + '<img src="' + $.trim($(this).find('image_url').text()) + '" alt="' + $.trim($(this).find('image_comment').text()) + '" width="' + imgWidth + '" />';
                        	images += '<br/><p>'+$(this).find('image_comment').text()+'</p></div>';
						});

                        $('<div class="galleryItem">' + images + '</div>').appendTo("#galleryWrapper");

                    }

                });


        // Set Initial
        blom.gallery.currentItem = parseInt(startItem);
        blom.gallery.slideToItem(blom.gallery.currentItem);
        blom.gallery.totalItems = totalItems;
        // Set wrapper width
        $("#gallery").width(1000 * totalItems).fadeIn(400);

        // Remove Old Navigation
        if ($('#galleryNavigation').length > 0) {
            $("#galleryNavigation").remove();
        }

		// Create overlay navigation
		$('<div id="overlaynav"><div class="o-prev"></div><div class="o-next"></div></div>').appendTo('#gallery');

        // Create Gallery Navigation
        $("#navigation").append('<div id="galleryNavigation" ><a id="toggle-caption" href="#">CAPTION: OFF</a><br/><br/> <a href="#" class="prev"><--</a> <span id="currentItem">' + (parseInt(startItem)+1) + '</span>:' + totalItems + ' <a href="#" class="next">--></a></div> ');
		$('html').unbind('keydown').bind('keydown', blom.gallery.keyPress);
        $(".galleryItem").bind("click", blom.gallery.slideToNext);
		$("#navigation a.next, .o-next").bind("click", blom.gallery.slideToNext);
        $("#navigation a.prev, .o-prev").bind("click", blom.gallery.slideToPrev);
		$('#toggle-caption').bind('click', blom.gallery.toggleCaptions );
		// var move = $('.galleryItem').height() - $('#navigation').height() - 60;
		// 	if( move < 0 ){
		// 		move = 10;
		// 	}
		// 	$('#galleryNavigation').css('margin-top',move );
    }
});
},

keyPress: function () {
	// log('keypress');
    if (event.keyCode === 39 || event.keyCode === 32) {
        blom.gallery.slideToNext();
    } else if (event.keyCode === 37) {
        blom.gallery.slideToPrev();
    }
},

slideToNext: function () {

    if (blom.gallery.currentItem < (blom.gallery.totalItems - 1)) {
        blom.gallery.currentItem++;
        blom.gallery.slideToItem(blom.gallery.currentItem);
    }
	return false;
},

slideToPrev: function () {
    if (blom.gallery.currentItem > 0) {
        blom.gallery.currentItem--;
        blom.gallery.slideToItem(blom.gallery.currentItem);
    }
	return false;
},

slideToItem: function (itemIndex) {
    $("#galleryWrapper").animate({
        "margin-left": itemIndex * -1000
    }, 300);
	// log("slideTo", itemIndex);
    $("#currentItem").text((itemIndex+1));
    // $("#gallery").height($("#galleryWrapper div").eq(itemIndex).height());
},

toggleCaptions: function(){
	
	if( $(this).hasClass('on') === true ){
		$('.gallery-image p').fadeOut(100);
		$(this).text('CAPTION: OFF').removeClass('on');
	} else {
		$('.gallery-image p').fadeIn(100);
		$(this).text('CAPTION: ON').addClass('on');
	}
	
	return false;
},

openView: function () {
    $("#gallery").fadeIn(400);
},

closeView: function () {
	this.isOpen = false;
    $("#gallery").remove();
    $("#galleryNavigation").remove();
	$('html').unbind('keydown');
},

// GENERIC
toString: function () {
    return this.me;
},

}

blom.gallery = new Class();

$(document).ready(function () {
    blom.initClass("gallery");
}); 
