/**
 * @title		yBox
 * @vesrion		0.0.1
 * @author		ylo
 */
// js class on html tag
document.getElementsByTagName("html")[0].className = "js";


/**
 * @section		png fix configuration
 */
pxPath = 'tpl/img/common/visu/px.gif';


// general inits
$(document).ready(function () {
	
	// init isIE
	manageIsIE.init();
	
	// png fix
	fixPng();
	
});


/**
 * @section		create global var ieIE 6, 7 & 8
 */
var isIE = false;
var manageIsIE = {
	init: function () {
		if (jQuery.browser.msie) {
			isIE = true;
			if(parseInt(jQuery.browser.version) == 6) {
				isIE = 6;
			} else if (parseInt(jQuery.browser.version) == 7) {
				isIE = 7;
			} else if (parseInt(jQuery.browser.version) == 8) {
				isIE = 8;
			}
		}
	}
};

/**
 * @section		manage forms
 */
var manageForm = {
	onFocusEmptyField: function (obj, value) {
		
		$(obj).bind('focus', function () {
			var elmt = $(this);
			var curentValue = elmt.val();
			if(curentValue == value) {
				elmt.val('');
			}
		});
		
		$(obj).bind('blur', function () {
			var elmt = $(this);
			var curentValue = elmt.val();
			if(curentValue == '') {
				elmt.val(value);
			}
		});
		
		if(value == undefined) {alert('Unknown value');}
		
	}
};

/**
 * @section		fix png images
 * @affect		IE6
 */
function fixPng(elmt) {

	if(isIE == 6) {
		if(elmt) {
			var container = $(elmt);
		} else {
			var container = $('html');
		}
		var oImage = $('img', container);
		
		if(!oImage.length)  return;
		for(var i=0;i<oImage.length;i++) {
			if(oImage[i].src.indexOf('.png') > -1) {
				with(oImage[i].style) {
					background = 'none';
					filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + oImage[i].src + '", sizingMethod="image")';
				}
				oImage[i].src = pxPath;
			}
		}
	}
	
}

/**
 * @section		drop down menu (main menu)
 */
var manageSlideDownMenu = {
	config: {
		duration: 200
	},
	enter: function (obj) {
		// WARNING : soulever l'événement sur l'élément LI, pas sur le A
		$(obj).addClass('select');
		var $this = $('> ul', obj); 
		$this.css({display:'block'});/*.slideDown(manageSlideDownMenu.config.duration, function () {
			//$(this).removeAttr('style')
			manageFullscreenBg.update();
		});*/
		
	},
	leave: function (obj, config) {
		switch (config) {
			case 'slide' :
				$('> ul', obj).stop({clearQueue:true, gotoEnd:true}).slideUp(manageSlideDownMenu.config.duration, function () {
					//$(this).removeAttr('style')
					$(obj).removeClass('select');
					
				});
			break;
			default :
				//$('> ul', obj).stop({clearQueue:true, gotoEnd:true}).removeAttr('style');
				$('> ul', obj).stop({clearQueue:true, gotoEnd:true}).css({'display': 'none'});
				$(obj).removeClass('select');
		}
	}
};

/**
 * @section		carousel
 */
