/**************************************************************
 *	INIT (MAIN)
 *
 *	Initiates all Items and sets Eventhandler.
 *
***************************************************************/



/**************************************************************
		GLOBAL VARS
***************************************************************/

var ieVersion = null;
var browserType = null;


/**************************************************************
		MAIN - onDocumentReady
***************************************************************/

$(function()
{
	//alert('Diese Seite ist vorübergehend in Bearbeitung und \nsteht Ihnen morgen wieder in vollem Umfang zu Verfügung! \n\nDanke für Ihr Verständnis!');
	
	// GET IE-VERSION
	var version = navigator.appVersion;
	var pattern = /[MSIE\ ][0-9]/;
	
	ieVersion = pattern.exec(version);
	
	browserType = navigator.appName;
	
	$('a#tta').attr('href','tta/index.php');
	$('a#tbb').attr('href','tbb/index.php');
	
	// Arrows:
	$('a#tta').mouseenter(function(evt){ $('div.top div.arrows .img').hide(); })
	$('a#tta').mouseleave(function(evt){ $('div.top div.arrows .img').show(); })
	$('a#tbb').mouseenter(function(evt){ $('div.bottom div.arrows .img').hide(); })
	$('a#tbb').mouseleave(function(evt){ $('div.bottom div.arrows .img').show(); })
	
	// INIT SHADOW-BOX
	initShadowBox();
	
	loadNews();
	loadNewCourses();
	
	// Decrease Functionality for IE 7 and lower ------------------
	if (browserType == 'Microsoft Internet Explorer' && ieVersion < 8)
	{
		// Show Info
		$('div.infoFrameErr').html('<div class="infoTextErr"><img src="images/warning.gif" alt="" width="15" height="15" /><p>&nbsp;WARNUNG! Sie verwenden eine alte Version des Browsers.<br />Einige Features, die nicht unterst&uuml;tzt werden, sind deaktiviert. Es wird dringend empfohlen den Browser zu aktualisieren!</p></div>');
	}
});


function noClick(evt)
{
	// Prevent HTML Link-Function
	evt.preventDefault();	
}

function onExternalLinkClick(evt)
{
	evt.preventDefault();
	window.open($(this).attr('href'));
}

//------------------------------
//	Shadowbox
//------------------------------
function initShadowBox()
{
	Shadowbox.clearCache();
	Shadowbox.setup();
	
	Shadowbox.init({
	    autoplayMovies: true
	});
}

//------------------------------
//	News
//------------------------------
//*
function loadNews()
{
	var htmlContent = '<div class="news infowindow" style="display: none"><div class="loading"><img src="images/loading/loading_black.gif" width="32" height="32" alt="" /><p>News werden geladen ...</p></div></div>';
	
	$('div.newsFiller').html(htmlContent);
	$('div.newsFiller div.news').fadeIn(1000);
	
	$.post("tta/scripts/php/loadNews.script.php", '', setNews);
}

function showNews(evt)
{
	$('div.newsFiller div.news .newswrapper').fadeIn(1000);
}

function setNews(data)
{
	var htmlContent = '<div class="newswrapper" style="display: none"><div class="content">' + data + '</div></div>';	
	$('div.newsFiller div.news').append(htmlContent);
	
	$('div.newsFiller div.news .loading').fadeOut(1000, showNews);
	$('a.external').click(onExternalLinkClick);
	
	
	// Set Styles depending on Browser ------------------
	if (false)//(browserType == 'Netscape')
	{
		$('div.bottom div.news').css('cursor','move');
		$('div.bottom div.news div.content').css('position','relative');
		$('div.bottom div.news div.content').css('top','160px');
		
		movingNews = window.setInterval(moveNews, 50);
		
		// Scrolling
		$('div.news').mousedown(onNewsMouseDown);
	}
	else
	{
		$('div.bottom div.news div.content').css('display','block');
		$('div.bottom div.news div.content').css('height','145px');
		$('div.bottom div.news div.content').css('overflow','auto');
	}
}

var newsScrolling = false;
var hitPosY = 0;
var currentTop = 0;

