/* UBO - Javascript Functions Document */
/* Correction UBO */
function initAccordions(){
	var options = {autoHeight: false, collapsible: true, active:false};
	jQuery('.accordion').each(function(i, elm){
		console.log('initAccordion');
		$(elm).accordion(options);
		$(elm).find('.accordion-toggler-level-1').click(function (){
			$(elm).find('.accordion-content-level-2').slideUp('slow', function(){
				$(elm).find('.accordion-level-2').css('display', 'block');
				$(elm).find('.accordion-level-2').accordion('destroy').accordion(options);
			});
		});
	});
}

function initSlides(){
	jQuery('ul li.collapsible').each(function(i, elm){
		if($(elm).find('ul').size() == 1){
			if(!$(elm).hasClass('active')) $(elm).find('ul').hide();
			$(elm).find('a:first').click(function(e){
				if($(elm).hasClass('active')){
					$(elm).find('ul').slideUp('slow', function() {
						$(elm).removeClass('active');
					});
				}
				else{
					$(elm).addClass('active');
					$(elm).find('ul').slideDown('slow', function() {
						
					});
				}
				return false;
			});
		}
	});
}

function horizontalTicker(container, element, previous, next, tabs, width, duration, position){
	var nbElements = container.find(element).size();
	var pos, margin;
	if(nbElements > 1){
		container.width(width*nbElements);
		previous.click(function(e){
			if(parseInt(container.css('margin-left'))+width <= 0){
				container.stop(false, true).animate({marginLeft: parseInt(container.css('margin-left'))+width}, {duration:duration, easing:'easeInOutSine', complete:function(){
						if(position != null){
							pos = Math.abs(parseInt(container.css('margin-left')) / width) + 1; 
							position.text(pos+' / '+nbElements);
						}
						current = nbElements - ((nbElements*width + parseInt(container.css('margin-left'))) / width);
						if(tabs != null){
							tabs.find('li').each(function(i, elm){
								if(i == current) jQuery(elm).addClass('active');
								else jQuery(elm).removeClass('active');
							});
						}
					}
				});
			}
			else{
				container.stop(false, true).animate({marginLeft:-(nbElements-1)*width}, {duration:duration, easing:'easeInOutSine', complete:function(){
						if(position != null){
							pos = Math.abs(parseInt(container.css('margin-left')) / width) + 1; 
							position.text(pos+' / '+nbElements);
						}
						current = nbElements - ((nbElements*width + parseInt(container.css('margin-left'))) / width);
						if(tabs != null){
							tabs.find('li').each(function(i, elm){
								if(i == current) jQuery(elm).addClass('active');
								else jQuery(elm).removeClass('active');
							});
						}
					}
				});
			}
			return false;
		});
		next.click(function(e){
			if(parseInt(container.css('margin-left'))-width > -nbElements*width){
				container.stop(false, true).animate({marginLeft: parseInt(container.css('margin-left'))-width}, {duration:duration, easing:'easeInOutSine', complete:function(){
						if(position != null){
							pos = Math.abs(parseInt(container.css('margin-left')) / width) + 1; 
							position.text(pos+' / '+nbElements);
						}
						current = nbElements - ((nbElements*width + parseInt(container.css('margin-left'))) / width);
						if(tabs != null){
							tabs.find('li').each(function(i, elm){
								if(i == current) jQuery(elm).addClass('active');
								else jQuery(elm).removeClass('active');
							});
						}
					}
				});
			}
			else{
				container.stop(false, true).animate({marginLeft:0}, {duration:duration, easing:'easeInOutSine', complete:function(){
						if(position != null){
							pos = Math.abs(parseInt(container.css('margin-left')) / width) + 1; 
							position.text(pos+' / '+nbElements);
						}
						current = nbElements - ((nbElements*width + parseInt(container.css('margin-left'))) / width);
						if(tabs != null){
							tabs.find('li').each(function(i, elm){
								if(i == current) jQuery(elm).addClass('active');
								else jQuery(elm).removeClass('active');
							});
						}
					}
				});
			}
			return false;
		});
	}
	
	// Tabs
	if(tabs != null){
		tabs.find('li').each(function(i, elm){
			jQuery(elm).click(function(e){
				
				current = i;
				margin = width*current;
				
				container.stop(false, true).animate({marginLeft:-margin}, {duration:duration, easing:'easeInOutSine', 
					complete:function(){
						if(tabs != null){
							tabs.find('li').each(function(j, elm2){
								if(j == current) jQuery(elm2).addClass('active');
								else jQuery(elm2).removeClass('active');
							});
						}
					}
				});
				return false;
			});
		});
	}
}

