/* Plugins UI */

/* Cleaning des fields INPUT - focus */
(function($){
	$.fn.clean_field = function(){
		return this.each(function(){
			var $field = $(this);
			if(!$field.is('input')) $field = $('textarea', $field);
			$field.data('val', $field.val());
			$field.data('rel', !$field.attr('rel')? 0 : $field.attr('rel'));
			$field.bind('focus', function(e){
				var that = $(this);
				if(that.val() == that.data('rel')){
					that.data('val', that.val());
					that.data('initialize', true);
					$(this).val('');
				}						
			});
			
			$field.bind('blur', function(e){
				var that = $(this);
				var value = that.data('val');
				if($.trim(that.val()) == '' || value == that.val()){
					that.val(value);	
				}			
			});		
		});	
	};
})(jQuery);

/* Animation Principal Menu - hover */
(function($){
	$.fn.animate_menu = function(){
		
		return this.each(function(){
			var $link = $(this);
			var $bckg = $('span.shadow', $link);			
			$link.bind('mouseenter', function(e){
				if(!$link.hasClass('active')) $bckg.css({width: '30%', display: 'block', opacity: 100}).stop().animate({width: '100%'}, {queue: false, duration: 400});
			});
			
			$link.bind('mouseleave', function(e){
				if(!$link.hasClass('active')) $bckg.animate({width: '30%', opacity: 0}, {queue: false, duration: 400}, function(){});					
			});
			
			$link.bind('click', function(e){
				if($link.hasClass('item')) return true;
				else {
					canarie.tools.stopEvent(e);
					var rel = $link.attr('rel').split('||');
					var $mParent = $('.' + rel[0]);
					if(rel.length < 2) return;
					var $activeUl = $('ul:visible', $mParent);
					var $relUl = $('#' + rel[1]);
					if(!$relUl.is('ul')) window.location.href = $link.attr('href');
					else {
						if($activeUl.is('ul')){
							if($activeUl.attr('id') != $relUl.attr('id')){
								$('ul', $mParent).css({display: 'none'});
								$relUl.slideDown(500, function(){
									//				
								});
							}
						}
						else $relUl.slideDown(500, function(){
							//				
						});
					}				
					return false;
				}
			});			
		});	
	};
})(jQuery);

/* Accordeons */
(function($){
	$.fn.accordeon = function(){
		return this.each(function(){
			var $container = $(this);
			var $content = $('.content', $container);
			($container.hasClass('visible'))? $content.data('isVisible', true) : $content.data('isVisible', false);
			var $title = $('.title', $container);		
			$('.icon, em', $title).bind('click', function(e){
				canarie.tools.stopEvent(e);
				toogle();
				return false;
			});
			
			var toogle = function(){				
				($content.data('isVisible'))? _close() : _open();
				return false;
			};
			
			var _open = function(){
				$content.data('isVisible', true);
				$('.icon', $title).addClass('visible');
				var height = $('.wrapper', $content).outerHeight();
				$content.stop().animate({height: height}, {'duration': 450, 'queue': false, 'easing': 'easeInOutQuad', 'complete': function(){
					$container.addClass('visible');
					$content.css({overflow: 'visible'});
				}});
			};
			
			var _close = function(){
				$content.data('isVisible', false);
				$('.icon', $title).removeClass('visible');
				$content.css({overflow: 'hidden'}).stop().animate({height: 0}, {'duration': 450, 'queue': false, 'easing': 'easeInOutQuad', 'complete': function(){
					$container.removeClass('visible');																																																																					 
				}});
			};	
		});	
	};
})(jQuery);

/* Print page - click */
(function($){
	$.fn.print = function(){
		return this.each(function(){
			var $link = $(this);
			
			$link.bind('click', function(e){
				canarie.tools.stopEvent(e);			
				window.print();
				return false;
			});
		});	
	};
})(jQuery);

/* Text size page - click */
(function($){
	$.fn.size = function(){
		return this.each(function(){
			var $link = $(this);
			var maxSize = 20;
			var minSize = 14;
			
			$link.bind('click', function(e){
				canarie.tools.stopEvent(e);			
				($link.hasClass('more'))? txtIncrement() : txtDecrement();			
				return false;
			});
			
			var txtIncrement = function(){
				var $contents = $('.block .content');
				$contents.each(function(i, elt){
					var $content = $(elt);
					var size = parseInt($content.css('font-size').replace('px', ''));
					if(size >= maxSize) return;
					var $parent = $content.parents('.accordeon:first');
					$content.css({'font-size': (size+2) + 'px'});
					if($parent.is('div') && $parent.hasClass('visible')){
						$content.css({height: $('.wrapper', $content).outerHeight()});
					}				
				});			
			};
			
			var txtDecrement = function(){
				var $contents = $('.block .content');
				$contents.each(function(i, elt){
					var $content = $(elt);
					var size = parseInt($content.css('font-size').replace('px', ''));
					if(size <= minSize) return;
					var $parent = $content.parents('.accordeon:first');
					$content.css({'font-size': (size-2) + 'px'});
					if($parent.is('div') && $parent.hasClass('visible')){
						$content.css({height: $('.wrapper', $content).outerHeight()});
					}	
				});		
			};
		});	
	};
})(jQuery);