// search magnifier
$("#search-magnifier").hover(
	function() {
		this.src='static/images/search-magnifier-on.gif';
	},
	function() {
		this.src='static/images/search-magnifier2.gif';
	}
);

$("input.search-body-input").focus(function() {
	if ($(this).val() == 'Recherche') {
		$(this).val('');
	}
});
$("#recherche-form").submit(function() {
	var type     = $("#recherche-form [name=type]:checked").val();
	var text	 = $("#recherche-form [name=text]").val();
	var url = "recherche/" + type + ":" + text;

	if (text == 'Recherche') {
		$("#recherche-form [name=text]").focus();
		return false;
	}
	redirect(url);
	return false;
});

var current_title = 'title';

// title/actor picker:
$("#search-title-hover").hover(
	function() {
		if (current_title == 'title') {
			$("#search-title").attr('class' ,'top-on');
			$("#search-actors-container").show();
		} else {
			$("#search-actors").attr('class' ,'top-on');
			$("#search-title-container").show();
		}
	},
	function() {
		if (current_title == 'title') {
			$("#search-title").attr('class' ,'top');
			$("#search-actors-container").hide();
		} else {
			$("#search-actors").attr('class' ,'top');
			$("#search-title-container").hide();
		}
	}
);
$("#search-actors").focus(
	function() {
		current_title = 'actors';
		$("#search-title-container").hide();
		$("#search-actors-container").show();

		$("#search-actors").attr('class', 'top');

		$("#search-title").attr('class', 'under');

		$("#search-actors-container").after( $("#search-title-container") );
	}
);
$("#search-actors").click(
	function() {
		current_title = 'actors';
		$("#search-title-container").hide();
		$("#search-actors-container").show();

		$("#search-actors").attr('class', 'top');

		$("#search-title").attr('class', 'under');

		$("#search-actors-container").after( $("#search-title-container") );

		$("#search-actors-radio").attr('checked', 'checked');
	}
);
$("#search-title").click(
	function() {
		current_title = 'title';
		$("#search-actors-container").hide();
		$("#search-title-container").show();

		$("#search-title").attr('class', 'top');

		$("#search-actors").attr('class', 'under');

		$("#search-title-container").after( $("#search-actors-container") );

		$("#search-title-radio").attr('checked', 'checked');
	}
);


// advanced search:
function hideAdvancedSearch() {
	$("#search-advanced-menu").hide();
	$("#search-advanced").removeClass("active");
	$("#search-advanced-hover").removeClass("active");
}
function showAdvancedSearch() {
	$("#search-advanced-menu").show();
	$("#search-advanced").addClass("active");
	$("#search-advanced-hover").addClass("active");

	hideGenreSearch();
	hideCategorySearch();
}
$("a").click(function() {
	if (!$(this).hasClass("dropdown")) {
		hideAdvancedSearch();
		hideGenreSearch();
		hideCategorySearch();
	}
});
$("#search-advanced-hover").click(function() {
	if ($("#search-advanced-hover").hasClass("active")) {
		hideAdvancedSearch();
	} else {
		showAdvancedSearch();
	}

	return false;
});
// genre search:
function hideGenreSearch() {
	$("#search-genre-menu").hide();
	$("#search-genre").removeClass("active");
	$("#search-genre-hover").removeClass("active");
}
function showGenreSearch() {
	$("#search-genre-menu").show();
	$("#search-genre").addClass("active");
	$("#search-genre-hover").addClass("active");

	hideAdvancedSearch();
	hideCategorySearch();
}
$("#search-genre-hover").click( function() {
	if ($("#search-genre-hover").hasClass("active")) {
		hideGenreSearch();
	} else {
		showGenreSearch();
	}
	return false;
});
$("#search-genre-menu li").click(function() {
	redirect($(this).children("a").attr("href"));
	return false;
});
$("#search-advanced-menu li").click(function() {
	redirect($(this).children("a").attr("href"));
	return false;
});
// category search:
function hideCategorySearch() {
	$("#search-category-menu").hide();
	$("#search-category").removeClass("active");
	$("#search-category-hover").removeClass("active");
}
function showCategorySearch() {
	$("#search-category-menu").show();
	$("#search-category").addClass("active");
	$("#search-category-hover").addClass("active");

	hideAdvancedSearch();
	hideGenreSearch();
}
$("#search-category-hover").click( function() {
	if ($("#search-category-hover").hasClass("active")) {
		hideCategorySearch();
	} else {
		showCategorySearch();
	}
	return false;
});
$("#search-category-menu li").click(function() {
	redirect($(this).children("a").attr("href"));
	return false;
});

