$(document).ready(function() {

  initEvent($(".flags .country.choice a.flag"));

  $("#page").click(function(event) {
    hideLanguage(event);
  });
  

  $("#map-other-countries a").click(function(event) {
	  return RE4ARedirection("on-map-click",false,undefined,"WORLD");
  });
  
  
  $("#page a").click(function(event) {
    if (!$(this).parent().hasClass(".choice")) {
      setCookie($(this));
    } else {
    	return false;
    }
    
    if ($(this).parents("li.country:first").hasClass(".country6")) {
      var choice = $.cookie('vsct-re-redirect');
      
      // IF COOKIE >> go to user choice
      // ELSE >> Display pop up
      if(choice != undefined && choice == "1"){
          // User choose to go to rail europe, so we should remove tgv-europe user preferences for country/lang
          $.cookie("preference_distributed_country", null, { path: '/'});
          $.cookie("preference_country_lang", null, { path: '/'});
      }
      
      return TGVERedirection("on-map-click",false,undefined,"GB");
    }
  });

  applyFancyBox();
});

function setCookie(o) {

  var url = o.attr("href");  
  var positionPays = url.indexOf("europe.", 0);
  var positionLang = url.indexOf("/", positionPays);
  var positionFinPays = url.indexOf(":", positionPays+7);    

  if (positionFinPays == -1 || positionFinPays >= positionLang) {
    positionFinPays = positionLang;
  }

  var cdpays = url.substring(positionPays + 7, positionFinPays);
  var cdlang = url.substr(positionLang + 1, 2);

  if (cdpays == "com") {
    cdpays = "WW";
  }
  else {
    cdpays = cdpays.toUpperCase();
  }
  
  var options = {
    expires:365,
    path:'/'
  };

  $.cookie("preference_country_lang", cdpays + "_" + cdlang, options);

  if (cdpays == "WW") {
    var distributed_country_pos = url.indexOf("DISTRIBUTED_COUNTRY=", positionLang);
  
    if (distributed_country_pos > -1) {
      var distributed_country = url.substring(distributed_country_pos + 20, distributed_country_pos + 22);
      $.cookie("preference_distributed_country", distributed_country, options);
    }
  }
}  

function getCountryCode(container) {

  var r = (/(^|\s)(code_\S+)(\s|$)/).exec(container.attr("class"));

  if (r && r[2]) {
    return r[2];
  }
  return null;
}

function displayLanguage(event) {
  // Hide any previous displayed choice (fix JIRA EAG-298)
  $(".flags .country.choice.active").removeClass("active");
	
  var c = $(this).parents("li.country:first");
  var code = getCountryCode(c);

  if (!c.hasClass("active")) {
    c.addClass("active");
    
    if($("." + code, c).length == 0) {
    	var copy = $("." + code, "#foot").clone(true);

    	c.append(copy);
    	initEvent($("a",copy));
    }
  }
}

function hideLanguage(event) {

  if ($("li.active ul:first").length) {
    var $item_active = $("li.active ul:first");
    var listCoordinate = $item_active.offset();
    var listHeight = $item_active.outerHeight();
    var listWidth = $item_active.outerWidth();

    var minX = listCoordinate.left;
    var maxX = minX + listWidth;
    var minY = listCoordinate.top;
    var maxY = minY + listHeight;

    if (event.pageX < minX || event.pageX > maxX || event.pageY < minY || event.pageY > maxY) {
      var c = $(".flags .country.choice a.flag").parents("li.active:first");
      var code = getCountryCode(c);
      c.removeClass("active");
      $("ul",c).remove();
    }
  }
}

function initEvent(target) {	  
  target.click(displayLanguage);
}
