// JavaScript Document

(function($){

	var ver = 'jquery.artifica.toolsbox-1.1.0';
	
	$.fn.reverseOrder = function() {
		return this.each(function() {
			$(this).prependTo( $(this).parent() );
		});
	};
	
	$.fn.artificaToolsbox = function(vars) { 
	
		var element = $(this);
		var buttons = (vars.buttons != undefined) ? vars.buttons : 'print';
		var sizes = (vars.sizes != undefined) ? vars.sizes : '';
		var defaultSize = (vars.defaultSize != undefined) ? vars.defaultSize : '';
		var contrast = (vars.contrast != undefined) ? vars.contrast : '';
		var place = (vars.place != undefined) ? vars.place : 'after';
		
		var listSizes = sizes.split(',');
		
		var start = function() {
			loadStylesheets(listSizes);
			loadListButtons();
		};
		
		var loadListButtons = function() {
			
			var str = '';
			var list = buttons.split(',');
			var html = '';
	
			if(list.length > 0)
			{
				//if(element.find('#print').length > 0) $('#print').remove();	
				//if(element.find('#typomoins').length > 0) $('#typomoins').remove();
				//if(element.find('#typoplus').length > 0) $('#typoplus').remove();
				//if(element.find('#typoconstrast').length > 0) $('#typoconstrast').remove();
				
				if(place == 'before')
				{
					html = element.html();
					element.html('');
				}
	
				for(var i = 0; i < list.length; i++)
				{					
					switch(list[i])
					{
						case "print" :
							
							if($('#print').length == 0) element.append('<a href="#" title="Imprimer" id="print">imprimer le contenu</a>\n');
	
							$('#print').bind('click', function(){window.print(); return false;});
							$('#print').attr('href', 'javascript:void(0);');
																			
							break;
							
						case "typoMore" :
								
							if(sizes != '')
							{
								if($('#btnTypoMore').length == 0) element.append('<a href="#" id="btnTypoMore">Agrandir le texte</a>\n');
								
								$('#btnTypoMore').bind('click', function(){return setTypoSize('more', listSizes, defaultSize)});
								$('#btnTypoMore').attr('href', 'javascript:void(0);');
							}
	
							break;
							
						case "typoLess" :
						
							if(sizes != '')
							{								
								if($('#btnTypoLess').length == 0) element.append('<a href="#" id="btnTypoLess">R&eacute;duir le texte</a>\n');
								
								$('#btnTypoLess').bind('click', function(){return setTypoSize('less', listSizes, defaultSize)});
								$('#btnTypoLess').attr('href', 'javascript:void(0);');
							}
							
							break;
							
						case 'typoContrast' :
						
							if(contrast != '')
							{							
								if($('#btnTypoContrast').length == 0) element.append('<a href="#" id="btnTypoContrast">Contrast</a>\n');
								
								$('#btnTypoContrast').bind('click', function(){return setTypoContrast(contrast)});
								$('#btnTypoContrast').attr('href', 'javascript:void(0);');
							}
							
							break;						
							
						default:
	
							break;
					}
				}
	
				if(place == 'before') element.append(html);
			}
		};
		
		var loadStylesheets = function() {
			
			// COOKIES
			var current = $.cookie('typo_size');			
			if(current != null && !$('body').hasClass(current)) $('body').addClass(current);
			
			if(contrast != '') getTypoContrast(contrast);
		};
		
		start();
	}
	
	function setTypoSize(argType, argSizeArray, argDefault)
	{
		// COOKIES
		var currentOld = $.cookie('typo_size');
		var newi = argDefault;
	
		var current = currentOld;
		var list = argSizeArray;
	
		if(list.length > 0)
		{
			if(currentOld == null) currentOld = list[0];
			
			for(var i = 0; i < list.length; i++)
			{		
				if(current == list[i]) newi = i;
			}	
		
			switch(argType)
			{
				case 'less' :
				
					newi--;
					if(newi > -1) current = list[newi];
					break;
					
				case 'more' :
				
					newi++;
					if(newi < list.length) current = list[newi];
					break;	
			}
			
			$('body').removeClass(currentOld);
			$('body').addClass(current);
			$.cookie('typo_size', current, {path:'/'});		
		}
			
		return false;
	}	
	
	
	function getTypoContrast(argClass)
	{
		var current = $.cookie('typo_contrast');			
		if(current != null && !$('body').hasClass(current) && current == '1') $('body').addClass(argClass);
	}
	
	function setTypoContrast(argClass)
	{
		var current = $.cookie('typo_contrast');
		
		if(current == null) current = '0';
		
		//!$('body').hasClass(contrast)
		switch(current)
		{
			case '0' :
			
				current = '1';
				$('body').addClass(argClass);
				break;
			
			case '1' :
			
				current = '0';
				$('body').removeClass(argClass);
				break;			
		}
		
		$.cookie('typo_contrast', current, {path:'/'});
		
		return false;
	}
	
	$.fn.artificaToolsbox.ver = function() { return ver; };

})(jQuery);
