$(document).ready(function(){
  
  var arrowpos = {0: "4px", 1:"45px", 2:"86px"};
  var firstLoad = true;
  
  $(".language-switcher").click(function(){
    location.href = $(this).attr("href")+location.hash;
    return false;
  });
  
  var itemSubStatus = new Array();
	
	// Gamescom Anfang
	$('.item_gamescom a').click(function() {
		window.location.href = this.href;
	    return false;
	});

	$(".item_gamescom").click(function() {
		var	$that = $(this),
			$link = $("a", $that);
		$link.trigger('click');
	});
	$(".item_gamescom").mouseover(function() {
		var $that = $(this);
		if(!$that.hasClass('active')) {
			$("a", $(this)).css('color', '#fff');	
		}
	}).mouseout(function(){
		var $that = $(this);
		if(!$that.hasClass('active')) {
			$("a", $(this)).css('color', '#000');	
		}
	});
	// Gamescom Ende

  $(".item").click(function(){
    $(".item-container .item-sub").css("display", "none");
    $("#"+$(this).attr("rel")).css("display", "block");
    
    $(".arrow").css("top", arrowpos[$(this).index()]);
    
    $(".item").removeClass("active");
    $(this).addClass("active");
    
    if(itemSubStatus[$(this).attr("rel")] != undefined){
      $("#"+$(this).attr("rel")).children(".sub_menu[rel='"+itemSubStatus[$(this).attr("rel")]+"']").trigger("click");
    }
    else if(location.hash.length == 0){
      $("#"+$(this).attr("rel")).children(".sub_menu:eq(0)").trigger("click");
    }
    
    if($("#"+$(this).attr("rel")).children(".sub_menu").children("a.active").length == 0 && firstLoad != true){
      $("#"+$(this).attr("rel")).children(".sub_menu:eq(0)").trigger("click"); 
    }
    
  });
  
  $(".sub_menu").click(function(){
    
    $(".sub_menu").find("a").removeClass("active");
    $(this).find("a").addClass("active");
    
    location.hash=$(this).attr("rel");
    
    itemSubStatus[$(this).parent().attr('id')] = $(this).attr("rel");
    
    $(".item_content").html($(this).next(".item-desc").html());
    
    return false;
  });
  
  $(".item_jobs").click(function(){
    var content = $(this).children(".item_jobs-desc").html();
    $("#item_jobs-desc").html(content);
    
    location.hash=$(this).attr("rel");
    
    $(".item_jobs").removeClass("active");
    $(this).addClass("active");
  });
  
  if($("#items .item.active").length > 0){
    $("#items .active").trigger("click");
      firstLoad = false;
  }
  
  
  if(location.hash.length > 0){
    $(".sub_menu").each(function(){
      
      if($(this).attr('rel') == location.hash.substring(1)){
        $("#items div[rel='"+$(this).parent('.item-sub').attr("id")+"']").trigger("click");
        $(this).trigger("click");
      } 
    });
    
    $(".item_jobs").each(function(){
      if($(this).attr('rel') == location.hash.substring(1)){
        //$("#items div[rel='"+$(this).parent('.item-sub').attr("id")+"']").trigger("click");
        $(this).trigger("click");
      } 
    });
  }
  
});