// the poll
$("ul.poll li").click(function() {
	$(this).children("input").attr("checked", "checked");
	$("ul.poll li").removeClass('active');
	$(this).addClass('active');
});

function animatePoll(voted) {
	var total_results = $("#total_results").text();
	if (voted) {
		total_results++; // this result
		var choice = $("#poll input[name=choice]:checked").val();
	}
	$("#total_results").text(total_results);
	var counter = 0;
	$(".poll-result-bar").each(function() {
		var result = $(this).text();
		if (voted && counter == choice) {
			result++;
		}

		var per = Math.floor(result / total_results * 100);
		$(this).parent().siblings('.poll-percent').text(per + '%'); // set the percent

		var width = result / total_results * 240; // max width
		$(this).animate( { width: width + "px" }, { duration:600 } );
		counter++;
	});
}

$("#poll").submit(function() {
	var data = $("#poll").serialize();

	$.post($("#poll").attr("action"), data );

	$("#poll").slideUp("slow");
	$("#poll-results").slideDown("slow", function() { 	animatePoll(true); } );
	
	return false;
});

if ($.cookie('poll' + $("#poll input[name=poll_id]").val())) {
	$("#poll").hide();
	$("#poll-results").show(1, function() { animatePoll(false); } );
}

// featured:
$("ul.featured li a").hover(
	function() {
		$(this).parents("td").children(".featured-movie:first").hide();
		$('#f' + $(this).attr('rel')).show();
	},
	function() {
		$(this).parents("td").children(".featured-movie:first").show();
		$('#f' + $(this).attr('rel')).hide();
	}
);

// flip the features titles
function reallyChangeTitles() {
	$(".title-switcher").each(
		function() {
			if (checkOTitle() && !$(this).hasClass('otitle')) { // change to otitle
				var old_title = $(this).text();
				$(this).text($(this).attr('title'));
				$(this).attr('title', old_title);
				$(this).addClass('otitle');
			} else 	if (!checkOTitle() && $(this).hasClass('otitle')) { // change back
				var old_title = $(this).text();
				$(this).text($(this).attr('title'));
				$(this).attr('title', old_title);
				$(this).removeClass('otitle');
			}
		}
	);
}
function changeTitles() { // on the link
	if ($.cookie('otitle') == 'true') {
		$.cookie('otitle', 'false', { path: '/' });
	} else {
		$.cookie('otitle', 'true', { path: '/' });
	}
	reallyChangeTitles();
	return false;
}


if ($.cookie('otitle') == 'true') { // don't wait for DOM ready, otherwise it's too slow
	$("#nav-titres-originaux").addClass('active');
	reallyChangeTitles();
}
$("#nav-titres-originaux").click(function() {
	$("#nav-titres-originaux").toggleClass('active');
	changeTitles();
	$(this).blur();

	if( $('#miniScheduleFlash').length > 0 ) {
		//remote call to flash to switch titles
		var movie = document.getElementById('miniScheduleFlash');
		movie.setUseOriginal( checkOTitle() );
	}

	if( $('#largeScheduleFlash').length > 0 ) {
		var movie = document.getElementById('largeScheduleFlash');
		movie.setUseOriginal( checkOTitle() );
	}

	if( $('#promoFlash').length > 0 ) {
		var movie = document.getElementById('promoFlash');
		movie.setUseOriginal( checkOTitle() );
	}
	return false;
});

// on demand:
$("#ondemand-week-new").click(function() {
	$("#ondemand-week").removeClass("active");
	$(this).addClass("active");

	var num_rows = $("#ondemand-list-new li").length;
	if ($("#ondemand-list").hasClass("items-per-page-8")) {
		var num_per_page = 8;
	} else if ($("#ondemand-list-new").hasClass("items-per-page-12")) {
		var num_per_page = 12;
	}
	var num_pages = Math.ceil(num_rows / num_per_page);

	$("#ondemand-list-new").addClass('pagination-content').show();
	$("#ondemand-list").removeClass('pagination-content').hide();

	updatePagination(1, num_pages); // regenerate pagination

	$("#"+pagination_id_prefix+"1").addClass('active');

	$("ul.pagination-content li").hide();
	$("ul.pagination-content li").slice(0, num_per_page).show();

	$(this).blur();

	return false;
});

