// globals recording the photo currently chosen and the limit depending on how many photos there are
var current_photo;
var maximum;

// unobtrusively set up the gallery onload
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		};
	}
};

// function which inserts a span with a 50% transparent PNG to overlay on top of currently selected image
function createOverlay(target){
	var overlay = document.createElement("span");
	overlay.id = "overlay";
	target.appendChild(overlay);
};

function showPic(whichPic){
	// reset the transparent overlay when a new pic is selected by looping through the gallery
	var gallery = document.getElementById("gallery_thumbs");
	var thumbs = gallery.getElementsByTagName("li");
	var overlay = document.getElementById("overlay");
	for (var i=0; i < thumbs.length; i++) {
		// does the thumb have an overlay span? If so remove it
		if(thumbs[i].getElementsByTagName("span")[0]){
			thumbs[i].removeChild(overlay);
		}
	};
	
	// apply the overlay to the picture just chosen
	var chosen = whichPic.parentNode;
	createOverlay(chosen);
	
	var main_image = document.getElementById("photo_gallery");
	var placeholder = document.getElementById("photo");
	
	var source = whichPic.getAttribute("href");	
	
	// create new image
	var new_img = document.createElement("img");
	new_img.setAttribute("id","photo");
	new_img.setAttribute("src",source);
	
	if(whichPic.className=="portrait"){
		var width = "380px";
		var height = "570px";
	} else {
		var width = "570px";
		var height = "380px";
	}
	new_img.setAttribute("width",width);
	new_img.setAttribute("height",height);
	new_img.setAttribute("alt",whichPic.title);
	new_img.setAttribute("title","");
	
	//replace current image with new one
	main_image.replaceChild(new_img,placeholder);
		
	//change the caption to read what is in the selected images' link
	//var text = whichPic.getAttribute("title");
	//var caption = document.getElementById("caption");
	//caption.innerHTML = text;
	
	//let the script know the photo that has been selected for navigational purposes
	current_photo = whichPic.id;
};

function prepareGallery(){
	if(!document.getElementsByTagName || !document.getElementById) {
		return false;
	}
	if(!document.getElementById("gallery_thumbs")){
		return false;
	}
	var gallery = document.getElementById("gallery_thumbs");
	
	// put an overlay on the very first image, the default
	var default_thumb = gallery.getElementsByTagName("li")[0];
	createOverlay(default_thumb);
	
	// default current photo
	current_photo = "photo_"+1;
	
	// add onclicks to each link in the gallery
	var links = gallery.getElementsByTagName("a");
	// the limit is set here meaning you can add as many list items as you want without changing this
	maximum = links.length;
	
	for (var i=0; i < links.length; i++) {
		adjust = i + 1;
		links[i].id = "photo_"+adjust;
		links[i].onclick = function(){
			showPic(this);
			return false;
		};
	};
};

function preparePress(){
	if(!document.getElementsByTagName || !document.getElementById) {
		return false;
	}
	if(!document.getElementById("placeholder")){
		return false;
	}
	
	var links = document.getElementById("press_links").getElementsByTagName('a');

	for (var i=0; i < links.length; i++) {
		links[i].onclick = function(){
			showPress(this);
			return false;
		};
	};
	
	var filenames  = ['vogue-1.png', 'body-and-soul.png', 'stella.png', 'you.png', 'the-london-magazine.jpg', 'evening-standard-1.png', 'tatler.png', 'evening-standard.png', 'vogue.png','daily-express.jpg'];
	preloadImages(filenames);
};

function preloadImages(images){
	var src = 'images/press/';
	var preload = new Array;

	for (var i=0; i < images.length; i++) {
			preload[i] = new Image();
			preload[i].src = src+images[i];
	};
};

function showPress(link){
	
	lis = document.getElementById('press_links').getElementsByTagName('li');
	for (var i=0; i < lis.length; i++) {
		lis[i].className = '';
	};
	
	var placeholder = document.getElementById("scan");
	var caption = document.getElementById("caption");
	var zoom = document.getElementById("zoom");

	var thisLI = link.parentNode.className = 'selected';
	var thisCaption = link.title;
	var thisWidth = link.className.substring(1,4);
	var thisHeight = link.className.substring(5,8);
	var thisZoom = (link.className.search("large-version") != -1 ? true : false );
	var thisImg = link.href;
	
	var new_img = document.createElement("img");
	new_img.setAttribute("id","scan");
	new_img.setAttribute("src",thisImg);
	new_img.setAttribute("width",thisWidth);
	new_img.setAttribute("height",thisHeight);
	
	document.getElementById('placeholder').replaceChild(new_img,placeholder);
	
	if(thisZoom == true){
		zoom.style.display = 'block';
		placeholder.className = 'zoomable';
		zoom.href = thisImg.replace(/.jpg/, '-large.jpg');
		placeholder.onclick = function(){
			window.location.href = zoom.href;
			return false;
		}
	} else {
		zoom.style.display = 'none';
		placeholder.className = '';
		zoom.href = '';
	}	
	
		// caption.innerHTML = thisCaption;
};

//unobtrusive loading of gallery
addLoadEvent(prepareGallery);
addLoadEvent(preparePress);
