/**
 * @author Greg
 */
(function($){

    $.fn.extend({ 
        
        //pass the options variable to the function
        SlideGallery: function(options) {


            //Set the default values, use comma to separate the settings, example:
            var defaults = {
                gallery_width : null,
                gallery_class : null,
                filmstrip: true,
                filmstrip_width : null,
                filmstrip_height : null,
                filmstrip_item_width : null,
                filmstrip_item_height : null,
                filmstrip_nb_item : 4,
                filmstrip_scroll_item : 3,
                show_panel : false,             
                panel_width: null,
                panel_height: null
            };
                
            var options =  $.extend(defaults, options);

            return this.each(function() {
                var gallery = $(this);
                var total = $('li',gallery).length;
                    
                var filmstrip = $(gallery);
                $('<ul></ul>')
                    .attr('id','galleryImages')
                    .attr('class','galleryImages')
                    .html($(filmstrip).html())
                    .insertBefore(filmstrip);
    
                var pannel = $('ul#galleryImages');
    
    
                $('a',pannel).each(function(){
                    //$(this).attr('rel','gallery');
                    $('img',this).attr('src',$(this).attr('href'));
                    $(this).attr('href',$(this).attr('fullsize'));
                });
                
                // setting filmstrip
                $(filmstrip).jcarousel({
                    visible: options.filmstrip_nb_item,
                    scroll: options.filmstrip_scroll_item,
                     itemFirstInCallback: {
                        onBeforeAnimation: function(car,li,idx,state){
                            if(state=='init') return;
                            $(pannel).data('jcarousel').scroll(parseInt(idx));
                        }
                    }
                });
                
               
                // setting pannel 
                var rel = $('a:eq(0)',pannel).attr('rel');
                $('a[rel=' + rel + ']',pannel).colorbox();
                
                $(pannel).jcarousel({
                    visible:1,
                    scroll:1,
                    itemLoadCallback: {
                        onBeforeAnimation: function(car,state){
                            if(state=='init') return;
                            var idx = car.first;
                            $(filmstrip).data('jcarousel').scroll(parseInt(idx));
                        }
                    }
                });
                
                ps_h =  $(pannel).height();
                ps_b_h  = $(pannel).data('jcarousel').buttonPrev.height();  
                ps_b_w  = $(pannel).data('jcarousel').buttonPrev.width(); 
                pbtop = ((ps_h / 2) - ps_b_h) + 'px';
                pboff = '-'+ (ps_b_w + 5 )+'px';
                
                console.debug($('#galleryImages').find('img').height());                
                console.debug($('#gallery').find('img').height());                
                
                $(pannel).css({position:'relative'}).data('jcarousel').buttonPrev.css({ position:'absolute',top:pbtop,left:pboff});
                $(pannel).css({position:'relative'}).data('jcarousel').buttonNext.css({ position:'absolute',top:pbtop,right:pboff});
                
                
                fs_h =  $(filmstrip).height();
                fs_b_h  = $(filmstrip).data('jcarousel').buttonPrev.height();  
                fs_b_w  = $(filmstrip).data('jcarousel').buttonPrev.width(); 
                btop = ((fs_h / 2) - fs_b_h) + 'px';
                boff = '-'+ (fs_b_w + 5 ) +'px';
                

                                
                $(filmstrip).css({position:'relative'}).data('jcarousel').buttonPrev.css({ position:'absolute',top:btop,left:boff});
                $(filmstrip).css({position:'relative'}).data('jcarousel').buttonNext.css({ position:'absolute',top:btop,right:boff});
                            
                
                
                // setting events 
                $('li a',filmstrip).click(function(e){
                    e.preventDefault();
                    var idx = $(this).parent('li').attr('jcarouselindex');
                    $(pannel).data('jcarousel').scroll(parseInt(idx));
                });    
                
            });
        },
        
        
        FilmstripGallery: function(options){
        	var defaults = {
                filmstrip_nb_item : 3,
                filmstrip_scroll_item : 3,
                buttonNextHTML:'<span></span>',
                buttonPrevHTML:'<span></span>'
            };
            
            var options =  $.extend(defaults, options);
            console.debug(options,'options');
            return this.each(function() {
                var gallery = $(this);
                var total = $('li',gallery).length;
                    
                var filmstrip = $(gallery);
                
    			var itemW = parseInt($('li',this).width());
              
                
                // setting filmstrip
                $(filmstrip).jcarousel({
                    visible: options.filmstrip_nb_item,
                    scroll: options.filmstrip_scroll_item,
                    buttonNextHTML:'<span></span>',
                	buttonPrevHTML:'<span></span>'
                });
                
                /*
                fs_h =  $(filmstrip).height();
                fs_b_h  = $(filmstrip).data('jcarousel').buttonPrev.height();  
                fs_b_w  = $(filmstrip).data('jcarousel').buttonPrev.width(); 
                btop = ((fs_h / 2) - fs_b_h) + 'px';
                boff = '-'+ (fs_b_w + 5 ) +'px';
                
                $(filmstrip).css({position:'relative'}).data('jcarousel').buttonPrev.css({ position:'absolute',top:btop,left:boff});
                $(filmstrip).css({position:'relative'}).data('jcarousel').buttonNext.css({ position:'absolute',top:btop,right:boff});
                     */    
                $('a',this).colorbox(options);
            
            });
            
        },
        
        //pass the options variable to the function
        TileGallery: function(options) {
        	var h = $(window).height() - 30;
        	var w = $(window).width() - 30 ;
        	
            var defaults = {
                scalePhotos:true,
                maxWidth: w,
                maxHeight: h
            };
            var options =  $.extend(defaults, options);
            return this.each(function() {
                $('a',this).colorbox(options);
            });
        },
        
        Go2Paradize: function(options){
        	var defaults = { clickable: false };
            var options =  $.extend(defaults, options);
        	return this.each(function() {
        		$('a',this).click(function(e){ e.preventDefault(); });
                $(this).go2paradize(options);
        		
            });
            
            
        	
        },
        
        CycleGallery: function(options){
        	var defaults = { fx: 'fade'	 };
            var options =  $.extend(defaults, options);
        	return this.each(function() {
        		$('a',this).click(function(e){ e.preventDefault(); });
        		
        		$(this).children('li').each(function(){
        				beforeSlide( $(this) );
        		} );
        		
                $(this).cycle(options);
        		
            });
            
            
        	
        }

    });
    
})(jQuery);

function beforeSlide( currSlideElement )
{
	
        						var inf = $(currSlideElement).children('div.info');
        						if( inf.length == 0 )
        						{
        							addInfo( currSlideElement );
        							inf = $(currSlideElement).children('div.info');
        						}
        						
}
function addInfo( slideElement )
{
	var inf = $(slideElement).children('div.info');
	
	if( inf.length == 0 )
    {
		inf = $('<div>');
	    inf.addClass('info');
		var img = $(slideElement).children('img');
		var p = $('<div>');
		$(p).addClass('bandeau');
		$(inf).css('backgroundImage', 'url(/resources/application/layouts/public/images/bg_menu.png)');
		p.html( img.attr('title') );
		$(inf).append(p);
		$(slideElement).append(inf);
	}
	
}

