$('html').removeClass('no-js').addClass('js');
if($.browser.webkit){$('body').addClass('webkit');}
if($.browser.opera){$('body').addClass('opera');}
if($.browser.mozilla){$('body').addClass('mozilla');}
var isMobile = false;
if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)){ isMobile = true;}
if(isMobile){$('body').addClass('mobile');}
var lang = navigator.language;
	
var oneDior = {
	containerWidth : null, 
	containerHeight : null, 
	bkgdWidth : null, 
	bkgdHeight : null, 
	minWidth : 1000, 
	maxWidth : 1280,
	maxBkgdWidth : 1280,
	minHeight : 595, 
	maxHeight : 720,
	maxBkgdHeight : 720,
	//rapportMin : null,
	rapportMax : null,
	init : function(){
		//this.rapportMin = this.minHeight / this.minWidth;
		this.rapportMax = this.maxHeight / this.maxWidth;
		//this.containerResize();
		
		this.positionningSite();
	},
	containerResize : function(){
		var docWidth = document.body.offsetWidth || document.documentElement.offsetWidth;
		var docHeight = window.innerHeight || document.documentElement.clientHeight;
		
		// Dimensionnement
		if(docWidth >= this.maxWidth && docHeight >= this.maxHeight){
			this.containerHeight = this.maxHeight;
			this.containerWidth = this.maxWidth;
		}else if(docWidth <= this.minWidth || docHeight <= this.minHeight){
			this.containerHeight = this.minHeight;
			this.containerWidth = this.minWidth;
		}else{
			var tmpHeight = Math.min(Math.max(this.minHeight,docHeight),this.maxHeight);
			if(tmpHeight == this.minHeight){
				this.containerHeight = this.minHeight;
				this.containerWidth = this.minWidth;
			}else{
				var tmpWidthFromHeight =  Math.min(Math.max(this.minWidth,Math.floor(tmpHeight/this.rapportMax)),this.maxWidth);
			
				if(tmpWidthFromHeight == this.minWidth){
					this.containerHeight = this.minHeight;
					this.containerWidth = this.minWidth;
				}else if(tmpWidthFromHeight>docWidth){
					var tmpWidth = Math.min(Math.max(this.minWidth,docWidth),this.maxWidth);
					
					if(tmpWidth == this.minWidth){
						this.containerHeight = this.minHeight;
						this.containerWidth = this.minWidth;
					}else{
						var tmpHeightFromWidth =  Math.min(Math.max(this.minHeight,Math.floor(tmpWidth*this.rapportMax)),this.maxHeight);
						this.containerHeight = tmpHeightFromWidth;
						this.containerWidth = tmpWidth;
					}
				}else{
					this.containerHeight = tmpHeight;
					this.containerWidth = tmpWidthFromHeight;
				}
			}
		}
		
		$('#container').css('height',this.containerHeight+"px");
		$('#container').css('width',this.containerWidth+"px");
		$('#main').css('height',(this.containerHeight-$('#header').height()-$('#footer').height())+"px");
		
		// Positionnement
		if(docHeight<=this.minHeight) {
			$('#container').css('top',"0px");
			$('#container').css('marginTop',"0px");
		} else {
			$('#container').css('top',"50%");
			$('#container').css('marginTop',"-"+Math.ceil(this.containerHeight/2)+"px");
		}
		if(docWidth<=this.minWidth) {
			$('#container').css('left',"0px");
			$('#container').css('marginLeft',"0px");
		} else {
			$('#container').css('left',"50%");
			$('#container').css('marginLeft',"-"+Math.ceil(this.containerWidth/2)+"px");
		}
		
		if($('#background')){
			this.backgroundResize();
		}
	},
	backgroundResize : function(){
		var docWidth = document.body.offsetWidth || document.documentElement.offsetWidth;
		var docHeight = window.innerHeight || document.documentElement.clientHeight;
		
		// Dimensionnement
		if(docWidth >= this.maxBkgdWidth && docHeight >= this.maxBkgdHeight){
			this.bkgdHeight = this.maxBkgdHeight;
			this.bkgdWidth = this.maxBkgdWidth;
		}else if(docWidth <= this.minWidth || docHeight <= this.minHeight){
			this.bkgdHeight = this.minHeight;
			this.bkgdWidth = this.minWidth;
		}else{
			var tmpHeight = Math.min(Math.max(this.minHeight,docHeight),this.maxBkgdHeight);
			var tmpWidthFromHeight =  Math.min(Math.max(this.minWidth,Math.floor(tmpHeight/this.rapportMax)),this.maxBkgdWidth);
			
			if(tmpWidthFromHeight == this.minWidth){
				this.bkgdHeight = this.minHeight;
				this.bkgdWidth = this.minWidth;
			}else if(tmpWidthFromHeight>docWidth){
				var tmpWidth = Math.min(Math.max(this.minWidth,docWidth),this.maxBkgdWidth);
				
				if(tmpWidth == this.minWidth){
					this.bkgdHeight = this.minHeight;
					this.bkgdWidth = this.minWidth;
				}else{
					var tmpHeightFromWidth =  Math.min(Math.max(this.minHeight,Math.floor(tmpWidth*this.rapportMax)),this.maxBkgdHeight);
					this.bkgdHeight = tmpHeightFromWidth;
					this.bkgdWidth = tmpWidth;
				}
			}else{
				this.bkgdHeight = tmpHeight;
				this.bkgdWidth = tmpWidthFromHeight;
			}
		}
		
		$('#background').css('height',this.bkgdHeight+"px");
		$('#background').css('width',this.bkgdWidth+"px");
		
		// Positionnement
		if(docHeight<=this.minHeight) {
			$('#background').css('top',"0px");
			$('#background').css('marginTop',"0px");
		} else {
			$('#background').css('top',"50%");
			$('#background').css('marginTop',"-"+Math.ceil(this.bkgdHeight/2)+"px");
		}
		if(docWidth<=this.minWidth) {
			$('#background').css('left',"0px");
			$('#background').css('marginLeft',"0px");
			if($('#header_checkout').length){
				$('#header').css('left',"0px");
				$('#header').css('marginLeft',"10px");
				$('#header_checkout').css('left',"0px");
				$('#header_checkout').css('marginLeft',"10px");
			}
			if ($('#main_checkout').length) {
				$('#main_checkout').css('left',"0px");
				$('#main_checkout').css('marginLeft',"10px");
			} 
		} else {
			$('#background').css('left',"50%");
			$('#background').css('marginLeft',"-"+Math.ceil(this.bkgdWidth/2)+"px");
			if($('#header_checkout').length){
				$('#header').css('left',"50%");
				$('#header').css('marginLeft',"-490px");
				$('#header_checkout').css('left',"50%");
				$('#header_checkout').css('marginLeft',"-490px");
			}
			if ($('#main_checkout').length) {
				$('#main_checkout').css('left',"50%");
				$('#main_checkout').css('marginLeft',"-490px");
			} 
		}	
	},
	positionningSite : function(){
		var docWidth = document.body.offsetWidth || document.documentElement.offsetWidth;
		var docHeight = window.innerHeight || document.documentElement.clientHeight;
		
		if(docHeight<=this.minHeight) {
			$('#container').css('top',"0px");
			$('#container').css('marginTop',"0px");
		} else {
			$('#container').css('top',"50%");
			$('#container').css('marginTop',"-298px");
		}
		if(docWidth<=this.minWidth) {
			$('#container').css('left',"0px");
			$('#container').css('marginLeft',"0px")
		} else {
			$('#container').css('left',"50%");
			$('#container').css('marginLeft',"-500px");
		}
		
		if($('#background')){
			this.backgroundResize();
		}	
	}
}

