// JavaScript Document

/*
The code in this file or document and its designs, methods, data, know-how and all other related elements are the property of LUXSON ltd © Copyright 2009-2010 and/or may contain code operated under third party licence(s); unauthorised use is therefore prohibited, including (without limitation) copying, editing, adapting, reverse engineering or any other similar or related action, in part or in full. Separate rights may also exist for LUXSON and/or client and/or third party content and/or services. For licensing information please contact LUXSON ltd.
*/


var imageGalCount = 0;
var imageGalArray = new Array();

var videoGalCount = 0;
var videoGalArray = new Array();

function initGallery(){
	var flickrScript = document.createElement('script');
	flickrScript.type = 'text/javascript';
	flickrScript.src = 'http://api.flickr.com/services/rest/?api_key=cd85d84f7833b666ef2458ffb0d74240&method=flickr.photos.search&user_id=43850143@N06&per_page=28&format=json&jsoncallback=resultsFlickrFeed';
	document.getElementsByTagName("head")[0].appendChild(flickrScript);	
	
	var youtubeScript = document.createElement('script');
	youtubeScript.type = 'text/javascript';
	youtubeScript.src = 'http://gdata.youtube.com/feeds/api/users/christianreliefug/uploads?v=2&alt=json-in-script&callback=resultsYoutubeFeed&format=5&max-results=4';
	document.getElementsByTagName("head")[0].appendChild(youtubeScript);
}

function resultsFlickrFeed(o){
  	
  	for(var i=0; i < o.photos.photo.length; i++){
		
		var id = o.photos.photo[i].id;
	
		var si = "http://farm"+o.photos.photo[i].farm+".static.flickr.com/"+o.photos.photo[i].server+"/"+o.photos.photo[i].id+"_"+o.photos.photo[i].secret+"_s.jpg";
		var ti = "http://farm"+o.photos.photo[i].farm+".static.flickr.com/"+o.photos.photo[i].server+"/"+o.photos.photo[i].id+"_"+o.photos.photo[i].secret+".jpg";
	
		var title = o.photos.photo[i].title;
		
		imageGalArray.push({id:id, thumb:si, target:ti, title:title});
				
	}
		
	displayImages();
	
}


function resultsYoutubeFeed(o){
	
	var feed = o.feed;
    var entries = feed.entry || [];

    for (var i = 0; i < entries.length; ++i) {
      	var entry = entries[i];
		
		var id = entry.id.$t;
		
		var si = entry.media$group.media$thumbnail[0].url;
		
		var ti = entry.content.src;
		
		var title = entry.title.$t;
		
		videoGalArray.push({id:id, thumb:si, target:ti, title:title});
		
	}
	
	displayVideos();
	 
}

function displayImages(){
	
	var imgAnchor=document.createElement('A');
	imgAnchor.href=imageGalArray[imageGalCount].target;
	
	var tgt = imageGalArray[imageGalCount].target;
	var ti = imageGalArray[imageGalCount].title;
		
	imgAnchor.onclick=function(){
		luxBox.liteW(tgt, {title:ti});
		return false;
	}
	
	var imgThumb = new Image();
	imgThumb.className = 'galleryImage';
	
	imgThumb.setAttribute("alt",ti);
	
	imgThumb.onload = function() {
		
		imgThumb.setAttribute("width",this.width);
		imgThumb.setAttribute("height",this.height);
		
		document.getElementById("imageHolder").appendChild(imgAnchor);
		imgAnchor.appendChild(imgThumb);
		
		if(imageGalCount < imageGalArray.length-1){
				
			imageGalCount++;
		
			setTimeout(displayImages, 50);
		
		}
	}
	
	imgThumb.onresize = function() {
		imgThumb.setAttribute("width",this.width);
		imgThumb.setAttribute("height",this.height);
	}
	
	imgThumb.src = imageGalArray[imageGalCount].thumb;
	
}

function displayVideos(a){
	
	var imgHolder=document.createElement('DIV');
	imgHolder.className = 'galVideoHol';
		
	var imgAnchor=document.createElement('A');
	imgAnchor.href=videoGalArray[videoGalCount].target;
		
	var imgThumbOverlay = new Image();
	imgThumbOverlay.src = "images/content/galleryOverlay.gif";
	imgThumbOverlay.setAttribute("width",120);
	imgThumbOverlay.setAttribute("height",91);
	
	var tgt = videoGalArray[videoGalCount].target;
	var ti = videoGalArray[videoGalCount].title;
	
	var amdt = tgt.substr(tgt.lastIndexOf("/")+1, tgt.indexOf("?")-(tgt.lastIndexOf("/")+1));
	
	imgAnchor.onclick=function(){
		luxBox.liteW(amdt, {title: ti, type:'youtube'});
		return false;
	}
	
	var imgThumb = new Image();
	
	imgThumb.setAttribute("alt",ti);
	
	imgThumb.onload = function() {
				
		imgThumb.setAttribute("width",120);
		imgThumb.setAttribute("height",90);
		
		document.getElementById("videoHolder").appendChild(imgHolder);
		imgHolder.appendChild(imgAnchor);
		imgAnchor.appendChild(imgThumb);
		imgAnchor.appendChild(imgThumbOverlay);
		
		if(videoGalCount < videoGalArray.length-1){
			
			videoGalCount++;
		
			displayVideos();
		
		}
	}
	
	imgThumb.onresize = function() {
		imgThumb.setAttribute("width",120);
		imgThumb.setAttribute("height",90);
	}
	
	imgThumb.src = videoGalArray[videoGalCount].thumb;
	
}

function addLoadEvent(func) { 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') { 
	window.onload = func; 
  } else { 
	window.onload = function() { 
	  if (oldonload) { 
		oldonload(); 
	  } 
	  func(); 
	} 
  } 
} 

addLoadEvent(initGallery);