var Background = new Class({
	options: {
		ImageWidth : 2000,
		ImageHeight : 1749,
		ScreenWidth : window.getWidth().toInt(),
		ScreenHeight : window.getHeight().toInt(),
		url:''
	},
	initialize: function(elm,options){
		this.setOptions(options);
		this.container = $(elm);
		this.bgoverlay  = new Element('div', {id: 'bgoverlay'});
		this.overlay  = new Element('div', {id: 'overlay'});
		this.bg  = new Element('img', {id: 'fullbackground'});
		this.ratio = this.options.ScreenWidth / this.options.ImageWidth;
		this.bgoverlay.setStyles({
			'width':this.options.ScreenWidth +'px',
			'height':this.options.ScreenHeight +'px',
			'position':'absolute',
			'top':'0',
			'left':'0',
			'z-index':'100',
			'overflow':'hidden'
		});
		this.bg .setStyles({
			'width':(this.options.ImageWidth*this.ratio).toInt() +'px',
			'height':(this.options.ImageHeight*this.ratio).toInt()+'px',
			'z-index':'200'
		});
		this.overlay.setStyles({
			'width':this.options.ScreenWidth +'px',
			'height':this.options.ScreenHeight +'px',
			'position':'absolute',
			'top':'0',
			'left':'0',
			'z-index':'300',
			'overflow':'auto'
		});
		this.container.setStyles({
			'position':'relative',
			'margin-left' : ((this.options.ScreenWidth/2)-(955/2)).toInt() +'px'
		});
		this.setBG(this.options.url);
// 		this.elm2resize = {0:'bgoverlay',1:'overlay',2:'bg',3:'container'};
	},
	setBG:function(url){
		this.bg.set('src',url);
	},
	injectBG:function(){
		this.container.inject(this.overlay);
		this.bg.inject(this.bgoverlay);
 		this.overlay.inject(this.bgoverlay);
		this.bgoverlay.inject(document.body);
	},
	resizeBG:function(el){
// 		var elr = (el)?el:0;
// 		var elm2rsz = this.elm2resize[elr];
		this.ratio = window.getWidth() / this.options.ImageWidth;
// 		if(elm2rsz=='bgoverlay'){
// 			var m1 = new Fx.Morph(this.bgoverlay,{'wait': false, 'transition': 'quart:out', 'duration': 100,'onComplete':this.resizeBG(elr+1)}).start({'width':window.getWidth() +'px', 'height': window.getHeight() +'px'});
// 		}
// 		if(elm2rsz=='overlay'){
// 			var m2 = new Fx.Morph(this.overlay,{'wait': false, 'transition': 'quart:out', 'duration': 100,'onComplete':this.resizeBG(elr+1)}).start({'width':window.getWidth() +'px', 'height': window.getHeight() +'px'});
// 		}
// 		if(elm2rsz=='bg'){
// 			var m3 = new Fx.Morph(this.bg,{'wait': false, 'transition': 'quart:out', 'duration': 100,'onComplete':this.resizeBG(elr+1)}).start({'width':(this.options.ImageWidth*this.ratio).toInt()+'px', 'height': (this.options.ImageHeight*this.ratio).toInt() +'px'});
// 		}
// 		if(elm2rsz=='container'){
// 			var m4 = new Fx.Morph(this.container,{'wait': true, 'transition': Fx.Transitions.Expo.easeOut, 'duration': 800}).start({'margin-left': ((window.getWidth() /2)-(955/2)).toInt() +'px'});
// 		}

		var m1 = new Fx.Morph(this.bgoverlay,{'wait': true, 'transition': Fx.Transitions.Expo.easeOut, 'duration': 100}).start({'width':window.getWidth() +'px', 'height': window.getHeight() +'px'});
		var m2 = new Fx.Morph(this.overlay,{'wait': true, 'transition': Fx.Transitions.Expo.easeOut, 'duration': 100}).start({'width':window.getWidth() +'px', 'height': window.getHeight() +'px'});
		var m3 = new Fx.Morph(this.bg,{'wait': true, 'transition': Fx.Transitions.Expo.easeOut, 'duration': 100}).start({'width':(this.options.ImageWidth*this.ratio).toInt()+'px', 'height': (this.options.ImageHeight*this.ratio).toInt() +'px'});
		var m4 = new Fx.Morph(this.container,{'wait': true, 'transition': Fx.Transitions.Expo.easeOut, 'duration': 800}).start({'margin-left': ((window.getWidth() /2)-(955/2)).toInt() +'px'});
	}
});
Background.implement(new Options);
Background.implement(new Events);

