
$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();
	});
	*/
	
	
	//$j("meta[name=description]").attr("content","test");

	if(screen.height == 768){
		//if($.browser.msie){
			//$j("#PageContainer").css("min-height",$j(window).height()-$j("#Top").height()-$j("#Footer").height()-5);
			//alert($j("#Top").height());
		//}
	}
	
	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();
	}
	

	$('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 hideVideoPopup()
{
	$("#VideoPopup").jqmHide();
}
function showBTWPopup(index, image, width, height, alt)
{
	var text = "";
	var my_url = "pages.php?page_id=79&btw="+index;
	
	$.getJSON(my_url, function(data) {
		text = data;
		
		$j("#OverlayDIV").appendTo("body");
		var html = "<div id=\"VideoPopup\" style='background: white; width:"+(width)+"px; height:"+(height+48)+"px; padding-left:22px; position: absolute; top: 50%; margin-top: -"+((height+38)/2)+"px; margin-left: -"+((width)/2)+"px'><div class=\"head\" style='text-align: right; background: white; margin-right: 8px; margin-top: 8px; margin-bottom: 10px;'><a href=\"javascript: void(0);\" onClick=\"javascript: hideBTWPopup();\">";
		html += "<img src=\"/images/close.jpg\" /></a></div>";
		if(image!="")
			html += "<div id='imagediv' style='float: left; position: relative; height: "+(height-15)+"px;'><img style='position: absolute; bottom: 0;' src='"+image+"' alt='"+alt+"'/></div>";
		html += "<div id='textdiv' style='float: right; margin-left: 15px; width: 346px; margin-right: 8px;'><img src='/images/btw_big.jpg' /><br /><br />"+text+"</div>";
		html += "</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");
			});
		}
		
		if($j("#textdiv").height()>height){
			$j("#VideoPopup").height($j("#textdiv").height()+70);
			$j("#imagediv").height($j("#textdiv").height());
			//margin-top: -"+((height+28)/2)+"px;
			$j("#VideoPopup").css("margin-top","-"+(($j("#textdiv").height()+68)/2)+"px");
		}else if($j("#textdiv").height()<height){
			$j("#VideoPopup").height($j("#textdiv").height()+70);
			$j("#imagediv").height($j("#textdiv").height());
			$j("#VideoPopup").css("margin-top","-"+(($j("#textdiv").height()+68)/2)+"px");
		}
		
		$("#VideoPopup").css({"left":"50%"}).jqm({"onShow": show, "onHide": hide}).jqmShow();
	});
	
	//alert(text);
}

function hideBTWPopup()
{
	$("#VideoPopup").jqmHide();
}

