/**
 * @author gabriel
 */

var CURRENTCAT;
var catPositions = new Array(0, 1942, 3134, 4155, 5449, 6606, 7862);
var CATSTRUCTURE = new Array(
	["7-startseite/4-start"],
	["8-infos/7-ueber-die-jtms", "8-infos/8-thema", "8-infos/9-veranstaltungsort"],
	["10-programm/75-programmuebersicht", "10-programm/12-workshops", "10-programm/13-diskussionen", "10-programm/14-rahmenprogramm", "10-programm/15-medieneinblicke"],
	["11-anmeldung/16-anmeldung"],
	["12-team/17-ueber-uns", "12-team/18-teamer-werden", "12-team/19-interview-projektleitung", "12-teampartner/20-partner"]
);


//// static vars
var naviWidth = 454;
var contentWidth = 640;


$(document).ready(function () 
{
	//init Resize
	$(window).resize(function() {resizeWindow(true);});
	
	//init StartAnimation
	$('.welcome_bigLogo').hide().delay(400).fadeIn(6000);
	
	
	
	initEvents();
	initEmailForm();
	initDetailView();
	initTinyPopups();
	initAjaxContent();
	initPage();
	initDeepLinking();
	
	//changeCat(0);
	initElementAnimation();
	
	/// Countdown ///
	$("#jmttime").countdown({date:"november 5, 2011"});
	//$("#releasetime").countdown2({date:"october 15, 2011"});
	
	
	/// Video Contest ///
	init_vcElements();
	
	initModernizr();
})

function initEvents()
{
	
	// ScrollEvent for navigationbar
	$(window).scroll(function () {
		if(CURRENTCAT == 0)
		{
			if($(window).scrollTop() > $(window).height() - 35	)
			{
				$("#eworld").css({
					position: "fixed",
					top: - $(window).height() + 35,
					left: - $(window).scrollLeft()
				})
				if($(window).width() > contentWidth+200) $("#logojmt").fadeIn("fast");
			} else {
				$("#eworld").css({
					position: "absolute",
					top: 0,
					left: 0
				})
				$("#logojmt").fadeOut("fast");
			}
		} else {
			if($(window).scrollTop() > 282)
			{
				$("#eworld").css({
					position: "fixed",
					top: -282,
					left: - $(window).scrollLeft()
				})
				if($(window).width() > contentWidth+200) $("#logojmt").fadeIn("fast");
			} else {
				$("#eworld").css({
					position: "absolute",
					top: 0,
					left: 0
				})
				$("#logojmt").fadeOut("fast");
			}
		}
    });

	
	$(".avcform").live("click", function(){
		$(this).attr("value", "");
	})
	
	
	//simple Btns Events
	
	$(".simpleBtnOpacitySoft").css({opacity:.5}).mouseover(function(){
		$(this).css({opacity:1})
	}).mouseout(function(){
		$(this).css({opacity:.5})
	})
	
	$(".simpleBtnOpacityHard").css({opacity:1}).mouseover(function(){
		$(this).css({opacity:.5})
	}).mouseout(function(){
		$(this).css({opacity:1})
	})
	

	
	// Anker Scroll Animation
	
    $('a[href*=#]').bind("click", function(event){
        event.preventDefault();
        var ziel = $(this).attr("href");
        $("html,body").animate({
            scrollTop: $(this).offset().top
        }, "fast", function(){
            location.hash = ziel;
        });
    });
	
	
	/*$('#eworld').drag(function( e, target ){
		if(target.offsetX <= 0 && target.offsetX > - $(this).width() + $(window).width())
		{
			$( "#content" ).css({
	       	 left: target.offsetX
	        });
			//switchWorld(target.offsetX);
			
		}
		//$("#output").html($('#eworld').position().left)
	});*/
	
	
	$(".footer img").mouseover(function(){
		var mytemp = $(this).attr("mover");
		$(this).attr("src", mytemp);
	}).mouseout(function(){
		var mytemp = $(this).attr("mout");
		$(this).attr("src", mytemp);
	})
}


function switchWorld(offsetX)
{

}


