var init_liste_a_afficher = "";
var init_onglet_menu = "";
var anim_menu = "";

//Modif Typographie des titres et diverses zones, et effet coins sur menus et autres éléments.
//$("#sousmenu").addClass("accueil");
Cufon.replace('.typoCufon', {fontFamily: 'DIN'});
Cufon.replace('#contenu h2:not(.critique)', {fontFamily: 'AvantGardeGothic'});
Cufon.replace('h4:not(.palmares)', {fontFamily: 'AvantGardeGothic'});
Cufon.replace('h6', {fontFamily: 'DIN'});
Cufon.replace('#centre_bas h2', {fontFamily: 'DIN'});
//Cufon.replace('#menu_section ul li a.typoAvant', {hover: true, fontFamily: 'AvantGarde', textShadow: '1px -1px #444 '});
Cufon.replace('#colonne_centrale .entry_critique h3', {fontFamily: 'AvantGardeGothic'});
Cufon.replace('#colonne_centrale .buzzSection', {fontFamily: 'AvantGardeGothic'});
Cufon.replace('#colonne_droite .buzzSection', {fontFamily: 'AvantGardeGothic'});
//Cufon.replace('#zone_commentaires h3, #buzz h3', {fontFamily: 'AvantGarde'});
//Cufon.replace('#la_une #nouvelle_en_une #contenu_photo a', {hover:true, fontFamily: 'AvantGardeGothic', textShadow: '1px 1px #000'});
//Cufon.replace('#la_une #nouvelle_en_une #contenu_photo p.section', {hover:true, fontFamily: 'AvantGarde'});
$("#jouez .contenu_externe").corner("top curl 5px cc:#ffffff"); 

$(document).ready(function() {
	//activation du menu de Showbizz
	//activating_menu();
	
	//Skinning Listes déroulantes
	jQuery("#regions").select_skin('#regions');
	jQuery("#films").select_skin('#films');
	jQuery("#salles").select_skin('#salles');
				   
	$("#menu_section").mouseenter(function () {anim_menu="off";});			
	$("#menu_section").mouseleave(function () {anim_menu="on";});	
	$("#sousmenu").mouseenter(function () {anim_menu="off";});			
	$("#sousmenu").mouseleave(function () {anim_menu="on";});		
	$("#sousmenu").mouseleave(function() {
		if(anim_menu=="on") activating_menu();	
	});
	$("#partagez").click(function() { $("#listeLiens").slideToggle(); });
	$("#fermer_partagez").mouseenter(function() { $(this).css("background-color","#e8e8e8"); });
	$("#fermer_partagez").mouseleave(function() { $(this).css("background-color","#fff"); });
	$("#fermer_partagez").click(function() { $("#listeLiens").slideToggle(); });

	$(".afficher_bitly").click(function() {
		var locationObj=document.location;
		var source = $(this).attr("alt");
		//convertir le lien
		d3f4ults = {version:    '2.0.1',login:      'branchezvous',apiKey:     'R_0aa93c7eee9d5c179e08d68ed66ef9f6',history:    '1',longUrl:    locationObj};
		// URL pour connecter l'API
		var daurl = "http://api.bit.ly/shorten?"+"version="+d3f4ults.version+"&longUrl="+d3f4ults.longUrl+"&login="+d3f4ults.login+"&apiKey="+d3f4ults.apiKey+"&history="+d3f4ults.history+"&format=json&callback=?";               
		//alert(d3f4ults.longURL);
		// Utiliser l'API
		$.getJSON(daurl, function(data){var output = data.results[document.location].shortUrl+"+-+"; var avant=source+output; window.location=avant; });    
	});

	/*effets sur onglets de la section Dernieres nouvelles*/
	$("ul#onglets_dernieres_nouvelles li").click(function() {
		var liste_a_afficher = $(this).attr("id");
		//alert(liste_a_afficher);
		if(liste_a_afficher!=init_liste_a_afficher) {
			if(!$(this).hasClass("on")) {
				if(liste_a_afficher=="nouveau") {
					$("#populaire").removeClass("on");
					$("ul#liste_populaire").fadeOut(); 
					$(this).addClass("on");
					$("ul#liste_nouveau li").equalHeights();
				} else {
					$("#nouveau").removeClass("on");
					$("ul#liste_nouveau").fadeOut(); 
					$(this).addClass("on");
					$("ul#liste_populaire li").equalHeights();
				}										
				init_liste_a_afficher = liste_a_afficher;
			}
		} 
	});
	//FONCTION pour créer un lien Bit.ly pour les services Web comme Twitter, Facebook, etc...
	/*$(".afficher_bitly").click(function() {
		var locationObj=document.location;
		var source = $(this).attr("alt");
		var defaults = {version:    '2.0.1',login:      'branchezvous',apiKey:     'R_0aa93c7eee9d5c179e08d68ed66ef9f6',history:    '1',longUrl:    locationObj};//convertir le lien
		var daurl = "http://api.bit.ly/shorten?"+"version="+defaults.version+"&longUrl="+defaults.longUrl+"&login="+defaults.login+"&apiKey="+defaults.apiKey+"&history="+defaults.history+"&format=json&callback=?";// URL pour connecter l'API               
		$.getJSON(daurl, function(data){var output = data.results[defaults.longUrl].shortUrl+"+-+"; var avant=source+output; window.location=avant; });    // Utiliser l'API
	});*/
	
	$('#col_2_largeurs *').tooltip({showURL:false});
	$('#colonne_droite *').tooltip({showURL:false});
	$('#liste_populaire *').tooltip({showURL:false});
	
	//FONCTION pour adapter les images et iframes aux grandeurs de notre template BV
	$('#colonne_centrale img, #colonne_centrale iframe').attr("width", function(){
		initWidth = $(this).attr("width");
		if($(this).attr('width') > 468) {
			$(this).attr('height',((468 * $(this).attr('height'))/initWidth)); 
			return 468;
		}
		return $(this).attr('width') * 1;
	}); 
	
	//FONCTION pour adapter les vidéos YouTube/DailyMotion/Vimeo et autres aux grandeurs de notre template BV
	$('#colonne_centrale object, #colonne_centrale p object').css("margin","0 auto");
	regexp = /\/meteo\//gi;
	var url_document = document.location; 
	//if( url_document.match(regexp) ) {} else {
		$('#colonne_centrale object, #colonne_centrale object embed, #colonne_centrale p object embed').each(function(){
			var targetWidth = 468;
			var initWidth;
			if($(this).attr('width') > targetWidth) {
				initWidth = $(this).attr("width");
				$(this).attr('height', ((targetWidth * $(this).attr('height'))/initWidth));
				$(this).attr('width', targetWidth);                       
			}   
		});
	//}


});

