$(function () {
    // set the textfade link
	$('.nextProjectAndTextFadeLinks').append('<div class="textFade"><span></span><a href="#">text ausblenden</a> </div>');
	// text fading
	var fadeText = function () {
	    var fadeInText = 'text einblenden';
		var fadeOutText = 'text ausblenden';
		// fade in if text is already faded out
		// fade out if text is visible
		if ($(this).hasClass('fadedOut')) {
			$('.overlay').css({display: 'block', opacity: 0.65, backgroundColor: '#000'});
			$('.nextProject').fadeIn();
			if($('body').hasClass('contentPage')) {
				$('.text, .fileUnder').fadeIn();
			}
			if($('body').hasClass('navPage')) {
				$('.mainNav ul ul').fadeIn();
			}
			$('.textFade a').text(fadeOutText);
			// $('.textFade span').css({"});
			$('.textFade span').removeClass('opaque').addClass('transparente');
			$(this).removeClass('fadedOut');
		} else {
			$('.overlay, .text, .mainNav ul ul, .nextProject, .fileUnder').fadeOut();
			$('.textFade a').text(fadeInText);
			// $('.textFade span').css({background: '#000', opacity: '.65', filter: "alpha(opacity='65')"});
			$('.textFade span').addClass('opaque').removeClass('transparente');
			$(this).addClass('fadedOut');
		}
		return false;
	}
	
	// register handlers
	$('.textFade a').bind('click', fadeText);

    // get the items
	var items = $('.item');
	
	var showItem = function (e, i) {
		// always hide the navigation
		$('.mainNav ul ul').fadeOut();
		// perform animation only if item is not already active
		if(!$(e).parent().hasClass('active')) {
			$('.textFade').show();
			$('.text').show();
		    $('.item').fadeOut('slow');
			$(items[i]).fadeIn('slow');
			if(!$('.textFade a').hasClass('fadedOut')) {
				$('.fileUnder, .nextProject').fadeIn('slow');
			}
			if($('.textFade a').hasClass('fadedOut')) {
			    $('.text, .overlay').hide();	
			}
		}
		// show the text if it is hidden
		if(!$('.textFade a').hasClass('fadedOut')) {
		    $('.text').show();
		}
		if($('.fileUnder').css('display') == 'none' && !$('.textFade a').hasClass('fadedOut')) {
			$('.fileUnder').show();
		}
		if($('.nextProject').css('display') == 'none' && !$('.textFade a').hasClass('fadedOut')) {
			$('.nextProject').show();
		}
		if($('.textFade').css('display') == 'none' && !$('.textFade a').hasClass('fadedOut')) {
			$('.textFade').show();
		}
		if($(items[i]).hasClass('itemNoTextFade') && !$('.textFade a').hasClass('fadedOut')) {
			$('.textFade').hide();	
		}
    }
	
	// build the pager
	var buildPager = function () {
		var l = items.length;
		var html = '<ul>';
		for (i = 0; i < l; i++)	{
			html += '<li><a href="#" rel="';
			html += i;
			html += '"><span class="bg"></span><span class="txt">';
			html += i + 1;
			html += '</span></a></li>';
		}
		html += '</ul>';
		$('.paging').append(html);
	}
	
	buildPager();
	
	var showItemInit = function () {
		$('.paging li').eq(0).addClass('active');
		if($(items[i]).eq(0).hasClass('itemNoTextFade')) {
			$('.textFade a').hide();							  
		}
    }
	showItemInit();	
	// register pager event handler
	$('.paging a').bind('click', function () {
	   showItem($(this), $(this).attr('rel'), false);
	   $('.paging span.bg').css({opacity: .27});
	   $('.paging li').removeClass('active');
	   $(this).closest('li').addClass('active').find('span.bg').css({opacity: 1});
	   return false;
	});
	
	// pager eye candy 
	$('.paging span.bg').hover(function () {
		if (!$(this).parent().parent().hasClass('active')) {
			$(this).animate({
            	opacity: .7
       		}, 500);
		}
    }, 
	function () {
		if (!$(this).parent().parent().hasClass('active')) {
			$(this).animate({
				opacity: .27
			}, 500);
		}
	});
	
	var fadeInCurrentNavBranch = function () {
		$('.mainNav li').removeClass('activeTemp');
		$('.mainNav li.activeDisabled').addClass('active').removeClass('activeDisabled');
		var links = $('.mainNav ul li');
		links.each(function () {
			if($(this).hasClass('active')) {
				$(this).find('ul').first().fadeIn('slow');
			}
		});
	}
	// register file under links
	// to open the navigation
	$('.fileUnder').bind('click', function () {
		// fade out text
		$('.fileUnder, .text,  ul ul, .nextProject, .textFade').fadeOut();
		// fade in navigation - but only in the branch with active links
		fadeInCurrentNavBranch();
		return false;
    });
	
	// navigate
	$('.mainNav a').bind('click', function () {
		// hide all submenues
		$('.mainNav ul ul').hide();
		$('.mainNav li').removeClass('activeTemp');
		$('.mainNav li.active').removeClass('active').addClass('activeDisabled');
		// hide all text and show the overlay
		$('.text, .nextProject, .fileUnder, .textFade').fadeOut();
		$('.overlay').show();
		if($(this).parent().hasClass('active') || $(this).parent().hasClass('activeDisabled')) {
			fadeInCurrentNavBranch();
		}
		// animate the overlay to the right width
		$('.overlay').animate({width: '300px'});
		
	    // show the submenu if there is no link to a page
		if($(this).attr('href') == '#') {
			$(this).parents('ul').show();
			$(this).parents('li').addClass('activeTemp');
		    $(this).parent().find('ul').first().fadeIn('slow');
			return false;
		}
	});
});
