var apiKey = "7da8e6d2da62b17d8a9797660a3d049e";
var size = 0;//0 = square, 1 = Thumbnail, 2 = Small, 3 = Medium, 4 = Large
var langType = "php";

function getHttpObject(){
if(window.XMLHttpRequest){
return new XMLHttpRequest();
}
else if(window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");
}
else{
//alert("AJAX not Supported.");
return null;
}
}


var getData=function(url, target, parseFunction, xml, loader){

if(loader){
var loader = document.createElement("img");
loader.setAttribute("id", "loader");
loader.setAttribute("src", "../images/thumbLoader.gif");
loader.setAttribute("alt", "Loading");
target.appendChild(loader);
}

var xmlRequest = getHttpObject();

xmlRequest.open("GET",url,true);

xmlRequest.onreadystatechange=function(){
if(xmlRequest.readyState == 4){
if(xmlRequest.status == 200){
if(xml){
var xmlResponse = xmlRequest.responseXML.documentElement;
removeWhiteSpace(xmlResponse);
}else{
var xmlResponse = xmlRequest.responseText;
}
if(loader){
target.removeChild(loader);
}
parseFunction(xmlResponse, target);
}
else{
//alert("Problem retrieving XML data");
}
}else{
//alert("Working!");
}	
}
xmlRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlRequest.send(null);
}

function removeWhiteSpace(root) {
var i = 0;
while (i != root.childNodes.length) {
if (root.childNodes[i].nodeType == 3) {
root.removeChild(root.childNodes[i])
} else {
var j = 0;
while (j != root.childNodes[i].childNodes.length) {
if (root.childNodes[i].childNodes[j].nodeType == 3) {
root.childNodes[i].removeChild(root.childNodes[i].childNodes[j])
} else {
j++;
}
}
i++;
}
}
return root;
}

var flickrSet = function(xml, target){
for(var i = 0; i<xml.firstChild.getElementsByTagName("photo").length; i++){
var galleryItem = document.createElement("li");
var photo = xml.firstChild.getElementsByTagName("photo")[i];
target.appendChild(galleryItem);
var imgId = photo.getAttribute("id");
var processFlickrPhotoInfo = getData(langType+"/flickr."+langType+"?apiKey="+apiKey+"&id="+imgId+"&type=info", galleryItem, flickrPhotoInfo, true, false);
}
}

var flickrPhotoInfo = function(xml, target){
var photoInfo = xml.firstChild;
var imgId = photoInfo.getAttribute("id");
var imgUrl = photoInfo.getElementsByTagName("urls")[0].getElementsByTagName("url")[0].firstChild.nodeValue;
var imgTitle = photoInfo.getElementsByTagName("title")[0].firstChild;
if(imgTitle != null){
var imgTitleText = imgTitle.nodeValue;
}else{
var imgTitleText = "";
}
var imgDescription = photoInfo.getElementsByTagName("description")[0].firstChild;
if(imgDescription != null){
var imgDescriptionText = imgDescription.nodeValue;
}else{
var imgDescriptionText = "";
}

var galleryAnchor = document.createElement("a");
galleryAnchor.onclick = function(){
popUp(galleryAnchor, imgDescriptionText, imgTitleText);
return false;
}
galleryAnchor.setAttribute("rel", imgUrl);

var imgAuthor = photoInfo.getElementsByTagName("owner")[0].getAttribute("realname");
if(imgAuthor != ""){
galleryAnchor.setAttribute("title", imgTitleText + " by " + imgAuthor);
}else{
galleryAnchor.setAttribute("title", imgTitleText);
}
target.appendChild(galleryAnchor);
var processFlickrPhoto = getData(langType+"/flickr."+langType+"?apiKey="+apiKey+"&id="+imgId+"&type=photo", galleryAnchor, flickrPhoto, true, false);
}

var flickrPhoto = function(xml, target){
var photo = xml.firstChild.getElementsByTagName("size")[size];
var galleryImg = document.createElement("img");
galleryImg.setAttribute("src", photo.getAttribute("source"));
galleryImg.setAttribute("alt", "Gallery Image");
target.appendChild(galleryImg);
galleryImg.parentNode.setAttribute("href", xml.firstChild.getElementsByTagName("size")[3].getAttribute("source"));
galleryImg.parentNode.setAttribute("rev", xml.firstChild.getElementsByTagName("size")[3].getAttribute("width"));
}

var popUp = function(target, descriptionText, imgTitleText){
var bodyNode = document.getElementsByTagName("body")[0];
var img = target.getAttribute("href");
var imgPage = target.getAttribute("rel");
var overlay = document.createElement("div");
overlay.setAttribute("id", "overlay");
var polaroid = document.createElement("div");
polaroid.setAttribute("id", "polaroid");
polaroid.style.width = target.getAttribute("rev")+"px";
if(imgTitleText != ""){
var title = document.createElement("h3");
title.appendChild(document.createTextNode(imgTitleText));
polaroid.appendChild(title);
}
var imgAnchor = document.createElement("a");
imgAnchor.setAttribute("href", target.getAttribute("rel"));
var imgNode = document.createElement("img");
imgNode.setAttribute("src", img);
imgNode.setAttribute("title", "See this photo on Flickr.");
imgNode.setAttribute("alt", "");
imgAnchor.appendChild(imgNode);
polaroid.appendChild(imgAnchor);
if(descriptionText != ""){
imgNode.setAttribute("alt", descriptionText);
var description = document.createElement("p");
description.appendChild(document.createTextNode(descriptionText));
polaroid.appendChild(description);
}
overlay.onclick = function(){
bodyNode.removeChild(overlay);
}
overlay.appendChild(polaroid);
bodyNode.appendChild(overlay);
}

var flickrGallery = function(photoset, target) {
var galleryList = document.createElement("ul");
target.appendChild(galleryList);
var clearAll = document.createElement("div");
clearAll.className = "clearAll";
target.appendChild(clearAll);
var processFlickrSet = getData(langType+"/flickr."+langType+"?apiKey="+apiKey+"&photoset="+photoset+"&type=photoset", galleryList, flickrSet, true, false);
};


var intFlite = function(){
var galleries = document.getElementsByTagName("div");
for(var i = 0; i < galleries.length; i++){
if(galleries[i].className == "flite"){
var flite = new flickrGallery(galleries[i].getAttribute("title"), galleries[i]);
}
}
}
window.onload = intFlite;