var tw = {};

$(document).ready(function(){
	tw.ui();
	tw.comments();
	tw.misc();
	tw.team();
});

/***********************************************/

tw.ui = function() {

	/* FontDetection für Calibri (FontExtend)
	********************************************/
	if($.fontAvailable('Calibri') == false) {
		$('body').addClass('altFontFace');
	}

	/* IE6-User werden beglückt
	********************************************/
	if($.browser.msie && ($.browser.version <= '6.0')) {
		var infoStageStatus = $('#infoStage').css('display');
		var title		   = 'Wichtiger Hinweis';
		var image		   = '<img src="/pub/images/body/ie6out.gif" width="47" height="50" alt="" title="" class="L" />';
		var message		 = 'Du benutzt einen veralteten Internet Explorer von Microsoft, der eher ins Museum als auf deine Festplatte gehört.<br />' + "\n";
			message		+= 'Diese Webseite kannst du nutzen, doch mit einem richtig <del>guten</del> neuen Browser, sieht diese Webseite echt viel geiler aus.' + "\n";

		if(infoStageStatus == 'block') {
			var html  = '<h3>' + title + '</h3>' + "\n";
				html += message;
			$('#infoStageInner').prepend(html);
		} else {
			setErrorBox(title, image, message);
		}

		var changeEvent = 'click';
	} else {
		var changeEvent = 'change';
	}

	/* HeaderInfo einblenden bei Bedarf
	********************************************/

	$('#header h1 a').each(function(){
		$(this).hover(
			function() {
				headerAnimation = 1;
				$.timer(1000, function(timer){
					if(headerAnimation == 1) {
						$('#headerInfo').animate({
							marginTop: '110px'
						}, {
							duration: 500,
							queue: true
						});
					}
					timer.stop();
				});
			},
			function() {
				headerAnimation = 0;
				$('#headerInfo').animate({
					marginTop: '-110px'
				}, 500);
			}
		).focus(
			function() {
				$('#headerInfo').animate({
					marginTop: '110px'
				}, 500);
			}
		).blur(
			function() {
				$('#headerInfo').animate({
					marginTop: '-110px'
				}, 500);
			}
		);
	});

	/* InfoStage
	********************************************/

	$('#infoStageCloseLink a').live('click', function(){
		$('div#infoStage').slideUp(300, function(){
			$(this).remove();
		});
		return false;
	});

	$('#globError').each(function(e){
		var title   = $(this).find('h3').text();
		var image   = '';
		var message = $(this).find('h3').next().html();

		$(this).remove();
		setErrorBox(title, image, message);
	});

	/* ErrorBox
	********************************************/

	function setErrorBox(title, image, errors) {
		var outputHeader = '<div id="infoStage">' + "\n";
		var errorType    = typeof errors;

		outputHeader += '	<div id="infoStageInner" class="viewport">' + "\n";
		outputHeader += '		<div class="clearfix">' + "\n";

		if(image != '') {
			outputHeader += image;
		}

		outputTitle = '			<h3>' + title + '</h3>' + "\n";

		if(errorType == 'string') {
			outputText = '<p>' + errors + '</p>';
		} else {
			outputText = '			<ul>' + "\n";

			for(var i = 0; i < errors.length; i++) {
				outputText += '				<li class="msg1">' + errors[i] + '</li>' + "\n";
			}

			outputText += '			</ul>' + "\n";
		}

		outputFooter  = '			<p id="infoStageCloseLink" class="noMargin small"><a href="#">Diese Meldung ausblenden / schließen</a></p>' + "\n";
		outputFooter += '		</div>' + "\n";
		outputFooter += '	</div>' + "\n";
		outputFooter += '</div>' + "\n";

		var infoStage = $('#infoStage');

		if(infoStage.length == 1) {
			var output = (outputTitle + outputText);
			infoStage.find('#infoStageCloseLink').before(output);
		} else {
			var output = (outputHeader + outputTitle + outputText + outputFooter);
			$('#stage').before(output);
			$('#infoStage').hide().slideDown(500);
		}
		window.location.href='#infoStage';
	}
	
	/* FancyBox
	********************************************/
	
	$('img.imageZoom').each(function(){
		$(this).closest('a').fancybox({
			zoomSpeedIn: 200,
			zoomSpeedOut: 200
		});
	});
}

/***********************************************/