function carrousel(container, element, previous, next, tabs, height, duration, autoStart, timing){
	var nbElements = container.find('.'+element).size();
	var current = 0;
	var margin;
	if(nbElements > 1){
		previous.click(function(e){
			if(parseInt(container.css('margin-top'))+height <= 0){//Correction UBO
				container.stop(false, true).animate({marginTop: parseInt(container.css('margin-top'))+height}, {duration:duration, easing:'easeInOutSine', complete:function(){
						current = nbElements - ((nbElements*height + parseInt(container.css('margin-top'))) / height);
						if(tabs != null){
							tabs.find('li').each(function(i, elm){
								if(i == current) jQuery(elm).addClass('active');
								else jQuery(elm).removeClass('active');
							});
						}
					}
				});
			}
			else{
				container.stop(false, true).animate({marginTop:-(nbElements-1)*height}, {duration:duration, easing:'easeInOutSine', complete:function(){
						current = nbElements - ((nbElements*height + parseInt(container.css('margin-top'))) / height);
						if(tabs != null){
							tabs.find('li').each(function(i, elm){
								if(i == current) jQuery(elm).addClass('active');
								else jQuery(elm).removeClass('active');
							});
						}
					}
				});
			}
			return false;
		});
		next.click(function(e){
			if(parseInt(container.css('margin-top'))-height > -nbElements*height){
				container.stop(false, true).animate({marginTop: parseInt(container.css('margin-top'))-height}, {duration:duration, easing:'easeInOutSine', complete:function(){
						current = nbElements - ((nbElements*height + parseInt(container.css('margin-top'))) / height);
						if(tabs != null){
							tabs.find('li').each(function(i, elm){
								if(i == current) jQuery(elm).addClass('active');
								else jQuery(elm).removeClass('active');
							});
						}
					}
				});
			}
			else{
				container.stop(false, true).animate({marginTop:0}, {duration:duration, easing:'easeInOutSine', complete:function(){
						current = nbElements - ((nbElements*height + parseInt(container.css('margin-top'))) / height);
						if(tabs != null){
							tabs.find('li').each(function(i, elm){
								if(i == current) jQuery(elm).addClass('active');
								else jQuery(elm).removeClass('active');
							});
						}
					}
				});
			}
			return false;
		});
		// AutoStart
		if(autoStart == true){
			autoStart = function(){
				if(parseInt(container.css('margin-top'))-height > -nbElements*height){
					container.stop(false, true).animate({marginTop: parseInt(container.css('margin-top'))-height}, {duration:duration, easing:'easeInOutSine', complete:function(){
							current = nbElements - ((nbElements*height + parseInt(container.css('margin-top'))) / height);
							if(tabs != null){
								tabs.find('li').each(function(i, elm){
									if(i == current) jQuery(elm).addClass('active');
									else jQuery(elm).removeClass('active');
								});
							}
						}
					});
				}
				else{
					container.stop(false, true).animate({marginTop:0}, {duration:duration, easing:'easeInOutSine', complete:function(){
							current = nbElements - ((nbElements*height + parseInt(container.css('margin-top'))) / height);
							if(tabs != null){
								tabs.find('li').each(function(i, elm){
									if(i == current) jQuery(elm).addClass('active');
									else jQuery(elm).removeClass('active');
								});
							}
						}
					});
				}
			};
			var timer = setInterval(autoStart, timing);
			
			container.bind({
				'mouseover':function(){
					clearInterval(timer);
				},
				'mouseout':function(){
					timer = setInterval(autoStart, timing);
				}
			});
		}
		
		// Tabs
		if(tabs != null){
			tabs.find('li').each(function(i, elm){
				jQuery(elm).click(function(e){
					
					current = i;
					margin = height*current;
					
					container.stop(false, true).animate({marginTop:-margin}, {duration:duration, easing:'easeInOutSine', 
						complete:function(){
							if(tabs != null){
								tabs.find('li').each(function(j, elm2){
									if(j == current) jQuery(elm2).addClass('active');
									else jQuery(elm2).removeClass('active');
								});
							}
						}
					});
					return false;
				});
			});
		}
	}
}

