/*jslint passfail: true, white: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true, indent: 2 */
/*global $, google, GIcon, G_DEFAULT_ICON, GMarker, GEvent, GOverviewMapControl */

"use strict";

/**
 * Config
 */

var FONTSIZE_COOKIE_NAME = 'fontsize';

/**
 * Libs
 */

function goHome()
{
  document.location.href = '/';
}

function verifyFontSize(size)
{
  return (size === 'small' || size === 'normal' || size === 'big' || size === 'bigger');
}

function loadFontSize()
{
  return $.cookie(FONTSIZE_COOKIE_NAME);
}

function setFontSize(size)
{
  if (!verifyFontSize(size))
  {
    return false;
  }
  $('body').removeClass('small normal big bigger').addClass(size);
  return true;
}

function saveFontSize(size)
{
  if (!verifyFontSize(size))
  {
    return false;
  }
  $.cookie(FONTSIZE_COOKIE_NAME, size, {'path': '/'});
  return setFontSize(size);
}

var fixed_menu = false;

function hide_submenus(speed)
{
  $('#menu ul.depth2').each(function () {
    if ($(this).parent().hasClass('open'))
    {
      return;
    }
    $(this).hide(speed);
  });
}

function menu_hover_on()
{
  if (!fixed_menu)
  {
    $(this).addClass('hover');
  }
}

function menu_hover_off()
{
  if (!fixed_menu)
  {
    $(this).removeClass('hover');
  }
}

function topmenu_hover_on()
{
  if (!fixed_menu)
  {
    $(this).addClass('ui-state-hover');

    var current_menu = $(this).parent();
    current_menu.addClass('open');
    //hide_submenus('slow');
    current_menu.removeClass('open');
    current_menu.find('ul.depth2').show('slow');
  }
}

function topmenu_hover_off()
{
  if (!fixed_menu)
  {
    $(this).removeClass('ui-state-hover');
  }
}

/**
 * Google API
 */

var GOOGLE_API = typeof(google) === 'undefined' ? false : true;

if (GOOGLE_API)
{
  google.load('maps', '2.x');
}

var contactMap, contactMapCompanyPoint;

function contactMapZoomIn()
{
  contactMap.zoomIn();
}

function contactMapZoomHere()
{
  contactMap.setCenter(contactMapCompanyPoint, 13);
}

/**
 * Actions
 */

