function fixLayout () {

	var bgPosition = Math.round($(window).width()/2+154-457);
	if (bgPosition < 0) { bgPosition = 0; }
	$('body').css({
		'background-position'	: bgPosition + 'px 0'
	});

	var headerSide = Math.round(($(window).width()-960)/2);
	if (headerSide < 0) { headerSide = 0; }
	$('#header>.left, #header>.right').css({
		'width' : headerSide + 'px'
	});
	
	$('.mail-wrapper, .video-wrapper').height(Math.max(
		$('.video-wrapper>.video:last').height() + 100,
		$(document).height(),
		$(window).height(),
		$('body').height(),
		$('html').height()
	));
}

function setOn (rel) {
	$('body>#header>.front').find('[rel=' + rel + ']').addClass('on');
}

$(function () {
	fixLayout(); $(window).resize(function () { fixLayout(); });
	$('a[rel=_nw], .mceContentBody a:not(.fancybox)').live('click',function (e) {
		e.preventDefault();

		if ($(this).attr('href').match(/^http:\/\/(www\.)?(montreal)?completementcirque\.(com|ca)/i) || $(this).attr('href').substr(0,1) == '/') {
			location.href = $(this).attr('href');
		}

		else if ($(this).attr('href').substr(0,6) == 'mailto') {
			location.href = $(this).attr('href');
		}

		else {
			if(typeof _gaq != 'undefined'){
				_gaq.push(['_trackEvent', 'Offsite Links', 'Click', $(this).attr('href')]);
			
				// tracking billeterie
				if ($(this).attr('href').match(/^https:\/\/billet.latohu.net/i)) {
					_gaq.push(['_link', $(this).attr('href')]);
				}
			}

			window.open($(this).attr('href'));
		}
	});

	$('.fancybox').attr('rel', 'gallery').fancybox({
		cyclic: true,
		overlayOpacity: 0.6,
		overlayColor: '#000',
		titleShow: false
	});

// collapsable places in the about section
	$('.about-places>li>.title>.collapser').click(function (e) {
		$(this).closest('li').siblings('li').children('.collapsable').slideUp(200, function () {
			$(this).siblings('.title').children('.collapser').text('+');
		});
		
		$(this).parent().siblings('.collapsable').slideToggle(200, function () {
			$(this).siblings('.title').children('.collapser').text($(this).siblings('.title').children('.collapser').text() == '+' ? '-' : '+');			
		});

	});
	
// share the current page.
// on facebook
	// $('ul.share>li.facebook').click(function () {
	// 	window.open('http://facebook.com/sharer.php?u=' + encodeURIComponent(location.href) + '&t=' + encodeURIComponent(document.title) ,'sharer','toolbar=0,status=0,width=626,height=436');
	// });
	
// on twitter
	// $('ul.share>li.twitter').click(function () {
	// 	(function(){window.twttr=window.twttr||{};var D=550,A=450,C=screen.height,B=screen.width,H=Math.round((B/2)-(D/2)),G=0,F=document,E;if(C>A){G=Math.round((C/2)-(A/2))}window.twttr.shareWin=window.open('http://twitter.com/share','','left='+H+',top='+G+',width='+D+',height='+A+',personalbar=0,toolbar=0,scrollbars=1,resizable=1');E=F.createElement('script');E.src='http://platform.twitter.com/bookmarklets/share.js?v=1';F.getElementsByTagName('head')[0].appendChild(E)}());
	// });
	
// by mail
	// get elements default values.
	mail_default_values = {
		'mail-from'		: $('.mail-wrapper :input[name=mail-from]').val(),
		'mail-from-name': $('.mail-wrapper :input[name=mail-from-name]').val(),
		'mail-to'		: $('.mail-wrapper :input[name=mail-to]').val(),
		'mail-to-name' 	: $('.mail-wrapper :input[name=mail-to-name]').val()
	}
	
	$('.mail-wrapper :input').live('focus', function(e) {
		if ($(this).val() == mail_default_values[$(this).attr('name')]) {
			$(this).val(''); 
		}
	});
	
	$('.mail-wrapper :input').live('blur', function(e) {
		if ($(this).val() == '') {
			$(this).val(mail_default_values[$(this).attr('name')]); 
		}
	});
	
	$('.share span.mail').live('click', function () {
		var m = $('.mail-wrapper').clone()

		if ($(this).closest('.video-wrapper').size() > 0) {
			$('<input type="hidden" name="id_video" value="' + $(this).attr('rel') + '" />').appendTo(m.find('form>.mail-content'));
		}
		
		m.appendTo('body').show();
	});
	
	// share by mail specific
	// cancel
	$('.mail-wrapper :input[rel="cancel"]').live('click', function () {
		$(this).closest('.mail-wrapper').remove();
	});
	
	// retry
	$('.mail-wrapper :input[rel="retry"]').live('click', function () {
		$(this).closest('form').children('div.mail-error').hide();
		$(this).closest('form').children('div.mail-content').show();
	});
	
	// send
	$('.mail-wrapper :input[type="submit"]').live('click', function (e) {
		e.preventDefault();
		
		var f = $(this).closest('form');
		
		$.ajax({
			url: '/webservices/mail',
			type: 'POST',
			data: f.serialize() + '&mail-page-link=' + encodeURIComponent(location.href) + '&mail-page-title=' + encodeURIComponent(document.title),
			success: function (data) {
				var d = $.parseJSON(data);
				var m = $('.mail-wrapper:last');
				
				m.find('.mail-content').hide();
				
				if (d.success == true) {
					m.find('.mail-success').show();
				} else {
					m.find('.mail-error').show();
				}
			}
		});
	});
});