$("#ondemand-week").click(function() {
	$("#ondemand-week-new").removeClass("active");
	$(this).addClass("active");

	var num_rows = $("#ondemand-list li").length;
	if ($("#ondemand-list").hasClass("items-per-page-8")) {
		var num_per_page = 8;
	} else if ($("#ondemand-list").hasClass("items-per-page-12")) {
		var num_per_page = 12;
	}
	var num_pages = Math.ceil(num_rows / num_per_page);

	$("#ondemand-list").addClass('pagination-content').show();
	$("#ondemand-list-new").removeClass('pagination-content').hide();

	updatePagination(1, num_pages); // regenerate pagination
	$("#"+pagination_id_prefix+"1").addClass('active');

	$("ul.pagination-content li").hide();
	$("ul.pagination-content li").slice(0, num_per_page).show();


	$(this).blur();

	return false;
});

/****** generic pagination starts here ****/
var pagination_id_prefix = "pg";
var num_pages = 1; // simple default value
function updatePagination(page, maxPages ) {
	var next = $('a#pagination-next');
	var prev = $('a#pagination-prev');
	var pageNums = $('ul#pagination-pages');

	setHash(page);

	var html = '';
	
	maxPages = parseInt(maxPages);
	page = parseInt(page);
	
	//DO THE PAGE NAV BUTTONS
	
	if( page == 1 && maxPages <= 1 ) {
		prev.addClass('disable');
		next.addClass('disable');
	} else if( page == 1 ) {
		prev.addClass('disable');
		next.removeClass('disable');
	} else if( page == maxPages ) {
		prev.removeClass('disable');
		next.addClass('disable');
	} else {
		prev.removeClass('disable');
		next.removeClass('disable');
	}
	
	//DO THE PAGE NUMBRERS

	if ( maxPages < 10 ) { 	// there are fewer total pages than 6 (5 in a row)
		for ( i = 1; i < maxPages; i++ ) {
			html += '<li><a href="#" id="'+pagination_id_prefix+i+'">'+i+'</a></li>';
		}
		html += '<li><a href="#" id="'+pagination_id_prefix+maxPages+'" class="last">'+maxPages+'</a></li>';
	} else if ( page < 4) { 	//if page is less than 5 but there are more than 6 total pages
		for( i = 1; i < 4; i++ ) {
			html += '<li><a href="#" id="'+pagination_id_prefix+i+'">'+i+'</a></li>';
		}
		html += '<li><a href="#" id="'+pagination_id_prefix+4+'" class="last">'+4+'</a></li>';
		html += '<li>&hellip;</li>';
		html += '<li><a href="#" rel="'+maxPages+'" id="'+pagination_id_prefix+maxPages+'" class="last">'+maxPages+'</a></li>';
	} else if ( (maxPages - page) < 5 ) {
		html = '<li><a href="#" id="'+pagination_id_prefix+'1" class="last">1</a></li>';
		html += '<li>&hellip;</li>';

		for( i = maxPages - 5; i < maxPages; i++ ) {
			html += '<li><a href="#" id="'+pagination_id_prefix+i+'">'+i+'</a></li>';
		}
		html += '<li><a href="#" id="'+pagination_id_prefix+maxPages+'" class="last">'+maxPages+'</a></li>';
	} else {
		html = '<li><a href="#" id="'+pagination_id_prefix+'1" class="last">1</a></li>';
		html += '<li>&hellip;</li>';

		for( i = page - 2; i < page + 2; i++ ) {
			html += '<li><a href="#" id="'+pagination_id_prefix+i+'">'+i+'</a></li>';
		}
		html += '<li><a href="#" id="'+pagination_id_prefix+(page+2)+'" class="last">'+(page+2)+'</a></li>';
	
		html += '<li>&hellip;</li>';
		html += '<li><a href="#" id="'+pagination_id_prefix+maxPages+'" class="last">'+maxPages+'</a></li>';
	}
	
	pageNums.html(html);
	pageNums.show();
}

