/**
 * @author Joachim Stehmann | www.daspixelium.de
 */

/* ----------------------------------------------------------------- */
	/**
	 * grays all listed articles 
	 */
	function muteLis()
	{
		$('div#contents ul#articleList li div.article').addClass( "muted" );
		$('div#contents ul#articleList li div.article a').addClass( "muted" );
		$('div#contents ul#articleList li div.bordered a').addClass( "umuted" );
	}

	/**
	 * highlights the selected list-element (-> article)
	 */
	function highlightLi( curLi )
	{
		var curArticle	= curLi.children('div.article');
		var story		= curArticle.children('div.story');
		var shortP		= story.children('div.short');
		var fullP		= story.children('div.full');
		var headline	= story.children('h4');
		var moreP		= shortP.children('p.link');
		var lessP		= fullP.children('p.link');
		var uplink		= curLi.children('div.bordered');
		
		curArticle.removeClass( "muted" );
		headline.children('a').removeClass( "muted" );
		moreP.children('a').removeClass( "muted" );
		lessP.children('a').removeClass( "muted" );
		uplink.children('a').removeClass( "umuted" );
	}

	/**
	 * highlights the article over which the mouse-cursor is currently hovering
	 * -> currently not used in the page
	 */
	function hoverCurLi()
	{
		$('div#contents ul#articleList li').each( function(){
			var curArticle	= $(this).children('div.article');
			var story		= curArticle.children('div.story');
			var shortP		= story.children('div.short');
			var fullP		= story.children('div.full');
			var headline	= story.children('h4');
			var moreP		= shortP.children('p.link');
			var lessP		= fullP.children('p.link');
			var uplink		= $(this).children('div.bordered');
			
			$(this).hover(
				function(){
					curArticle.removeClass( "muted" );
					headline.children('a').removeClass( "muted" );
					moreP.children('a').removeClass( "muted" );
					lessP.children('a').removeClass( "muted" );
					uplink.children('a').removeClass( "umuted" );
				 },
				function(){
					curArticle.addClass( "muted" );
					headline.children('a').addClass( "muted" );
					moreP.children('a').addClass( "muted" );
					lessP.children('a').addClass( "muted" );
					uplink.children('a').addClass( "umuted" );
				}
			)
		});
	}
	
/*
	function hoverCurLi()
	{
		$('div#contents ul#articleList li').each( function(){
			var curArticle	= $(this).children('div.article');
			var story		= curArticle.children('div.story');
			var shortP		= story.children('div.short');
			var fullP		= story.children('div.full');
			var headline	= story.children('h4');
			var moreP		= shortP.children('p.link');
			var lessP		= fullP.children('p.link');
			var uplink		= $(this).children('div.bordered');
			
			$(this).hover(
				function(){
					curArticle.removeClass( "muted" );
					headline.children('a').removeClass( "muted" );
					moreP.children('a').removeClass( "muted" );
					lessP.children('a').removeClass( "muted" );
					uplink.children('a').removeClass( "umuted" );
				 },
				function(){
					curArticle.addClass( "muted" );
					headline.children('a').addClass( "muted" );
					moreP.children('a').addClass( "muted" );
					lessP.children('a').addClass( "muted" );
					uplink.children('a').addClass( "umuted" );
				}
			)
		});
	}
*/
/* ----------------------------------------------------------------- */
	/**
	 * checks if a section is accessed from an article-detail-page
	 * if a section is accessed from an article-detail-page the section-overview-state is skipped 
	 */
	
	function fromArticleDetailPage()
	{
		if ( window.location.search != "" )
		{
			var rawParam	= window.location.search;
			var param		= rawParam.substr(1); 
			var keyValue	= param.split("=");

			var curHash		= window.location.hash;
			
			if ( keyValue[0] == "from" && keyValue[1] == "Ind" )
			{
				toggleSectionPagePresentation();
				
				if ( curHash != "" )
				{
					var $targetId	= $( curHash ), $targetAnchor = $('[name=' + curHash.slice(1) +']');
					var $target		= $targetId.length ? $targetId : $targetAnchor.length ? $targetAnchor : false;
					if ($target)
					{
						var targetOffset = $target.offset().top;
						$('html, body').animate({scrollTop: targetOffset}, 1);
						return false;
			  		}
				}
			}
		}
	}

/* ----------------------------------------------------------------- */

	function toggleSectionPagePresentation()
	{
		$('ul#articleList').show();
		$('div#overview').hide();
	}

	/**
	 * initially displays a short section-overview
	 * displays the full article list on click of a subnavi-link
	 * grays all listed articles
	 * highlights the selected article
	 */

	function toggleOverviewPagePresentation()
	{
		$('div#overview').show();
		$('ul#articleList').hide();

		$('.subnavi a').click( function()
		{
			toggleSectionPagePresentation();
			muteLis();
			
			var selectedArticleId	= $( this ).attr('href');
			var selectedLi			= $('div#contents ul#articleList').find( 'li'+selectedArticleId );

			highlightLi( selectedLi );
		});
	}
/* ----------------------------------------------------------------- */

	/**
	 * toggles short, full text
	 * toggles thumbnail, large image
	 */

	function showMore( sP, tI, fP, fI )
	{
		sP.hide();
		tI.hide();
		fP.show();
		fI.show();
	}

	function showLess( sP, tI, fP, fI )
	{
		fP.hide();
		fI.hide();
		sP.show();
		tI.show();		
	}

	var state = 'isShort';

	function showFullArticle(){
		$('.article').each( function(){
			var story		= $(this).children('div.story');
			var shortP		= story.children('div.short');
			var fullP		= story.children('div.full');
			var headline	= story.children('h4');
			var moreP		= shortP.children('p.link');
			var lessP		= fullP.children('p.link');

	
			var images		= $(this).children('div.img');
			var thumbImg	= images.children('img.thumb');
			var fullImg		= images.children('img.full');

			$(fullP).children('p.link').css("display","none");
			showMore( shortP, thumbImg, fullP, fullImg );
		});
	}


	function toggleArticlePresentation()
	{
		$('.article').each( function()
		{
			var story		= $(this).children('div.story');
			var shortP		= story.children('div.short');
			var fullP		= story.children('div.full');
			var headline	= story.children('h4');
			var moreP		= shortP.children('p.link');
			var lessP		= fullP.children('p.link');

	
			var images		= $(this).children('div.img');
			var thumbImg	= images.children('img.thumb');
			var fullImg		= images.children('img.full');
			
			headline.children('a').click( function()
			{
				switch ( state )
				{
					case 'isShort':
						showMore( shortP, thumbImg, fullP, fullImg );
						state	= 'isFull';
						return false;
					break;
					case 'isFull':
						showLess( shortP, thumbImg, fullP, fullImg );
						state	= 'isShort';
						return false;
					break;
				}
			})
			
			moreP.children('a.more').click( function()
			{
				showMore( shortP, thumbImg, fullP, fullImg );
				state	= 'isFull';
				return false;
			})
	
			lessP.children('a.less').click( function()
			{
				showLess( shortP, thumbImg, fullP, fullImg );
				state	= 'isShort';
				return false;
			})
		});
	}