function installStuff() {
	$("#addlist li").click(function() {
		$("#adddesc li").hide();
		$("#desc-"+this.id).slideDown(100);
		this.blur();
	});
	$("#gorandom").click(function() {
		$.get("?random=1",function(xml){
			var videoname = $("name",xml).text();
			var videolink = $("link",xml).text();
			var videofile = $("file",xml).text();
			
			$("body").addClass("mediabody");
			$("#content").html('<div id="mediabox"><div id="media">Go get some Flash, dumbass</div></div>');
			swfobject.embedSWF(videofile, "media", "760", "560", "9.0.0");
			
			$("#count").addClass('emphasis');
			$("#count").html('MOAR');
			
			$("#permalink").html('link: <a href="'+videolink+'">'+videoname+'</a>');
		});
	});
	$("#surprise").click(function() {
		$.get("?random=1",function(xml){
			var videoname = $("name",xml).text();
			var videolink = $("link",xml).text();
			var videofile = $("file",xml).text();
						
			ov_show(videofile, videoname, {});
		});
		
		return false;						  
	});
}

$(installStuff);

function getPageSize(){
	var d = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (d&&d.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (d&&d.clientHeight) || document.body.clientHeight;
	return [w,h];
}

function ov_init() {
	$('a.ov-link').click(function(){
		ov_show(this.href,this.title,ov_parseParams(this.rel));
		this.blur();
		return false;
	});
}

function ov_show(target, caption, params) {
	var type = null;

	if(params.type)
	{
		type = params.type;
	}
	else
	{
		if(target.toLowerCase().match(/\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/) !== null)
		{
			type = "image";
		}
		else if(target.toLowerCase().match(/\.swf$/) !== null)
		{
			type = "flash";
		}
		else
		{
			type = "iframe";
		}
	}
	
	if(!caption) 
	{
		caption = "";
	}
	
	if(document.getElementById("ov-shadow") === null)
	{
		$("body").append('<div id="ov-shadow"></div>');
	}
	else
	{
		$("#ov-box").remove();
	}
	
	$("body").append('<div id="ov-box"><div id="ov-loading"></div><div id="ov-window"></div></div>');
	
	$("#ov-shadow").click(ov_hide);
	$("#ov-shadow").fadeIn("slow", function() { $("#ov-box").show(); });
	$("#ov-box").center();	
	
	switch(type)
	{
	case 'image':
		image = new Image();
		image.onload = function(){    
			image.onload = null;
			
			var pageSize = getPageSize();
			var x = pageSize[0] - 100;
			var y = pageSize[1] - 100;
			var imageWidth = image.width;
			var imageHeight = image.height;
			if (imageWidth > x) {
				imageHeight = imageHeight * (x / imageWidth); 
				imageWidth = x; 
			} 
			if (imageHeight > y) { 
				imageWidth = imageWidth * (y / imageHeight); 
				imageHeight = y; 
			}
			
			$("#ov-window").append('<img src="'+target+'" width="'+imageWidth+'" height="'+imageHeight+'" alt="'+caption+'" title="'+caption+'"/>');
			$("#ov-loading").hide();
			$("#ov-window").show();
			ov_centralize(imageWidth+20,imageHeight+20);
		};
		
		image.src = target;
		break;
	case 'flash':
		var width = params.width || 800;
		var height = params.height || 600;
		
		$("#ov-window").append('<div id="ov-media"></div>');
		swfobject.embedSWF(target, "ov-media", width, height, "9.0.0", null, null, null, null, function(e) {
			$("#ov-loading").hide();
			$("#ov-window").show();
			ov_centralize(width+20,height+20);														 
		});
		
		break;
	case 'iframe':
		var pageSize = getPageSize();
		var width = params.width || pageSize[0] - 200;
		var height = params.height || pageSize[1] - 200;
		
		$("#ov-window").append('<iframe frameborder="0" hspace="0" src="'+target+'" id="ov-iframe" name="TB_iframeContent'+Math.round(Math.random()*1000)+'" onload="tb_showIframe()" style="width:'+width+'px;height:'+height+'px;" ></iframe>');
		
		$("#ov-loading").hide();
		$("#ov-window").show();
		ov_centralize(width+20,height+20);
		break;
	}
	
	if(caption) 
	{
		$("#ov-window").append('<p id="ov-caption">'+caption+'</p>');
	}
}

function ov_hide() {
	$("#ov-shadow,#ov-box").remove();
	return false;
}

function ov_centralize(w,h) {
	$("#ov-box").css({marginLeft: '-' + parseInt((w / 2),10) + 'px', width: w + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7))
	{ 
		$("#ov-box").css({marginTop: '-' + parseInt((h / 2),10) + 'px'});
	}
}

function ov_parseParams(query) {
	var params = {};
	if (!query) { return false; }
	var pairs = query.replace(/^[^\?]+\??/,'').split(/[;&]/);
	for(var i = 0; i < pairs.length; i++) {
		var kv = pairs[i].split('=');
		if ( ! kv || kv.length != 2 ) {continue;}
		var key = unescape( kv[0] );
		var val = unescape( kv[1] );
		val = val.replace(/\+/g, ' ');
		params[key] = val;
	}
	return params;	
}

$(ov_init);