/************************
*	Entry point
*************************/
$(document).ready(function() {	
	bindCufon()
	bindMainMenu();
	bindSlideshow();
	if ($('.scroll-pane').length) {
		if (!$.browser.safari) {
			bindSlider();
		} else {
			$(window).load(function() {
				bindSlider();
			});
		}
	}
	
	$("#home .news").accordion(
		{
			header:'h3', 
			collapsible:true, 
			autoHeight:false
		}
	);
	
	$(".large-accordion").accordion(
		{
			header:'h5', 
			collapsible:true, 
			autoHeight:false
		}
	);
	
	$(".menu-gauche .accordion").accordion(
		{
			header:'h5', 
			collapsible:true, 
			autoHeight:false,
			active: false
		}
	);
	
	fixSlideshowPNG();
});

function fixSlideshowPNG() {
	var i;
	for (i in document.images) {
		if (document.images[i].src) {
			var imgSrc = document.images[i].src;
			if (imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') {
				document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
			}
		}
	}
}

/************************
*	CUFON
*	Activer seulement sur Windows pour une meilleure lisibiilité
*************************/
function bindCufon() {
	if (navigator.appVersion.indexOf("Mac") == -1) {
		var opts = {};
		
		// Options supplémentaires pour les liens MSIE
		if ($.browser.msie) {
			opts.forceHitArea = true;
			opts.hover = true;
		}
		
		Cufon
			.set('fontFamily', 'AWConquerorSansLight')
			.replace
				('h1', opts)
				('h2', opts)
				('h3', opts)
				('.menu-top a', opts)
		
		opts.textShadow = '2px 2px rgba(30, 49, 71, 0.8)';
		
		Cufon.replace
				('.main-menu > li > a', opts)
				('#home .container .news h2', opts)
				('.footer span', opts)
		
		opts.textShadow = '1px 1px rgba(252, 243, 167, 0.6)';
		
		Cufon.replace	
				('#image-desc a', opts)
				('.ariane a', opts)
				
	}
}

/************************
*	MAIN MENU
*************************/

// Globales
var menu_timeout_speed = 500;
var menu_timer = 0;
var menu_item = 0;

function bindMainMenu() {
	// Tallest
	$('.sous-menu').each(function() {
		tallest = 0;
		$(this).find('> li').each(function() {
			var height = $(this).height();
			if (height > tallest) {
				tallest = height;
			}
		});
		
		$(this).find('> li').height(tallest);
	});
	
	$('#jsddm > li').bind('mouseover', openMainMenu);
	$('#jsddm > li').bind('mouseout',  timerMainMenu);
	
	$(document).bind('click', closeMainMenu);
}


function openMainMenu() {
	cancerTimerMainMenu();
	closeMainMenu();
	menu_item = $(this).find('ul').css('visibility', 'visible');
}

function closeMainMenu() {
	if(menu_item) {
		menu_item.css('visibility', 'hidden');
	}
}

function timerMainMenu() {
	menu_timer = window.setTimeout(closeMainMenu, menu_timeout_speed);
}

function cancerTimerMainMenu() {
	if(menu_timer) {
		window.clearTimeout(menu_timer);
		menu_timer = null;
	}
}

/************************
*	SLIDESHOW
*************************/

// Globales
var slideshow_length = 0;
var slideshow_active = 1;
var slideshow_interval = 8000;
var slideshow_speed = 4000;
var slideshow_speed_text = 0;

function bindSlideshow() {	
	slideshow_length = $('.slideshow .holder > *').length;
	slideshow_active = Math.floor(Math.random() * (slideshow_length - 1 + 1)) + 1;
	
	$('.slideshow .holder > *').css(
		{
			position:'absolute',
			top:'0',
			left:'0'
		}
	);
	
	$('.slideshow .holder > *:not(:eq(' + (slideshow_active - 1) + '))').hide();
	$('#image-desc table td:not(:eq(' + (slideshow_active - 1) + '))').hide();
	
	// Interval
	window.setInterval(moveSlideshow, slideshow_interval);
}

function moveSlideshow() {
	$('.slideshow .holder > *:eq(' + (slideshow_active - 1) + ')').fadeOut(slideshow_speed);
	
	slideshow_active++;
	
	// Si on dépasse la longueur du slideshow, on revient à 1
	if (slideshow_active > slideshow_length) {
		slideshow_active = 1;
	}
	
	$('.slideshow .holder > *:eq(' + (slideshow_active - 1) + ')').fadeIn(slideshow_speed);
	
	// Fade in fade out des titres de programme
	$('#image-desc table td:eq(' + (slideshow_active - 1) + ')').fadeIn(slideshow_speed_text);
	$('#image-desc table td:not(:eq(' + (slideshow_active - 1) + '))').fadeOut(slideshow_speed_text);
	//$('#image-desc table td:eq(' + (slideshow_active - 1) + ')').fadeIn(slideshow_speed);
	//$('#image-desc table td:not(:eq(' + (slideshow_active - 1) + '))').fadeOut(slideshow_speed);
}

// Console.log fix
if (typeof(console) === 'undefined') {
	var console = {}
	console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
}


/************************
*	SLIDER CARREFOUR
*************************/
var slider_lire;
var slider_voir;
var slide_interval;
var slide_interval_speed = 2000;
var slide_speed = 300;
var slider_offset = 105;
var slider_voir_width = 0;
var slider_lire_width = 0;

function bindSlider() {
	// Setup initial
	$('.scroll-pane').each(function() {
		var slider = $(this);
		
		slider.find('ol li.prev a').bind('click', onClickSliderLeft);
		slider.find('ol li.next a').bind('click', onClickSliderRight);
	});
	
	slider_lire = $('.scroll-pane:first');
	slider_voir = $('.scroll-pane:last');
	
	slider_voir.find('> div').css({'left' : (-slider_voir.find('> div').width() + slider_voir.width())});
	
	slide_interval = setInterval(slideInterval, slide_interval_speed);
}

function slideInterval() {
	slideTo(slider_lire.find('> div'), -1);
	slideTo(slider_voir.find('> div'), 1);
}

function onClickSliderLeft() {
	slideTo($(this).parents('ol').prev('div'), 1);
	
	clearSlideInterval();
	
	return false;
}

function onClickSliderRight() {
	slideTo($(this).parents('ol').prev('div'), -1);
	
	clearSlideInterval();
	
	return false;
}

function slideTo(slide, direction) {
	slider_animating = true;
	
	slide.clearQueue();
	
	var to = slide.position().left + (direction * slider_offset);
	
	if (to > 0) {
		to = 0;
		clearSlideInterval();
	} else if (to < (-slide.width() + slide.parent().width())) {
		to = (-slide.width() + slide.parent().width());
		clearSlideInterval();
	}
	
	slide.animate(
		{
			left:to
		},
		slide_speed,
		function() {
			slider_animating = false;
		}
	);
}

function clearSlideInterval() {
	clearInterval(slide_interval);
}

