
Windows.overlayShowEffectOptions = false;
Windows.overlayHideEffectOptions = false;

function showMediaRating(mediaid, value)
{
	new Ajax.Request("/media/ajax.php", {
		method: 'post',
		parameters: 'addrating=1&media_id='+mediaid+'&rating='+value
	});
	
	$('_your_rating_user').hide();
}

function showMediaCheck(mediaid)
{
	new Ajax.Request("/media/ajax.php", {
			method: 'post',
			parameters: 'getdata=1&formats[]=3&formats[]=6&formats[]=7&formats[]=8&formats[]=9&media_id='+mediaid,
			onSuccess: function(transport, json) {
				eval('json = '+transport.responseText);
				if(!json.getdata) return false;
				
				mediaData[mediaid] = json.getdata;
				showMedia(mediaid, true);
			}
		});
}

function showMedia(mediaid, no_check)
{
	// Defince local vars
	var widthpad = 30+250;
	var heightpad = 50;
	var sidebar_width = 250;
	var minversion = [10,0];
	var media = mediaData[mediaid] || false;
	var no_check = no_check || false;
	
	if(!media)
	{
		// Check for new data, if not cached
		if(!no_check) {
			showMediaCheck(mediaid);
		}
		
		return false;
	}
	
	if(media.type == "video")
	{
		// Danish media type
		dk_type = "video";
		
		// Choosen format
		if(media.fmt[9])
		{
			format = 9;
			html='<div id="ntamedia_overlay_video" style="width: 640px; height: 480px;"><p><b>Der kræves Adobe Flash version '+minversion.join(".")+'<br />eller nyere for at kunne se denne video</b></p><p><b><a href="http://www.adobe.com/go/getflashplayer">Klik her for at hente den nyeste udgave</a></b></p></div>';
			
			height = 480;
			width = 640;
		}
		else if(media.fmt[3])
		{
			format = 3;
			html='<div id="ntamedia_overlay_video" style="width: 640px; height: 480px;"><p><b>Der kræves Adobe Flash version '+minversion.join(".")+'<br />eller nyere for at kunne se denne video</b></p><p><b><a href="http://www.adobe.com/go/getflashplayer">Klik her for at hente den nyeste udgave</a></b></p></div>';
			
			height = 480;
			width = 640;
		}
		else if(!no_check)
		{
			showMediaCheck(mediaid);
			return false;
		}
		else
		{
			format = 999;
			html='<img src="/api/ntamedia/thumbnail/missing_video.png" alt="Henter billede..." align="middle">';
			
			height = 480;
			width = 640;
		}
	}
	else
	{
		// Danish media type
		dk_type = "billede";
		
		// Get window size
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			width = window.innerWidth;
			height = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
		
		// Choose format
		if(media.fmt[8] && height>960+heightpad && width>1280+widthpad)
		{
			format = 8;
			html='<div id="ntamedia_overlay_image" style="width: 1280px; height: 960px; background-image: url('+media.fmt[format]+');"><img src="/graphics/trans-pixel.gif" width="100%" height="100%"></div>';
			
			height = 960;
			width = 1280;
		}
		else if(media.fmt[7] && height>750+heightpad && width>1000+widthpad)
		{
			format = 7;
			html='<div id="ntamedia_overlay_image" style="width: 1000px; height: 750px; background-image: url('+media.fmt[format]+');"><img src="/graphics/trans-pixel.gif" width="100%" height="100%"></div>';
			
			height = 750;
			width = 1000;
		}
		else if(media.fmt[6])
		{
			format = 6;
			html='<div id="ntamedia_overlay_image" style="width: 720px; height: 540px; background-image: url('+media.fmt[format]+');"><img src="/graphics/trans-pixel.gif" width="100%" height="100%"></div>';
			
			height = 540;
			width = 720;
			
		}
		else if(!no_check)
		{
			showMediaCheck(mediaid);
			return false;
		}
		else
		{
			format = 999;
			html='<img src="/api/ntamedia/thumbnail/missing_image.png" alt="Henter billede..." align="middle">';
			
			height = 540;
			width = 720;
		}
	}
	
	// Generate sidebar
	sidebar = '<p><b>'+media.title+'</b><br />'+media.description+'</p>';
	sidebar = sidebar + '<p><b>Bedømmelse:</b> ';
	stars_yellow = media.rating;
	for(var i = 0; i < stars_yellow; i++) {
		sidebar = sidebar + '<img src="/graphics/knewstuff.png">';
	}
	stars_grey = 5-media.rating;
	for(var i = 0; i < stars_grey; i++) {
		sidebar = sidebar + '<img src="/graphics/knewstuff_grey.png">';
	}
	sidebar = sidebar + '</p>';
	sidebar = sidebar + '<p id="_your_rating_nouser" style="display: none;"><b>Din bedømmelse:</b> <i>Du skal være logget ind for at kunne bedømme videoerne</i></p>';
	sidebar = sidebar + '<p id="_your_rating_user" style="display: none;"><b>Din bedømmelse:</b> <select onchange=\'showMediaRating("'+mediaid+'", this.value)\'><option value=""> - Vælg - </option><option value="1">Meget dårlig</option><option value="2">Dårlig</option><option value="3">Middel</option><option value="4">God</option><option value="5">Meget god</option></select></p>';
	sidebar = sidebar + '<p><b>Bruger:</b> <a href="/lodge/'+media.user_id+'/">'+media.user_name+'</a></p>';
	sidebar = sidebar + '<p><b>Visninger:</b> '+media.views+'</p>';
	sidebar = sidebar + '<p><b>Uploadet:</b> '+media.created+'</p>';
	sidebar = sidebar + '<p><br /><center><input type=\"button\" onclick=\"return closeWindow();\" value=\"Luk '+dk_type+'\"></center></p>';
	
	// Generate window content
	html = '<table cellpadding=0 cellspacing=0 border=0 width="'+(width+sidebar_width)+'px" height="'+height+'px"><tr><td width="'+width+'px" align="center" style="border: 1px solid #CECECE;">'+html+'</td><td width="'+sidebar_width+'px" valign="top" style="padding: 5px; text-align: left;">'+sidebar+'</td></tr></table>';
	
	// Create window
	var confirm = new Window({
		className: "alphacube",
		width: width+4+sidebar_width,
		height: height+4+4,
		zIndex: 100,
		draggable: false,
		resizable: false,
		minimizable: false,
		maximizable: false,
		closable: true,
		title: media.title,
		showEffect: Element.show,
		hideEffect: Element.hide,
		wiredDrag: false,
		destroyOnClose: true,
		centered: true
		});
    confirm.setHTMLContent('<center><div style="width: '+(width+sidebar_width)+'px; height: '+height+'px; display: table-cell; vertical-align: middle; text-align: center; padding: 2px;">'+html+'</div></center>');
    confirm.showCenter(true);
    
    if(media.fmt[format])
    {
		new Ajax.Request("/media/ajax.php", {
			method: 'post',
			parameters: 'addview=1&haverated=1&media_id='+mediaid,
			onSuccess: function(transport, json) {
				//alert(transport.responseText);
				eval('json = '+transport.responseText);
				if(!json.logged_in) {
					$('_your_rating_nouser').show();
				} else if(!json.haverated) {
					$('_your_rating_user').show();
				}
			}
		});
    }
    
    if(media.type == "video" && media.fmt[format] &&
       ( flashembed.isSupported(minversion) || ( flashembed.isSupported([6,65]) && navigator.userAgent.indexOf('Windows') > -1 ) ) )
    {
    	var config = {
			key: '#@0e8f9c42a7d244434ef', 
			clip: {
				autoPlay: true,
				url: media.fmt[format].file,
				scaling: 'orig',
				accelerated: true,
				bufferLength: 2
			},
			onLoad: function() {
		        this.setVolume(100);
		    },
			plugins: { 
				controls: {
					background: 'rgba(0, 0, 0, 1)',
					height: 32,
					volumeColor: "rgba(255, 194, 51, 1)",
					progressColor: "rgba(255, 194, 51, 1)",
					timeSeparator: " / ",
					autoHide: {
						enabled:true,
						hideDelay: 1000,
						mouseOutDelay: 500,
						hideStyle: "fade",
						hideDuration: 400,
						fullscreenOnly: false
					},
					tooltips: {
						buttons:true,
						scrubber:true,
						volume:true,
						marginBottom: 5,
						stop: "Stop",
						play: "Afspil",
						fullscreen: "Fuldskærm",
						unmute: "Unmute",
						pause: "Pause",
						fullscreenExit: "Luk fuldskærm",
						mute: "Mute"
					}
				}
			},
			play: {
				replayLabel: 'Afspil igen'
			},
			canvas: {
				backgroundColor: '#000000', 
				backgroundGradient: 'medium'
			}
		};
		
		config.plugins['ntacdn'] = {
			url: 'flowplayer.rtmp-3.2.1.swf',
			netConnectionUrl: 'rtmp:'+media.fmt[format].connect
		}
		config.clip['provider'] = 'ntacdn';
		
    	flowplayer("ntamedia_overlay_video", {
    		src:"/libs/flowplayer-3.2/flowplayer.commercial-3.2.2.swf",
    		version: minversion
    		}, config).load();
    }
	    
	return false;
}

function closeWindow()
{
	Windows.closeAllModalWindows(); 
	return false;
}
