
$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();
	}
	
	$j("#MenuLine").width($j("#MenuLine ol").width());
	

	$('a.image-popup').lightBox();
});

function showVideoPopup(url, width, height,org_video,download_text, error)
{
	$("#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("slow");
	}
	var hide = function(hash) {
		hash.w.fadeOut("slow", function(){
				$(this).find("#Video").html("");
				$(this).find(".download a").attr('href', 'javascript: void(0);');
			});
		hash.o.fadeOut("slow", 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 showFramePopup(url, width, height)
{
	if(height>$(window).height())
		height = $(window).height()-50;
	$j("#OverlayDIV").appendTo("body");
	var html = "<div id=\"VideoPopup\" style='background: white; width:"+(width+22)+"px; height:"+(height+44)+"px; padding-left:22px; position: absolute; top: 50%; margin-top: -"+((height+44)/2)+"px; margin-left: -"+((width+44)/2)+"px'><div class=\"head\" style='text-align: right; background: white;'><a href=\"javascript: void(0);\" onClick=\"javascript: hideFramePopup();\">";
	html += "<img src=\"/images/lightbox/lightbox-btn-close.gif\" /></a></div>";
	html += "<iframe src="+url+" width="+width+" height="+height+" style='border: none'></iframe></div>";
	
	
	
	$("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);
		}
	}
	
	
	var show = function(hash) {
		$j("#OverlayDIV").css("display","block");
		hash.w.fadeIn("slow");
	}
	
	var hide = function(hash) {
		hash.w.fadeOut("slow", function(){
			$(this).remove();
		});
		hash.o.fadeOut("slow",function() {
			$j("#OverlayDIV").css("display","none");
		});
	}
	
	$("#VideoPopup").css({"left":"50%"}).jqm({"onShow": show, "onHide": hide}).jqmShow();
}

function hideFramePopup()
{
	$("#VideoPopup").jqmHide();
}

function hideVideoPopup()
{
	$("#VideoPopup").jqmHide();
}
