//Änderungen Übersicht
// Nr	 Nick		Datum		Text
// <001> Tweezer	20120103 	Anpassung der Höhe bei TS und Server Widget

$(document).ready(function () {
	$('.n1').append('<span class="hover"></span>');
	$('#homelink span').addClass('hover-home');
	$('#homelink span').removeClass('hover');
	$('.n1').hover(		
		function() {		
			$(this).children('.hover').fadeIn(500);
			$(this).children('.hover-home').fadeIn(500);			
		}, 	
		function() {		
			$(this).children('.hover').fadeOut(700);
			$(this).children('.hover-home').fadeOut(500);			
	});
		$("#navigation ul:first").dropmenu();
		$("#features > ul").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
		$('#sponsor-karussel').jcarousel({
			scroll: 1,
			auto: 5,
			wrap: 'circular',
			initCallback: initCallback,
			buttonNextHTML: null,
			buttonPrevHTML: null
		});
		function initCallback(carousel) {
			$('#sponsor-next').bind('click', function() {
				carousel.next();
				return false;
			});

			$('#sponsor-prev').bind('click', function() {
				carousel.prev();
				return false;
			});
		};
		
		
		// Videoplayer-Functions
		// Toogle Playlist
		$('#toogleList').bind('click', function() {
			if($('#playerlist').is(':visible')) {
				$('#playerlist').slideUp(500);
			}
			else {
				$('#playerlist').slideDown(500);
			}
			return false;
		});
		
		// Next Video
		$('#next').bind('click', function() {
			var currentVid = $('#playerlist a.selected').attr('id');
			if(currentVid!=undefined) {
				currentVid = parseInt(currentVid.substring(5,6))+1;
				var anzahlVid = parseInt($('#playerlist a').size());
				if(currentVid==anzahlVid){
					$('#playfirstvid').click();
				}
				else {
					$('#video'+currentVid).click();
				}
			}
			else {
				$('#playfirstvid').click();
			}
			return false;
		});
		// Last Video
		$('#last').bind('click', function() {
			var currentVid = $('#playerlist a.selected').attr('id');
			var anzahlVid = parseInt($('#playerlist a').size());
			if(currentVid!=undefined) {
				currentVid = parseInt(currentVid.substring(5,6))+1;
				if(currentVid==1){
					$('#video'+(anzahlVid-1)).click();
				}
				else {
				var n = '#video'+(currentVid-2);
					$(n).click();
				}
			}
			else {
				$('#video'+(anzahlVid-1)).click();
			}
			return false;
		});
		var params = { allowScriptAccess: "always", bgcolor: "#cccccc", wmode : "transparent" };
		var atts = { id: "myytplayer" };
		swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "382", "161", "8", null, null, params, atts);
		$('#ytph').css({visibility: 'hidden'});
		
		// Shoutbox 
		var sbIsOut = 0;
		var gsIsOut = 0;
		var tsIsOut = 0;
		$('#shoutbox_toogle').bind('click', function() {
			if(sbIsOut==0) {
				sbIsOut = 1;
				if(gsIsOut==1) { $('#gameserver_toogle').click(); }
				if(tsIsOut==1) { $('#ts_toogle').click(); }
				$('#shoutbox_container').animate({ 
					width:"220px" 
				   }, 500, function() {
							$('#shoutbox_container').css({overflow:'visible'});
							$('#shoutbox_content').animate({ 
								height:"410px" 
							   }, 1000, function() {
									} 
							);
						} 
				);
			}
			else {
				sbIsOut = 0;
				$('#shoutbox_content').animate({ 
					height:"100px" 
				   }, 500, function() {
							$('#shoutbox_container').css({overflow:'hidden'});
							$('#shoutbox_container').animate({ 
								width:"40px" 
							   }, 1000, function() {
									} 
							);
						} 
				);
			}
			return false;
		});
		
		// Gameserver
		$('#gameserver_toogle').bind('click', function() {
			if(gsIsOut==0) {
				gsIsOut = 1;
				if(sbIsOut==1) { $('#shoutbox_toogle').click(); }
				if(tsIsOut==1) { $('#ts_toogle').click(); }
				$('#gameserver_container').animate({ 
					width:"230px"
				   }, 500, function() {
							$('#gameserver_container').css({overflow:'visible'});
							$('#gameserver_content').animate({ 
								//height:"210px" <001>
								height:"110px"
							   }, 1000, function() {
										
									} 
							);
						} 
				);
			}
			else {
				gsIsOut = 0;
				$('#gameserver_content').animate({ 
					height:"100px" 
				   }, 500, function() {
							$('#gameserver_container').css({overflow:'hidden'});
							$('#gameserver_container').animate({ 
								width:"40px" 
							   }, 1000, function() {
									} 
							);
						} 
				);
			}
			return false;
		});
		
		
		// Teamspeak-Server
		$('#ts_toogle').bind('click', function() {
			if(tsIsOut==0) {
				tsIsOut = 1;
				if(sbIsOut==1) { $('#shoutbox_toogle').click(); }
				if(gsIsOut==1) { $('#gameserver_toogle').click(); }
				$('#ts_container').animate({ 
					width:"320px"
				   }, 500, function() {
							$('#ts_container').css({overflow:'visible'});
							$('#ts_content').animate({ 
								//height:"350px" <001>
								height:"550px"
							   }, 1000, function() {
									
									} 
							);
						} 
				);
			}
			else {
				tsIsOut = 0;
				$('#ts_content').animate({ 
					height:"100px" 
				   }, 500, function() {
							$('#ts_container').css({overflow:'hidden'});
							$('#ts_container').animate({ 
								width:"40px" 
							   }, 1000, function() {
									} 
							);
						} 
				);
			}
			return false;
		});
		
});

