function doToggle(id) {
	
	var obj = $(id);
	var im = $("im" + id);
	
	Effect.toggle(
		obj, 
		'slide',
		{
			duration: 0.5,
			afterFinish:function(obj) {
				if(obj.element.style.display == "none") {
					im.src = "/images/plus.gif";
				} else {
					im.src = "/images/minus.gif";
				}
			}
		}
	);
}

function showSmilies() {
	var win = open('/smilies.html', 'smilies', 'width=300,height=600,scrollbars');	
}

function showTextile() {
	var win = open('/textile.html', 'textile', 'width=600,height=400,scrollbars');
}

function showImageUpload() {
	$('audupload').style.display = 'none';
	$('vidform').style.display = 'none';
	$('imgupload').style.display = 'block';
}

function showAudioUpload() {
	$('vidform').style.display = 'none';
	$('imgupload').style.display = 'none';
	$('audupload').style.display = 'block';
}

function showVideoForm() {
	$('imgupload').style.display = 'none';
	$('audupload').style.display = 'none';
	$('vidform').style.display = 'block';
}

function displayMedia(mediaId) {
	var media = $('media');
	while(media.hasChildNodes()) {
		media.removeChild(media.firstChild);
	}
	
	var img = document.createElement("img");
	img.src="/images/throbber.gif";
	
	var div = document.createElement("div");
	div.className = "throbber";
	
	div.appendChild(img);
	
	media.appendChild(div);
	
	var handler = function(t) {
		var xml = t.responseXML.documentElement;
		
		var title;
		
		while(media.hasChildNodes()) {
			media.removeChild(media.firstChild);
		}
		
		var child = xml.firstChild;
		while(child) {
			
			switch(child.nodeName) {
				case 'name': title = child.firstChild.nodeValue; break;
				case 'mediaItem': addMediaItem(child); break;
			}
			
			child = child.nextSibling;
		}
		
		$('sectionTitle').innerHTML = title;
	};
	
	new Ajax.Request(
		'/media/section/' + mediaId,
		{
			method: 'get',
			onSuccess: handler,
			onError: function() { alert("Error"); },
			asynchronous: true
		}
	);
	
	return false;
}

function addMediaItem(xml) {

	var fileName;
	var comment;

	try {
	
	var child = xml.firstChild;
	while(child) {

		switch(child.nodeName) {
			case 'fileName': if(child.firstChild) { fileName = child.firstChild.nodeValue; } break;
			case 'title': if(child.firstChild) { comment = child.firstChild.nodeValue; } break;
		}	
	
		child = child.nextSibling;

	}

	var clean = "";
	if(comment) { clean = comment.replace('"', '\''); }

	var html = "<div class='mediaItem'\"><a href=\"/images/upload/" + fileName + "\"" +
		"rel=\"lightbox[album]\" class=\"thumb\" title=\"" + clean + "\">" + 
		"<img alt=\"" + clean + "\" src=\"/images/spacer.gif\" " +
		"style=\"background-image:url('/images/thumbnails/" + fileName + "');\"/><br/>" + 
		clean + "</a></div>";
		

	new Insertion.Bottom('media', html);

		fileName = "";
		title = "";

	} catch(e) {
		alert(e);
	}
}

function fullscreen(el) {

	var im = new Image();
	im.src = el.src;

// get dimensions and slope for actual image
	var w = im.width;
	var h = im.height;
	var m = h / w;

// get dimensions and slope for screen
	var scrX = document.viewport.getWidth();
	var scrY = document.viewport.getHeight();
	var scrM = scrY / scrX;

// if the image is proportionally taller than the screen, we resize to width
	if(m > scrM) {

		var py = Math.round(scrX * m);
		var dy = py - scrY;

		el.style.left = "0px";
		el.style.width = scrX + "px";
		
// top
		el.style.top = "0px";

// middle
//		el.style.top = (-(dy / 2)) + "px";
		
// bottom
//		el.style.top = (-dy) + "px";
		el.style.height = py + "px";

// otherwise we resize to height
	} else {

		var px = Math.round(scrY / m);
		var dx = px - scrX;

		el.style.top = "0px";
		el.style.height = scrY + "px";

		el.style.left = (-(dx / 2)) + "px";
		el.style.width = px + "px";
	}
}

function postLoadImage(image) {
	
		var im = new Element(
			"img", 
			{ 
				id: 'bgim', 
				alt: '', 
				style: 'display: none;'
			}
		);
		
		var preload = new Image();
		preload.onload = function() {
			im.src = preload.src;
			fullscreen($('bgim'));
			Effect.Appear($('bgim'), { duration: 0.4 });
		};
	
		$('bg').appendChild(im);
		preload.src = image;
}

Event.observe(
	window, 
	"load", 
	function() { 
		
		var nav = $('nav');
		
		var pageName = $('page');
		pageName.setOpacity(0.85);
		
		nav.setOpacity(0.85);
	}
);
Event.observe(window, "resize", function() { fullscreen($('bgim')); });

function fbs_click() {
	var u=location.href;
	var t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}
