/**************************************************************
 *	INIT (MAIN)
 *
 *	Initiates all Items and sets Eventhandler.
 *
***************************************************************/



/**************************************************************
		GLOBAL VARS
***************************************************************/

var ieVersion = null;
var browserType = null;


/**************************************************************
		MAIN - onDocumentReady
***************************************************************/

$(function()
{
	// 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');
	
	
	$.post("tta/scripts/php/loadNews.script.php", '', setNews);
	$.post("tta/scripts/php/loadNewCourses.script.php", 'type=index', setNewCourses);
	
	
	
	
	// 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();	
}


//------------------------------
//	News
//------------------------------
/**/
function setNews(data)
{
	var htmlContent = '<div class="news"><div class="content">' + data + '</div></div>';
	
	$('div.newsFiller').html(htmlContent);
	
	
	// 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');
	}
	
	
	
	//$('div.news a.sitelink').click(onSitelinkClick);
	$('div.news a[class!=sitelink][class!=sitelink newsItem][class!=mail]').click(noClick);
}

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 setNewCourses(data)
{
	var htmlContent = data;
	//htmlContent = htmlContent.replace(/<a/g,'<div');
	//htmlContent = htmlContent.replace(/<\/a>/g,'</div>');
	
	htmlContent = '<div class="courses"><h4><a href="tta/index.php#a_courses_program" >Neue Kursbeginne:</a></h4><div class="contentBox"><div class="content">' + htmlContent + '</div></div></div>';	
	
	$('div.courseFiller').html(htmlContent);
	//$('div.courses div.content').html(htmlContent);

	
	// 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);
		}
	}
}