$(document).ready(function() {
	// projets
	// $('#menu-projets li').slice(5).remove();
	// $('#menu-projets').append('<li><a href="" class="sub more">...</a></li>').hide();
	// $('#menu-projets .more').attr('href', $('#menu-projets').prev('a').attr('href'));
	// $('#menu-projets').prev().click(function() {
	// 	$('#menu-projets').slideToggle('fast');
	// 	return false;
	// });

	// widget rss
	$('#footer .widget_rss ul').slice(1).hide();
	$('#footer .widget_rss h3').mouseover(function() {
		if ($('#footer .widget_rss :animated').length != 0)
			return false;
		if ($(this).parent().find('ul').is(':hidden')) {
			$('#footer .widget_rss ul').slideUp();
			$(this).parent().find('ul').slideDown();
		}
	});

	// external links in new windows
	$('a[@href^="http://"]:not([@href^="http://'+ location.host +'"])').click(function() {
		window.open($(this).attr('href'));
		return false;
	});

	// image thumbnails
	$('#content img[@src~=".thumbnail."]')
	// .each(function() {
	// 	$(this).parent('a').css({width: $(this).width(), 'margin-left': 'auto', 'margin-right': 'auto', 'display': 'block'});
	// })
	.parent('a').addClass('thumbnail')
	.click(function() {
		var img = $(this).find('img');
		var t = img.attr('alt') || this.title || this.name || null;
		var g = this.rel || false;
		tb_show(t, this.href, g);
		this.blur();
		return false;
	});

	// preview type buttons
	$('#articles-preview-normal').click(function() {
		if ($('div.entry').is(':hidden')) {
			$('div.entry').slideDown();
			// $.cookie('articles-collapsed', null, { path: '/'});
		}
	});
	$('#articles-preview-reduced').click(function() {
		if ($('div.entry').is(':visible')) {
			$('div.entry').slideUp();
			// $.cookie('articles-collapsed', 'yes', { path: '/', expires: 365});
		}
	});
	if ($.cookie('footer-collapsed') == 'yes')
		$('#articles-preview-reduced').click();

	// remove navigation if empty
	if ($('.navigation .alignleft').html() == '&nbsp; ' && $('.navigation .alignright').html() == ' &nbsp;')
		$('.navigation').remove();

	$('a.thumbnail:first-child').each(function(i) {
		$(this).attr('rel', 'g'+i).addClass('gal');
		$(this).siblings('a').attr('rel', 'g'+i).addClass('gal');
	});

	$('.alert_old').click(function() {
		$(this).slideUp();
	});

	//
	ec3_schedule2iphcal();

	// footer
	$('#footer-button a, #footer-collapsed').click(function() {
		showHide_footer();
		return false;
	});
	if ($.cookie('footer-collapsed') == 'yes')
		showHide_footer();

	// webcam
	webcam_start();
	$('#webcam').click(function() {
		webcam_reload();
	});
});

var webcam_timer;
var webcam_delay = 5000;

function webcam_start() {
	webcam_timer = setInterval('webcam_reload()', webcam_delay);
}

function webcam_reload() {
	var now = new Date();
	var webcam_url = 'http://epimac.epita.fr/local/webcam.jpg?' + now.getTime();

	$('#webcam').attr('src', webcam_url);
}

function showHide_footer() {
	$('#footer-content').slideToggle();
	$('#footer-button').slideToggle();
	$('#footer-collapsed').slideToggle('normal', function() {
		if ($('#footer-collapsed').is(':visible'))
			$.cookie('footer-collapsed', 'yes', { path: '/', expires: 365});
		else {
			$.cookie('footer-collapsed', null, { path: '/'});
		}
	});
}

function ec3_schedule2iphcal() {
	$('.ec3_schedule').each(function() {
		t = $(this);
		t.after(iphcal_tpl);
		tn = t.next();
		tn.find('.infos').html($(this).html());
		if (t.find('.single').length == 1) {
			var expression = /(\w.+)\s(\w.+)\s(\w.+)\s(\w.+)/;
			expression.exec(t.find('.single').html());
			day = RegExp.$1; number = RegExp.$2; shortmonth = RegExp.$3;
		}
		else {
			var expression = /(\w.+)\s(\w.+)\s(\w.+)\s(\w.+)\s(\w.+)/;
			expression.exec(t.find('.range .ec3_start').html());
			day = RegExp.$1; number = RegExp.$2; shortmonth = RegExp.$3;
		}
		tn.find('.icon .day').html(day);
		if (number.substr(0,1) == '0')
			number = number.substr(1);
		tn.find('.icon .number').html(number);
		tn.find('.icon .shortmonth').html(shortmonth.substr(0,3));
		t.remove();
	});

	$('.iphcal .infos').each(function() {
		$(this).attr('rel', $(this).width());
	});
	$('.iphcal .infos').css('display', 'none').css('width', '0px').find('p').fadeOut(0);
	$('.iphcal').click(function() {
		infos = $(this).parent().find('.infos');
		if (infos.is(':animated'))
			return false;
		if (infos.width() == 0) {
			
			infos.css('display', 'block').animate({width: infos.attr('rel')+'px'}, function() { $(this).find('p').fadeIn('normal'); });
		}
		else {
			infos.find('p').fadeOut('fast');
			infos.animate({width: '0px'}, function() { $(this).css('display', 'none') });
		}
	});
}
