function viewportWidth() {
	return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
}

function reCenter(el){
	oldWidth = windowWidth;
	newWidth = viewportWidth();
	difference = parseInt(newWidth - oldWidth)/2;
	
	slideshowDiv.style.left = (parseInt(slideshowDiv.style.left)+difference)+"px";
	//new Effect.MoveBy('slideshow_strip', 0, difference, {transition: Effect.Transitions.sinoidal, duration: .5});
	windowWidth = newWidth;
}

function newWindow(e, options){
	var url;
	var title;

	if(options == null || typeof(options) == "undefined"){
		options = "";
	}else if(typeof(options) != "string"){
		options = options.join(",");
	}
	
	if(typeof(e.href) != "undefined"){
		url = e.href;
	}else{
		url = "http://www.lioneldeluy.com";
	}
	
	if(typeof(e.title) != "undefined"){
		title = e.title;
	}else{
		title = "Lionel Deluy";
	}
	var newwindow = window.open(url, title, options);
	if (window.focus) {newwindow.focus()}
}

function moveTo(curEl, dir){
	disableNav();
	var e = document.getElementById(curEl);
	var i = parseInt(e.id.split("_")[e.id.split("_").length-1]);
	var nextEl = document.getElementById("slideshow_img_"+(i+1));
	var prevEl = document.getElementById("slideshow_img_"+(i-1));
	var marginAmt;
	
	if(getStyle(e.id, "margin-right")){
		marginAmt = parseInt(getStyle(e.id, "margin-right"));
	}else if(getStyle(e.id, "margin")){
			margArry = getStyle(e.id, "margin").split(" ");
			marginAmt = parseInt(margArry[1]);
	}else{
		marginAmt = 0;
	}
			
	if(dir == "next"){
		var offset = (e.offsetWidth/2)+marginAmt+(nextEl.offsetWidth/2);
		offset = -offset;
		curEl = nextEl;
	}else if(dir=="prev"){
		var offset = (e.offsetWidth/2)+marginAmt+(prevEl.offsetWidth/2);
		curEl = prevEl;
	}
	
	new Effect.MoveBy('slideshow_strip', 0, offset, {transition: Effect.Transitions.sinoidal,
		beforeStart: function(){spotlight(curEl.id);},
		afterFinish: function(){updateNav(curEl.id);}});	
}

function spotlight(curEl){
	for(i=0;i<slideshowImages.length;i++){
		if(slideshowImages[i].id == curEl){
			new Effect.Fade(slideshowImages[i], {to: 1});
		}else{
			new Effect.Fade(slideshowImages[i], {to: .1});
		}
	}	
}

function getStyle(x,styleProp)
{
	if(typeof(x) == "string"){
		var x = document.getElementById(x);
	}
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function totalWidth(el){
	var el = document.getElementById(el);
	var images = el.getElementsByTagName('img');
	var width = 500;
	for(i=0;i<images.length;i++){
		width+=parseInt(images[i].offsetWidth);
		if(getStyle(images[i], "margin-left")){
			width+=parseInt(getStyle(images[i], "margin-left"));
		}
		if(getStyle(images[i], "margin-right")){
			width+=parseInt(getStyle(images[i], "margin-right"));
		}
		
	}
	return width;
}

function createNav(){
	var parent = document.getElementById('slideshow');
	
	prevNav = document.createElement('a');
	prevNav.href = "#";
	prevNav.id = "slideshow_prev";
	prevNav.innerHTML = "&lt;";
	prevNav.onclick = function(){return false;}
	parent.appendChild(prevNav);
	
	nextNav = document.createElement('a');
	nextNav.href = "#";
	nextNav.id = "slideshow_next";
	nextNav.innerHTML = "&gt;";
	nextNav.onclick = function(){return false;}
	parent.appendChild(nextNav);
}

function disableNav(){
	var prevNav = document.getElementById("slideshow_prev");
	var nextNav = document.getElementById("slideshow_next");
	prevNav.onclick = function(){return false;}
	nextNav.onclick = function(){return false;}
}

function updateNav(curEl){
	var prevNav = document.getElementById("slideshow_prev");
	var nextNav = document.getElementById("slideshow_next");
	var e = document.getElementById(curEl);
	var i = parseInt(e.id.split("_")[e.id.split("_").length-1]);
	var nextEl = document.getElementById("slideshow_img_"+(i+1));
	var prevEl = document.getElementById("slideshow_img_"+(i-1));
	
	if(prevEl){
		prevNav.removeAttribute('class');
		prevNav.removeAttribute('className');
		prevNav.onclick = function(){moveTo(curEl, "prev"); return false;}
	}else{
		prevNav.onclick = function(){return false;}
		prevNav.className = "disabled";
	}
	
	if(nextEl){
		nextNav.removeAttribute('class');
		nextNav.removeAttribute('className');
		nextNav.onclick = function(){moveTo(curEl, "next"); return false;}
	}else{
		nextNav.onclick = function(){return false;}
		nextNav.className = "disabled";
	}
}

initSlideshow = function(){
	slideshowDiv = document.getElementById('slideshow_strip');
	slideshowImages = slideshowDiv.getElementsByTagName('img');
	
	windowWidth = slideshowDiv.parentNode.offsetWidth;
		
	//slideshowDiv.style.width = totalWidth('slideshow_strip')+"px";
	slideshowDiv.parentNode.style.overflow = "hidden";
	center = (windowWidth/2);
	centerOffset = center-((slideshowImages[0].width)/2);
	new Effect.MoveBy('slideshow_strip', 0, centerOffset, {transition: Effect.Transitions.sinoidal });
	
	for(i=0;i<slideshowImages.length;i++){
		slideshowImages[i].id = "slideshow_img_"+(i+1);
	}
	curEl = document.getElementById('slideshow_img_1');
	spotlight(curEl.id);
	
	createNav();
	updateNav(curEl.id);
	fadeOverlay();
}

function changeOverlay(){
	var overlay = document.getElementById('ss_overlay');
	overlay.innerHTML = "<img src='img/spinner.gif' alt='spinner' />&nbsp;&nbsp;loading...";
}

function fadeOverlay(){
	var overlay = document.getElementById('ss_overlay');
	new Effect.Fade(overlay);
}

function removeImages(){
	postImages = [];
	for(i=3;i<slideshowImages.length;i++){
		var theId = "slideshow_img_"+(i+1);
		slideshowImages[i].id = theId;
		postImages.push(slideshowImages[i]);
	}
	
	for(i=0;i<postImages.length;i++){
		slideshowDiv.removeChild(document.getElementById(postImages[i].id));
	}
}

function loadImages(){
	for(i=0;i<postImages.length;i++){
		slideshowDiv.appendChild(postImages[i]);
	}
	
	for(i=0;i<slideshowImages.length;i++){
		slideshowImages[i].id = "slideshow_img_"+(i+1);
	}
}