/**
 * @author Johannes Kroker
 */
var homeID;

$.address.init(function(event){
    $('.teaser').address(function(){
        return $(this).attr('id').replace(location.pathname, '');
    });
}).externalChange(function(event){
    var selectedId = (event.value == '') ? 'Home' : event.pathNames[0].substr(0, 1).toUpperCase() +
    event.pathNames[0].substr(1);
    showPost(selectedId);
});

$(document).ready(function(){
	//JavaScriptCheck entfernen
	$('#javascript_check').remove();
	

	// INIT
	var numberofposts = $('#buehne .medium').length;
	$("#teaser").append("<ul>");
	for ( var a = 0; a < numberofposts; a++) {
		var picid;
		picid = $("#buehne .medium:eq(" + a + ")").attr("id");

		$("#teaser ul").append("<li class='teaser video' id=" + picid + ">");
		$(".medium[id*=" + picid + "] p[class=bild]").remove().appendTo("#teaser ul li[id*=" + picid + "]");
		$("#teaser ul").append("</li>");
	}
	$("#teaser").append("</ul>");
	
	
	closeInfo();
	
    homeID = $('.active').attr("id");
    
    
    //INIT Btn
    $('.teaser').click(function(event){
        var selectedId = $(this).attr("id");
        showPost(selectedId);
        closeInfo();
    });
        
	$('#info_oeffnen').click( function(){showInfo(); });
    $('#info_schliessen').click( function(){ closeInfo(); });
       
});

$(window).load(function(){
    /* ----- Seitenaufbau ------ */
    $('#main_preloader').fadeOut('fast',function(){
        $('#wrap').fadeTo('slow', 1);
    });
});

function showPost(selectedId){
    if (selectedId == "Home") {
        selectedId = homeID;
    }

    //RELOAD IFRAME
    var location = $('.active iframe').attr("src");
    $('.active iframe').attr("src", location);
   
    $('.medium').removeClass("active");
    $(".medium[id*=" + selectedId + "]").addClass("active");
    
    $('.medium').addClass("hidden");
    $(".medium[id*=" + selectedId + "]").removeClass("hidden");
    
    $('.description').removeClass("active");
    $(".description[id*=" + selectedId + "]").addClass("active");
    $('.teaser.active').removeClass("active");
    $(".teaser[id*=" + selectedId + "]").toggleClass("active");
    $.scrollTo('0px', 400, {
        margin: true
    });

    //VIMEO PLAYER LADEN, WENN BILD GEPOSTET (MAIN PAGE):
    var vimeoVideo = $('.active h2').attr("id");
    if(vimeoVideo && $(".medium[id*=" + selectedId + "] iframe").attr("src")==undefined){
    	$(".medium[id*=" + selectedId + "]").prepend('<iframe src="http://player.vimeo.com/video/'+vimeoVideo+'?title=0&amp;byline=0&amp;portrait=0" width="720" height="408" frameborder="0"></iframe>');
    }
    
    //VIMEO PLAYER LADEN, WENN BILD GEPOSTET (DETAIL PAGE):
    var vimeoVideoDetail = $('#detailinfo h2').attr("id");
    if(vimeoVideoDetail && $("#videoDetail iframe").attr("src")==undefined){
    	$("#videoDetail").prepend('<iframe src="http://player.vimeo.com/video/'+vimeoVideoDetail+'?title=0&amp;byline=0&amp;portrait=0" width="720" height="408" frameborder="0"></iframe>');
    }
    
    $('#videoDetail object, #videoDetail object embed, #videoDetail span embed, #videoDetail iframe, .medium object, .medium object embed, .medium span embed, .medium iframe').attr({
        width: 720,
        height: 408
    });
}

function closeInfo(){
	$("#info_schliessen").fadeOut();
    $('.description.active *').fadeOut(function(){
     	$('.description.active').animate({width: 0+"px", left:722+"px", opacity: 0}, { queue:false, duration:400 });
     	$("#info_oeffnen").fadeIn();
    });
}


function showInfo(){
    $("#info_oeffnen").fadeOut();
    $('.description.active').animate({width: 724+"px", left:-2+"px", opacity: 1}, function(){
    	 $('.description.active *').fadeIn();
    	 $("#info_schliessen").fadeIn();
    });
}
