//Common static functions (should be implemented as jquery extensions...)
function getIEVersionNumber() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

//Chargement
$(document).ready(function(){
  // Enable slider (Carousel enseignes)
  if (getIEVersionNumber() == '8') {
    $('.slider').cycle({
      fx:'fade',
      speed:   800, 
      timeout: 6000,
      pause:1
    });
  }
  else {
    $('.slider').cycle({
        fx:'scrollHorz',
        next:   '.slide-right', 
        prev:   '.slide-left',
        speed:   800, 
        timeout: 6000,
        pause:1
    });
  }
  


  //Enable ghost text in search textbox
  $('#motsCles').ghostText();
  
  $('#voirTousSecteurs').click(function() {$('#tousSecteurs').show(500, function(){$('#voirTousSecteurs').hide();$('#reduireTousSecteurs').show();});});
  $('#reduireTousSecteurs').click(function() {$('#tousSecteurs').hide(500, function(){$('#reduireTousSecteurs').hide();$('#voirTousSecteurs').show();});});

  //Cluetipe
  $('a.load-local').cluetip({
    arrows: true,
    local:true,
    cursor: 'pointer', 
    hoverIntent: {
      sensitivity: 3,
      interval: 100,
      timeout: 0
    }, 
    fx: {
      open: 'show', 
      openSpeed: ''
    }
  });

  //Widget
  $('#nav a#nb_selection').click(function (e) {
		e.preventDefault();
		$('#widget').load('http://www.lesechosdelafranchise.com/selection/selection.php');
		$('#widget').modal();
		return false;
  });
  
  //Bloc 200 enseignes
  var _max=0;
  $('#companies .col').each(
     function(){
         _max=Math.max(_max,this.offsetHeight)
     }
  );
  var lcolonne=0;
  var nbbloc=0;
  $('#companies .col').each(
     function(){
         $(this).find("div").each(
             function (){
                 lcolonne=lcolonne+this.offsetHeight;
                 nbbloc++;
             }
         );
         if( lcolonne < _max){
       	     if(nbbloc==1)adj=20; 
             else if(nbbloc==2) adj=10;
             else adj=0;
             adj=adj+(_max-lcolonne);
             $(this).find("div:last-child").css({'padding-bottom': adj+'px' });
         }
         lcolonne=0;
         nbbloc=0;
     }
  );
  
  //Header adv_search
	$('#frm_adv_search').submit(function(){
		if($('#motsCles').attr('value')==$('#motsCles').attr('defaultValue')){
//			alert('Merci de saisir votre recherche.');
			$('#motsCles').focus();
			return false;
		}
    });
  

  
  //Bloc actu tab slider
	//$("#networknews").css("min-height","147px").css("_height", "147px");
	
	
	var varTemp=Math.floor(Math.random()*3);
	if (varTemp == 0)
	{
	$("div.actualites").css("display", "block");
	$("div.interviews, div.videos").css("display", "none");
	$(".networkslide li.interviews, .networkslide li.videos").addClass("inactive");
	$(".networkslide li.actualites").removeClass("inactive");
	}
	if (varTemp == 1)
	{
	$("div.interviews").css("display", "block");
	$("div.actualites, div.videos").css("display", "none");
	$(".networkslide li.actualites, .networkslide li.videos").addClass("inactive");
	$(".networkslide li.interviews").removeClass("inactive");
	}
	if (varTemp == 2)
	{
	$("div.videos").css("display", "block");
	$("div.actualites, div.interviews").css("display", "none");
	$(".networkslide li.actualites, .networkslide li.interviews").addClass("inactive");
	$(".networkslide li.videos").removeClass("inactive");
	}
	
	$(".networkslide li").hover(function(){
		$(".networkslide li").not("this").addClass("inactive");
		$(this).removeClass("inactive");
		$("#networknews > div").css("display", "none");
		$(this).css("cursor", "pointer");
		
		if(this.className == "actualites") 
		{$("#networknews div.actualites").css("display", "block");}
		
		if(this.className == "interviews") 
		{$("#networknews div.interviews").css("display", "block");}
		
		if(this.className == "videos") 
		{$("#networknews div.videos").css("display", "block");}
		});
		


var selected_li = $("li").index( $('.selected')[0] );
var selected_ul=Math.floor(selected_li/6);

$('.vignettes').cycle({fx:'scrollHorz', speed:600,timeout:0, startingSlide:parseInt(selected_ul), next:'#slidepagination .pagination .suivante', prev:'#slidepagination .pagination .precedente', nowrap:1});

tooltip(); //fonction ligne 282

onglet_partenaires(); // fonction ligne

});

 