function changeCat(targetCat)
{
	if(CURRENTCAT != targetCat)
	{
		CURRENTCAT = targetCat
		
		var newLeft = catPositions[CURRENTCAT]
		if(CURRENTCAT != 0) newLeft -= ($(window).width() - 640) / 2;
		
		$("html,body").stop().animate({
			scrollLeft:  newLeft,
			scrollTop: 0
		}, "fast", function(){
			for(var i = 0; i <= 6; i++)
			{
				if (i != CURRENTCAT)
				{
					$("#cat" + i).hide();
					$("#navi" + i).hide();
				}
				else {
					$("#cat" + i).fadeIn(200, function(){
						showSubCat(0)
					});
					$("#navi" + i).fadeIn("fast");
					
				}
			}
		})
		
		/* move Avatar */
		if( lWSC && MYAV) {
			var nexXAV = newLeft + ( 100 + ($(window).width() - 200) * Math.random() );
			lWSC.broadcastGamingEvent({
				moveleft: nexXAV
			});
		}
		
		
		resizeWindow(false);	

		
		/*if (moveWorld != false) {
			$('#eworld').animate({
				left: catPositions[targetCat]
			}, "fast");
		}*/
		//$("#output").append(CURRENTCAT)
		
		/* temp navi cat 


		for(var t = 1; t <= 4; t++)
		{
			if(t != CURRENTCAT+1) $("#navi0 a:nth-child("+t+")").removeClass('navi-activ')
			else $("#navi0 a:nth-child("+t+")").addClass('navi-activ')
		}*/
		
	}
}

function showSubCat(subCat)
{
	for(var i = 0; i < CATSTRUCTURE[CURRENTCAT].length; i++)
	{
		if (i != subCat)
		{
			$("#cat" + CURRENTCAT +""+ i).slideUp("fast");
			$("html,body").stop().animate({
				scrollTop: 0
			}, "fast");
		}
		else {
			$("#cat" + CURRENTCAT +""+ i).slideDown("fast");
		}
		
	}
	
}




function resizeWindow(e)
{
	if (CURRENTCAT == 0) {
		$("#eworld").stop().animate({height: $(window).height()},"fast");
		$(".content").stop().animate({top: $(window).height()},"fast");
		var newLeft = ($(window).width() - contentWidth) / 2;
			$("#cat0").css({left: newLeft});
			$("#navi0").css({left: newLeft + 45 });
		$(".welcome_Header").css({left: ($(window).width() - 240)/2 })
		$(".welcome_bigLogo").css({left: ($(window).width() - 260)/2 })
		$(".welcome_teaser").css({left: ($(window).width() - $(".welcome_teaser").width())/2})
	} else {
		$("#eworld").stop().animate({height: 320},"fast");
		$(".content").stop().animate({top: 320},"fast");
		if(e){
			$("html,body").stop().animate({
				scrollLeft:  catPositions[CURRENTCAT] -($(window).width() - 640) / 2
			}, 100);
		} 
	}
	if($(window).width() > 900) $("#logojmt").fadeOut("fast");
}









function initElementAnimation()
{
	ew_aniCould1();
	ew_aniCould2();
	ew_aniBallon1();
	ew_aniBallon2();
}

function ew_aniCould1()
{
	$("#ew_cloud1").animate({left:1000}, 50000, "linear").animate({left:850}, 50000, function(){ew_aniCould1()});
}
function ew_aniCould2()
{
	$("#ew_cloud2").animate({left:8000}, 500000, "linear").animate({left:950}, 500000, function(){ew_aniCould2()});
}
function ew_aniBallon1()
{
	$("#ew_ballon1").animate({left:3800}, 80000, "linear").animate({left:3200}, 80000, function(){ew_aniCould2()});
}
function ew_aniBallon2()
{
	$("#ew_ballon2").animate({left:3600}, 150000, "linear").animate({left:3100}, 150000, function(){ew_aniCould2()});
}


/***** Deep Linking ******/


function deepLink(target){
	deepLinkingChangeTitel(target)
}

function initDeepLinking(){
	$.address.init(function(e){
		$('.navi > a').address(function(){
			return $(this).attr('href');
		});
	}).change(function(e){
		e.value = e.value.substring(1)
		deepLinkingChangeTitel(e.value);
	});
}

function deepLinkingChangeTitel(e)
{
	var titel = "| ";
	if(e == "") titel = "";
	titel += e.toUpperCase();
	$.address.title("Jugendmedientage 2011 " + titel)
	$.address.path(e)
	var aKey = 0;
		switch (e) {
			case "startseite":
				aKey = 0;
				break;
			case "info":
				aKey = 1;
				break;
			case "programm":
				aKey = 2;
				break;
			case "anmeldung":
				aKey = 3;
				break;
			case "team&partner":
				aKey = 4;
				break;
			case "wettbewerb":
				aKey = 4;
				break;
				
			//quick and dirty
			case "startseite/faq":
				aKey = 0;
					showPopupBackground();
					createPopupFrame("jmtjoom/index.php/18-footer/28-faq", "popupCloseSingle")
				break;
				
		}
		changeCat(aKey);
}


function initModernizr() {
	if (Modernizr.canvas) {
	  //alert("canvas ok!")
	} else {
		alert("Dein Brower unterstüzt leider nicht die neuen HTML5 Features. Die Chatfunktion könnte fehlerhaft sein...")
	}
}
	












