/**
 * @author RMachel
 */

$(document).ready(function() {

	// Media Library
	if($('div.mediaLibrary').length>0)
	{
		var boxesFadeOut = new Array();
		var boxesFadeIn = new Array();
		
		if ($.browser.msie) { //ie wont fade out the children, so we do it ourselves
			boxesFadeOut.push($('div.mediaLibrary .list ul li:first .articleTeaser'));
			boxesFadeOut.push($('div.mediaLibrary .list ul li:first .articleTeaser h4 a img'));
			boxesFadeOut.push($('div.mediaLibrary .list ul li:first .articleTeaser h4 a span'));
			boxesFadeOut.push($('div.mediaLibrary .list ul li:first .articleTeaser .artikelimg img'));
		} else {
			boxesFadeOut.push($('div.mediaLibrary .list ul li:first .articleTeaser'));
		}

		boxesFadeIn = fadeBoxes(boxesFadeIn, boxesFadeOut, 0.4);

		/* Init JCarousel for Mediathek */
	    $('div.mediaLibrary .list ul').jcarousel({
			vertical: true,
			scroll: 3,
			animation: "slow",
			buttonPrevHTML: '<div>&nbsp;<img src="/static/images/medialibrary/medialibrary_jcarousel_prev.gif" alt="Vorherige" />&nbsp;</div>',
			buttonNextHTML: '<div>&nbsp;<img src="/static/images/medialibrary/medialibrary_jcarousel_next.gif" alt="Nächste" />&nbsp;</div>'
	    });

		/* clickevent on list links */		
		$('div.mediaLibrary .list ul a').click(function(){
			var url = $(this).attr("href");
			boxesFadeOut = new Array();
			
			if ($.browser.msie) { //ie wont fade out the children, so we do it ourselves
				boxesFadeOut.push($(this).parent().parent().parent());
				boxesFadeOut.push($(this).parent().parent().parent().find("h4 a img"));
				boxesFadeOut.push($(this).parent().parent().parent().find("h4 a span"));
				boxesFadeOut.push($(this).parent().parent().parent().find(".artikelimg img"));
			} else {
				boxesFadeOut.push($(this).parent().parent().parent());
			}
			
			boxesFadeIn = fadeBoxes(boxesFadeIn ,boxesFadeOut, 0.4);
			
			updateFlvContent(url);
			return false;
		})
	}
});



/**
 * Functions
 */

function fadeBoxes(boxIn, boxOut, alpha)
{
	for(var i= 0; i<= boxOut.length-1; i++) {
		boxOut[i].fadeTo("slow", alpha);
	}
	
	for(var i= 0; i<= boxIn.length-1; i++) {
		boxIn[i].fadeTo(0, 1);
		//boxIn[i].removeAttr("filter");
		//boxIn[i].css("zoom","1");
	}
	
	return boxOut;
}

function updateFlvContent(url) {
	var detailFlashUrl = url.replace(".html",",layoutVariant=MediaLibraryDetailFlash");
	$.get(detailFlashUrl, function(data){
  		$(".mediaLibrary .detail").remove();
		$(".mediaLibrary h2").after(data);
	});
}

function eventToFLV(id,typ,prm) {
	thisMovie(id).sendEvent(typ,prm);
};

function thisMovie(id) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[id];
	} else {
		return document[id];
	}
};