(function($) {
	
	/* gestion diaporama */
	$.fn.diaporama = function(options) {
		var diapo = $(this); 
		var opts = $.extend( {}, $.fn.diaporama.defaults, options);
		var ul = $('ul', diapo);
		var loader = $('.loading', diapo);
		var img = $('img', loader);
		var mainpicdiv = $('.diapo_main p span', diapo);
	
		
		function imageLoadCallBack() {
			loader.removeClass('loading').append(this);
		}
		function switchon() {
			img_src = $(this).children('a').children('img').attr('src');
			img_src = img_src.replace('thumb80x43', 'thumb319x215');
			var url = $(this).children('a').attr('href');
			if ($.img_src && $.img_src == img_src) {
				return false;
			}
			;
			$.img_src = img_src;

			$('li', ul).bind('mouseover', function(e) {
				var li = $(e.target);
			}).bind('blur', function(e) {
				var li = $(e.target);
			}).bind('click', function(e) {
				var li = $(e.target);
			});

			var li = $(this);
			li.css('opacity', opts.opOn);
			li.children().filter('div.img_frame').addClass('img_frame_on');
			li.addClass('imgOn');

			var liSibs = li.siblings();
			liSibs.removeClass('imgOn').css('opacity', opts.opOff).children(
			'div.img_frame').removeClass('img_frame_on');

			$(img).fadeOut(function() {
			}, 100).load(imageLoadCallBack).error(function() {
				// notify the user that the image could not be loaded
			}).ready(function() {
				loader.addClass('loading');
				setTimeout(function() {
					$(img).attr('src', $.img_src).fadeIn() ;
				}, 100) ;
			});

			
			$(mainpicdiv).html(
					'<a href="'+url+'">' + $(this).children('a').children('img').attr('title') + '</a>');
		}

		function switchoff() {
			return false;
		}
		var config = {
				/*
				 * sensitivity: 1, // number = sensitivity threshold (must be 1 or
				 * higher) interval: 500, // number = milliseconds for onMouseOver
				 * polling interval over: switchon, // function = onMouseOver
				 * callback (REQUIRED) timeout: 500, // number = milliseconds delay
				 * before onMouseOut out: switchoff // function = onMouseOut
				 * callback (REQUIRED)
				 */
				sensitivity : 1,
				interval : 100,
				over : null,
				timeout : 0,
				out : null
		};
		ul
		.each(function() {
			var lis = $('li',$(this));
			
			var surl = $('a',lis).attr('href');
			if (window.console) {
				//console.log($('a',lis[0]));
			}
			
			lis.each(function() {
			$(this).css( {
				'opacity' : opts.opOff,
				'position' : 'relative'
			})
			.hoverIntent(switchon, switchoff)
			.append(
			'<div class="img_frame off"><a href="' + $('a',this).attr('href') + '" target="' + $('a',this).attr('target') + '"><img src="/css/ui/diapo_selected.gif"></a></div>'); /*	'<div class="img_frame off"><a href="' + $('a',this).attr('href') + '" target="_blank"><img src="/css/ui/diapo_selected.gif"></a></div>');*/
			}) ;	
		}); // each

		ul.children(':first').css('opacity', opts.opOn).children().filter(
		'div.img_frame').addClass('img_frame_on');

	} // ain

	$.fn.diaporama.defaults = {
			opOn : 1,
			opOff : 0.5
	}
	/* end diaporama */
	/* table manage */
	$.fn.tablemanage = function(options) {
		if(this.length==0) {
			return false; 
		}
		
		var thisId = $(this).attr('ID') ;
		
		var opts = $.extend( {}, $.fn.tablemanage.defaults, options);
		var controls = $('li a', $(this));
		var tBody = $('tbody', $(this)) ;
		var pagenum = 1;
		var controls_current = controls[0];
		controls_current.className = 'current';

		controls.each(function() {
			var a = $(this);
			a.click(function() {
				page = a.attr('title');
				controls_current.className = '';
				switch (page) {
				case 'next':
					pagenum = (pagenum == opts.maxpages ? 1 : pagenum + 1);
					break;
				case 'prev':
					pagenum = (pagenum == 1 ? opts.maxpages : pagenum - 1);
					break;
				default:
					pagenum = parseInt(page);
				}

				switch_content(pagenum, tBody, thisId);
				controls_current = controls[pagenum - 1];
				controls_current.className = 'current';
				return false;
			});
		});
		
		function switch_content(pagenum, tBody, thisId) {
			if (0 < pagenum < opts.maxpages) {
				switch (thisId) {
				case 'le_fil_info_sports':
					url = '/actualite/le_fil_info_sports/bloc_content.php?page=';
					break;
				case 'le_fil_info_elections':
					url = '/actualite/le_fil_info_elections_2010/bloc_content.php?page=';
					break;	
				case 'fil_lifestyle':
					url = '/lifestyle/bloc_content.php?page=';
					break;						
				case 'fil_lifestyle_sante':
					url = '/lifestyle/sante/bloc_content.php?page=';
					break;						
				default:
					url = '/actualite/le_fil_info/bloc_content.php?page=';
					break;
				}
				$.get(url + pagenum, function(html) {
					tBody.html(html);
				});
			}
		} // switch_content

	}; // end main
	$.fn.tablemanage.defaults = {
			maxpages : 5
	};
	/* slider */

	$.fn.slider = function() {
		function _in(obj) {
			$(this).addClass('current').siblings().removeClass('current');
			$.each($.imgs, function() {
				/*
				 * img_src = img_src.replace(/\.thumb/,''); if($.img_src &&
				 * $.img_src == img_src) { return false }; $.img_src = img_src ;
				 * 
				 * $(this).fadeOut(function(){}) .load(imageLoadCallBack)
				 * .error(function () { //alert('error'); //notify the user that
				 * the image could not be loaded })
				 * .ready(function(){$('#loader')
				 * .addClass('loading');setTimeout(function(){$(img).attr('src',
				 * $.img_src).fadeIn()},500)});
				 * 
				 */});
		}
		function _out(obj) {
		}

		return this
		.each(function() {
			elem = $('h3', $(this));
			$.imgs_cont = $('ul.slider', $(this));
			$.imgs = $('ul.slider img', $(this));

			var id = $(this).attr('id');
			$.controls = $(
					'<span id="' + id + '_controls" class="slider_controls"><a href="#" class="current">1</a><a href="#">2</a><a href="#">3</a><a href="#">4</a></span>')
					.insertAfter(elem);
			$.controls_items = $.controls.children();

			config = {
					sensitivity : 10, // number = sensitivity threshold
					// (must be 1 or higher)
					interval : 200, // number = milliseconds for onMouseOver
					// polling interval
					over : _in, // function = onMouseOver callback
					// (REQUIRED)
					timeout : 500, // number = milliseconds delay before
					// onMouseOut
					out : _out
					// function = onMouseOut callback (REQUIRED)
			};
			$.each($.controls_items, function(i) {
				$(this).i = i;
				// this.ind = i ;
				// this.ind.extend($(this))
				// $(this).index = i ;
				$(this).hoverIntent(config);
			});
		});
	};

	/* table manage */
})(jQuery);

