$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, 1, 24, TransMenu.reference.topLeft);

		// 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);

				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').append(aMenuHtml.join(""));

		TransMenu.initialize();
	}
	
	$("#Language").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);
		}
	);

	//$('a.image-popup').lightBox();
});

var tid;
var slideUpTimeout = 700;
function LanguageSlideDown()
{
	if(!$("#LanguageSelector:visible").length)
		$("#LanguageSelector").slideDown("fast");
}
function LanguageSlideUp()
{
	$("#LanguageSelector").slideUp("fast");
}

function showMap(language, linkText) {
	
	$j("#overlayDIV").css("display","block");

	var html = "<div id=\"GoogleMapPopup\"><div class=\"head\"><a href=\"javascript: void(0);\" onClick=\"javascript: hideGoogleMap();\">";
	html += "<img src=\"/images/lightbox/lightbox-btn-close.gif\" /></a></div>";
	html += '<div id="Map"><iframe width="540" height="432" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.at/maps?f=q&amp;source=s_q&amp;hl='+ language +'&amp;geocode=&amp;q=Niederlaaberstra%C3%9Fe+3,++4611+Buchkirchen&amp;sll=47.635784,13.590088&amp;sspn=10.558172,19.775391&amp;ie=UTF8&amp;ll=48.221756,14.04542&amp;spn=0.020015,0.036478&amp;z=14&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="http://maps.google.at/maps?f=q&amp;source=embed&amp;hl='+ language +'&amp;geocode=&amp;q=Niederlaaberstra%C3%9Fe+3,++4611+Buchkirchen&amp;sll=47.635784,13.590088&amp;sspn=10.558172,19.775391&amp;ie=UTF8&amp;ll=48.221756,14.04542&amp;spn=0.020015,0.036478&amp;z=14&amp;iwloc=A" style="color:#0000FF; text-align:left" target="_blank">'+ linkText +'</a></small></div></div>';
	
	if ($("#GoogleMapPopup").length)
	{
		$("#GoogleMapPopup").remove();
	}
	$("body").append(html);

	var show = function(hash) {
		hash.w.fadeIn("slow");
	}
	var hide = function(hash) {
		hash.w.fadeOut("slow", function(){
				$(this).find("#Map").html("");
			});
		hash.o.fadeOut("slow", function(){
				$(this).remove();
			});
		
		$j("#overlayDIV").css("display","none");
	}
		
	$("#GoogleMapPopup").css({"left":"50%", "margin-left":"-270px"}).jqm({"onShow": show, "onHide": hide}).jqmShow();

}

function hideGoogleMap() {	
	$("#GoogleMapPopup").jqmHide();
}

function showVideoPopup(url, width, height)
{
	$j("#overlayDIV").css("display","block");
	
	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>";
	
	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 = 640;
	//height= 400;
	
	var so = new SWFObject("/upload/videos/"+url+".swf", "video_popup", width, height, "9", "#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("slow");
	}
	var hide = function(hash) {
		hash.w.fadeOut("slow", function(){
				$(this).find("#Video").html("");
			});
		hash.o.fadeOut("slow", function(){
				$(this).remove();
			});
		
		$j("#overlayDIV").css("display","none");
	}
	
	if($.browser.msie && $.browser.version<=6)
	{
		$("#VideoPopup").css("position","absolute");

		alignDetail();
		$(window).scroll(function()
		{
		    alignDetail();
		});
	}
	
	$("#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 alignDetail()
{
	var scrollTop = $(window).scrollTop();
	var height = $("#VideoPopup").outerHeight();
	var margin_top = parseInt(scrollTop);
	$("#VideoPopup").css({"margin-top": margin_top+"px"});
}

function hideVideoPopup()
{
	$("#VideoPopup").jqmHide();
}