function Mmx_Block_Gallery(galleryUid)
{
	this.galleryUid = galleryUid;


    /**
     * Init the light box
     * 
     * @param options lightbox options
     * @return void
     */
    this.initLightBox = function (options)
    {
    	galleryUid = this.galleryUid;
    	
    	// apply lightBox plugin to the thumbs
    	$('#block-gallery-photos-thumbbox-'+galleryUid+' a.lightbox-thumbs').lightBox(options);
    	
    	// tiggers lightBox when Diaporama link is clicked
        $('#block-gallery-photos-diaporama-'+galleryUid).click(function(){
            $('#block-gallery-photos-thumbbox-'+galleryUid+' a.lightbox-thumbs:first').click();
        });
        
        // tiggers lightBox when the thumb in the main box is clicked
        // start the slideshow from this image. 
        // find out the image ID from the class name (there is probaly a better way to do that)
        $('#block-gallery-main-'+galleryUid).click(function(){
            var imgId = $('#block-gallery-main-'+galleryUid).attr('class');
            var tableau= imgId.split(new RegExp("[-]+"));
            imgId = '#lightbox-thumbs-'+galleryUid + '-' + tableau[tableau.length-1];
            $(imgId).click();
            return false;
        });
    }

    /**
     * Init event listener on thumbs (preview or start the lightbox), preload images, set toggle listener on the
     * show all thumbs if in preview mode
     * 
     * @param options
     * @return void
     */
    this.intThumbnailsNavigation = function (options)
    {
    	// We can't directly use this.galleryUid because "this" will refer to the caller object in event methods like "click()"
    	galleryUid = this.galleryUid;
    	
    	if(options.preview){
            $("#block-gallery-photos-thumbbox-"+galleryUid+" .block-gallery-photos-thumbs")
	        .click(function(){
	             var photoId = this.id;
	             var tableau= photoId.split(new RegExp("[-]+"));
	             photoId = 'block-gallery-main-' + tableau[tableau.length-1];  
	             $("#block-gallery-main-"+galleryUid).removeClass().addClass(photoId).attr('title', this.title);
	             $("#block-gallery-photos-main-"+galleryUid).attr('src', this.href).attr('alt', this.alt);
	              return false;
	        })  

	        .mouseover(function(){
	            var $f = $(this);
	            $f.css({opacity:".70"}).stop().animate({opacity:"1"},500);
	            });
	    
            // Show all images
            $("#block-gallery-photos-toggle-"+galleryUid).click(function(){
                var height = $("#block-gallery-photos-thumbbox-"+galleryUid).css('height');
                if('auto' == height){
                    $("#block-gallery-photos-thumbbox-"+galleryUid).css('height', '62px');
                	$(this).removeClass('showless').addClass('showmore');
                }else{
		            $("#block-gallery-photos-thumbbox-"+galleryUid).css('height', 'auto');
		            $(this).removeClass('showmore').addClass('showless');
		        }
		    
		    });
	    
	    }else{
	        $("#block-gallery-photos-thumbbox-"+galleryUid+" .block-gallery-photos-thumbs")
	            .click(function(){
	                var imgId = this.id;
	                var tableau= imgId.split(new RegExp("-+"));
	                var lightboxImgId = '#lightbox-thumbs-'+galleryUid+ '-' + tableau[tableau.length-1]
	                $(lightboxImgId).click();
	                return false;
	            });
	   }
	
	    // preload images
	    $(".block-gallery-photos-thumbs").each(function(){
	        var image = new Image();
	        image.src = this.href;
	        image.alt = this.alt;
	    });
    }

}


/**
 * Set or Unset a photo as featured
 * 
 * @param object
 *            object who triggers the event
 * @param options
 *            contant the base ajax url "baseUrl"
 * @return void
 */
function setFeatured(callerObject, options){
    var id = callerObject.id;
    var parentUlId = $(callerObject).parents('ul').attr("id");
    // alert(id);
    // alert($(callerObject).parents('ul').attr("id"));
    
    $.ajax({
        url: options.baseUrl+"/id/"+id,
        type: "GET",
        complete: function(){$("#ajax-loader").remove();},
        success: function(response){ 
            response = eval('(' + response + ')'); 
            // $('#colprincipale').prepend('<div class="message_'+
			// response.status +'"> '+ response.message +'</div>');
            // $('.message_confirm').fadeTo(2000, 1).fadeOut(1000);
            if('confirm' == response.status){
                $("#" + parentUlId + " .featured").removeClass('featured').addClass('notfeatured');
                $("#"+id).removeClass('notfeatured').addClass('featured');
                // alert($("#"+id).attr('id'));
            } 
        }
    })       
}

/**
 * Set or Unset a photo as best featured
 * 
 * @param object
 *            object who triggers the event
 * @param options
 *            contant the base ajax url "baseUrl"
 * @return void
 */
function setBestFeatured(callerObject, options){
    var id = callerObject.id;
    $.ajax({
        url: options.baseUrl+"/id/"+callerObject.id,
        type: "GET",
        complete: function(){$("#ajax-loader").remove();},
        success: function(response){ 
            response = eval('(' + response + ')'); 
            if('confirm' == response.status){
                $("#"+id).toggleClass('featured-'+options.type).toggleClass('notfeatured-'+options.type);
            } 
        }
    })       
}