//OGM pour nb séléction
function modifier_nb_selection(_nb){
	$('#nb_selection').text(_nb+' enseigne'+(_nb>1 ? 's' : '')+' sélectionnée'+(_nb>1 ? 's' : ''));	
}
//OGM pour comparateur
function modifier_bouton_comparateur(_page,_res,_id){
	_msg=(!_res ? 'Ajouter au' : 'Enlever du')+' comparateur';
	_target='#res_comparateur'+_id;
	switch(_page){
		case 'enseignes':
			$(_target).empty();
			$(_target).append('<span class="icon"></span>'+_msg);
			$(_target).removeClass();
			$(_target).addClass('bloc');
			if(_res)
				$(_target).addClass('ok');
			else
				$(_target).addClass('comparateur');
			break;
//		case 'recherche':
//			if(_res){
//				_msg='Fait partie du comparateur';
//				_target=$(_target).parent().parent();
//				$(_target).empty();
//				$(_target).append(_msg);
//			}
//			break;
		case 'comparateur':
			window.location=window.location;
			break;
		case 'widget_selection':
			$('#img_maselection_'+_id).attr({ src: (!_res ? '/img/widget-icon-comp.gif' : '/img/widget-icon-compdelete.gif')});
			break;
		case 'rechercheList':
			$(_target).empty().append(_msg);
			break;
		default:
	}	
}
function modifier_bouton_selection(_page,_res,_id){
	_msg=(!_res ? 'Ajouter à' : 'Enlever de')+' ma sélection';
	_target='#res_selection'+_id;
	switch(_page){
		case 'enseignes':
			$(_target).empty();
			$(_target).append('<span class="icon"></span>'+_msg);
			$(_target).removeClass();
			$(_target).addClass('bloc');
			if(_res)
				$(_target).addClass('ok');
			break;
		case 'recherche':
			if(_res){
				$(_target).attr('alt','Enlever de ma sélection');
				$(_target).attr('src','../../../hab/icon-selected.gif');
			}
			else{
				$(_target).attr('alt','Ajouter à ma sélection');
				$(_target).attr('src','../../../hab/icon-star-plus.png');
			}
			break;			
//			if(_res){
//				_msg='Enlever de ma sélection';
//				_class='selected';
//			}
//			else{
//				_msg='Ajouter à ma sélection';
//				_class='selection';
//			}
//			$(_target).empty().append(_msg);
//			_target=$(_target).parent().parent();
//			$(_target).removeClass().addClass(_class);
			break;
		case 'comparateur':
			break;
		case 'widget_selection':
			if(!_res)
				$('#bloc_maselection_'+_id).remove();
			break;
		case 'rechercheList':
			$(_target).empty().append(_msg);
			break;
		default:
	}	
}

function comparateur(_href,_id,_type){
	$.getJSON(_href, {quoi: _type} , function(json){
		_page=json.page;
		_comparateur=json.comparateur;
		_selection=json.selection;
		modifier_nb_selection(_selection.nb);
		_message=(json.comparateur.message!='') ?  json.comparateur.message : json.selection.message;
		if(_message!=''){
			alert(_message);
		}
		if(_comparateur!='')
			modifier_bouton_comparateur(_page,_comparateur.res,_id);
		if(_selection!='')
			modifier_bouton_selection(_page,_selection.res,_id);
		return false;
	});
	return false;
}

/** Limiter le nb de caractères */
function compteCaractere(_elt,_max)
{
       var texte               = _elt.value;
       var imputNbCaractere    = document.getElementById('nbre_caractere');

       if(_elt.value.length > _max){
               alert('Ce champ est limité à '+_max+' caractères');
               _elt.value = texte.substring(0, _max);
       }
}
/// petite bulle de la page recherche enseigne
function tooltip(){	
		xOffset = 10;
		yOffset = 20;
	$("a.selection, a.selected").hover(function(e){
		this.t = $(this).find('img').attr('alt');
		this.title = "";
		$("body").append("<p id='selecttooltip'>"+ this.t +"</p>");
		$("#selecttooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px").fadeIn("fast");
    },
	function(){this.title = this.t; $("#selecttooltip").remove();});
	$("a.selection, a.selected").mousemove(function(e){$("#selecttooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");});
};

function onglet_partenaires(){
	// test a delete $("#article.partenaire").css("background","#ddd");
	$("#article.partenaire dd:last-child").css("border","none");
	$("#article.partenaire").addClass("onglets");
	$("#article.partenaire.onglets h2.active").find("+ dl").css("display","block");
	$("#article.partenaire.onglets h2").hover(
		function(){$(this).addClass('hover');},
		function(){$(this).removeClass('hover')}
	).click(function()
		{if("$(this):not(.active)"){
				$("#article.partenaire.onglets h2.active").removeClass("active").find("+ dl").hide();
				$(this).addClass("active").find("+ dl").show();
		}});
}