(function($)
{	
	var $this = null;
	var uId;
	var params = null;
	var arrayTemp = new Array();
	
	var tabTrad = new Array();
	tabTrad['fr'] = new Array();
	tabTrad['uk'] = new Array();
	
	tabTrad['fr']['hideinfo'] = "Masquer l'actualité";
	tabTrad['fr']['showinfo'] = "Afficher l'actualité";
	tabTrad['uk']['hideinfo'] = "Hide news";
	tabTrad['uk']['showinfo'] = "View news";
	
	var defaultParamsWidget = 
	{
		delayFadeInOut:1000,
		btnSwitch:'.switchState',
		blocNews:'.btnNews',
		currentState:'open',
		targetPosition:'right',
		marginTopOriginBlocNews:0,
		contentBlocNews:'.txtNews',
		lengthMaxTxt:50
	}
	var paramsAnalyser = 
	{
		countBlocNews:null,
		widthBloc:null,
		heightBloc:null
	}
	
	//methode public
	var PublicMethodes =
	{
		init:function(paramUser)
		{
			$this = $j(this);
			uId = $this.attr('id');
			
			return this.each(function()
			{
				params = $.extend(defaultParamsWidget , paramUser);
				
				PrivateMethodes.analyseWidget(params.blocNews);
				EventHandlerAnim.unBindBtn();
				EventHandlerAnim.clickSwicthBtn();
				//EventHandlerAnim.clickNews();
				//PrivateMethodes.minimizeTxt();
			});
		}
	};
	
	//methode privée
	var PrivateMethodes = 
	{
		analyseWidget:function(_blocNews)
		{
			var allBlocNews = '#'+uId+' '+_blocNews;
			paramsAnalyser.widthBloc = $j(allBlocNews+':eq(0)').width();
			paramsAnalyser.heightBloc = $j(allBlocNews+':eq(0)').height();
			$j(allBlocNews).each(function(i) {
				arrayTemp.push($(this).position().left);
				$(this).css({'left':arrayTemp[i]});
			});
			$j(allBlocNews).css('position','absolute');
			
		},
		animateToggle:function()
		{
			if(params.currentState == 'open') {
				var targetNumPos;
				if (params.targetPosition == 'right') targetNumPos = arrayTemp[arrayTemp.length - 1];
				else targetNumPos = arrayTemp[0];
				
				$j($this).removeClass('open').addClass('close');
				$j('#'+uId+' '+params.blocNews).each(function(i) {
					$(this).animate({'left':targetNumPos,'marginLeft':(5-5*i),'marginTop':params.marginTopOriginBlocNews+(5-5*i) },params.delayFadeInOut+100*i, function() {
						params.currentState = 'close';
						$j(params.btnSwitch).children('.titleSwitch').html(tabTrad[window.langue]['showinfo']);
					});
				});
				
			} else {
				$j('#'+uId+' '+params.blocNews).each(function(i) {
					$(this).css({'marginLeft':'','marginTop':''});
					$(this).animate({'left':arrayTemp[i]},params.delayFadeInOut+100*i, function() {
						params.currentState = 'open';
						$j($this).removeClass('close').addClass('open');
						$j(params.btnSwitch).children('.titleSwitch').html(tabTrad[window.langue]['hideinfo']);
					});
				});
			}
		},
		minimizeTxt:function() {
	
		/* Version au clic sur les bouton choisi */ 
		// Limiter le nombre de caractère dans les encart de droite
			$j(params.contentBlocNews).each(function(i) {
	            var text_original = $j(this).text();
	            var text_court = text_original.substring(0, params.lengthMaxTxt);
	            $j(this).text(text_court);
	            $j(this).append(document.createTextNode(" ..."));
			});

		}
		// end of mini txt
	}
	
	var EventHandlerAnim = 
	{
		/*clickNews:function()
		{
			$j(params.blocNews).bind('click' , function()
			{
					console.log($j(this).children('a'));
				if (!$j(this).hasClass(params.btnSwitch)) {
					//$j(this).children('a:eq(0)').click();
				}
			});
		},*/
		clickSwicthBtn:function()
		{
			$j(params.btnSwitch).bind('click' , function()
			{
				if(params.currentState == 'open') $j(this).addClass('switchActivated');
				else $j(this).removeClass('switchActivated');
				PrivateMethodes.animateToggle();			
			});
		},
		unBindBtn:function()
		{
			$j(params.btnSwitch).unbind('click');
			//$j(params.blocNews).unbind('click');
		}
	}
	
	
	$.fn.widgetNews = function(methode)
	{
		// Method calling logic
		if(PublicMethodes[methode]) 
		{
			return PublicMethodes[methode].apply(this, Array.prototype.slice.call(arguments , 1 ));
		}//fin if
		else if (typeof methode === 'object' || ! methode ) 
		{
			return PublicMethodes.init.apply(this , arguments);
		}//fin else if
		else 
		{
			$.error( 'Method ' +  methode + ' does not exist' );
		}//fin else
	};
})(jQuery);

