$(function(){
	$('a[href=/expend_video.php]').fancybox({type:'iframe',width:740,height:410,padding:0});
	
	$('input, textarea, select, button').uniform();
	$('.scroll-pane').jScrollPane();
	
	$('a.ifb').fancybox();
	$('a.ffb').fancybox({type:'iframe',width:580,height:580});
	$('a.nl').fancybox({type:'iframe',width:760,height:580,padding:0});
	
	$('a[href^="#"]:not([href="#"])').live('click',function(){
		$('html,body').animate({scrollTop: $('a[name="'+$(this).attr('href').substr(1)+'"]').offset().top},1000);
		return false;
	});
	$('a[href*=iframe/actualite]').live('click',function(){
		var a = $(this);
		$.fancybox({
			'padding'		: 20,
			'title'			: '',
			'type'			: 'iframe',
			'href'			: a.attr('href')
		});
		return false;
	});
	$(window).scroll(function() {
		var top = $(window).scrollTop();
		var height_left = $('div.leftcol').height();
		var height_right = $('div.rightcol').height();
		var height = $('div.centercol').height()-(height_right>height_left?height_right:height_left);
		if(top>428 && top<height+428) {
			$('div.leftcol, div.rightcol').stop();
			$('div.leftcol, div.rightcol').animate({'margin-top':top-428},250);
		}
		else if(top<=428) {
			$('div.leftcol, div.rightcol').stop();
			$('div.leftcol, div.rightcol').animate({'margin-top':0},250);
		}
	});
	$('a[name^=add_selection]').live('click',function(){
		var a = $(this);
		if(a.attr('name')=='add_selection') {
			var id = a.attr('rel').split(' ');
			for(var i = 0; i < id.length ; i++) {
				a.after('<a href="#" name="add_selection '+id[i]+'" rel="duo'+i+'"></a>');
				$('a[name="add_selection '+id[i]+'"][rel=duo'+i+']').click();
			}
			$.fancybox({
				'padding'		: 0,
				'title'			: '',
				'width'			: 320,
				'height'		: 240,
				'href'			: chemin_root+'ajax/selection.php?action=add_selection '+id[0]+' '+id[1]
			});
		}
		else {
			var ajouter = {
				top:a.offset().top+a.height()/2,
				left:a.offset().left+a.width()/2
			};
			mySelection(a.attr('name'));
			if($('a[name=liste-selection]').length) {
				var selection = {
					top:$('a[name=liste-selection]').offset().top+$('a[name=liste-selection]').height()/2,
					left:$('a[name=liste-selection]').offset().left+$('a[name=liste-selection]').width()/2
				};
				$('body').append('<div id="plus" style="position:absolute;font-size:24px;">+</div>');
				$('div#plus').css({'left':ajouter.left,'top':ajouter.top}).animate({'left':selection.left,'top':selection.top},1000,function(){
					$(this).remove();
					a.fadeOut(500);
					if(a.attr('rel')!='duo0') {
						if($('#liste-selection').length) {
							$('#liste-selection').slideUp(250,function(){
								$('a[name=liste-selection]').click();
							});
						}
						else {
							$('a[name=liste-selection]').click();
						}
					}
				});
			}
			else {
				a.fadeOut(500);
			}
			if(a.attr('rel')!='duo0' && a.attr('rel')!='duo1') {
				$.fancybox({
					'padding'		: 0,
					'title'			: '',
					'width'			: 320,
					'height'		: 240,
					'href'			: chemin_root+'ajax/selection.php?action='+a.attr('name')
				});
			}
		}
		return false;
	});
	$('a[name^=rem_selection]').live('click',function(){
		var a = $(this);
		mySelection(a.attr('name'));
		var li = $(this).parents('li.checkbox');
		if(li.length) li.fadeOut(500,function(){ $(this).remove(); });
		return false;
	});
	$('a[name^="liste"]').live('click',function(){
		var a = $(this);
		$.post(chemin_root+'ajax/gestion_selection.php','liste='+$(this).attr('name').substr(6)+(id_chapitre?'&id_chapitre='+id_chapitre:''),function(data){
			var display = $('#'+a.attr('name')).css('display');
			if(display=='none' || display==undefined) {
				$('#'+a.attr('name')).remove();
				a.after(data);
				$('#'+a.attr('name')).slideDown(250);
			}
			else {
				$('#'+a.attr('name')).slideUp(250,function(){
					$('#'+a.attr('name')).remove();
				});
			}
		});
		return false;
	});
});
function mySelection(action) {
	$.ajax({
		type: 'POST',
		async: false,
		url: chemin_root+'ajax/gestion_selection.php',
		data: 'action='+action,
		success: function(data){
			eval(data);
		}
	});
}