function initPagination() {
	if ($("#pagination-pages li").length > 0) {
		if (window.location.hash) {
			var page_num = window.location.hash.substr(1);

			var num_rows = $(".pagination-content .pagination-item").length;
			var first_visible = $(".pagination-content .pagination-item:visible");
			var current_index = $(".pagination-content .pagination-item").index(first_visible);

			if ($(".pagination-content").hasClass("items-per-page-8")) {
				var num_per_page = 8;
			} else if ($(".pagination-content").hasClass("items-per-page-10")) {
				var num_per_page = 10;
			} else if ($(".pagination-content").hasClass("items-per-page-12")) {
				var num_per_page = 12;
			}

			var num_pages = Math.ceil(num_rows / num_per_page);

			if (page_num > num_pages) {
				page_num = 1;
			}

			updatePagination(page_num , num_pages); // regenerate pagination

			current_index = (page_num - 1) * num_per_page;
			$(".pagination-content .pagination-item").hide();
			$(".pagination-content .pagination-item").slice(current_index, current_index + num_per_page).show();

			$("#" + pagination_id_prefix + page_num).addClass("active"); // set active on the new link
		} else {
			var page_num = 1;

			num_pages = $("#pagination-pages li").length;
			updatePagination(page_num, num_pages); // start pagination on first page

			$("#" + pagination_id_prefix + page_num).addClass("active"); // highlight first page
		}
	}
	$(".pagination-content").parent(".pagination-content-container").attr("style", "background: none");
	$(".pagination-content").show();
}

//$(document).ready(function () {
	initPagination();
//});

$("#pagination-pages a").live("click", function(){ // handle regular pagination link
	$(this).blur();

	var num_rows = $(".pagination-content .pagination-item").length;
	var first_visible = $(".pagination-content .pagination-item:visible");
	var current_index = $(".pagination-content .pagination-item").index(first_visible);

	if ($(".pagination-content").hasClass("items-per-page-8")) {
		var num_per_page = 8;
	} else if ($(".pagination-content").hasClass("items-per-page-10")) {
		var num_per_page = 10;
	} else if ($(".pagination-content").hasClass("items-per-page-12")) {
		var num_per_page = 12;
	}

	var num_pages = Math.ceil(num_rows / num_per_page);

	var page_num = $(this).attr('id').substr(pagination_id_prefix.length);

	updatePagination(page_num , num_pages); // regenerate pagination

	current_index = (page_num - 1) * num_per_page;
	$(".pagination-content .pagination-item").hide();
	$(".pagination-content .pagination-item").slice(current_index, current_index + num_per_page).show();

	$("#" + pagination_id_prefix + page_num).addClass("active"); // set active on the new link
	return false;
});
// deal with the prev/next buttons
$("#pagination-prev").live("click", function() {
	if (!$(this).hasClass('disable')) {
		$(this).blur();

		var num_rows = $(".pagination-content .pagination-item").length;
		var first_visible = $(".pagination-content .pagination-item:visible");
		var current_index = $(".pagination-content .pagination-item").index(first_visible);

		if ($(".pagination-content").hasClass("items-per-page-8")) {
			var num_per_page = 8;
		} else if ($(".pagination-content").hasClass("items-per-page-10")) {
			var num_per_page = 10;
		} else if ($(".pagination-content").hasClass("items-per-page-12")) {
			var num_per_page = 12;
		}

		var num_pages = Math.ceil(num_rows / num_per_page);
		current_index -= num_per_page;

		$(".pagination-content .pagination-item").hide();
		$(".pagination-content .pagination-item").slice(current_index, current_index + num_per_page).show();

		var page_num = (current_index + num_per_page) / num_per_page;

		updatePagination(page_num, num_pages); // regenerate pagination

		$("#" + pagination_id_prefix + page_num).addClass("active"); // set active on the new link
	}
	return false;
});
$("#pagination-next").live("click", function() {
	if (!$(this).hasClass('disable')) {
		$(this).focus();

		var num_rows = $(".pagination-content .pagination-item").length;
		var first_visible = $(".pagination-content .pagination-item:visible");
		var current_index = $(".pagination-content .pagination-item").index(first_visible);

		if ($(".pagination-content").hasClass("items-per-page-8")) {
			var num_per_page = 8;
		} else if ($(".pagination-content").hasClass("items-per-page-10")) {
			var num_per_page = 10;
		} else if ($(".pagination-content").hasClass("items-per-page-12")) {
			var num_per_page = 12;
		}

		var num_pages = Math.ceil(num_rows / num_per_page);
		current_index += num_per_page;

		$(".pagination-content .pagination-item").hide();
		$(".pagination-content .pagination-item").slice(current_index, current_index + num_per_page).show();

		var page_num = (current_index + num_per_page) / num_per_page;

		updatePagination(page_num, num_pages); // regenerate pagination

		$("#" + pagination_id_prefix + page_num).addClass("active"); // set active on the new link
	}
	return false;
});
/****** generic pagination ends here ****/

