var tw = {};

$(document).ready(function(){
	tw.misc();
	tw.mybox();
});

tw.misc = function() {
	(function($) {
	   $.fn.found = function(myFunction) {
		  if(this.length) {
			 myFunction.call(this);
		  }
	   };
	})(jQuery);
	}

tw.mybox = function() {
	$('#myboxList').found(function(){
		var myboxList	= $(this);
		var boxWidth	= myboxList.width();
		var numberBoxes = myboxList.find('#myboxListThumbs li').size();

		/* Href auslesen und gewünschten Moderator anzeigen */
		var locationHref  = window.location.href;
		var matches       = locationHref.match(/#myboxmod_(.+)$/);
		var memberToShow  = matches ? matches[1] : "";

		if(memberToShow != '') {
			var memberEntryNo = $('a[href=#myboxmod_' + memberToShow + ']').attr('rel');
			//var memberEntryNo = parseInt($('#myboxList>ul>li').index($('li#myboxmod_' + memberToShow)[0]) + 1);
		} else {
			var memberEntryNo = 1;
		}

		myboxList.attr('id', 'myboxListActive').find('#myboxList > ul').width((boxWidth * numberBoxes) + 'px');
		$('#myboxListThumbs li:nth-child(' + memberEntryNo + ') a').addClass('myboxListActive');
		$('#myboxList>ul>li:nth-child(' + memberEntryNo + ') div.myboxListImage').addClass('myboxListActive').animate({ marginTop: '190px' }, 150);

		var contentBoxHeight = ($('#myboxList>ul>li:nth-child(' + memberEntryNo + ') div.myboxListInfo').height() + 30);
		$('#myboxList').height(contentBoxHeight);

		var boxWidth     = $('#myboxList').width();
		var slideWidth   = ((memberEntryNo * boxWidth) - boxWidth);
		$('#myboxList > ul').css('marginLeft', '-' + slideWidth + 'px');
	});

	$('#myboxListThumbs a').bind('click', function(e){
		e.preventDefault();

		var e            = $(this);
		var urlTarget    = e.attr('href');
		var boxNo        = parseInt($('ul#myboxListThumbs>li a').index(this) + 1);
		var numberBoxes  = $('#myboxListThumbs li').size();
		var boxWidth     = $('#myboxList').width();
		var slideWidth   = ((boxNo * boxWidth) - boxWidth);

		/* Sendungen aller Moderatoren ausblenden */
		$('div.myboxPlusInfo').slideUp(300);

		/* Höhe der Inhaltsbox ändern */
		var contentBoxHeight = ($('#tm_' + boxNo + ' div.myboxListInfo').height() + 30);
		$('#myboxList').animate({
			height: contentBoxHeight + 'px'
		}, 500);

		/* Aktiven Mod setzen */
		$('a.myboxListActive').removeClass('myboxListActive');
		e.addClass('myboxListActive');

		$('#myboxList > ul').animate({
			marginLeft: '-' + slideWidth + 'px'
		}, 500);

		$('div.myboxListImage').each(function(){
			$(this).animate({ marginTop: '-190px' }, 150);
		});
		$('#tm_' + boxNo + ' div.myboxListImage').animate({
			marginTop: '190px'
		}, 250);

		window.location.href = urlTarget;
	});

	/* Sendungen der einzelnen Moderatoren ein- und ausblenden
	********************************************/

	$('div.myboxPlusInfo').hide();

	$('a.showModerator').bind('click', function(e){
		var moderator     = $(this).attr('href').substr(1);
		var displayStatus = $('#' + moderator).css('display');

		if(displayStatus == 'block') {
			$('#' + moderator).slideUp(500);
		} else {
			$('#' + moderator).slideDown(300);
		}

		return false;
	});
}