$(function () {
 /**
  * Spam protection
  */

  $('.liam').each(function () {
    var a = $(this).text()
      .replace(new RegExp(' ___[a-zA-Z0-9 -]+___ '), '@')
      .replace(new RegExp(' __[a-zA-Z0-9 -]+__ ', 'g'), '.')
    ;
    $(this).html('<a href="mai' + 'lt' + 'o:' + a + '" class="ema' + 'il">' + a + '</a>');
  });

 /**
  * Prelims
  */

  var absolute_url = document.location.toString(), fragment;
  if (absolute_url.match('#'))
  {
    fragment = absolute_url.split('#')[1];
  }
  else
  {
    fragment = undefined;
  }

 /**
  * Accessibility
  */

  $('#fontsizing').html('<a class="small" tabindex="570">A</a>&nbsp;<a class="normal" tabindex="571">A</a>&nbsp;<a class="big" tabindex="572">A</a>&nbsp;<a class="bigger" tabindex="573">A</a>');

  setFontSize(loadFontSize());

  $('#fontsizing > *').click(function () {
    saveFontSize($(this).attr('class'));
  });

 /**
  * Usability
  */

  $('a[href^="http://"]').not('.internal').attr({target: '_blank'}).addClass('external');
  $('a[rel="attachment"]').attr({target: '_blank'});
  $('a[rel="attachment"][href$=".pdf"]').after(' (<abbr title="Portable Document Format">PDF</abbr>)');

  $('#head').hover(function () {
//     $(this).fadeTo(0, .5).fadeTo('slow', 1);
  }, function () {
    // ...
  }).click(function () {
    goHome();
  });

 /**
  * Accordion effects
  */

  if (fragment)
  {
    $('#teams #' + fragment).parent().parent().prev().addClass('active');
    $('#countrylist #' + fragment).parent().addClass('active');
  }
  else
  {
    $('#team1').parent().addClass('active');
    $('#countrylist #europe').parent().addClass('active');
  }

  $('#teams').accordion({
    header: 'h2',
    active: '.active',
    autoHeight: false,
    animated: false
  });

  $('#countrylist').accordion({
    header: 'h2',
    active: '.active'
  });

  if (fragment)
  {
    $.scrollTo('#' + fragment);
  }

 /**
  * Layout/Style
  */

  // Main menu
  hide_submenus(0);
  $('#menu a')
    .click(function () {
      fixed_menu = true;
      $(this).css('font-weight', 'bolder');
    })
    .hover(menu_hover_on, menu_hover_off)
    .focus(menu_hover_on)
    .blur(menu_hover_off);
  $('#menu span.item').hover(menu_hover_on, menu_hover_off);
  $('#menu a.depth1')
    .hover(topmenu_hover_on, topmenu_hover_off)
    .focus(topmenu_hover_on)
    .blur(topmenu_hover_off);

  $('#menu ul.depth2:has(.active)').show();
  $('#menu a.depth1.active').parent().find('ul.depth2').show();

  // Move active branch to the top of the menu
  //$('#menu li.depth1:has(.active)').prependTo('#menu ul.depth1').addClass('current');
  var branch = $('#branch').attr('class');
  if (branch) {
    $('#menu li.depth1:has(.' + branch + ')').prependTo('#menu ul.depth1').addClass('current');
  }

  // Highlight last word of the info-intro boxes
  $('#introinfo a').each(function () {
    var parts = $(this).text().split(" ");
    $(this).html('<span>' + parts.join('</span>&nbsp;<span>') + '</span>');
    $(this).find(':last').addClass('hl');
  });

 /**
  * Maps
  */

  if (GOOGLE_API)
  {
    // Company map
    var contactbox_map_new = true;
    $('#contactbox .map-button').each(function () {
      $(this).after('<div id="map"></div>');
      $('#map').dialog({
        title: $(this).text(),
        width: 800,
        height: 550,
//         modal: true, Breaks gmap on reopen
        autoOpen: false
      });
    }).click(function () {
      $('#map').dialog('open');
      if (contactbox_map_new)
      {
        contactMap = new google.maps.Map2(document.getElementById("map"));
        contactMap.setUIToDefault();
        contactMap.addControl(new GOverviewMapControl());

        contactMapCompanyPoint = new google.maps.LatLng(48.2326, 16.4136);
        contactMap.setCenter(contactMapCompanyPoint, 4);

        var gsIcon = new GIcon(G_DEFAULT_ICON);
        gsIcon.image = "/images/maps/gsdot.png";

        var gsMarker = new GMarker(contactMapCompanyPoint, { icon: gsIcon });
        contactMap.addOverlay(gsMarker);

        var info = $('#contactbox .4map').html() + '<div><a href="javascript:contactMapZoomHere()">Zoom Here</a> - <a href="' +  $(this).attr('href') + '" target="_blank">Bigger map</a> - <a href="' +  $(this).attr('href') + '&amp;pw=2" target="_blank">Print</a></div>';
        gsMarker.openInfoWindow(info);

        GEvent.addListener(gsMarker, 'click', function () {
          gsMarker.openInfoWindow(info);
        });
      }
      contactbox_map_new = false;
      return false;
    });
  }

  // Partner maps
  $('#partnerlist .map').hide();
  $('#partnerlist .map-button').click(function () {
    var mapc = $(this).parent().parent().parent().parent().next();
    var link = $(this).attr('href');
    //$(this).parent().html('&nbsp;');
    mapc.show();
    mapc = mapc.find('td');
    if (!mapc.is('.loaded'))
    {
      mapc.addClass('loaded');
      mapc.html('<iframe width="780" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + link + '&amp;output=embed"></iframe>');
    }
    $(this).next().show();
    $(this).hide();
    return false;
  });
  $('#partnerlist .map-button-close').click(function () {
    var mapc = $(this).parent().parent().parent().parent().next();
    mapc.hide();
    $(this).prev().show();
    $(this).hide();
    return false;
  });
});