/****** lightbox starts here ****/
function toggleLightbox(id) {
	$('div.lightbox-wrapper').hide();
	var mylb = $("#" + id).parents('div.lightbox-wrapper');
	mylb.show();

	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;

	//centering
	if ($("#blanket:visible").length > 0) {
		$(".lightbox-wrapper").css({  
			"top":  windowHeight/2 - mylb.height()/2 + document.documentElement.scrollTop,
			"left": windowWidth/2  - mylb.width()/2,
			"z-index": 1000
		});
	} else {
		$(".lightbox-wrapper").css({  
			"top":  $(this).scrollTop() + 100,
			"left": windowWidth/2  - mylb.width()/2,
			"z-index": 1000
		});
	}
	$("#" + id + " input[type=text]:first").removeClass("original").focus().select();

	return false;
}
$("#blanket").click(function() {
	// kill the player
	$("#lightbox-player").replaceWith("<div id='lightbox-player'></div>");
	closeLightbox();
});
function closeLightbox() {
	if($.browser.msie){
		$("#blanket").hide();
		$('div.lightbox-wrapper').hide();
	} else {
		$("#blanket").fadeOut();
		$('div.lightbox-wrapper').fadeOut();
	}
	return false;
}
$(document).keyup(function(event) {
	if (event.keyCode == 27) { // ESC
		closeLightbox();
		hideAdvancedSearch();
		hideGenreSearch();
		hideCategorySearch();
	}
});

function showPlayer(file) {
	if (!file) {
		return false;
	}
	$("#blanket").height($("body").height());
	$("#blanket").show();

	toggleLightbox('lightbox-player');
	var flashvars = {
		  movieToLoad: file,
		  volume: getVolume()
	};

	var params = {
	  menu: "false",
	  quality:"high",
	  wmode : "opaque",
	  allowFullScreen: 'true',
	  host : $("base").attr('href')
	};

	var attributes = {
	};

	swfobject.embedSWF("static/swf/videoPlayer.swf", "lightbox-player", "602", "349", "9.0.0","expressInstall.swf", flashvars, params, attributes);

	return false;
}

/****** lightbox ends here *****/

/****** bande-annonce series starts here ****/

$(document).ready(function () {

		var flashvars = {
  		  host : $("base").attr('href'),
		  movieToLoad : $('#flashSeriesVideoPlayer').text(),
		  closeDisabled : 'true',
		  volume: getVolume()
		};
		
		var params = {
		  menu: "false",
		  quality:"high",
		  wmode : "opaque",
		  allowFullScreen: 'true'
		};
		
		var attributes = {
		};
		
		swfobject.embedSWF("static/swf/videoPlayer.swf", "flashSeriesVideoPlayer", "602", "349", "9.0.0","expressInstall.swf", flashvars, params, attributes);

});	

/******bande-annonce series ends here ****/

/****** schedule print lightbox starts here ***/
$("ul.lightbox-checkbox li label").click(function() {
	$(this).toggleClass('active');
	if ($(this).attr('id') == 'forall' && $(this).hasClass('active')) {
		$("ul.lightbox-checkbox li label").addClass('active');
		$("input[name=channel[]]").attr("checked", true);
		$("#all").attr("checked", true);
	} else if ($(this).attr('id') == 'forall' && !$(this).hasClass('active')) {
		$("ul.lightbox-checkbox li label").removeClass('active');
		$("input[name=channel[]]").attr("checked", false);
		$("#all").attr("checked", false);
	} else if (!$(this).hasClass('active')) {
		$("#" + $(this).attr("for")).attr("checked", false);
		$("#forall").removeClass('active');
		$("#all").attr("checked", false);
	} else if ($(this).hasClass("active")) {
		$("#" + $(this).attr("for")).attr("checked", true);
	}
	if ($("ul.lightbox-checkbox li label.active").length == 4) {
		$("#forall").addClass('active');
		$("#all").attr("checked", true);
	}
	return false; // abort the label event in non-ie
});

/****** schedule print lightbox ends here ***/

