var overlay_conf = {target: '#gallery', expose: '#f1f1f1'};
var gallery_conf = {speed: 500, template: '<div><strong>${title}</strong></div><div>${description}</div><span>Image ${index}/${total}</span>'};
var phototeque_conf = $.extend({preload:false}, gallery_conf); 

$(document).ready(function(){
	
    if( $("#portlet_4444_tabs").length ){
            // Tabulations Flash infos
            $("#portlet_4444_tabs").tabs("#portlet_4444 > .portlet_4444_value");
    }
	
    if( $(".gallery a").length ){
    	// Gallery remplace l'url accessible par l'url Jquery Tools pour le slideshow
        $(".gallery a").each(function(){
        	$(this).removeAttr('href');
            $(this).attr('href', $(this).attr('img_url'));
            $(this).removeAttr('img_url');
        });
    	// Gallery phototheque
    	$(".gallery a").overlay(overlay_conf).gallery(gallery_conf);
    }
    
    if( $(".scrollable").length ){
    	//pagination a consulter/phototheque
    	$(".scrollable").scrollable({size:3}).navigator().mousewheel();
    }
    

    // Phototèque
    if( $("#phototeque").length ){
    	// Gallery remplace l'url accessible par l'url Jquery Tools pour le slideshow
        $("#phototeque a").each(function(){
        	$(this).removeAttr('href');
            $(this).attr('href', $(this).attr('img_url'));
            $(this).removeAttr('img_url');
        });
	    $("#phototeque").scrollable().circular().autoscroll({
	        autoplay: true, 
	        interval: 5000
	    }).mousewheel();
	    $("#phototeque a:not(.cloned)").overlay(overlay_conf).gallery(phototeque_conf);		// a:not(.cloned) to fix the 'circular' plugin behaviour (add 10 elements)
    }


    $(".gallery_sortable").sortable({connectWith: '.connectedSortable',
                                     dropOnEmpty: false,
                                     placeholder: 'ui-state-highlight',
                                     stop: function(event, ui){
                                        $.ajax({
                                            type: "POST",
                                            url: "moveObjectToPosition",
                                            data: {'id':ui.item[0].id, 'position:int': ui.item.index() + (ui.item.parent().index()*12)} // le calcul de la position est réalisé par rapport à l'index des balises <ul> et <li> pour ne pas fausser le calcul il ne faut pas enlever les balises <div> englobant les <ul> (album)'
                                        })
                                        ui.item.parent().parent().children("ul:not(:last)").each(function(index){
                                            n = $(this).children("li.item").length
                                            if(n==11){
                                                a = $(this).next('ul').children('li:first').detach();
                                                a.insertBefore($(this).children('li.last'));
                                            }
                                            if(n==13){
                                                a = $(this).children('li.item:last').detach();
                                                a.insertBefore($(this).next('ul').children('li.item:first'));
                                            }
                                        })
                                      }
                                     });
    $(".gallery_sortable").disableSelection();
    
    $('.portlet_recherche a.more').click(function() {
    	$(this).parent().parent().children('.item:nth-child(n+7)').show();
    	$(this).parent().children('a.less').show();
    	$(this).parent().children('span').hide();
    	$(this).hide();
    	return false;
    });
    $('.portlet_recherche a.less').click(function() {
    	$(this).parent().parent().children('.item:nth-child(n+7)').hide();
    	$(this).parent().children('a.more').show();
    	$(this).parent().children('span').show();
    	$(this).hide();
    	return false;
    });

//    $("#sortable1, #sortable2").sortable().disableSelection();
//    $("ul.tabs").tabs("div.galleries > ul");
//    var $tabs = $("#tabs").tabs();

//    var $tab_items = $(".batch a").droppable({
//        accept: ".connectedSortable li",
//        hoverClass: "ui-state-hover",
//        drop: function(ev, ui) {
//            alert('drop');
//            var $item = $(this);
//            var $list = $($item.find('a').attr('href')).find('.connectedSortable');
//
//            ui.draggable.hide('slow', function() {
//                $tabs.tabs('select', $tab_items.index($item));
//                $(this).appendTo($list).show('slow');
//            });
//        }
//    });
});

/*$("#portlet_38794255 img").each(function(){
      w = $(this).width();
      maxwidth = 160;
      if (w > maxwidth){
          $(this).attr('width', maxwidth);
      }
  });*/

//#gallery_sortable { list-style-type: none; margin: 0; padding: 0; }
//#gallery_sortable li { margin: 3px 3px 3px 0; padding: 1px; float: left; width: 100px; height: 90px; font-size: 4em; text-align: center; }
