var portfolioReq = ajaxRequest();

var theItems;

function portfolioInit() {
	ajaxConnect(portfolioReq,"GET","./fetch.php5",true,initCallback);
}

function initCallback() {
	if(portfolioReq.readyState == 4 && portfolioReq.status == 200) {
		theItems = eval(portfolioReq.responseText);
		drawItems(theItems);
	}
}

function drawItems(theItems) {
	for(i = 0; i < theItems.length; i++) {
		theItem = theItems[i];
		
		theLink = document.createElement("a");
		theLink.href = "#";
		theLink.id = i;
		if(theLink.addEventListener) { theLink.addEventListener("click",showDetail,false); }
		else if(theLink.attachEvent) { theLink.attachEvent("onclick",showDetail); }
		theTitle = document.createTextNode(theItem.title);
		theLink.appendChild(theTitle);
		
		theLI = document.createElement("li");
		theLI.appendChild(theLink);
		theLI.className = "item";
		theLI.style.backgroundImage = "url('./icons/" + theItem.name + ".gif')";
		theLI.style.backgroundRepeat = "no-repeat";

		/*Meebo('makeSharable', {
			element: theLI,
			url:"http://www.andrewwatterson.com/",
			title: theTitle,
			thumbnail: imageURL
		});*/

		document.getElementById(theItem.type).appendChild(theLI);
	}
}

function showDetail(evt) {

	evt['target'] ? index = this.id : index = evt['srcElement']['id'];

	clearDetail();

	document.getElementById('fader').style.height = document.body.scrollHeight;

	theItem = theItems[index];

	document.getElementById('fader').style.display = "block";
	document.getElementById('detail_container').style.display = "block";
	document.getElementById('detail').style.display = "block";
	
	theImage = document.createElement("img");
	theImage.src = "./images/" + theItem.name + ".jpg";
	theImage.alt = "portfolio detail";
	document.getElementById('detail_image').appendChild(theImage);
	
	theImage.onload = function() { Meebo('makeSharable', {
		element: theImage,
		shadow: 'none',
		title: theItems[index].title
	});};

	document.getElementById('detail_text').appendChild(document.createTextNode(theItem.description));
	
	document.getElementById('detail_link').style.backgroundImage = "url('./download_icon.gif')";
	document.getElementById('detail_link').style.backgroundRepeat = "no-repeat";	

	for(i = 0; i < theItem.links.count; i++) {
		theLink = document.createElement("a");
		theLink.appendChild(document.createTextNode(theItem.links[i].text));
		theLink.href = theItem.links[i].link;
		theLink.target = "_blank";
		document.getElementById('detail_link').appendChild(theLink);
	}

	Meebo('makeSharable', {
		element: 'detail_link',
		shadow: 'none',
		title: theItem.links[0].text,
		url: theItem.links[0].link
	});
}

function clearDetail() {
	while(document.getElementById('detail_image').firstChild) {
		document.getElementById('detail_image').removeChild(document.getElementById('detail_image').firstChild);
	}
	while(document.getElementById('detail_text').firstChild) {
		document.getElementById('detail_text').removeChild(document.getElementById('detail_text').firstChild);
	}
	while(document.getElementById('detail_link').firstChild) {
		document.getElementById('detail_link').removeChild(document.getElementById('detail_link').firstChild);
	}
}

function closeDetail() {
	document.getElementById('detail').style.display = "none";
	document.getElementById('detail_container').style.display = "none";
	document.getElementById('fader').style.display = "none";
}

function ajaxRequest() {

	var theRequest;
	
	if(window.XMLHttpRequest) {
		theRequest = new XMLHttpRequest();
	}
	
	else if(window.ActiveXObject) {
		theRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if(!theRequest) { theRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
	}

	if(theRequest) {
		return theRequest;
	} else {
		alert("AJAX not supported.");
	}
}

function ajaxConnect(theRequest,reqType,theURL,asynch,respHandle) {

	if(theRequest) {
		if(reqType.toUpperCase() != "POST") {
			theRequest.onreadystatechange = respHandle;
			theRequest.open(reqType,theURL,asynch);
			theRequest.send(null);
		} else {
			var post_data = arguments[5];
			theRequest.onreadystatechange = respHandle;
			theRequest.open(reqType,theURL,asynch);
			if(post_data != null && post_data.length > 0) {
				theRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
				theRequest.send(post_data);
			}
		}
	}
}

function nl2br(text) {
	text=escape(text);
	return unescape(text.replace(/(%5Cr%5Cn)|(%5Cn%5Cr)|%5Cr|%5Cn/g,'<br />'));
}