/*************************************************************/
/**************** Mon Horaire starts here ********************/
function validate_email(input) {
	var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
	if( !pattern.test( input ) ) {
		return false;
	}
	return true;
}
// enable or disable a reminder
// enable = true to add reminder, false to remove reminder
// value = "{CHANNEL}-{TIME}-{SHOWID}"
// refresh = whether or not to reload the page (used when signing-in)
function monHoraireReminder(enable, value, refresh) {
	if (monHoraireIsSignedIn()) {
		var data = { 'action' : 'schedule' , 'id' : value , 'enable' : enable };
		$.post("services/mon_horaire.php", data, function() {
			if (refresh) {
				window.location.reload(false);
			}
		});

		return true;
	} else {
		$(":input[name=postaction]").val('monHoraireReminder(' + enable + ', \'' + value + '\', true);');
		toggleLightbox('mon-horaire-lightbox-wrapper');
	}
	return false;
}
function flashMonHoraireReminder(enable, value) {
	monHoraireReminder(enable, value, false);
}
function redirect(uri) {
	var base_href = $("base").attr('href');
	if (base_href.substr(base_href.length-1) == '/' && uri.charAt(0) == '/') {
		uri = uri.substr(1);
	}
	uri = base_href + uri;
	window.location = uri;
}
function setHash(string) {
	window.location.hash = string;
}

// the mon horaire lightbox tabs:
$("#nav-mon-horaire").click(function() {
	if (!monHoraireIsSignedIn()) {
		$(this).blur();
		$(":input[name=postaction]").val('-1');
		toggleLightbox('mon-horaire-lightbox-wrapper');
		return false;
	}
});
$("#mon-horaire-ouverture").click(function() {
	$("#mon-horaire-nouveaux-membres").removeClass('active');
	$(this).addClass('active');
	$("#mon-horaire-nouveaux-membres-wrapper").hide();
	$("#mon-horaire-ouverture-wrapper").show();
	$(this).blur();
	return false;
});
$("#mon-horaire-nouveaux-membres").click(function() {
	$("#mon-horaire-ouverture").removeClass('active');
	$(this).addClass('active');
	$("#mon-horaire-ouverture-wrapper").hide();
	$("#mon-horaire-nouveaux-membres-wrapper").show();
	$(this).blur();
	return false;
});
// the text->password input type switcher
$(".text-input-switch-label").focus(function(){
	$(this).hide();
	$(this).next().attr("style", "display: block").focus();
	return true;
});

// need onChange rather than onFocus
$(".original").change(function() {
	if ($(this).hasClass('original')) {
		$(this).select(); $(this).removeClass('original');
	}
});

// newsletter form
$("#newsletter-form").submit(function() {
	var error = false;

	if ($("#newsletter-form a.button.je_minscris").hasClass("disabled") || $("#newsletter-form a.button.je_minscris").hasClass("good")) {
		return false;
	}

	if ($("#newsletter-form [name=email]").val() == '' || !validate_email($("#newsletter-form [name=email]").val())) { // validate email
		error = true;
		$("#newsletter-form [name=email]").addClass('error');
	} else {
		$("#newsletter-form [name=email]").removeClass('error');
	}

	if (!error) {
		$("#newsletter-form a.button.je_minscris").addClass("disabled");
		var data = $(this).serialize();
		$.post($(this).attr("action"), data, function(data) {
			$("#newsletter-form a.button.je_minscris").addClass("good");
		});

	}
	return false;
});
$("#sabonner-newsletter-form").submit(function() {
	var error = false;

	if ($("#sabonner-newsletter-form a.button.je_minscris").hasClass("disabled") || $("#sabonner-newsletter-form a.button.je_minscris").hasClass("good")) {
		return false;
	}

	if ($("#sabonner-newsletter-form [name=email]").val() == '' || !validate_email($("#sabonner-newsletter-form [name=email]").val())) { // validate email
		error = true;
		$("#sabonner-newsletter-form [name=email]").addClass('error');
	} else {
		$("#sabonner-newsletter-form [name=email]").removeClass('error');
	}
	
	if ($("#sabonner-newsletter-form [name=email]").val() != $("#sabonner-newsletter-form [name=email2]").val()) {
		error = true;
		$("#sabonner-newsletter-form [name=email2]").addClass('error');
	} else {
		$("#sabonner-newsletter-form [name=email2]").removeClass('error');
	}
	

	if (!error) {
		$("#sabonner-newsletter-form a.button.je_minscris").addClass("disabled");
		var data = $(this).serialize();
		$.post($(this).attr("action"), data, function(data) {
			$("#sabonner-newsletter-form a.button.je_minscris").addClass("good");
		});

	}
	return false;
});