function onNewsMouseDown(evt)
{
	newsScrolling = true;
	
	currentTop = parseInt($('div.news div.content').css('top'));
	hitPosY = evt.clientY;
	
	$('html').live('mouseup', stopNewsScrolling);
	$('html').live('mouseleave', stopNewsScrolling);
	
	$('html').live('mousemove', onNewsScrolling);
}

function stopNewsScrolling(evt)
{
	$('html').die('mouseup',stopNewsScrolling);
	$('html').die('mouseleave',stopNewsScrolling);
	
	$('html').die('mousemove',onNewsScrolling);
	
	newsScrolling = false;
}


function onNewsScrolling(evt)
{
	evt.preventDefault();
	
	if (newsScrolling)
	{
		var mouseDiff = hitPosY - evt.clientY;
		
		
		var top = currentTop - mouseDiff;
		
		
		if (top < 160 && top > (0 - parseInt($('div.news div.content').attr('clientHeight'))))
		{
			$('div.news div.content').css('top', top);
		}
	}
}


function moveNews()
{
	if (!newsScrolling)
	{
		var newsContent = 'div.news div.content';
		
		var top = parseInt($(newsContent).css('top')) - 1;
		
		$(newsContent).css('top', top);
		
		if (parseInt($(newsContent).css('top')) + parseInt($(newsContent).attr('clientHeight'))  < 0)
			$(newsContent).css('top', 160);
	}
}
//*/



//------------------------------
//	Courses
//------------------------------

function loadNewCourses()
{
	var htmlContent = 
		'<div class="courses infowindow" style="display: none">' +
			'<div class="loading">' +
				'<img src="images/loading/loading_black.gif" width="32" height="32" alt="" />' +
				'<p>Neue Kurse werden geladen ...</p>' +
			'</div>' +
		'</div>';
	
	$('div.courseFiller').html(htmlContent);
	$('div.courseFiller div.courses').fadeIn(1000);
	
	$.post("tta/scripts/php/loadNewCourses.script.php", 'type=index', setNewCourses);
}

function showNewCourses(evt)
{
	$('div.courseFiller div.courses .courseswrapper').fadeIn(1000);
}

function setNewCourses(data)
{
	var htmlContent = 
		'<div class="courseswrapper" style="display: none">' +
			'<h4>Neue Kurse:</h4>' +
			'<div class="contentBox">' +
				'<div class="content">' + data + '</div>' +
			'</div>' +
		'</div>';	
	
	$('div.courseFiller .courses').append(htmlContent);
	
	$('div.courseFiller div.courses .loading').fadeOut(1000, showNewCourses);
	
	
	// Set Styles depending on Browser ------------------
	if (false)//(browserType == 'Netscape')
	{
		$('div.top div.courses div.content').css('position','relative');
		$('div.top div.courses div.contentBox').css('height','126px');
		$('div.top div.courses div.contentBox').css('overflow','hidden');
		
		$('div.top div.courses').css('cursor','move');
		
		// Scrolling
		$('div.courses').mousedown(onCoursesMouseDown);
	}
	else
	{
		$('div.top div.courses div.content').css('display','block');
		$('div.top div.courses div.content').css('height','120px');
		$('div.top div.courses div.content').css('overflow','auto');
	}
}


var coursesScrolling = false;

function onCoursesMouseDown(evt)
{
	coursesScrolling = true;
	
	currentTop = parseInt($('div.courses div.content').css('top'));
	hitPosY = evt.clientY;
	
	$('html').live('mouseup',stopCoursesScrolling);
	$('html').live('mouseleave',stopCoursesScrolling);
	
	$('html').live('mousemove',onCoursesScrolling);
}

function stopCoursesScrolling(evt)
{
	coursesScrolling = false;
	
	$('html').die('mouseup',stopCoursesScrolling);
	$('html').die('mouseleave',stopCoursesScrolling);
	
	$('html').die('mousemove',onCoursesScrolling);
}


function onCoursesScrolling(evt)
{
	evt.preventDefault();
	
	if (coursesScrolling)
	{
		var mouseDiff = hitPosY - evt.clientY;
		var top = currentTop - mouseDiff;
		
		if (top < 0 && top > (-20 - (parseInt($('div.courses div.content').attr('clientHeight')) - parseInt($('div.courses div.contentBox').attr('clientHeight')))))
		{
			$('div.courses div.content').css('top', top);
		}
	}
}