tw.comments = function() {

	/* Kommentare zoomen
	********************************************/

	contentWidth = $('#content').width();
	sidebarWidth = $('#sidebar').width();

	$('a.zoomComments').bind('click', function(e){
		e.preventDefault();

		var self       = $(e.target).attr('class');
		var content    = $('#content');
		var sidebar    = $('#sidebar');
		var moderators = $('#moderatorsByPost');

		if(sidebar.width() == sidebarWidth) {
			content.addClass('nowrap').animate({ width: 0 }, 400);
			moderators.hide(500);
			sidebar.animate({ width: 940 }, 500);

			// Kommentarfeld verschieben
			var respondForm = $('#respondHolder1').html();
			$('#respondHolder2').html(respondForm);
			$('#respondHolder1').empty();
		} else {
			sidebar.animate({ width: sidebarWidth }, 200, '', 'showContent');
			content.animate({ width: contentWidth }, 300).removeClass('nowrap');
			moderators.show(500);

			// Kommentarfeld verschieben
			var respondForm = $('#respondHolder2').html();
			$('#respondHolder1').html(respondForm);
			$('#respondHolder2').empty();
		}
	});

	/* Aktuellen Kommentar anspringen
	********************************************/

	var url = window.location.href.split('#');

	if(url.length > 1) {
		if(url[1].indexOf('comment-') != -1) {
			var comment   = $('li[id=' + url[1] + ']');
			var commentBg = comment.css('background-color');

			comment.css('background-color', 'rgb(255,255,0)').animate( { backgroundColor: commentBg }, 1800);
		}
	}
}

/***********************************************/

tw.misc = function() {
	(function($) {
	   $.fn.found = function(myFunction) {
		  if(this.length) {
			 myFunction.call(this);
		  }
	   };
	})(jQuery);

	$('#twIntro img').bind('click', function(e){
		FlashReplace.replace("twIntro", "/pub/swf/twIntro.swf", "twIntroX", 280, 158);
	});

	/* Buttons versenden Formular */
	$('button.formSubmit').click(function(e){
		var value = $(e.target).attr('value');
		$(e.target).closest('form').submit();
	});
	/*******************************************/

	$('div.sectionBox div.sectionBoxRow:even').addClass('sectionBoxRowAlt');

	/*******************************************/
	}

tw.team = function() {
	$('#teamList').found(function(){
		var teamList	= $(this);
		var boxWidth	= teamList.width();
		var numberBoxes = teamList.find('#teamListThumbs li').size();

		/* Href auslesen und gewünschten Moderator anzeigen */
		var locationHref  = window.location.href;
		var matches       = locationHref.match(/#teammod_(.+)$/);
		var memberToShow  = matches ? matches[1] : "";

		if(memberToShow != '') {
			var memberEntryNo = $('a[href=#teammod_' + memberToShow + ']').attr('rel');
			//var memberEntryNo = parseInt($('#teamListMembers>ul>li').index($('li#teammod_' + memberToShow)[0]) + 1);
		} else {
			var memberEntryNo = 1;
		}

		teamList.attr('id', 'teamListActive').find('#teamListMembers > ul').width((boxWidth * numberBoxes) + 'px');
		$('#teamListThumbs li:nth-child(' + memberEntryNo + ') a').addClass('teamListActive');
		$('#teamListMembers>ul>li:nth-child(' + memberEntryNo + ') div.teamListMembersImage').addClass('teamListActive').animate({ marginTop: '190px' }, 150);

		var contentBoxHeight = ($('#teamListMembers>ul>li:nth-child(' + memberEntryNo + ') div.teamListMembersInfo').height() + 30);
		$('#teamListMembers').height(contentBoxHeight);

		var boxWidth     = $('#teamListMembers').width();
		var slideWidth   = ((memberEntryNo * boxWidth) - boxWidth);
		$('#teamListMembers > ul').css('marginLeft', '-' + slideWidth + 'px');
	});

	$('#teamListThumbs a').bind('click', function(e){
		e.preventDefault();

		var e            = $(this);
		var urlTarget    = e.attr('href');
		var boxNo        = parseInt($('ul#teamListThumbs>li a').index(this) + 1);
		var numberBoxes  = $('#teamListThumbs li').size();
		var boxWidth     = $('#teamListMembers').width();
		var slideWidth   = ((boxNo * boxWidth) - boxWidth);

		/* Sendungen aller Moderatoren ausblenden */
		$('div.teamMemberList').slideUp(300);

		/* Höhe der Inhaltsbox ändern */
		var contentBoxHeight = ($('#tm_' + boxNo + ' div.teamListMembersInfo').height() + 30);
		$('#teamListMembers').animate({
			height: contentBoxHeight + 'px'
		}, 500);

		/* Aktiven Mod setzen */
		$('a.teamListActive').removeClass('teamListActive');
		e.addClass('teamListActive');

		$('#teamListMembers > ul').animate({
			marginLeft: '-' + slideWidth + 'px'
		}, 500);

		$('div.teamListMembersImage').each(function(){
			$(this).animate({ marginTop: '-190px' }, 150);
		});
		$('#tm_' + boxNo + ' div.teamListMembersImage').animate({
			marginTop: '190px'
		}, 250);

		window.location.href = urlTarget;
	});

	/* Sendungen der einzelnen Moderatoren ein- und ausblenden
	********************************************/

	$('div.teamMemberList').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;
	});
}