// signin form action
$("#mon-horaire-signin-form").submit(function() {
	var error = false;

	// validate email
	if ($("#mon-horaire-signin-form [name=email]").val() == '') {
		error = true;
		$("#mon-horaire-signin-form [name=email]").addClass('error');
	} else {
		$("#mon-horaire-signin-form [name=email]").removeClass('error');
	}

	// validate password
	if ($("#mon-horaire-signin-form [name=password]").val() == '') {
		error = true;
		$("#mon-horaire-signin-form [name=password]").addClass('error');
	} else {
		$("#mon-horaire-signin-form [name=password]").removeClass('error');
	}

	if (!error) {
		var data = $(this).serialize();

		$.post($(this).attr("action"), data, function(data) {
			if (data.result == 1) { // redirect
				var postaction = $("#mon-horaire-signin-form [name=postaction]").val();
				if (postaction != '-1') {
					_is_signed_in = true;
					eval(postaction);
					closeLightbox();
				} else {
					redirect('mon-horaire/');
				}
				return true;
			} else if (data.result == 0) {
				$("#mon-horaire-signin-form :input").each(function() {
					$(this).addClass('error');
				});
			}
		}, "json");
	}

	return false;
});

$("#mon-horaire-signin-form").keyup(function(event) {
	if (event.keyCode == 13) { // Enter
		$(this).submit();
	}
});

// signup form action:
$("#mon-horaire-signup-form").submit(function() {
	var error = false;
	$("#mon-horaire-signup-form :input :visible").each(function() {
		if ($(this).hasClass('original') || $(this).val() == '') {
			error = true;
			$(this).addClass('error');
		} else if ($(this).attr('name') == 'email' && !validate_email($(this).val())) { // validate email
			error = true;
			$(this).addClass('error');
		} else {
			$(this).removeClass('error');
		}
	});

	// validate password
	if ($("#mon-horaire-signup-form [name=password]").val() && $("#mon-horaire-signup-form [name=password]").val() != $("#mon-horaire-signup-form [name=password_again]").val()) {
		$("#mon-horaire-signup-form [name=password]").addClass('error');
		$("#mon-horaire-signup-form [name=password_again]").addClass('error');
	} else {
		$("#mon-horaire-signup-form [name=password]").removeClass('error');
		$("#mon-horaire-signup-form [name=password_again]").removeClass('error');
	}

	if (!error) {
		var data = $(this).serialize();

		$.post($(this).attr("action"), data, function(data) {
			if (data.result == 1) {
				var postaction = $("#mon-horaire-signin-form [name=postaction]").val();
				if (postaction != '-1') {
					_is_signed_in = true;
					eval(postaction);
					closeLightbox();
				} else {
					redirect('mon-horaire/');
				}
			} else if (data.result == 0) {
				for(var field in data.errors) {
					$("#mon-horaire-signup-form [name=" + field + "]").addClass('error');
				}
			}
		}, "json");
	}
	
	return false;
});

// order form
$("#online-form").submit(function() {
	var error = false;

	if ($("#online-form a.button.soumettre").hasClass("disabled") || $("#online-form a.button.soumettre").hasClass("good")) {
		return false;
	}
	
	$("#online-form :input").each(function() {
		if (($(this).hasClass('original') || $(this).val() == '') 
			&& $(this).attr('name') != 'phone_work' 
			&& $(this).attr('name') != 'other'
			 && $(this).attr('name') != 'app'
		) {
			error = true;
			$(this).addClass('error');
		} else if ($(this).attr('name') == 'email' && !validate_email($(this).val())) { // validate email
			error = true;
			$(this).addClass('error');
		} else {
			$(this).removeClass('error');
		}
	});

	if (!error) {
		$("#online-form a.button.soumettre").addClass("disabled");
		var data = $(this).serialize();
		$.post($(this).attr("action"), data, function(data) {
			closeLightbox();
			$("#online-form a.button.soumettre").addClass("disabled");
		});

	}
	return false;
});

//Contest


$("#contest-form").submit(function() {
	var canEnter = false;

	if ($("#contest-form a.button").hasClass("disabled") || $("#contest-form a.button").hasClass("good")) {
		return false;
	}
	
	canEnter = validateContest();
	
	/*$("#contest-form :input").each(function() {
	/*	if (($(this).hasClass('original') || $(this).val() == '') && $(this).attr('name') != 'phone_work') {
			error = true;
			$(this).addClass('error');
		} else if ($(this).attr('name') == 'email' && !validate_email($(this).val())) { // validate email
			error = true;
			$(this).addClass('error');
		}
		 else if ($(this).attr('name') == 'fname' && $(this).val() == '') { 
			error = true;
			$(this).addClass('error');
		}
		else {
			$(this).removeClass('error');
		}
	
	});
	*/
	//

	if (canEnter) {
		$("#contest-form a.button").addClass("disabled");
		var data = $(this).serialize();
		$.post($(this).attr("action"), data, function(data) {
			//closeLightbox();
			document.getElementById("message").innerHTML = data;
			//$("#contest-form a.button.soumettre").addClass("disabled");
		});
	}
	return false;
});


