jQuery.noConflict();
(function($) {
    $(function() {
        
        /**
         * Nicolas - 24 agosto 2011
         *
         * Quando si clicca nell'immagine d'anteprima dello slider, anzichè rimandare al post
         * facciamo partire il video
         */
		 (function()
		 {
		 	var self = this;
		 	this.$placeholder = $( '#jwplayer-wrapper' );
			
			
			this.showChosenVideo = function()
			{
				var $parent  = $( this ).closest( '.post' ),
					videosrc = $parent.children( '.url-of-the-video' ).val(),
					imagesrc = $parent.children( '.img-of-the-video' ).val();

				if( $( '#preview-jwplayer-placeholder_wrapper' ).size() )
				{
					$( '#preview-jwplayer-placeholder_wrapper' ).remove();
					
					$( '<div />', { id : 'preview-jwplayer-placeholder' } ).appendTo( self.$placeholder[ 0 ] );
				}
				
				jwplayer( 'preview-jwplayer-placeholder' ).setup( 
				{
					'flashplayer'	: '/cms/wp-content/uploads/jw-player-plugin-for-wordpress/player/player.swf',
					'id'		 	: 'jwplayer',
					'width'			: 640,
					'height'		: 360,
					'file'			: videosrc,
					'image'			: imagesrc,
					'skin'			: '/cms/wp-content/plugins/jw-player-plugin-for-wordpress/skins/bekle.zip'
				});
				
				return false;
			};

		 	$( '#fps-container .fps-full-img > a' ).bind( 'click', this.showChosenVideo );
			$( '#fps-container .post:first-child' ).find( '.fps-full-img > a' ).trigger( 'click' );
		 			 	
		 })();
        
         if ( $.cookie('mode') == 'list' ) {
            list_update();
        } else if ( $.cookie('mode') == 'grid' ) {
            grid_update();
        }

        $('#mode').toggle(
            function(){
                if ( $.cookie('mode') == 'list' ) {
                    $.cookie('mode','grid');
                    grid();
                } else {
                    $.cookie('mode','list');
                    list();
                }
            },
            function(){
                if ( $.cookie('mode') == 'grid') {
                    $.cookie('mode','list');
                    list();
                } else {
                    $.cookie('mode','grid');
                    grid();
                }
            }
        );

        function list(){
            $('#mode').addClass('flip');
            $('#loop')
                .fadeOut('fast', function(){
                    list_update();
                    $(this).fadeIn('fast');
                })
            ;
        }

        function grid(){
            $('#mode').removeClass('flip');
            $('#loop')
                .fadeOut('fast', function(){
                    grid_update();
                    $(this).fadeIn('fast');
                })
            ;
        }

        function list_update(){
            $('#loop').addClass('list').removeClass('grid');
            $('#loop').find('.thumb img').attr({'width': '190', 'height': '190'});
            $('#loop').find('.post')
                .mouseenter(function(){
                    $(this)
                        .css('background-color','#FFEA97')
                        .find('.thumb').hide()
                        .css('z-index','-1');
                })
                .mouseleave(function(){
                    $(this)
                        .css('background-color','#f5f5f5')
                        .find('.thumb').show()
                        .css('z-index','1');
                });
            $('#loop').find('.post').click(function(){
                location.href=$(this).find('h2 a').attr('href');
            });
            $.cookie('mode','list');
        }

        function grid_update(){
            $('#loop').addClass('grid').removeClass('list');
            $('#loop').find('.post').removeAttr('style').unbind('mouseenter').unbind('mouseleave');
            $('#loop').find('.thumb img').attr({'width': '290', 'height': '290'});
            $.cookie('mode', 'grid');
        }
 
    })
})(jQuery)

