
$j = $; //jQuery.noConflict();

$j(document).ready(function(){
	// disable old events
	/*
	$j('li.Level0Item > ol').hide();

	$j('li.Level0Item').mouseover(function(){
		console.log('hover');
		$j('> ol', this).slideDown();
	}).mouseout(function(){
		$j('> ol', this).slideUp();
	});
	*/

	
	if (TransMenu.isSupported())
	{
		function init_transmenu_subitems(transmenu, subitems)
		{
			subitems.each(function(i){
				var link = $j('> a', this);
				var item = transmenu.addItem(link.text(), link.attr('href'), link.attr('target'));

				var subitems = $j('> ol > li', this);
				if (subitems.length)
				{
					var subtransmenu = transmenu.addMenu(transmenu.items[i], i);
					init_transmenu_subitems(subtransmenu, subitems);
				}
			});
		}

		function hide_transmenu()
		{
			TransMenuSet.registry[0].hide();
		}

		var menuitems = $j('li.Level0Item').add('li.TopMenuItem').add('#MoilHeader');

		var ms = new TransMenuSet(TransMenu.direction.down, 13, 0, TransMenu.reference.bottomLeft);

		//TransMenu.onactivate = function () { alert("test"); };
		
		
		
		// generate transmenu from html
		menuitems.each(function(){
			var subitems = $j('> ol > li', this);
			if (!subitems.length)
			{
				this.onmouseover = hide_transmenu;
			}
			else
			{
				var link = $j('> a', this);
				var transmenu = ms.addMenu(link[0], 0);
				
				//alert(transmenu.onactivate);
				//alert(this.id);
				
				transmenu.onactivate = function()
				{
					$(this.oActuator).addClass('Level0ItemActivated');
				}
				transmenu.ondeactivate = function()
				{
					$(this.oActuator).removeClass('Level0ItemActivated');
				}
				

				init_transmenu_subitems(transmenu, subitems);
			}
		});

		// remove menu from html
		menuitems.find('> ol').remove();

		/*
		menu.onactivate = function() {};
		menu.ondeactivate = function() {};
		*/

		// init transmenu
        var aMenuHtml = [];
        for (var i = 0, menu = null; menu = TransMenu.registry[i]; i++) {
                aMenuHtml[i] = menu.toString();
        }
        
        
        $j('body').prepend(aMenuHtml.join(""));
        
        //$j('#Menu-Level0').remove();
        //$j('body').pepend(html);

		TransMenu.initialize();
		//$('*').each(function() { this.style.zIndex=10; });
		//alert($('.transMenu').length);
		//$('.transMenu').each(function() { this.style.zIndex=-1; });
		
		//$('#Menu-Level0').each(function() { this.style.zIndex=2; });
		//$j('#Menu-Level0').parent().apend($j('#Menu-Level0').parent().html());
		
	}
	

	$('a.image-popup').lightBox();
	
	//$("#Menu-Level3 .Menu-Level3Item, #Menu-Level3 .Menu-Level3ItemSelected").ifixpng();
	
	// Imagerotation
	if($("#PageContainer .banner.rotation").length) {
		setInterval("imageRotation()", 5000);
	};
	
	$("#TopNavi-Language-Text, #TopNavi-Language-Name,#ChooseLanguage").hover(
		function()
		{
			clearTimeout(tid);
			LanguageSlideDown();
		}
		,
		function()
		{
			tid = setTimeout("LanguageSlideUp()",slideUpTimeout);
		}
	);
	$("#LanguageSelector .language").hover(function()
		{
			$(this).addClass("selected");
			clearTimeout(tid);
		},
		function()
		{
			$(this).removeClass("selected");
			tid = setTimeout("LanguageSlideUp()",slideUpTimeout);
		}
	);
	
	var search_val = $("#search input[name=s]").val();
	if(!$("#search input[name=s]").val())
		$("#search input[name=s]").val(search_title);
	
	$("#search input[name=s]").focus(function(){
		if($(this).val() == search_val || $(this).val() == search_title)
			$(this).val("");
	})
	$("#search input[name=s]").blur(function(){
		if($(this).val() == '')
			$(this).val(search_title);
	})
	$("#search form").submit(function(){
		if($(this).find("input[name=s]").val() == search_title)
			$(this).find("input[name=s]").val("");
	});
	
	$("#VideoPopup").appendTo("body");
	
	$("a.specialHoverLanguage").hover(
		function() {
			$(this).css("background-color","#555555");
		},
		
		function() {
			$(this).css("background-color","#8c8c8c");
		}
	);
	
	$("img.flashVideo").each(function() {
		
		$this = $(this);
		
		var flash_height =  $this.outerHeight()+37;
				
		var so = new SWFObject("/flash/player.swf?q="+(Math.random()*1000000), "video_popup", $this.outerWidth(), flash_height, "1", "#ffffff");
		so.addParam('wmode', 'transparent');
		so.addParam('scale','default');
		so.addParam('quality','high');
		
		so.addVariable('video_source',''+$this.attr("title"));
		
		var flashdiv_id = 'FlashVideo_'+parseInt((Math.random()*1000000));

		$this.replaceWith('<div id="'+flashdiv_id+'">Loading</div>');
		
		so.write(""+flashdiv_id);
	});
});

