$(document).ready(function() {
	
//	// ----
//	
//	function hauteur_fenetre()
//	{
//	 if (window.innerHeight) return window.innerHeight  ;
//	 else if (document.body && document.body.offsetHeight) return document.body.offsetHeight;
//	 else return 0;
//	}
//	
//	function largeur_fenetre()
//	{
//	 if (window.innerWidth) return window.innerWidth  ;
//	 else if (document.body && document.body.offsetWidth) return document.body.offsetWidth;
//	 else return 0;
//	}
//	
//	var explode = new Date(2010, 5, 1, 18, 30, 0);
//	var remaining = explode - new Date();
//	var timer;
//	
//	function countdown() {
//		var remaining = explode - new Date();
//		if(remaining <= 0) {
//			$('#countdown').effect('explode', {pieces: 24}, 'slow');
//			clearInterval(timer);
//		}
//		var date = new Date(remaining);
//		$('#countdown p').text(date.getMinutes() + ' : ' + date.getSeconds());
//	}
//	
//	if(remaining > 0) {
//	
//		$('body').append('<div id="countdown"><p>countdown</p></div>');
//		$('#countdown').css({
//			position: 'absolute',
//			backgroundColor: '#000000',
//			width: largeur_fenetre() + 'px',
//			height: hauteur_fenetre() + 'px',
//			top: 0,
//			left: 0
//		});
//		$('#countdown p').css({
//			color: '#ffffff',
//			paddingTop: (Math.round(hauteur_fenetre() / 2) - 50) + 'px',
//			textAlign: 'center',
//			fontSize: '96px'
//		});
//		
//		timer = setInterval(function() { countdown(); }, 1000);
//		countdown();
//		
//	}
//	
//	
//	
//	// ----
	
	$('#footer-trademark ul.slider-trademark').sliderForBrand({
		speed: 150, time_auto: 1500
	});
	
	$('#footer-trademark div.content')
		.append('<div class="mask-prev"></div>')
		.append('<div class="mask-next"></div>');
	
//	$('#footer a.login').fancybox({
//		onComplete: initializePopupContent
//	});
	
	$('#footer a.contact').fancybox({
		onComplete: initializePopupContent
	});

//    $('#glassPub').fancybox({
//		'autoDimensions': true,
//                onComplete: initializePopupContent
//	});
//
//    $('#glassSpe').fancybox({
//		'autoDimensions': true,
//                onComplete: initializePopupContent
//	});
/* PAGE QUALITY */
$('a.slug_savoir-verres-vue').fancybox({
                onComplete: initializePopupContent
	});

$('a.slug_savoir-type-verres-vue').fancybox({
			onComplete: initializePopupContent
});
$('a.slug_savoir-progressifs-vue').fancybox({
                onComplete: initializePopupContent
	});

$('a.slug_savoir-matieres-vue').fancybox({
			onComplete: initializePopupContent
});
$('a.slug_savoir-options-vue').fancybox({
                onComplete: initializePopupContent
});

$('a.slug_savoir-specifiques-vue').fancybox({
			onComplete: initializePopupContent
});

/* PAGE OPTIONS */
$('a.slug_solaires-vue').fancybox({
                onComplete: initializePopupContent
	});

$('a.slug_solutions-eblouissement-vue').fancybox({
			onComplete: initializePopupContent
});
$('a.slug_eblouissement-protection-uv-vue').fancybox({
                onComplete: initializePopupContent
	});

$('a.slug_cocktail-vue').fancybox({
			onComplete: initializePopupContent
});
$('a.slug_solaires-sport').fancybox({
                onComplete: initializePopupContent
});

/* PAGE INNOVATIONS */
$('a.slug_innovations').fancybox({
                onComplete: initializePopupContent
	});

$('a.slug_innovations-verres-correcteurs').fancybox({
			onComplete: initializePopupContent
});
$('a.slug_innovations-verres-specifiques').fancybox({
                onComplete: initializePopupContent
	});

$('a.slug_innovations-traitements-verres').fancybox({
			onComplete: initializePopupContent
});
$('a.slug_innovations-solaires').fancybox({
                onComplete: initializePopupContent
});
$('a.slug_innovations-contactologie').fancybox({
			onComplete: initializePopupContent
});
$('a.slug_innovations-montures').fancybox({
                onComplete: initializePopupContent
});

/* PAGE CONTACTO (LENTILLES) */
$('a.slug_quelques-chiffres-vue').fancybox({
                onComplete: initializePopupContent
});

//	$('#footer-trademark h3 a').fancybox({
//		onComplete: initializePopupContent
//	});
		
	var gameLink = $('#content div.pushes div.game a, #news-game-link');
	gameLink.fancybox({
		onComplete: initializePopupContent,
		onStart: function() { $('#east-banner').hide(); },
		onClosed: function() { $('#east-banner').show(); }
	});
	if(gameLink.hasClass('auto-open')) gameLink.click();
	
//	$('#footer a.sitemap').fancybox({
//		onComplete: initializePopupContent
//	});
	
	if(!$.browser.safari)
		Cufon.replace('#header-menu li a span', {'font-family': 'avantgarde'});
	
	Cufon.replace('#north div.summary strong', {'font-family': 'avantgarde'});
	Cufon.replace('#north div.summary h2', {'font-family': 'avantgarde'});
	Cufon.replace('#east div.flatpage div.block strong', {'font-family': 'avantgarde'});
	Cufon.replace('#east div.flatpage div.block h2', {'font-family': 'avantgarde'});
	Cufon.replace('#footer-sitemap h3', {'font-family': 'avantgarde'});
	Cufon.replace('#footer-trademark h3', {'font-family': 'avantgarde'});
	
	setTimeout(function() { fixHeaderMenu($('#global')); }, 10);
	initializeNewsletter();
	initializeMenu();
	initializeVeritas();
	initializeBanners();
	
});