// Change la taille du texte de la zone de contenu
var currentSize, newSize;
var minSizeH3 = 30;
var minSizeH4 = 16;
var minSizeH5 = 16;
var minSizeP = 11;
var minSizeChapeau = 14;
var minSizeLi = 11;
var minSizeCite = 12;
var minSizeTheadThOdd = 22;
var minSizeTheadThEven = 20;
var minSizeTbodyTh = 18;
var minSizeTbodyTd = 11;
var minSizeLabel = 14;
var minSizeLabel2 = 11;
var sizeOffset = 5; // Variation de taille (+ 5px)
function changeFontSize(type){
	if(type == 'smaller'){
		jQuery('#left-column h3').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeH3) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column h4').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeH4) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column h5').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeH5) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column p').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeP && !$(elm).hasClass('chapeau') || currentSize > minSizeChapeau && $(elm).hasClass('chapeau')) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column li').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeLi) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column cite').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeCite) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column table thead tr th').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeTheadThOdd && $(elm).parent().hasClass('odd') || currentSize > minSizeTheadThEven && $(elm).parent().hasClass('even')) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column table tbody th').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeTbodyTh) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column table tbody td').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize > minSizeTbodyTd) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column label').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if((currentSize > minSizeLabel && (!$(elm).hasClass('checkbox') || !$(elm).hasClass('radio'))) || (currentSize > minSizeLabel2 && ($(elm).hasClass('checkbox') || $(elm).hasClass('radio')))) newSize = currentSize - 1;
			$(elm).css('font-size', newSize+'px');
		});
	}
	else if(type == 'bigger'){
		jQuery('#left-column h3').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeH3+sizeOffset) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column h4').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeH4+sizeOffset) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column h5').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeH5+sizeOffset) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column p').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeP+sizeOffset && !$(elm).hasClass('chapeau') || currentSize < minSizeChapeau+sizeOffset && $(elm).hasClass('chapeau')) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column li').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeLi+sizeOffset) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column cite').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeCite+sizeOffset) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column table thead tr th').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeTheadThOdd+sizeOffset && $(elm).parent().hasClass('odd') || currentSize < minSizeTheadThEven+sizeOffset && $(elm).parent().hasClass('even')) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column table tbody th').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeTbodyTh+sizeOffset) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column table tbody td').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if(currentSize < minSizeTbodyTd+sizeOffset) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
		jQuery('#left-column label').each(function(i, elm){
			currentSize = newSize = parseInt($(elm).css('font-size').replace('px', ''));
			if((currentSize < minSizeLabel+sizeOffset && !$(elm).hasClass('checkbox') && !$(elm).hasClass('radio')) || (currentSize < minSizeLabel2+sizeOffset && ($(elm).hasClass('checkbox') || $(elm).hasClass('radio')))) newSize = currentSize + 1;
			$(elm).css('font-size', newSize+'px');
		});
	}
}

