/**
 * @author Kristofer Forsell, Georgios Athanassiadis
 */

function Class() {
    this.me = "home";
    this.initialized = false;

    this.currentRatio;
    this.currentWidth;
	// this.xmlUrl = './../admin/index.php/xml';
	this.xmlUrl = './../photo_admin/index.php/xml';
}

Class.prototype = {

    // INIT
    init: function () {
        //this.initNav();
        this.isOpen = false;
		//store current active gallery
		blom.gallery.currentGallery;
	},

    loadThumbs: function () {
	
        if ( blom.home.initialized === false) {
			
			$('#loader').fadeIn();
		
            $.ajax({
                type: "GET",
                url: blom.home.xmlUrl,
                dataType: "xml",
                success: function (xml) {
					
					//log(xml);
					
                    // Hide the loader
                    $("#loader").hide();

                    // Clear the Grid
                    $("<div id='grid'></div>").appendTo("#content");
                    $("#grid").html("");
			
                    // Add the image_thumbs to the Grid
					var gId,oId,pair,gName,gTags;
					$(xml).find("blom > gallery").each(function (i) { 
                   
						gId = $.trim($(this).find('gallery_id').text());
						gName = $.trim($(this).find('gallery_name').text()).toLowerCase();
						gTags = $.trim($(this).find('gallery_tags').text()).toLowerCase();
						gTagsSplit  = gTags.split(',');
						gTags		= gTagsSplit.join(' ');
				
						var counter = 0;
					 	$(this).find("images").each(function (i) { 
								
								oId = counter;
								
								if( $(this).find('image_p_link').text() == 2 ){
									pair = "second";
									counter = counter -1;
								} else{
									pair = "first";
								} 
								
								//$("<div class='item " + $(this).attr("category") + " " + $(this).attr("id") + "'><img src='" + $(this).attr("src") + "' width='100px' height='100px' /></div>")
								$("<div id='i-" + $(this).find('image_id').text() + "' class='item o-" + oId + " g-" + gId + " " + pair + " " + gTags + " " + gName +"'><img src='" + $.trim($(this).find('image_thumb_url').text()) + "' alt='" + $.trim($(this).find('image_comment').text()) + "' width='100px' height='100px' /></div>")
								.css('opacity', '0')
								.appendTo("#grid")
								.append('<input type="hidden" class="img-src" value="' + $(this).find('image_url').text() + '" />')
								.delay( Math.random() * 1000 )
								.animate({ 'opacity' : 1 }, 500 );
								
								counter = counter +1;
                    	});	
					});
                    // Initialize the Grid
                    blom.home.buildGrid();
                   
                }
            });
			
		 	blom.home.initialized = true;
		

        } else {
			// log('Open cached home...');
            blom.home.openView();
        }

    },

    buildGrid: function () {
        $('#grid').masonry({
            itemSelector: '.item:visible',
            columnWidth: 50,
            resizeable: true,
            animate: false
        });

		blom.home.unBindItems();

        $('#grid .item').bind("mouseover", function () {
            $(this).stop(true,true).animate( { "opacity" : "0.8" }, 100 );
        })

        $('#grid .item').bind("mouseout", function () {
            $(this).stop(true,true).animate( { "opacity" : "1" }, 100 );
        })

        $('#grid .item').bind("click", function () {
            blom.closeViews();
			blom.gallery.currentGallery = $(this).attr('class').split('g-')[1].split(' ')[0];			
			var orderId = $(this).attr('class').split(' ')[1].split('o-')[1];
			if( $(this).hasClass('second') ){
				orderId = orderId-1;
			}
			$.address.value('gallery/'+blom.gallery.currentGallery+'/'+orderId);
            blom.gallery.loadGallery( blom.home.xmlUrl, orderId );
        })

    },
	
	unBindItems: function(){
		
		$('#grid .item').unbind("click");
		$('#grid .item').unbind("mouseover");
		$('#grid .item').unbind("mouseout");
		
	},

    filterItems: function (itemClass) {
		
		blom.home.loadThumbs();
        $("#grid .item").hide();
		
		var items = itemClass.split(' ');
		var i;
		
		$('#grid .item').each(function(){
			i = items.length-1;
			while(i >= 0){		
				if($(this).hasClass( items[i] )){
					$(this).fadeIn(400);
				}
				i--;
			}
		});
		
        $('#grid').masonry();

    },

    openView: function () {
		this.isOpen = true;
        $('#grid .item').show();
		$("#grid").fadeIn(400);
		$('#grid').masonry();
		blom.changeIndexTitle( 'Show All' );	
    },

    closeView: function () {
		this.isOpen = false;
		this.unBindItems();
        $("#grid").hide();
    },

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

}

blom.home = new Class();

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