/* viZaar Menu functions
 * (C) 2007 Weber eBusiness Services, Andre Simmert
 */

var menu_active = 0;

function show_composing(id, lang)
{
  $("#composings .composing_"+id+"_"+lang).show();
}

$(document).ready(function()
{

	$("#nav").children("li").hover(function() {
		$("#covers div").hide();
		$(this).css("background-color", "#9a9b9d");
	}, function() {
		$(this).css("background-color", "#9d0c15");
	});

	$("#nav li").hover(function() {
	  menu_active++;
	  $(this).siblings("li").children("ul").hide();
	  $(this).children("ul").show();
	}, function() {
    $("#composings img").hide();
	});

	$("#nav li.hasSub").hover(function() {
	  $("#covers .cover1").show();
	}, function() {});

	$("#nav ul li").hover(function() {
	  $(this).css("background-color", "#b5b4b2");
	}, function() {
	  $(this).css("background-color", "#9a9b9d");
	});

	$("#nav li ul").children("li").hover(function() {
		$("#covers .cover2").hide();
	}, function() {});

	$("#nav ul li.hasSub, #nav ul li ul li").hover(function() {
	  $("#covers .cover2").show();
	}, function() {});

	$("#nav").hover(function() {
	}, function() {
	  menu_active = 0;
		setTimeout(function(){
		  if (menu_active == 0) {
		    menu_active = 0;
				$("#nav ul li ul").hide();
				$("#nav ul").hide();
				$("#composings img").hide();
				$("#covers div").hide();
			}
		},1000);
	});
	
	
	
	/* Stage*/
	loadHomeStageNavHandler();

});

function loadHomeStageNavHandler() {
  
  // Change png-images for IE6
  if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
    var img_src = $('#stage .image_container img').attr('src');
  }  
  
  var repeatActionAfterTime = 8000;
  
  var link_element = "";
  var link_num = 0;
  var numberOfSlides = $('.stage_content').length;

  $('#stage .stage_content .nav_title').each(
    function() {
      if( $(this).html() == "" ) {
        $(this).html( $('#stage .stage_content .nav_title').index(this) + 1 );
      }
      
      if(!$(this).parent().hasClass('last_stage_element') ) {
        link_element = '<li class="stage_navlink_container stage_navlink_container'+link_num+'">' +
                       '<div class="stage_navlink">' + $(this).html() + '</div>' +
                       //'<div class="stage_active active">' + $(this).html() + '</div>' +
                       '</li>';
                       //'</li><li class="stage_link_'+link_num+' active" style="display: none;">' + $(this).html() + '</li>';
        $('#stagenav').append(link_element);
        link_num++;
      }
    }
  );
  
  $('#stagenav').wrapInner('<ul></ul>');
  $('#stagenav .active').eq(0).show();

  $('#stagenav li.stage_navlink_container:first').addClass('active_stage_navlink');

  $('#stagenav li.stage_navlink_container .stage_navlink').click(
    function() {
      // Only allow one animation at the same time
      var n = $.queue( $('div.stage_content')[0], "fx" );
      if (n.length >= 1) {
        return false;
      }
      
      var link_index = $('#stagenav li.stage_navlink_container .stage_navlink').index($(this));
      
      if( !$(this).parent().hasClass('active_stage_navlink') ) {
        if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
          $('#stage .stage_content').css('z-index', '1').hide().eq(link_index).css('z-index', '2').show();
        } else {
          $('#stage .stage_content').css('z-index', '1').fadeOut(800).delay(0).eq(link_index).css('z-index', '2').fadeIn(1000);
        }
        
        $('#stagenav li.stage_navlink_container').removeClass('active_stage_navlink');
        $(this).parent().addClass('active_stage_navlink');
      }
    }
  );

  /* fire automatically */
  
  timer = setInterval( function() {
    time_sliding();
  }, repeatActionAfterTime );
  
  $('.stage_container').hover(
    function() {
      clearInterval(timer);
    }, function() {
      timer = setInterval( function() {
        time_sliding();
      }, repeatActionAfterTime );
    }
  );
  
  
  $('#stagenav a').click(
    function() {
      return false; // Disable link
    }
  );
}



function time_sliding(slideWidth, currentPosition) {
  $('.stage_content:visible').each(
    function() {
      
      var stage_length = $('.stage .stage_content').length;
      var stage_index = $('.stage .stage_content').index($(this));
      stage_index++;
      
      if(stage_index == stage_length) {
        stage_index = 0;
      }
      
      if(window.navigator.userAgent.indexOf("MSIE 6") > -1){
        $('.stage_content').css('z-index', '1').hide().eq(stage_index).css('z-index', '2').show();
      } else {
        $('.stage_content').css('z-index', '1').fadeOut(800).delay(0).eq(stage_index).css('z-index', '2').fadeIn(1000);
      }
      
      $('#stagenav li.stage_navlink_container').removeClass('active_stage_navlink');
      $('#stagenav li.stage_navlink_container').eq(stage_index).addClass('active_stage_navlink');
      
    }
  );
}