function initializeBanners()
{
	var banner = $('#east > div.flatpage > img[alt]');
	
	if(! banner.length) return;
	
	var regexp = /(.*)visu_(.*)\.jpg/i;
	var source = banner.attr('src').replace(regexp, '$1$2.swf');
	
	banner.attr('id', 'east-banner');
	var my_cars= new Array();
	my_cars["wmode"]="transparent";

	swfobject.embedSWF(source, 'east-banner', 277, 310, "9.0.0","expressInstall.swf", null, my_cars);
}

function fixHeaderMenu(container, id)
{
	var current = $('#header-menu li.current');
	var top = 126;
	
	container.append(
		'<div id="header-shadow" style="background:transparent url(/css/front/global/header-menu-shadow.png) ' +
		'repeat-x;position:absolute; left:0; top:' + top + 'px; width:960px; height:13px;"></div>'
	);

	if(current.length) {
		var left = current.position().left;
		var width = current.width();
		
		$('#header-shadow').append(
			'<img style="float:left; margin-left:' + left + 'px" src="/css/front/global/header-menu-current-left.png" width="6" height="6" />' +
			'<img style="float:left;" src="/css/front/global/header-menu-current-center.png" width="' + (width - 12) + '" height="6" />' +
			'<img style="float:left;" src="/css/front/global/header-menu-current-right.png" width="6" height="6" />'
		);
	}
	
	if($.browser.msie && $.browser.version=="6.0") {
		$('#header-shadow').supersleight();
	}
}

