jQuery(document).ready(function() {
	var stop = false;
	
	jQuery( "#accordion_galerie h3" ).click(function( event ) {
		if (stop) 
		    {
			event.stopImmediatePropagation();
			event.preventDefault();
			stop = false;
		    }
	    });
	
	jQuery( "#accordion_galerie" )
	    .accordion({
		    header: "> div > h3"
			})
	    .sortable({
		    axis: "y",
		    handle: "h3",
		    stop: function() {stop = true; }
/*		    update : function (event, ui){
			    var id = this.id;
			    alert(id);
			    var serialize = jQuery(this).sortable("serialize");
			    var datas = 'task=galerie_order&'+serialize;
			}*/
	});
	    
	// LES TAILLES DES DIVS
	
	jQuery("#addgalerie").colorbox({width:"700", height:"500", iframe:true});
	
	
	jQuery(".info_img").colorbox({width:"810", height:"850", iframe:true});
	
	jQuery('ul[id^=album_] li p a.edit').colorbox({width:"670", height:"300", iframe:true});
	
	jQuery("a[id^=addfile_]").colorbox({width:"670", height:"280", iframe:true});
	
	jQuery("a[id^=editgal_]").colorbox({width:"670", height:"350", iframe:true});
	jQuery("a[id^=editgal_]").click(function(){
		jQuery(document).bind('cbox_closed', function(){
			window.location.reload();
			});
		});
	
	jQuery("a[id^=addgal_]").colorbox({width:"670", height:"380", iframe:true});
	jQuery("a[id^=addgal_]").click(function(){
		jQuery(document).bind('cbox_closed', function(){
			window.location.reload();
			});
		});	
		
	jQuery("a[id^=addpic_]").colorbox({width:"910", height:"780", iframe:true});
	jQuery("a[id^=addpic_]").click(function(){
		jQuery(document).bind('cbox_closed', function(){
			window.location.reload();
		});
	});									
	
	
	
	jQuery("ul[id^=album_]").sortable({
		update : function (event, ui)
		    {
			var id = this.id;
			var serialize = jQuery(this).sortable("serialize");
			var datas = 'task=galerie_order&'+serialize;
			jQuery.post(url, datas,
				    function(data){
					ret = jQuery.trim(data);
					ret = ret.split('::');
					if (ret[0] == 'OK')
					    {
					      	jQuery.growl('Position de l\'image changée', ret[1]);
						jQuery('#'+id).children('li').each(function (i, v){
							jQuery(v).find('span.ordre').html((i+1));
						    });
					    }
					else 
					    jQuery.growl('Erreur', ret[1], '', '-error');	
				    });
		    }
	    });
	jQuery("ul[id^=album_]").disableSelection();

	
	jQuery('ul[id^=album_] li > span a').live('mouseenter', function (event) {
		var img = jQuery(this).children('img');
		
		img.removeClass('galerie_max');
		var scr_width = screen.width;
		var ow = img.attr('width');
		var oh = img.attr('height');
        var pos = img.offset();
		var h_max = Math.ceil(screen.height - (event.screenY + 60 - (event.pageY - pos.top)));
		
		if (ow > scr_width){
			var left = 30;
			var w = scr_width - 60;
		} else {
			var w = ow;
		}
		if ((oh*w)/ow > h_max){
			var h = h_max - 75;
			w = Math.ceil((ow*h)/oh);
		} else {
			var h = Math.ceil((oh*w)/ow);
		}
		
		if ((pos.left + 40) - Math.ceil(w/2) >= 0)
			left = Math.ceil((pos.left + 12 + 35) - Math.ceil(w/2));
		if ((left + w) >= scr_width)
			left = scr_width - w - 30;
		
		img.addClass('galerie_max');
		jQuery('#galerietips img').attr('src', img.attr('src'));
		jQuery('#galerietips').css('position', 'absolute');
		jQuery('#galerietips').css('background', '#fff');
		jQuery('#galerietips').css('border', '#E46E10 solid 1px');
		jQuery('#galerietips').css('left',  left+"px");
		jQuery('#galerietips').css('top', (pos.top + 70)+"px");
		jQuery('#galerietips img').attr('width', w);
		jQuery('#galerietips img').attr('height', h);
		jQuery('#galerietips').stop(true,true).fadeIn(200);      
	});
	
	jQuery('ul[id^=album_] li > span a').live('mouseleave', function () {
		jQuery('#galerietips').stop(true,true).fadeOut(200);
	});
	
	
	
	
	/* opt for image */
	jQuery("ul[id^=album_] li").live('mouseenter', function (){
		var ordre = parseInt(jQuery(this).find('span.ordre').text());
		var n = Math.floor((ordre - 1)/9);
		jQuery("ul[id^=album_] li:nth-child(n+"+((9*n)+1)+"):not(:nth-child(n+"+(9*(n+1)+1)+"))").stop(true,false)
			.animate({marginLeft: 12-(25/9)+"px"}, 100, 'linear');
		jQuery(this).children("p").stop(true,false)
			.animate({display:"block", width:"25px"}, 250, 'linear', function(){
			jQuery(this).css("display", "block");
		});
	});
	jQuery('ul[id^=album_] li').live('mouseleave', function(){
		var ordre = parseInt(jQuery(this).find('span.ordre').text());
		var n = Math.floor((ordre - 1)/9);
		
		jQuery(this).children("p").stop(true,false)
			.animate({display:"none",  width:"0px"},  100, 'linear', function(){
			jQuery(this).css("display", "none");
		});
		jQuery("ul[id^=album_] li:nth-child(n+"+((9*n)+1)+"):not(:nth-child(n+"+(9*(n+1)+1)+"))").stop(true,false)
			.animate({marginLeft: "12px"}, 250, 'linear');
	});
	
	jQuery('ul[id^=album_] li p span.delete').click(function(){
		var tmp = jQuery(this).parent().parent().attr('id');
		tmp = tmp.split('_');
		var item_id = tmp[1];
		var datas = "task=del_gallery_img&item_id="+item_id;
		jQuery.post(url, datas, function(data){
			data = jQuery.trim(data);
			var ret = data.split('::');
			if (ret[0] == 'OK'){
				jQuery("li#galorder_"+item_id).remove();
				jQuery.growl('Image retirée', ret[1],'');
			} else {
				jQuery.growl('Erreur', ret[1],'','-error');
			}
		});
	});
	
	/* change gallery name */
		jQuery("input.galerie_titre").change(function(){
	
		var str = jQuery(this).attr("value");
		var nbr = jQuery(this).attr("id");
		var datas = 'task=galerie_title&title='+str+'&idgalerie='+nbr;
		jQuery.post(url, datas,
			    function(data){
				ret = jQuery.trim(data);
				ret = ret.split('::');
				if (ret[0] == 'OK')
				    {
					jQuery.growl('Titre de la galerie changé ', ret[1]);
					jQuery("h2.componentheading").text(str);
				    }
				else 
				    jQuery.growl('Erreur', ret[1], '', '-error');	    
			    });
	});
	
});
