/*
    BrentKirkwood.js
    Widgets
  
    Copyright (c) 2005, Mark Miller.  All rights reserved.
*/

var req;
var imgIndex = 0;
var itemsList;
var flipShown = false;
var animation = { duration:0, starttime:0, to:1.0, now:0.0, from:0.0, firstElement:null, timer:null };

function setup()
{
	// set up handlers if running under Dashboard
	if (window.widget)
	{
		widget.onhide = onhide;
		widget.onshow = onshow;
	}

	return 0;
}

if (window.widget)
{
	widget.onhide = onhide;
	widget.onshow = onshow;
	//widget.onremove = onremove;
}

function onshow()
{
	//var url = "http://brentkirkwood.com/photoblog/rss2.xml";
	var url = "BrentKirkwoodRSS.php";
	loadXMLDoc(url);
}

function onhide()
{
	imgIndex = 0;
}

function loadXMLDoc(url)
{
	req = new XMLHttpRequest();
	req.onreadystatechange = processReqChange;
	req.open("GET", url, true);
	req.overrideMimeType("text/xml");
	req.setRequestHeader("Cache-Control", "no-cache");
	req.send(null);
}

function processReqChange()
{
	// only if req shows "loaded"
	if (req.readyState == 4)
	{
		// only if "OK"
		if (req.status == 200)
		{
			// ...processing statements go here...
			parseXMLDoc(req.responseXML);
		} else {
			alert("There was a problem retreiving the XML data:\n" + req.statusText);
		}
	}
}

function parseXMLDoc(xmlData)
{
	var allItems = xmlData.getElementsByTagName("item");
	itemsList = new Array();

	for (var i = 0; i < allItems.length; i++)
	{
		var itemElm = allItems[i];

		var titleElm = itemElm.getElementsByTagName("title").item(0);
		var titleText = titleElm.firstChild.nodeValue;

		var linkElm = itemElm.getElementsByTagName("link").item(0);
		var linkURL = linkElm.firstChild.nodeValue;
		linkURL = linkURL.replace(/^ /, "");

		var descElm = itemElm.getElementsByTagName("description").item(0);
		var descText = descElm.firstChild.nodeValue;

		//var cdataElm = descElm.childNodes[1];
		var cdataElm = descElm.childNodes[0];
		var cdataText = cdataElm.nodeValue;
		var imgURL = cdataElm.data;
		imgURLArray = imgURL.split(/\n/);
		//imgURL = imgURLArray[2];
		imgURL = imgURLArray[1];
		imgURL = imgURL.replace(/\<br.*/, "");
		imgURL = imgURL.replace(/ \/\>/, "");

		itemsList.push(new Array(titleText, linkURL, imgURL));
	}

	selectThumbImage();
}

function selectThumbImage()
{
	var titleText = itemsList[imgIndex][0];
	var linkURL = itemsList[imgIndex][1];
	var imgURL = itemsList[imgIndex][2];

	var itemList = new Array(titleText, linkURL, imgURL);

	imgIndex++;

	if (imgIndex > 9)
		imgIndex = 0;

	var photoDiv = document.getElementById("photo");
	displayThumbImage(itemList, photoDiv);
}

function displayThumbImage(itemList, div)
{
	var titleText = itemList[0];
	var linkURL = itemList[1];
	var imgURL = itemList[2];
	var html = new String();

	html = html.concat(imgURL+' onclick=javascript:selectThumbImage(); onerror=javascript:window.location.reload();>');
	//html = html.concat(imgURL+' onclick=javascript:widget.openURL("'+linkURL+'"); onerror=javascript:window.location.reload();>');
	//html = html.concat(imgURL+' onclick=javascript:openBlog(); onerror=javascript:window.location.reload();>');

	div.innerHTML = html;
}

function openBlog(event)
{
	openURL("http://brentkirkwood.com/photoblog/");
}

function showBack()
{
	var front = document.getElementById("front");
	var back = document.getElementById("back");

	if (window.widget)
		widget.prepareForTransition("ToBack");

	front.style.display = "none";
	back.style.display = "block";

	if (window.widget)
		setTimeout ('widget.performTransition();', 0);  

	document.getElementById("fliprollie").style.display = "none";
}

function hideBack()
{
	var front = document.getElementById("front");
	var back = document.getElementById("back");

	if (window.widget)
		widget.prepareForTransition("ToFront");

	back.style.display = "none";
	front.style.display = "block";

	if (window.widget)
		setTimeout ('widget.performTransition();', 0);
}

function mousemove(event)
{
	if (!flipShown)
	{
		if (animation.timer != null)
		{
			clearInterval(animation.timer);
			animation.timer = null;
		}
        
		var starttime = (new Date).getTime() - 13;

		animation.duration = 500;
		animation.starttime = starttime;
		animation.firstElement = document.getElementById("flip");
		animation.timer = setInterval("animate();", 13);
		animation.from = animation.now;
		animation.to = 1.0;
		animate();
		flipShown = true;
	}
}

function mouseexit(event)
{
	if (flipShown)
	{
		// fade in the info button
		if (animation.timer != null)
		{
			clearInterval(animation.timer);
			animation.timer = null;
		}
    
		var starttime = (new Date).getTime() - 13;
    
		animation.duration = 500;
		animation.starttime = starttime;
		animation.firstElement = document.getElementById("flip");
		animation.timer = setInterval("animate();", 13);
		animation.from = animation.now;
		animation.to = 0.0;
		animate();
		flipShown = false;
	}
}

function animate()
{
	var T;
	var ease;
	var time = (new Date).getTime();

	T = limit_3(time-animation.starttime, 0, animation.duration);

	if (T >= animation.duration)
	{
		clearInterval (animation.timer);
		animation.timer = null;
		animation.now = animation.to;
	}
	else
	{
		ease = 0.5 - (0.5 * Math.cos(Math.PI * T / animation.duration));
		animation.now = computeNextFloat (animation.from, animation.to, ease);
	}

	animation.firstElement.style.opacity = animation.now;
}

function limit_3(a, b, c)
{
	return a < b ? b : (a > c ? c : a);
}

function computeNextFloat(from, to, ease)
{
	return from + (to - from) * ease;
}

function enterflip(event)
{
	document.getElementById("fliprollie").style.display = "block";
}

function exitflip(event)
{
	document.getElementById("fliprollie").style.display = "none";
}