function video(){
	var a=document.createElement("video"), b=!!a.canPlayType;
	if(b){
		b=new Boolean(b);
		b.ogg=a.canPlayType('video/ogg; codecs="theora"');
		b.h264=a.canPlayType('video/mp4; codecs="avc1.42E01E"');
		b.webm=a.canPlayType('video/webm; codecs="vp8, vorbis"');
	}
	return b;
}

var global_html5Params = null;
var global_flashvars = null;
var global_params = null;
var global_attributes = null;

function initWebsite(){

}

function updateSubtitle( captionContent, region ){
    $('#'+region).html(captionContent);
}

function buildPlayer(){
	var html5Params = global_html5Params;
	var flashvars 	= global_flashvars;
	var params 		= global_params;
	var attributes 	= global_attributes;

	if(CTRL_MMM._isIpadOrIphone){
		var idVideo = flashvars.video;
	}else{
		var idVideo = flashvars.video;
	}
	
	CTRL_MMM.buildPlayer(attributes.id,idVideo,$('#'+attributes.id).width(),$('#'+attributes.id).height(),html5Params,swfPlayer,flashvars,params,attributes);
	//CTRL_MMM.buildHTML5Context(attributes.id,idVideo,$('#'+attributes.id).width(),$('#'+attributes.id).height(),html5Params);
}

$(window).resize(function() {
	oneDior.positionningSite();
});

//$(document).ready(function() {
	oneDior.init();
	
	/*$('.mobile label').live('click', function(){
		$('input[id='+$(this).attr('for')+']').trigger('click');
	});*/
	
	$('a.close').live('click', function(event){
		event.preventDefault();
		$(this).parent().hide();
	});
	
	$('#bgInlinePopup a.close').click(function(event){
		event.preventDefault();
		$(this).parent().hide();
		$('#background').removeClass('popin');
	});
	
	$('.radio, .check').children('input[type=radio]').click(function() {
		var nameInput = $(this).attr('name');
		$('input[name='+nameInput+']').parent('.radio, .check').removeClass('check_on').addClass('check_off');
		
		if ($(this).is(':checked')) {
			$(this).parents('.radio, .check').removeClass('check_off').addClass('check_on');
		} else {
			$(this).parents('.radio, .check').removeClass('check_on').addClass('check_off');
		}
	});
	
	$('.check').children('input[type=checkbox]').click(function() {
		if ($(this).is(':checked')) {
			$(this).parents('.check').removeClass('check_off').addClass('check_on');
		} else {
			$(this).parents('.check').removeClass('check_on').addClass('check_off');
		}
	});
	
	$('a.deconnexion, a.connected').hover(
		function(){
			$('#helloboy').hide();
			$('#byegoodboy').show();
		},
		function(){
			$('#byegoodboy').hide();
			$('#helloboy').show();
		}
	);
	
	$('.linkaction').click(function(){
		linkAction($(this).data('action'),$(this).data('url'),$(this).data('font'),$(this).data('node_id'),$(this).data('format'),$(this).data('vignette1'),$(this).data('vignette2'),$(this).data('vignette3'),$(this).data('vignette4'),$(this).data('vignette5'),$(this).data('label'),$(this).data('detailurl'),$(this).data('hasassoc'),$(this).data('masterpiece'),$(this).data('xml'),$(this).data('sub'));
	});
//});
