var SlideshowImages;
var id=0;
var iconplay ="../images/icons/start.png";
var iconpause="../images/icons/control_pause_blue.png"
var iconnext ="../images/icons/control_end_blue.png";
var iconnext_disabled ="../images/icons/control_fastforward.png";
var iconprevious ="../images/icons/control_start_blue.png";
var iconprevious_disabled ="../images/icons/end.png";
var basedomein ="http://www.denhaaggaatloos.nl";
var defaulttimeout = 10000;
var loading;
var NumOfTry;
var wCur ="";
var hCur ="";


function SelectImage(id){
	var neximage =document.createElement("img");
	var previousimage =document.createElement("img");
	previousimage.src=(id==0)?(''):(id-1)
	neximage.src=(id==SlideshowImages.length-1)?(''):(id+1);
	var param="";
	var param = param + id;
	//alert(id);
	if(param.search("jpg")==-1){
		if(param.search(basedomein)!=-1){
			param =param.substring(basedomein.length,param.length)
			if(param.search("pg")==-1){

				param =param +"/pg1";
				alert(param);
			}else{
				param =param.replace("/pg","/fotos/pg")
			}
		}
		SlideshowImages ="";
		SlideshowImages = document.getElementById("album_container").getElementsByTagName("a");
		id=0;
		getpicpage(param);
		
	}else{
		if(param.search(basedomein)!=-1){
			param =param.substring(basedomein.length,param.length)
		}
		getpicture(param);
	}
	
	return false;
}

function Stopslideshow(){
		
	var img=document.getElementById("playbutton");
	img.src=iconplay;
	(window.detachEvent)?(img.detachEvent("onclick",Stopslideshow)):(img.removeEventListener("click",Stopslideshow,false));
	(window.attachEvent)?(img.attachEvent("onclick",StartSlideshow)):(img.addEventListener("click",StartSlideshow,false));
	clearTimeout(SildeshowTimer);
	SetButtons();
	return false;
}
function StartSlideshow(){
	var img=document.getElementById("playbutton");
	img.src=iconpause;
	
	(window.detachEvent)?(img.detachEvent("onclick",StartSlideshow)):(img.removeEventListener("click",StartSlideshow,false));
	(window.attachEvent)?(img.attachEvent("onclick",Stopslideshow)):(img.addEventListener("click",Stopslideshow,false));
	SlideImage();
}
function SlideImage(){
	if(id==SlideshowImages.length){id=0;return Stopslideshow();}
		for (var x = 1; x <= SlideshowImages.length; x++){
			var arrimg = "";
			var current_img ="";
			arrimg = arrimg + SlideshowImages[x]
			arrimg =arrimg.substring((arrimg.lastIndexOf("/")+1),arrimg.indexOf("#"));
			current_img =document.getElementById("selected_image").src;
			current_img = current_img.substr((current_img .lastIndexOf("/")+1));
			if(arrimg ==current_img){id=x+1;}
			if(id==11){id=0}
		}

	SelectImage(SlideshowImages[id]);
	//SildeshowTimer =setTimeout("SlideImage()", defaulttimeout);
	SildeshowTimer =setTimeout("SlideImage()", document.getElementById("timeout").options[document.getElementById("timeout").selectedIndex].value);

	id=id+1;
	SetButtons()
}
function SetButtons(){
	var nexbutton = document.getElementById("nexbutton");
	var previousbutton=document.getElementById("previousbutton");
	(id==0)?( previousbutton.src=iconprevious_disabled):(previousbutton.src=iconprevious);
	(id==0)?(previousbutton.className="slideshowcontrollboximg_inactive"):(previousbutton.className="slideshowcontrollboximg_active");
	(id==SlideshowImages.length-1)?(nexbutton .src=iconnext_disabled):(nexbutton .src=iconnext);
	(id==SlideshowImages.length-1)?(nexbutton.className="slideshowcontrollboximg_inactive"):(nexbutton.className="slideshowcontrollboximg_active");		
}

function PreviousImage(){
	(id>=1)?(id=id-1):('');
	(id>=0)?(SelectImage(SlideshowImages[id])):('');
	SetButtons();
}
function NextImage(){
	(id==SlideshowImages.length-1)?(id=id):(id=id+1);
	(id<=SlideshowImages.length-1)?(SelectImage(SlideshowImages[id])):('');
	SetButtons();
}

