var current_slide;

var ss_timeout;

function next_slide(slide, id, max) {
  var timeout = 8000 + (Math.random() * 3000);
  if (slide > 0) {    
    dojo.fadeOut({node: "slideshow_"+id+"_"+slide ,duration: 1000}).play();
    ss_timeout = window.setTimeout("next_slide("+(slide - 1)+", "+id+", "+max+")", "10000");
    current_slide = slide - 1;
  }else{
    // Reset
    dojo.fadeIn({node: "slideshow_"+id+"_"+(max - 1),duration: 1000}).play();
    for (var i = 0; i < (max - 1); i++) {
      dojo.fadeIn({node: "slideshow_"+id+"_"+i,delay: 1000, duration: 10}).play();
    }
    ss_timeout = window.setTimeout("next_slide("+(max - 1)+", "+id+", "+max+")", timeout);
    current_slide = (max - 1);
  }
  activateSlideChanger(id, current_slide, max);
  updateSlideDescription(id, current_slide, max);
}

function updateSlideDescription(id, slide, max) {
	
}

function go_to_slide(slide, id, max) {	  
	  window.clearTimeout(ss_timeout);	  
      // Reset
	  dojo.byId('slideshow_'+id+"_"+(max-1)).style.display = 'none';	  
	  for (var i = 0; i < (max - 1); i++) {			    
	    dojo.byId('slideshow_'+id+"_"+i).style.display = 'none';	    
	  }
	  dojo.fadeIn({node: "slideshow_"+id+"_"+slide, duration: 1}).play();
	  dojo.byId('slideshow_'+id+"_"+slide).style.display = 'block';
	  current_slide = slide;
	  activateSlideChanger(id, slide, max);
	  updateSlideDescription(id, slide, max);
	}

function manual_next_slide(id, max) {
	if (current_slide == 0) {
		current_slide = max;
	}	
	go_to_slide(current_slide - 1, id, max);
}

function manual_prev_slide(id, max) {
	if (current_slide == (max - 1)) {
		current_slide = -1;
	}
	go_to_slide(current_slide + 1, id, max);
}

function activateSlideChanger(id, slide, max) {	
	for (var i = 0; i < max; i++) {
		dojo.byId('slide_changer_'+id+'_'+i).className = 'slidechanger';
	}
	dojo.byId('slide_changer_'+id+'_'+slide).className = 'slidechanger active';
}

function init_slideshow(id, pictures) {
	dojo.addOnLoad(function() {
	  var below = "";
	  for (var i = 1; i <= pictures; i++) {
		  below += " <a href='javascript:void(null)' id='slide_changer_"+id+"_"+(pictures-i)+"' class='slidechanger' onClick='go_to_slide("+(pictures - i)+", "+id+", "+pictures+");'>&nbsp;</a>";
	  }	  
	  if (dojo.byId('picture_changer')) {
		  dojo.byId('picture_changer').innerHTML = below;
	  }
	  current_slide = (pictures - 1);
	  activateSlideChanger(id, current_slide, pictures);
	  updateSlideDescription(id, current_slide, pictures);
   });
}

function scroll_right(id, num_pictures) {
	  var l = dojo.style(dojo.byId("scroller"+id), "left");
	  if (l > 0 - ((num_pictures - 3) * 81)) {
	  dojo.animateProperty({
	            node: dojo.byId("scroller"+id),
	            duration: 500,
	            properties: {
	                left: l - 81                
	            }
	        }).play();
	   }else{
	      dojo.animateProperty({
	            node: dojo.byId("scroller"+id),
	            duration: 250,
	            properties: {
	                left: l - 40           
	            }
	        }).play();
	        dojo.animateProperty({
	            node: dojo.byId("scroller"+id),
	            duration: 250,
	            delay: 250,
	            properties: {
	                left: {
	                  start: l - 40,
	                  end: l
	                }
	             }
	        }).play();
	   }
	}

	function scroll_left(id) {
	  var l = dojo.style(dojo.byId("scroller"+id), "left");
	  if (l < 0) {
	  dojo.animateProperty({
	            node: dojo.byId("scroller"+id),
	            duration: 500,
	            properties: {
	                left: function() {
	                  return l + 81;
	                }
	            }
	        }).play();
	  }else{
	       dojo.animateProperty({
	            node: dojo.byId("scroller"+id),
	            duration: 250,
	            properties: {
	                left: l + 40           
	            }
	        }).play();
	        dojo.animateProperty({
	            node: dojo.byId("scroller"+id),
	            duration: 250,
	            delay: 250,
	            properties: {
	                left: {
	                  start: l + 40,
	                  end: l
	                }
	             }
	        }).play();
	  }
	}


	function show(large, id) {
	  dojo.byId("display"+id).src = large;
	}
	
	function g_scroll_right(id, num_pictures) {
		  var l = dojo.style(dojo.byId("scroller"+id), "left");
		  if (l > 0 - ((num_pictures - 4) * 106)) {
		  dojo.animateProperty({
		            node: dojo.byId("scroller"+id),
		            duration: 500,
		            properties: {
		                left: l - 106                
		            }
		        }).play();
		   }else{
		      dojo.animateProperty({
		            node: dojo.byId("scroller"+id),
		            duration: 250,
		            properties: {
		                left: l - 40           
		            }
		        }).play();
		        dojo.animateProperty({
		            node: dojo.byId("scroller"+id),
		            duration: 250,
		            delay: 250,
		            properties: {
		                left: {
		                  start: l - 40,
		                  end: l
		                }
		             }
		        }).play();
		   }
		}

		function g_scroll_left(id) {
		  var l = dojo.style(dojo.byId("scroller"+id), "left");
		  if (l < 0) {
		  dojo.animateProperty({
		            node: dojo.byId("scroller"+id),
		            duration: 500,
		            properties: {
		                left: function() {
		                  return l + 106;
		                }
		            }
		        }).play();
		  }else{
		       dojo.animateProperty({
		            node: dojo.byId("scroller"+id),
		            duration: 250,
		            properties: {
		                left: l + 40           
		            }
		        }).play();
		        dojo.animateProperty({
		            node: dojo.byId("scroller"+id),
		            duration: 250,
		            delay: 250,
		            properties: {
		                left: {
		                  start: l + 40,
		                  end: l
		                }
		             }
		        }).play();
		  }
		}	