function activating_menu() {
	$("#menu_section ul li").removeClass("selected");
	$("#sousmenu ul").css("display","none");
	$("#sousmenu_"+nom_section).css("display","block");
	if($("#menu_section ul li#"+nom_section).hasClass("noir")) $("#menu_section ul li#"+nom_section).removeClass("noir");
	$("#menu_section ul li#"+nom_section).toggleClass("selected");
	$("#sousmenu").removeClass();
	$("#sousmenu ul li#"+nom_soussection+'').addClass("selected");
	$("#sousmenu").addClass(nom_section);
}

/* jQuery select element skinning
 * version: 1.0.2 (17/01/2009)
 * @requires: jQuery v1.2 or later
 * adapted from Derek Harvey code  http://www.lotsofcode.com/javascript-and-ajax/jquery-select-box-skin.htm
 * Licensed under the GPL license  http://www.gnu.org/licenses/gpl.html
 * Copyright 2009 Colin Verot */
(function ($) {
    $.fn.select_skin = function (w) {
        return $(this).each(function(i) {
			var s;
			var c;
			var text_skin;
            s = $(this);
			var zeID = $(this).attr("id");
            // create the container
			if(zeID != "regions") s.wrap('<div class="cmf-skinned-select"></div>'); else  s.wrap('<div class="cmf-skinned-select2"></div>');
			//if(zeID == "regions") text_skin="typo_noire"; else text_skin="typo_blanche";
			c = s.parent();
			c.children().before('<div class="cmf-skinned-text '+text_skin+'">&nbsp;</div>').each(function() { if (this.selectedIndex >= 0) $(this).prev().text(this.options[this.selectedIndex].innerHTML) });
            c.width(s.outerWidth()-2);
            c.height(s.outerHeight()-2);
            // skin the container
            c.css('background-color', s.css('background-color'));
            c.css('position', 'relative');
            // hide the original select
            s.css( { 'opacity': 0,  'position': 'relative', 'z-index': 100 } );
            // get and skin the text label
            var t = c.children().prev();
            t.height(c.outerHeight()-s.css('padding-top').replace(/px,*\)*/g,"")-s.css('padding-bottom').replace(/px,*\)*/g,"")-t.css('padding-top').replace(/px,*\)*/g,"")-t.css('padding-bottom').replace(/px,*\)*/g,"")-2);
            t.width(c.innerWidth()-s.css('padding-right').replace(/px,*\)*/g,"")-s.css('padding-left').replace(/px,*\)*/g,"")-t.css('padding-right').replace(/px,*\)*/g,"")-t.css('padding-left').replace(/px,*\)*/g,"")-c.innerHeight());
            t.css('color', s.css('color'));
            t.css('font-size', s.css('font-size'));
            t.css('font-family', s.css('font-family'));
            t.css('font-style', s.css('font-style'));
            t.css( { 'opacity': 100, 'overflow': 'hidden', 'position': 'absolute', 'text-indent': '0px', 'z-index': 1, 'top': 0, 'left': 0 } );
            // add events
            c.children().click(function() {  t.text(this.options[this.selectedIndex].innerHTML); });
            c.children().change(function() {  t.text(this.options[this.selectedIndex].innerHTML); });
        });
    }
}(jQuery));

/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */
(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		//alert("Je suis dans la ft height");
		tallest = (minHeight) ? minHeight : 0;
		$(this).each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("height",tallest);
		});
	}
})(jQuery);


// Gestion des cookies sur les sites de BV
// Écrire un cookie
function EcrireCookie(nom,valeur,expiredays) {
	var argv=EcrireCookie.arguments;
	var argc=EcrireCookie.arguments.length;
	var expires=new Date();
	expires.setDate(expires.getDate()+expiredays);
	document.cookie=nom+"="+escape(valeur)+
	((expires==null) ? "" : ("; expires="+expires.toGMTString()));
}
//Obtenir un cookie	
function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1) endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//Lire un cookie
function LireCookie(nom) {
	var arg=nom+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg) return getCookieVal(j);
		i=document.cookie.indexOf(" ",i)+1;
		if (i==0) break;
	}
	return null;
}

//FONCTION d'affichage en différé des publicités dans le div de contenu des sites de BV!
function pub_differe(nomPub) {
	var nom_div_pub = nomPub;
	if(document.getElementById("defer-"+nom_div_pub)!= null||document.getElementById("defer"+nom_div_pub)!= "") {
		document.getElementById(nom_div_pub).style.display = "block";
		document.getElementById(nom_div_pub).appendChild(document.getElementById("defer-"+nom_div_pub));
	}
}

