/**
 * Initialize the jQuery functions
 */ 
jQuery(function($)
{
  initHover();
  initPreview();
  initEventRegistion();
  initBestBets();
  initAutocompletion();
  initFancybox();
});

/**
 * This function handles the hover effects. 
 */ 
function initHover()
{
  if (! $('.result-item, .result-item-nolink, .emphasize-item').length ) return;
  
  /* result-item hover*/
  $(".result-item").mouseover(function()
  {
    $(this).removeClass("result-item").addClass("result-item-hover");
    window.status=$(this).find("a").attr("href");
    return true;
  }).mouseout(function()
  {
    $(this).removeClass("result-item-hover").addClass("result-item");
    window.status='';
    return true;
  }).click(function()
  {
    if($(this).find("a").attr("href"))
      document.location=$(this).find("a").attr("href");
  });
  
  /* result-item nolink hover*/ 
  $(".result-item-nolink").mouseover(function()
  {
    $(this).removeClass("result-item-nolink").addClass("result-item-nolink-hover");
  }).mouseout(function()
  {
    $(this).removeClass("result-item-nolink-hover").addClass("result-item-nolink");
  }).click(function()
  {
    if($(this).find("a").attr("href"))
      document.location=$(this).find("a").attr("href");
  });
  
  /* highlight emphazise item */
  $(".emphasize-item").mouseover(function()
  {
    $(this).removeClass("emphasize-item").addClass("emphasize-item-hover");
    window.status=$(this).find("a").attr("href");
    return true;
  }).mouseout(function()
  {
    $(this).removeClass("emphasize-item-hover").addClass("emphasize-item");
    window.status='';
    return true;
  }).click(function()
  {
    if($(this).find("a").attr("href"))
      document.location=$(this).find("a").attr("href");
  });
  
}

/**
 * This function handles the preview images. 
 */ 
function initPreview()
{
  if (! $('.thumbnail, .largeimg').length ) return;
  
  /* Preview Image functions */
  $(".thumbnail").click( function()
  {
    var id=$(this).attr("id");
    $("#"+id).hide();
    var id_l=id.replace(/s$/, "l");
    $("#"+id_l).show();
    return false;
  });
  
  $(".largeimg").click( function()
  {
    var id=$(this).attr("id");
    $("#"+id).hide();
    var id_s = id.replace(/l$/, "s");
    $("#"+id_s).show();
  });
}


/**
 * This function handles input field for the event registration.
 */ 
function initEventRegistion()
{
  if (! $('.hideContent').length ) return;
  
  /* Hide content for radio buttons */
  $(".hideContent .radio").change( function(){
    var feldID = $(this).attr("name").replace("feld_", "");
    if ($(this).hasClass("contentHide"))
    {
      $("#"+feldID).hide();
    }
    if ($(this).hasClass("contentShow"))
    {
      $("#"+feldID).show();
    }
  });
}
  

/**
 * This function handles best bets more results disclosure.
 */ 
function initBestBets()
{
  if (! $('.more_bestbets').length ) return;
  
  /* best bets, hide/show div "more_bestbets" */
  $(".more_bestbets").hide();
  $("#show_link").show();
  
  $("#show_link").click( function()
  {
    $(".more_bestbets").show('slow');
    $("#show_link").hide();
    $("#hide_link").show();
  });
  $("#hide_link").click( function()
  {
    $(".more_bestbets").hide('slow');
    $("#show_link").show();
    $("#hide_link").hide();
  });
}

/**
 * This function handles the search autocompletion. 
 */ 