function initializePopupContent()
{
	$.fancybox.resize();
	Cufon.replace('#fancybox-inner h1', {'font-family': 'avantgarde'});
	Cufon.replace('#fancybox-inner h2', {'font-family': 'avantgarde'});
	Cufon.replace('#fancybox-inner label', {'font-family': 'avantgarde'});
	
	$('#fancybox-inner').css('left','0px');
	$('#fancybox-inner').css('top','0px');
	$('#fancybox-inner').css('bottom','0px');

	$('#fancybox-inner form').submit(function() {
		data = false;
		if($('#contact_message_object_id').length) {
			var data = {
				'contact_message[object_id]': $('#contact_message_object_id', this).val(),
				'contact_message[firstname]': $('#contact_message_firstname', this).val(),
				'contact_message[lastname]': $('#contact_message_lastname', this).val(),
				'contact_message[email]': $('#contact_message_email', this).val(),
				'contact_message[message]': $('#contact_message_message', this).val(),
				'contact_message[id]': $('#contact_message_id', this).val(),
				'contact_message[optician_id]': $('#game_customer_optician_id', this).val(),
				'contact_message[_csrf_token]': $('#contact_message__csrf_token', this).val()
			};
		} else if($('#game_customer_civility').length) {
			var data = {
				'game_my_customer[id]': $('#game_my_customer_id', this).val(),
				'game_my_customer[civility]': $('#game_my_customer_civility', this).val(),
				'game_my_customer[firstname]': $('#game_my_customer_firstname', this).val(),
				'game_my_customer[lastname]': $('#game_my_customer_lastname', this).val(),
				'game_my_customer[email]': $('#game_my_customer_email', this).val(),
				'game_my_customer[birthyear]': $('#game_my_customer_birthyear', this).val(),
				'game_my_customer[zipcode]': $('#game_my_customer_zipcode', this).val(),
				'game_my_customer[optician_id]': $('#game_my_customer_optician_id', this).val(),
				'game_my_customer[glasses]': $('#game_my_customer_glasses_1', this).attr('checked') ? '1' : ($('#game_my_customer_glasses_0', this).attr('checked') ? '0' : ''),
				'game_my_customer[_csrf_token]': $('#game_my_customer__csrf_token', this).val()
			};
		} else {
			var data = {
				'game_user[id]': $('#game_user_id', this).val(),
				'game_user[firstname]': $('#game_user_firstname', this).val(),
				'game_user[lastname]': $('#game_user_lastname', this).val(),
				'game_user[email]': $('#game_user_email', this).val(),
				'game_user[birthyear]': $('#game_user_birthyear', this).val(),
				'game_user[zipcode]': $('#game_user_zipcode', this).val(),
				'game_user[city]': $('#game_user_city', this).val(),
				'game_user[location]': $('#game_user_location', this).val(),
				'game_user[phone]': $('#game_user_phone', this).val(),
				'game_user[answer]': $('#game_user_answer', this).val(),
				'game_user[newsletter]': $('#game_user_newsletter:checked', this).val(),
				'game_user[optician_id]': $('#game_user_optician_id', this).val(),
				'parrainage_1': $('#parrainage_1', this).val(),
				'parrainage_2': $('#parrainage_2', this).val(),
				'parrainage_3': $('#parrainage_3', this).val(),
				'parrainage_4': $('#parrainage_4', this).val(),
				'parrainage_5': $('#parrainage_5', this).val(),
				'game_user[_csrf_token]': $('#game_user__csrf_token', this).val()
			};
		}
		if(data) {
			$.post($(this).attr('action'), data, function(html) {
				$('#fancybox-inner').html(html);
				initializePopupContent();
			});
		}
		return false;
	});
}

function initializeNewsletter()
{
	$('#header-search, #footer-newsletter').smallform();

	var container = $('#footer-newsletter');

	container.find('form').submit(function() {
		$.post('/newsletter', {
			'newsletter[id]': $('#newsletter_id').val(),
			'newsletter[email]': $('#newsletter_email').val(),
			'newsletter[_csrf_token]': $('#newsletter__csrf_token').val()
		}, function(html) {
			container.replaceWith(html);
			initializeNewsletter();
		});

		return false;
	});
}

function initializeMenu()
{
	var delay = 150;
	
	$('#header-menu li')
		.mouseenter(function() { $('#north div.rollovers div.summary.' + $(this).attr('class')).stop().animate({top: 0}, delay); })
		.mouseleave(function() { $('#north div.rollovers div.summary.' + $(this).attr('class')).stop().animate({top: '-200px'}, delay); });
}

function initializeVeritas()
{
	var logo = $('#header-presentation a.veritas img');
	$('#header-presentation').append('<p class="veritas">' + logo.attr('longdesc') + '</p>');
	var info = $('#header-presentation p.veritas');
	Cufon.replace(info.get(0), {'font-family': 'avantgarde'});
	info.show();
//	logo.hover(function() { info.show(); }, function() { info.hide(); });
}