function InitPage(){
	var screenHeight = window.getHeight().toInt();
	var screenWidth = window.getWidth().toInt();
 	var headerHeight = $('header').getStyle('height').toInt() + $('top_bar').getStyle('height').toInt()+ $('footer').getStyle('height').toInt()+20;
 	var contentHeight = $('content').getStyle('height').toInt();
  	if((headerHeight+contentHeight) < screenHeight){
		$('content').setStyles({
			height: screenHeight-headerHeight
		});
 	}
	$('content').getElements('p').each(function(el){
		var c = el.get('class');
		if(c==''){
			el.setStyle('clear','left');
		}
	
	});
}
function initMainNav(){
	$(document.body).getElements('li[id^=main-nav]').each(function(el){
		el.fade(0);
	});
	$('top_bar').addEvents({
		'mouseenter':function(){
			$(document.body).getElements('li[id^=main-nav]').each(function(el){
				if(el.get('class')!="active"){
					el.fade(0.7);
				}
			});
		},
		'mouseleave':function(){
			$(document.body).getElements('li[id^=main-nav]').each(function(el){
				el.fade(0);
			});
		}
	});
	$(document.body).getElements('li[id^=main-nav]').each(function(el){
		el.addEvents({
			'mouseenter':function(){
				el.fade(1);
			},
			'mouseleave':function(){
				el.fade(0.7);
			}
		});
	});
}
function initImgLink(){
	$(document.body).getElements('a[class=OSB]').each(function(el){
		el.addEvent('click',function(event){
			if(el.rel!='external'){
				event = new Event(event).stop();
				var openlink = SqueezeBox.fromElement(el, {parse:'rel'} );
			}
		});
	});
}
function checkTeaser(teaser){
	if(is_array(teaser)){
		SqueezeBox.fromElement(window, {url:teaser['url'],handler:teaser['handler'],size:{x: teaser['width'], y: teaser['height']}} );
	}
}
function recommander(page_title,page_url){
	SqueezeBox.fromElement(window, {url:'/misc/recommander.php?t='+page_title+'&u='+page_url,handler:'iframe',size:{x: 400, y: 450}} );
}
function checkBrowser(){
	var browser = navigator.appName;
	var version = navigator.appVersion;
	if(version.indexOf("MSIE")!=-1){
		var tmp = version.split("MSIE")
		var appVers = parseFloat(tmp[1]);
		if(appVers < 7){
			if(confirm('Votre navigateur internet est obsolète et ne permet pas un affichage correct de notre site,\n\nVeuillez installer une version plus récente\n\nou utilisez Mozilla Firefox, Safari, Opéra ou encore Chrome qui sont bien plus performants.\n\nSinon cliquez sur Ok pour mettre à jour Internet Explorer sur Windows Update, sur Annuler pour continuer sur Sessionlibre.com')){
				window.location = 'http://v4.windowsupdate.microsoft.com/fr/default.asp';
			}
		}
	}
}
function is_array( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Legaev Andrey
    // +   bugfixed by: Cord
    // +   bugfixed by: Manish
    // +   improved by: Onno Marsman
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: In php.js, javascript objects are like php associative arrays, thus JavaScript objects will also
    // %        note 1: return true  in this function (except for objects which inherit properties, being thus used as objects),
    // %        note 1: unless you do ini_set('phpjs.objectsAsArrays', true), in which case only genuine JavaScript arrays
    // %        note 1: will return true
    // *     example 1: is_array(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: is_array('Kevin van Zonneveld');
    // *     returns 2: false
    // *     example 3: is_array({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
    // *     returns 3: true
    // *     example 4: is_array(function tmp_a(){this.name = 'Kevin'});
    // *     returns 4: false
 
    var key = '';
 
    if (!mixed_var) {
        return false;
    }
 
    // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    this.php_js.ini = this.php_js.ini || {};
    // END REDUNDANT
 
    if (typeof mixed_var === 'object') {
 
        if (this.php_js.ini['phpjs.objectsAsArrays'] &&  // Strict checking for being a JavaScript array (only check this way if call ini_set('phpjs.objectsAsArrays', 0) to disallow objects as arrays)
            (
            (this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase &&
                    this.php_js.ini['phpjs.objectsAsArrays'].local_value.toLowerCase() === 'off') ||
                parseInt(this.php_js.ini['phpjs.objectsAsArrays'].local_value, 10) === 0)
            ) {
            return mixed_var.hasOwnProperty('length') && // Not non-enumerable because of being on parent class
                            !mixed_var.propertyIsEnumerable('length') && // Since is own property, if not enumerable, it must be a built-in function
                                mixed_var.constructor.name !== 'String'; // exclude String()
        }
 
        if (mixed_var.hasOwnProperty) {
            for (key in mixed_var) {
                // Checks whether the object has the specified property
                // if not, we figure it's not an object in the sense of a php-associative-array.
                if (false === mixed_var.hasOwnProperty(key)) {
                    return false;
                }
            }
        }
 
        // Read discussion at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_is_array/
        return true;
    }
 
    return false;
}
function HideEmailAddress(){
	$(document.body).getElements('a[href^=mailto]').each(function(el){
		$(el).href = el.href.replace('@','_@ntisp@m_');
// 		$(el).innerHTML = el.innerHTML.replace('@',' [at] ');
		el.addEvent('click',function(event){
			event = new Event(event).stop();
			var loc = el.href.replace('_@ntisp@m_','@');
			document.location = loc;
		});
	});
}
function doBookmark(){
	if ( navigator.appName != 'Microsoft Internet Explorer' ){
		window.sidebar.addPanel(document.title,document.location,"");
	}else{
		window.external.addfavorite(document.location,document.title);
	}
}
window.addEvents({
	'domready': function(){
		checkBrowser();
		initMainNav()
 		InitPage();
		fullybackground = new Background('container',{'url':backgroundURL});
		fullybackground.injectBG();
		HideEmailAddress();
 	},	
	'load': function(){
		initImgLink();
		InitPage();
		checkTeaser(teaser);
 	},
	'resize': function(){
		InitPage();
		fullybackground.resizeBG(0);
 	}
 });