function initAutocompletion()
{
  if (! $('#search').length ) return;
  
  function findValue(li)
  {
    if ( li == null )
    {
      return alert("No match!");
    }
    
    // if coming from an AJAX call, let's use the Id as the value
    if ( !!li.extra )
    {
      var sValue = li.extra[0];
    }
    // otherwise, let's just display the value in the text box
    else
    {
      var sValue = li.selectValue;
    }
  }
  
  function selectItem(li)
  {
    findValue(li);
  }
  
  function formatItem(row) {
    return " <span class=\"count\">" + row.count + " Treffer</span><span class=\"word\">" + row.name + "</span>";
  }
  
  function prep_data(data) {
    var rows = new Array();
    for(var i=0; i<data.length; i++)
    {
      rows[i] = { data:data[i], value:data[i].count, result:data[i].name };
    }
    return rows;
  }
  
  $("#search").autocomplete(
    "/rxml/common/inc/autocomplete.json",
    {
      autoFill:false,
      cacheLength:10,
      dataType: "json",
      delay:200,
      extraParams: {
        location: function() { return $("#search_t").val(); }
      },
      formatItem: formatItem,
      matchSubset:true,
      matchContains:true,
      max:10,
      minChars:3,
      onItemSelect:selectItem,
      onFindValue:findValue,
      parse: prep_data,
      scroll: false,
      width:225
    }
  );

  $("#searchbox-q").autocomplete(
    "/rxml/common/inc/autocomplete.json",
    {
      autoFill:false,
      cacheLength:10,
      dataType: "json",
      delay:200,
      extraParams: {
        location: function() { return $("#searchbox-ul").val(); }
      },
      formatItem: formatItem,
      matchSubset:true,
      matchContains:true,
      max:10,
      minChars:3,
      onItemSelect:selectItem,
      onFindValue:findValue,
      parse: prep_data,
      scroll: false,
      width:325
    }
  );
}


/**
 * Older scripts. Should be translated to jQuery syntax. 
 * */

/* Variable zur Zwischenspeicherung der Seitenbreite */ 
var prevWidth = 0;

/* IMAGEMAPS */
var T; // holds the timeout for imagemaps;
var activeCountry = ''; // holds id of visible Country for imagemaps



/* Seitenbreite für den Druck im IE auf auto setzen */ 
function removeWidth() {
  pm = document.getElementById("page-margins");
  prevWidth = pm.style.width;
  pm.style.width = "auto";
}

/* Seitenbreite zurücksetzen */ 
function restoreWidth() {
  pm = document.getElementById("page-margins");
  pm.style.width = prevWidth;
}



/* open url */
function openUrl ( url ) {
  if (url)
    window.location.href = url;
}

/* set classname of object O to toggle (mainly for IE's pseudohover) */
function toggleClass ( O, toggle, statusline ) {
  if ( O ) {
    O.className = toggle;
  }
  if (statusline) {
    window.status = statusline;
  }
}

/* restore the default value of an input field */ 
function removeDefault( O, text ) {
  if ( O.value == text )
    O.value = "";
  else if ( O.value == 0 ) // for konquerer+safari
    O.value = "";
}

/* set a default value for an input field */ 
function setDefault( O, text ) {
  if ( O != null && O.value == "" )
    O.value = text;
}

//=====================================================
// IMAGEMAP
// show country-information
// country ...  selector for ids
//=====================================================

function showMore ( country ) {
  //	document.getElementById('body').style.backgroundColor = 'red';
  if ( activeCountry != '' ) {
    hideMore ( activeCountry );
  }
  if ( document.getElementById( country ) ) {
    document.getElementById( country ).style.display = 'block';
  }
  activeCountry = country;
}

//=====================================================
// hide country-information
// country ...  selector for ids
//=====================================================

function hideMore ( country ) {
  if ( document.getElementById( country ) ) {
    activeCountry = '';
    document.getElementById( country ).style.display = 'none';
  }
}

function initFancybox () {
  if (! $('.imageSmall a').length ) return;
  
  $('.imageSmall a').fancybox({
    'titlePosition': 'inside',
    'overlayShow': true,
    'overlayOpacity': 0.6,
    'opacity': true,
    'speedIn': 300,
    'speedOut': 300,
    'changeSpeed': 200
  });
}