function hidehover(e){
	var n=1;
	while(n<6) {
		document.getElementById("hover"+ e + n).style.display='none';
		document.getElementById("nohover" + e).style.display='block';
		n++;
	}	
}
function showhover(n, e){
	hidehover(e);
	document.getElementById("nohover"+ e).style.display='none';
	document.getElementById("hover"+ e + n).style.display='block';
}

function loadAjaxHeadlines(url) {
	$('#sc_headlines').load(url).fadeOut(500).fadeIn(700);
	return false;
}

function chkFormularShoutBox() {
	if(document.getElementById('shoutbox_name').value == ""){
		alert('Du musst einen Namen eingeben!');
		document.getElementById('shoutbox_name').focus();
		return false;
	}
	if(document.getElementById('shoutbox_message').value == "" || document.getElementById('shoutbox_message').value == "Message"){
		alert('Du musst eine Nachricht eingeben!');
		document.getElementById('shoutbox_message').focus();
		return false;
	}
	return true;
}

// Videoplayer-Functions


var myplaylist = new Array();
var myplayname = new Array();
var first = 1;
var playing = 0;

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("myytplayer");
  setInterval(updateytplayerInfo, 250);
  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
  ytplayer.addEventListener("onError", "onPlayerError");
  ytplayer.setVolume(70);
}

function updateytplayerInfo() {
    if (ytplayer) {
		var currenttime = ytplayer.getCurrentTime(); 
		var duration =  ytplayer.getDuration(); 
		var progressInPerc = Math.round(currenttime/(duration/100) );
		if(playing) {
			$('#progress').width(progressInPerc+'%');
			$('#play').css({backgroundPosition:'-30px -14px'});
			$('#pause').css({backgroundPosition:'-60px -0px'});
		}
		else {
			$('#play').css({backgroundPosition:'-30px 0px'});
			$('#pause').css({backgroundPosition:'-60px -14px'});
		}
    }
}


function timeInfoFormat( seconds ) {
	var time = "0:";
	var minutes = 0;
	if( seconds >= 60 ) {
		minutes = Math.floor( seconds / 60 );
		seconds -= ( 60 * minutes );
		time = minutes.toString() + ":";
	}
	time += ( seconds < 10 ) ? "0" + seconds.toString() : seconds.toString();
	return time;
}

function cueNewVideo(id, startSeconds, element) {
  if (first==1) {
	$('#ytph').css({visibility: 'visible'});
	first = 0;
  }
  if (ytplayer) {
	$('#playerlist a').removeClass('selected');
	$('#' + element).addClass('selected');
	ytplayer.cueVideoById(id, startSeconds);
	var name = $('#' + element).children('b').text();
	$('#videoname').text(name);
  }
}

function onPlayerError(errorCode) {
	alert("An error occured: " + errorCode);
}


function playpause() {
	if(playing==1) {
		pause();
		$('#playpause').css({backgroundPosition: '-50px'});
	}
	else {
		play();
		$('#playpause').css({backgroundPosition: '0px -0px'});
	}
}

function play() {
  if (ytplayer) {
	ytplayer.playVideo();
	playing = 1;
  }
}

function pause() {
  if (ytplayer) {
	ytplayer.pauseVideo();
	playing = 0;
  }
}

function startOrPlay(id, startSeconds, element) {
	if(first) {
		cueNewVideo(id, startSeconds, element);
		play();
	}
	else {
		play();
	}
}

function startOrPlayOrPause(id, startSeconds, element) {
	if(!playing) {
		if(first) {
			cueNewVideo(id, startSeconds, element);
			play();
		}
		else {
			play();
		}
	}
	else {
		pause();
	}
}