// order form
function submit_pour_contact_form(buttobj, formid) {
	if ($(buttobj).hasClass("disabled") || $(buttobj).hasClass("good")) {
		return false;
	}

	var error = false;
		$("#contact-form :input").each(function() {
		if (($(this).hasClass('original') || $(this).val() == '')) {
			error = true;
			$(this).addClass('error');
		} else {
			$(this).removeClass('error');
		}
	});


	if (!error) {
		$(buttobj).addClass("disabled");
		var data = $("#" + formid).serialize();


		$("#contact-form :input").each(function() {
			$(this).attr('disabled', 'disabled');
		});

		$.post($("#" + formid).attr("action"), data, function(data) {
			$(buttobj).removeClass("disabled");
			$(buttobj).addClass("good");
			
			// wait 3s before resetting the form
			setTimeout("reset_pour_contcant_form();", 3000);
		});
	}
	return false;
}

function reset_pour_contcant_form() {
	$("#contact-form :input").each(function() {
		$(this).removeAttr('disabled');
		$(this).addClass('original');
	});

	$("#contact-form [name=prenom]").val("Prénom");
	$("#contact-form [name=nom]").val("Nom");
	$("#contact-form [name=email]").val("Courriel");
	$("#contact-form [name=phone]").val("Téléphone");
	$("#contact-form [name=service]").val(0);
	$("#contact-form [name=comments]").val("Commentaires");

	$("#contact-form-button").removeClass("good");
}


function submit_contact_form(buttobj, formid) {
	if ($(buttobj).hasClass("disabled") || $(buttobj).hasClass("good")) {
		return false;
	}
	$(buttobj).addClass("disabled");
	var data = $("#" + formid).serialize();
	$.post($("#" + formid).attr("action"), data, function(data) {
		$(buttobj).removeClass("disabled");
		$(buttobj).addClass("good");
	});
	return false;
}

$("#mon-horaire-password-reminder").submit(function() {
	var data = $(this).serialize();
	$.post($(this).attr("action"), data, function(data) {
	}, "json");
	closeLightbox();
	return false;
});


// the show details checkbox links
function addManualReminder(obj, id) {
	$(obj).blur();

	if ($(obj).hasClass('active')) {
		var enable = false;
	} else {
		var enable = true;
	}
	if (monHoraireReminder(enable, id, false)) {
		$(obj).toggleClass('active');
	}
}


/**************** Mon Horaire ends here **********************/
/*************************************************************/

/**************** FAQ starts here *******************/
$("#faq dt").click(function() {
	$(this).blur();

	$(this).toggleClass("active");
	$(this).next().toggleClass("active");

	return false;
});


/**************** FAQ ends here *******************/

/**** sabonner starts *****/
function sabonnerShowService() {
	var option = $("#sabonner-services-select option:selected");
	var index = $("#sabonner-services-select option").index(option);

	$("#sabonner-services li").removeClass("active");
	$("#sabonner-services li:eq("+index+")").addClass("active");
}
/**** sabonner ends here ****/

/***** series starts here ***/

function toggleExpando(obj) {
	// get the index of the next parent
	
	$(obj).toggleClass("active");

	var thisParent = $(obj).parents("tr");
	var thisIndex = $("#show-details-schedule tr").index(thisParent);
	var nextParent = thisParent.nextAll(".series-schedule-parent");
	if (nextParent.text()) {
		var nextParentIndex = $("#show-details-schedule tr").index(nextParent);
		//$("#show-details-schedule tr").slice(thisIndex +1, nextParentIndex).toggle();
		toggleTableRow($("#show-details-schedule tr").slice(thisIndex +1, nextParentIndex));
	} else {
		//$("#show-details-schedule tr").slice(thisIndex +1).toggle();
		toggleTableRow($("#show-details-schedule tr").slice(thisIndex +1));
	}

	return false;
}

function toggleTableRow(obj) {
	if (obj.css('display') == 'none') {
		obj.show();//css('display', 'table-row');
	} else {
		obj.hide();//css('display', 'none');
	}
}


/**** series ends here *****/

/***** START focus function - used by flash fullscreen ***/
function getFocus() {
	this.window.focus();
}



/***** END focus function - used by flash fullscreen ***/