function Slideshow(){

	SlideshowImages = document.getElementById("album_container").getElementsByTagName("a");
	var gallerybody = document.getElementById("gallery_body");
	var slideshownav =document.createElement("div")
	slideshownav.id = "slideshowcontrollbox";

	var previousbutton=document.createElement("img")
	previousbutton.src = iconprevious_disabled;
	previousbutton.alt ="previous";
	previousbutton.id ="previousbutton";
	previousbutton.className ="slideshowcontrollboximg_inactive";
	(window.attachEvent)?(previousbutton.attachEvent("onclick", PreviousImage)):(previousbutton.addEventListener("click", PreviousImage,false));

	

	var playbutton=document.createElement("img")
	playbutton.src = iconplay ;
	playbutton.alt ="play";
	playbutton.id="playbutton";
	playbutton.className ="slideshowcontrollboximg_active";
	(window.attachEvent)?(playbutton.attachEvent("onclick",StartSlideshow)):(playbutton.addEventListener("click",StartSlideshow,false));
	
	//var timeout = 
	selectf=document.createElement('select'); 
	selectf.id="timeout"; 
	selectf.name="test stuff"; 
	//soption = document.createElement('option'); 
	//selectf.appendChild(soption); 
	//soption.appendChild(document.createTextNode(name)); 
	selectf.options[0] = new Option ("5 sec",5000);
	selectf.options[1] = new Option ("10 sec",10000);
	selectf.options[2] = new Option ("15 sec",15000);
	selectf.options[3] = new Option ("20 sec",20000);
	

	var nexbutton=document.createElement("img");
	nexbutton.src = iconnext;
	nexbutton.alt ="next";
	nexbutton.id ="nexbutton";
	nexbutton.className ="slideshowcontrollboximg_active";
	(window.attachEvent)?(nexbutton.attachEvent("onclick",NextImage)):(nexbutton.addEventListener("click",NextImage,false));

	slideshownav.appendChild(previousbutton);
	slideshownav.appendChild(playbutton);
	slideshownav.appendChild(nexbutton);
	slideshownav.appendChild(selectf); 
	gallerybody.appendChild(slideshownav);


}

function getpicpage(page){
	
   var param = 'gallery_is_ajax=true&action=getpicpage&page=' + page;
	//alert(param);		
      new Effect.BlindUp('album_body', {afterFinish:function() {
      new Ajax.Updater('album_body', '/modules/module_gallery.php', {parameters: param,onComplete:function(){
       new Effect.BlindDown('album_body');
      }, asynchronous:true, evalScripts:true});
    }});

}




function FillPictureContainer(imagefile,imgdescription,views){
return '<div id="selected_image_container"><img id="selected_image" alt="hoi" src="'+ imagefile + '"><div id="imageDataContainer"><div id="imageData"><div id="imageDetails"><span id="caption">'+imgdescription+'</span><span id="numberDisplay">'+views+' x bekenen</span></div></div></div></div>'
}

function getpicture(picurl){
	var param = 'gallery_is_ajax=true&action=getpicture&picurl=' + picurl;
	
	if(!loading==true){
		loading = true;
		Element.hide('imageDataContainer');
		new Effect.BlindUp('main_image_content',{afterFinish: function() {
		//alert('afterfinish')
			new Ajax.Request(basedomein+'/modules/module_gallery.php', {parameters: param,onComplete:function(t){
				//alert(t.responseText)
				var image_properties = eval('('+ t.responseText +')');
				//alert(image_properties.image[0].imgdescription);
				//alert(image_properties.image[0].views);
				//$('main_image_content').innerHTML = '<img id="selected_image" alt="hoi" src="'+ image_properties.image[0].sourcefile + '">';
				$('main_image_content').innerHTML =FillPictureContainer(image_properties.image[0].sourcefile,image_properties.image[0].imgdescription,image_properties.image[0].views);
				Element.hide('imageDataContainer');
				yScale = (image_properties.image[0].Imheight /$('main_image').offsetHeight) * 100;
				new Effect.Scale('main_image', yScale, {scaleX: false, duration: 1,afterFinish:function(effect){
					new Effect.BlindDown('main_image_content',{afterFinish: function(){new Effect.Parallel(
			[ new Effect.SlideDown( 'imageDataContainer', { sync: true, duration: 100 + 0.25, from: 0.0, to: 1.0 }), 
			  new Effect.Appear('imageDataContainer', { sync: true, duration: 1.0 }) ], 
			{ duration: 0.65} 
		);loading =false;}})}});
	       			}, asynchronous:true, evalScripts:true});}}
		);
	}
     
}

//window.onload= Slideshow;