function sleep(delay) {
	var start = new Date().getTime();
	while (new Date().getTime() < (start + delay));
}

var tid;
var slideUpTimeout = 700;
function LanguageSlideDown()
{
	if(!$("#LanguageSelector:visible").length)
		$("#LanguageSelector").slideDown("fast");
}
function LanguageSlideUp()
{
	$("#LanguageSelector").slideUp("fast");
}

// banner
function showVideoPopup(url, width, height,org_video,download_text, error)
{
	// Rahmen hinzufügen
	//width +=40;
	height += 37;
	
	
	$("#VideoPopup .download a").attr("href", '/download/?'+url);
	var correct_flash_version = (deconcept.SWFObjectUtil.getPlayerVersion().major >= 9);
	
	if(!correct_flash_version)
	{
		alert(error);
		return;
	}
	
	var html = "<div id=\"VideoPopup\"><div class=\"head\"><a href=\"javascript: void(0);\" onClick=\"javascript: hideVideoPopup();\">";
	html += "<img src=\"/images/lightbox/lightbox-btn-close.gif\" /></a></div>";
	html += "<div id=\"Video\"></div><div class=\"download\"><a href=\"/download/?file="+org_video+"\">"+download_text+"</a></div></div>";
	
	if ($("#VideoPopup").length)
	{
		$("#VideoPopup").remove();
	}
	$("body").append(html);
	
	var width_ratio = parseFloat(width/800);
	var height_ratio = parseFloat(height/600);
	if(width_ratio>1 || height_ratio>1)
	{
		if(width_ratio>height_ratio)
		{
			width = 800;
			height = Math.ceil(height/width_ratio);
		}
		else
		{
			height = 600;
			width = Math.ceil(width/height_ratio);
		}
	}
	//width = 400;
	//height = 400;
	//alert(url);
	
	var so = new SWFObject("/flash/player.swf?q="+(Math.random()*1000000), "video_popup", width, height, "1", "#ffffff");
	so.addParam('wmode', 'transparent');
	so.addParam('scale','default');
	so.addParam('quality','high');
	
	so.addVariable('video_source','/upload/videos/'+url);
	
	var show = function(hash) {
		hash.w.fadeIn("fast");
	}
	var hide = function(hash) {
		hash.w.fadeOut("fast", function(){
				$(this).find("#Video").html("");
				$(this).find(".download a").attr('href', 'javascript: void(0);');
			});
		hash.o.fadeOut("fast", function(){
				$(this).remove();
			})
	}
	$("#VideoPopup #Video").css({"width":width+"px","height":height+"px"});
	$("#VideoPopup").css({"left":"50%", "margin-left":-(width/2)+"px"}).jqm({"onShow": show, "onHide": hide}).jqmShow();
	so.write('Video');
}
function hideVideoPopup()
{
	$("#VideoPopup").jqmHide();
}
function imageRotation() {
	$("#PageContainer .banner.rotation").each(function() {
		if($(this).find(".image").length < 2) {
			return;
		}
		
		if($(this).find(".image:visible").is(":last-child"))
		{
			$(this).find(".image:visible").fadeOut(2000);
			$(this).find(".image:first").fadeIn(2000);
		}
		else {
			$(this).find(".image:visible").fadeOut(2000).next().fadeIn(2000);
		}
	});
}