/*
 * Copyright 2007 Grant Zanetti
 */

var cOpen = null;

function pageResize()
{
	var w = pageWidth();
	var h = pageHeight();

	if (cOpen)
	{
		$("#" + cOpen).css({
				left: parseInt((w / 2) - ($("#" + cOpen).attr('clientWidth') / 2)) + 'px',
				top: parseInt((h / 2.2) - ($("#" + cOpen).attr('clientHeight') / 2)) + 'px'
			    });
	}
}

function centerWindow(name)
{
	var w = pageWidth();
	var h = pageHeight();

	$("#" + name).css('opacity', 0.0);
	$("#" + name).css('filter', 'alpha(opacity=100)');
	$("#" + name).css('display', 'block');

	$("#" + name).css({
			 left: parseInt((w / 2) - ($("#" + name).attr('clientWidth') / 2)) + 'px',
			 top: parseInt((h / 2.2) - ($("#" + name).attr('clientHeight') / 2)) + 'px'
			});

	$("#" + name).css('display', 'none');
	$("#" + name).css('opacity', '');
	$("#" + name).css('filter', '');
}

function openWindow(name)
{
	if (cOpen)
		closeWindow();
	
	centerWindow(name);

	cOpen = name;

	$("#" + name).fadeIn("slow");
}

function closeWindow()
{
	$("#" + cOpen).fadeOut("slow");
	cOpen = null;
}

function showContact(item)
{
	$("#contact_from").val('');
	$("#contact_subject").val('');
	$("#contact_body").val('');

	if (cOpen != 'contact')
		openWindow('contact');

	$("#contact_from").focus();
}

function showGallery(name, item)
{
	item.parentNode.className = 'menu_item_active';

	$.getJSON('gallery/' + name + '.json', function(data) {

		var html, i;

		html = '';

		$('#gallery_image').html('');

		html = '<div style="overflow: none; height: 74px; width: ' + (data.length * 74) + 'px;">';

		for (i = 0; i < data.length; i++)
		{
			html += '<img width=70 height=70 onclick="showImage(\'';
			html += data[i].imageFile;
			html += '\');" src="';
			html += data[i].thumbFile;
			html += '">';
		}

		html += '</div>';

		showImage(data[0].imageFile);

		$('#gallery_thumbs').html(html);

		if (cOpen != 'gallery')
			openWindow('gallery');

		item.parentNode.className = 'menu_item';
	});
}

function showTwitter(item)
{
	console.debug(item);
	item.parentNode.className = 'menu_item_active';

	$.get('cmd.cgi', { opt: 'twitterRSS' }, function(data) {
		var html = '';
		for (var i = 0; i < data.channel[0].item.length; i++)
		{
			var title = data.channel[0].item[i].title + '';
			title = title.replace(/thegrort: /i, '');
			var date = new Date;
			date.setTime(Date.parse(data.channel[0].item[i].pubDate));
			title = title.replace(/\@(\w+)/, '<a target="other" href="http://twitter.com/$1">@$1</a>');
			html += '<div><div>' + date + '</div><div style="padding-left: 30px; font-weight: normal; color: #aaa;">' + title + '</div></div><br>';
		}
		$("#rss_string").html(html);
		if (cOpen != 'rss')
			openWindow('rss');

		item.parentNode.className = 'menu_item';
	}, 'json');
}

function showImage(image)
{
	$("#gallery_image").html('<div><img width=320 height=320 src="' + image + '"></div>');
}

function sendEmail()
{
	var from, subject, body, xmlreq, err;

	from = $('#contact_from').val();
	subject = $('#contact_subject').val();
	body = $('#contact_body').val();

	if (!body)
		err = "Message contains no body";

	if (!subject)
		err = "Message has no subject";
	
	if (!from)
		err = "Message has no from address";

	if (!from.match(/.+@[a-zA-z0-1.-]+\.[a-zA-z0-1.-]+$/) && from)
		err = "Message has a bad from address";

	if (!err)
	{
		xmlreq = '<sendEmailRequest>';
		xmlreq += '<addr>' + encodeForXML(from) + '</addr>';
		xmlreq += '<subject>' + encodeForXML(subject) + '</subject>';
		xmlreq += '<msgbody>' + encodeForXML(body) + '</msgbody>';
		xmlreq += '</sendEmailRequest>';

		$.post('cmd.cgi', { xml: xmlreq }, sentEmail, 'json');
	}
	else
		showAlert(err);
}

function sentEmail(data)
{
	var xml, err;

	err = data['errstr'];

	if (err == 'Sent!')
	{
		$("#contact").fadeOut("slow");
		cOpen = null;
	}
	
	showAlert(err);
}

function showAlert(text)
{
	$("#alert_string").html(text);

	centerWindow('alert');

	$('#alert').fadeIn('slow');

	setTimeout(closeAlert, 3000);
}

function closeAlert()
{
	$('#alert').fadeOut('slow');
}

window.onresize = pageResize;

