/**
 * @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 : 1,
                filmstrip_scroll_item : 1,
                buttonNextHTML:'<span></span>',
                buttonPrevHTML:'<span></span>' ,
                auto : 5 , /* en ms */
               	wrap: 'last' ,
               	offset: 1 ,
               	start: 1
            };
            
            var options =  $.extend(defaults, 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>' ,
                	auto :options.auto ,
                	wrap: options.wrap  
                	
                	
                });
                
                /*
                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);
        		
            });
            
            
        	
        },
        Showcase : function(options){
        	var defaults  = {
        		
        			content_width:			600,
					content_height:			800,
					fit_to_parent:			true,
					auto:					true,
					interval:				3000,
					continuous:				true,
					loading:				true,
					tooltip_width:			200,
					tooltip_icon_width:		32,
					tooltip_icon_height:	32,
					tooltip_offsetx:		18,
					tooltip_offsety:		0,
					arrows:					true,
					buttons:				true,
					btn_numbers:			true,
					keybord_keys:			true,
					mousetrace:				false, /* Trace x and y coordinates for the mouse */
					pauseonover:			true,
					stoponclick:			false,
					transition:				'fade', /* hslide/vslide/fade */
					transition_delay:		0,
					transition_speed:		500,
					show_caption:			'onload', /* onload/onhover/show */
					thumbnails:				true,
					thumbnails_position:	'outside-last', /* outside-last/outside-first/inside-last/inside-first */
					thumbnails_direction:	'horizontal', /* vertical/horizontal */
					thumbnails_slidex:		1, /* 0 = auto / 1 = slide one thumbnail / 2 = slide two thumbnails / etc. */
					dynamic_height:			true, /* For dynamic height to work in webkit you need to set the width and height of images in the source. Usually works to only set the dimension of the first slide in the showcase. */
					speed_change:			true, /* Set to true to prevent users from swithing more then one slide at once. */
					viewline:				false, /* If set to true content_width, thumbnails, transition and dynamic_height will be disabled. As for dynamic height you need to set the width and height of images in the source. */
					custom_function:		null /* Define a custom function that runs on content change */
        	};
        	var options =  $.extend(defaults, options);
        	
        	return this.each(function() {
        			$('a',this).colorbox(options);
        			$(this).awShowcase(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);
	}
	
}