var manageCarousel = {
	init: function (obj) {
		
		if(!$('.wrapper ul li:first', obj).hasClass('first')) {
			$('.wrapper ul li:first', obj).addClass('first')
		}
		
		manageCarousel.updateContainerWidth(obj);
		
		var nbItem = $('> * > ul > li', obj).length;
		if(manageCarousel.config.nbMove >= nbItem) {
			$('.prevArrow, .nextArrow', obj).css({'visibility': 'hidden'});
		} else {
			$('.nextArrow', obj).click(function (e) {
				manageCarousel.next($(this).parent());
				return false;
			});
			
			$('.prevArrow', obj).click(function (e) {
				manageCarousel.previous($(this).parent());
				return false;
			});
		}
				
	},
	config: {
		'duration': 500,
		'auto': false,
		'waitToMove':4000,
		'pauseDuration':10000,
		'nbMove':6
	},
	updateContainerWidth: function (obj) {
		var totalWidth = 0;
		$('.wrapper ul li', obj).each(function () {
			totalWidth = totalWidth + manageCarousel.itemTotalWidth(this);
		});
		
		$('.wrapper > ul', obj).css({
			width: totalWidth+'px'
		});
	},
	play: function (elmt) {
		
		var obj = $(elmt);
		manageCarousel.next(obj);
		var cicleDuration = manageCarousel.config.waitToMove + manageCarousel.config.duration;
		function moveCarousel() {
			
			manageCarousel.next(obj);
		}
		cicle = setInterval(moveCarousel, cicleDuration);
		
	},
	pause: function (elmt) {
		
		var obj = $(elmt);
		function restart() {
			manageCarousel.play(obj);
		}
		
		window.clearInterval(cicle);
		if(typeof(pauseCicle) != 'undefined') {
			window.clearTimeout(pauseCicle);
		}
		pauseCicle = window.setTimeout(restart, manageCarousel.config.pauseDuration);
		
	},
	itemTotalWidth: function (obj, test) {
		var itemWidth 		= parseInt(String($(obj).width()).replace('px', '').replace(' ', ''));
		var leftMargin 		= parseInt(String($(obj).css('margin-left')).replace('px', '').replace(' ', ''));
		var rightMargin 	= parseInt(String($(obj).css('margin-right')).replace('px', '').replace(' ', ''));
		var leftPadding 	= parseInt(String($(obj).css('padding-left')).replace('px', '').replace(' ', ''));
		var rightPadding	= parseInt(String($(obj).css('padding-right')).replace('px', '').replace(' ', ''));
		var totalWidth = itemWidth + leftMargin + rightMargin + leftPadding + rightPadding;
		return totalWidth;
	},
	previous: function (obj) {
		if(!obj.hasClass('move')) {
			obj.addClass('move');
			
			var nbMove = manageCarousel.config.nbMove;
			
			//var distance = manageCarousel.itemTotalWidth($('.wrapper > ul > :last', obj));
			
			var distance = 0;
			var nbMove = manageCarousel.config.nbMove;
			var nbTotalItem = $('.wrapper > ul > li', obj).length;
			for(var i = 0; i < nbMove; i++) {
				// todo - calcul si le nombre d'item à déplacer est plus grand que le nombre d'item dispo
				distance = distance + manageCarousel.itemTotalWidth($('.wrapper > ul > li:eq('+(nbTotalItem - i - 1)+')', obj));
			}
			
			var lastItems = $('.wrapper > ul > li:gt('+(nbTotalItem - nbMove - 1)+')', obj);
			$('.wrapper > ul > li:gt('+(nbTotalItem - nbMove - 1)+')', obj).remove();
			
			$('.wrapper > ul', obj).css({'left':0}).prepend(lastItems);
			
			$('.wrapper > ul > .first', obj).removeClass('first');
			$('.wrapper > ul > :first', obj).addClass('first');
			
			$('.wrapper > ul', obj).css({'left': '-'+distance+'px'}).animate({
				'left': '0px'
			}, manageCarousel.config.duration, function () {
				obj.removeClass('move');
			});
			
		}
	},
	next: function (obj) {
		if(!obj.hasClass('move')) {
			obj.addClass('move');
			
			var distance = 0;
			var nbMove = manageCarousel.config.nbMove;
			for(var i = 0; i < nbMove; i++) {
				// todo - calcul si le nombre d'item à déplacer est plus grand que le nombre d'item dispo
				distance = distance + manageCarousel.itemTotalWidth($('.wrapper > ul > li:eq('+i+')', obj));
			}
			
			$('.wrapper > ul', obj).animate({
				'left': '-'+distance+'px'
			}, manageCarousel.config.duration, function () {
				
				var firstItems = $('.wrapper > ul > li:lt('+nbMove+')', obj);
				$('.wrapper > ul > li:lt('+nbMove+')', obj).remove();
				
				$('.wrapper > ul', obj).css({'left':0}).append(firstItems);
				
				$('.wrapper > ul > .first', obj).removeClass('first');
				$('.wrapper > ul > :first', obj).addClass('first');
				
				obj.removeClass('move');
			});
			
		}
	}
};
var manageFullscreenBg = {
	maxWidth: 0,
	maxHeight: 0,
	htmlWidth: 0,
	bgRatio: 0,
	init: function () {
		
		$(window).bind('resize', function () {
			manageFullscreenBg.update();
		});
		
		$('#bgWrapper img').load(function () {
			manageFullscreenBg.update();
		});
		
		manageFullscreenBg.update();
		
	},
	update: function () {
		$('#bgWrapper').hide();
		manageFullscreenBg.setValues();
		$('#bgWrapper').removeAttr('style');
		manageFullscreenBg.appliValues();
	},
	setValues: function () {
		
		var maxWidth = 0;
		var maxHeight = 0;
		var htmlWidth = 0;
		
		var bodyWidth = document.body.clientWidth;
		//alert('bodyWidth : '+bodyWidth);
		var bodyHeight = document.body.clientHeight;
		
		var windowWidth = $(window).width();
		//alert('windowWidth : '+windowWidth);
		var windowHeight = $(window).height();
		
		var documentWidth = $(document).width();
		//alert('documentWidth : '+documentWidth);
		var documentHeight = $(document).height();
		
		var htmlWidth = $('#wrapper').width();
		//alert('htmlWidth : '+htmlWidth);
		var htmlHeight = $('#wrapper').height();
		manageFullscreenBg.htmlWidth = htmlWidth;
		
		if(maxWidth < bodyWidth) {maxWidth = bodyWidth;}
		if(maxWidth < windowWidth) {maxWidth = windowWidth;}
		if(maxWidth < documentWidth) {maxWidth = documentWidth;}
		if(maxWidth < htmlWidth) {maxWidth = htmlWidth;}
		
		//alert('maxWidth : '+maxWidth);
		
		if(maxHeight < bodyHeight) {maxHeight = bodyHeight;}
		if(maxHeight < windowHeight) {maxHeight = windowHeight;}
		if(maxHeight < documentHeight) {maxHeight = documentHeight;}
		if(maxHeight < htmlHeight) {maxHeight = htmlHeight;}
		
		manageFullscreenBg.maxWidth = maxWidth;
		manageFullscreenBg.maxHeight = maxHeight;
		
	},
	appliValues: function () {
		
		// set container
		if(manageFullscreenBg.maxWidth <= manageFullscreenBg.htmlWidth) {
			$('#bgWrapper').css({'width': manageFullscreenBg.maxWidth+'px'});
		} else {
			$('#bgWrapper').css({'width': '100%'});
		}
		//$('#bgWrapper').css({'width': manageFullscreenBg.maxWidth+'px'});
		$('#bgWrapper').css({'height': manageFullscreenBg.maxHeight});
		
		// set img
		$('#bgWrapper img').css({'width': manageFullscreenBg.maxWidth+'px', 'height':'auto'});
		var imgHeight = parseInt($('#bgWrapper div').height());
		if(imgHeight < manageFullscreenBg.maxHeight) {
			$('#bgWrapper img').css({'width': 'auto', 'height': manageFullscreenBg.maxHeight+'px'});
		}
		
	}
};

// vertical center
var vCenter = {
	init: function () {
		$(window).bind('resize', function () {
			// vertical center
			vCenter.update();
		});
		vCenter.update();
	},
	update: function () {
		var contentWrapper = $('#wrapper');
		var paddingTop = contentWrapper.css('padding-top');
		var marginTop = contentWrapper.css('margin-top');
		contentWrapper.css({'margin-top': 0, 'padding-top': 0});
		if(marginTop) {
			marginTop = parseInt(String(marginTop).replace('px', ''));
		} else {
			marginTop = 0;
		}
		
		if(paddingTop) {
			paddingTop = parseInt(String(paddingTop).replace('px', ''));
		} else {
			paddingTop = 0;
		}
		TotalMarginPadding = paddingTop + marginTop
		
		var contentHeight = parseInt(String(contentWrapper.height()).replace('px', ''));
		var vOffset = contentHeight/2;
		var pageHeight = parseInt(String($(window).height()).replace('px', ''));
		var finalVal = ((pageHeight - contentHeight) / 2);
		if(finalVal < TotalMarginPadding) {finalVal = TotalMarginPadding;}
		
		contentWrapper.css({
			'padding-top': finalVal+'px'
		});
	}
};