// Fonctions js generiques a utilisation recurrente
function highlightMatchBySport(sport_name, id)
{
  document.getElementById('match_'+id).style.background = 'white url(/images/new/match_'+sport_name+'.jpg) no-repeat center top';
}

function restoreMatchBySport(id)
{
  document.getElementById('match_'+id).style.background = 'white url(/images/new/match_out.jpg) no-repeat center top';
}

var select_tournament = function(name)
{
  document.getElementById('match-search-what-input').style.color = '#000000';
  document.getElementById('match-search-what-input').value = name;
  document.getElementById('match-search-to-input').value = '';

  form_observer.onElementEvent();
}

var select_tv_channel= function(channel_id)
{
  document.getElementById('channels').value = channel_id;
  chaines_observer.onElementEvent();
}

var select_directory_city = function(city)
{
  document.getElementById('requete_ou').value = city;

  if(document.getElementById('requete_qui').value = '(Ex : Au dernier métro)')
  {
    document.getElementById('requete_qui').value = ''
  }

  document.form_directory.submit();
}

function clearInputValue(id, value)
{
  if (document.getElementById(id).value == value)
  {
    document.getElementById(id).value = '';
  }
}

var clicked_input = function(item, default_value)
{
  clearInputValue(item, default_value);
  document.getElementById(item).style.color = '#000';
}

function submitWithEnter(form_name, keycode)
{
  keycode = keycode.which;

  if (keycode == 13)
  {
    document.getElementById(form_name).submit();
  }
}

// @author Cyrille - switching bar details tabs and content
function switchBarTabs(id)
{
  var bar_tabs = new Array('bar-presentation','bar-opening-times');

  for(i=0;i<=bar_tabs.length-1;i++)
  {
    $(bar_tabs[i]).className = '';
    $(bar_tabs[i]+'-content').className = '';
  }

  $(id).className = 'selected-tab';
  $(id+'-content').className = 'selected-content';
}

function show_page(item)
{
  $('details-box').style.display='none';
  $('page-transports').style.display='none';
  $('page-bar-team').style.display='none';
  $(item).style.display='block';
}

function displayUserSubMenu(id)
{
  var status = $('sub-'+id).style.display;
  if (status != 'block')
  {
    $(id).style.backgroundImage = 'url(/images/new/arrow_down_green_out.gif)';
    $('sub-'+id).style.display = 'block';
  }
  else
  {
    $(id).style.backgroundImage = 'url(/images/new/arrow_right_green_out.gif)';
    $('sub-'+id).style.display = 'none';
  }
}

function loadBarPage()
{
  if ($('select-bar').value != '')
  {
    document.location = $('select-bar').value;
  }
}

/** Verifie si on ne post pas le message par defaut
 * @modified laurentb Retourner true quand c'est OK et interdire le texte vide
 */
function checkMessage(id, default_text, alert_text)
{
  if ($(id).value == default_text || $(id).value == '')
  {
    alert(alert_text);
    return false;
  }
  else
  {
    return true;
  }
}

/**
 * Fonctions JS pour la géolocalisation
 */

/**
 * Masque la div qui affiche la zone 
 * et affiche la div qui permet de rechercher une zone
 *
 * @author Vincent
 * @since 2008-12-23
 */
function show_zone_input ()
{
  $('change-geozone').style.display = 'none';
  $('select-geozone').style.display = 'block';
}

/**
 * Recherche si la valeur p_val est dans p_array
 *
 * @return -1 si pas trouvé sinon l'indice du tableau
 *
 * @author Vincent
 * @since 2008-12-239
 */
var geozone_in_array = function(p_val, p_array)
{
  for(var key in p_array)
  {
    if(p_array[key].toLowerCase() == p_val.toLowerCase())
    {

      return key;
    }
  }

  return false;
}

/**
 * Recherche l'adresse passé en paramètre
 */
var g_search_location = function (address, callback, from)
{
  geocoder = new GClientGeocoder();
  geocoder.setBaseCountryCode('FR');

  geocoder.getLocations(address, function(response){callback(address, response, from);});
}

function js_ucwords(str){
   // split string
   firstChar = str.substring(0,1);
   remainChar = str.substring(1);

   // convert case
   firstChar = firstChar.toUpperCase(); 
   remainChar = remainChar.toLowerCase();

   return firstChar + remainChar;
}

/**
 * Localisation du client et met à jour la homepage
 *
 * @author Vincent
 * @since 2009-01-05
 */
var client_localisation = function ()
{
  var location = google.loader.ClientLocation;
  
  if (location)
  {
    var current_city = location.address.city;
    var zone_id = geozone_in_array(current_city, geozone_city_tab_js);

    if(zone_id)
    {
      $('input_zone_id').value = zone_id;
      
      //Requête ajax
      form_observer.onElementEvent();
     
      $('change-geozone-name').innerHTML = js_ucwords(current_city);
      $('search-geozone-input').value = "Vous êtes à "+js_ucwords(current_city)+", précisez votre adresse ici.";
      $('default_geozone_input').value = "Vous êtes à "+js_ucwords(current_city)+", précisez votre adresse ici.";
    }
    else
    {
      g_search_location(current_city, change_to_department);
    }
  }
}

/**
 * Récupère le département et modifie la homepage
 * 
 * @author Vincent
 * @since 2009-01-05
 */
var change_to_department = function (search_zone, response)
{   
  //Trouvé par google
  if(response.Status.code == 200)
  {
    var department = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
    
    $('change-geozone-name').innerHTML = js_ucwords(department);

    var zone_id = geozone_in_array(department, geozone_departement_tab_js);

    if(zone_id)
    {
      $('input_zone_id').value = zone_id;
      
      //Requête ajax
      form_observer.onElementEvent();
      
      $('search-geozone-input').value = "Vous êtes en "+js_ucwords(department)+", précisez votre adresse ici.";
      $('default_geozone_input').value = "Vous êtes en "+js_ucwords(department)+", précisez votre adresse ici.";
    }
  }

  return false;
}

/**
 * Retourne la valeur si elle existe dans le tableau
 *
 * @author Vincent
 * @since 2009-03-23
 */
var get_value_if_exist = function(p_val, p_array)
{
  for(var key in p_array)
  {
    if(p_array[key].toLowerCase() == p_val.toLowerCase())
    {

      return p_array[key];
    }
  }

  return false;
}

