var rotate_delay = 4000; // delay in milliseconds (5000 = 5 secs)

var current = 0;
function isSlideShowing(){
	return document.slideform.slidebutton.value=='Stop'
}

function AutoStart(text) {

document.slideform.slidebutton.value = (text == "Stop") ? "Start" : "Stop";
rotate();

}


function rotate() {

       if (isSlideShowing()) {
          current = (current == document.slideform.slide.length-1) ? 0 : parseInt(current)+1;          
	  putBigImg(current);	         
          window.setTimeout("rotate()", rotate_delay);

       }

}

function startSlideShow(text){
         AutoStart(text);
	document.getElementById('play_button').style.display="none";
	document.getElementById('pause_button').style.display="inline";
	document.getElementById('stop_button').style.display="inline";

	//disable next & preview scroll button
	document.getElementById('previousScrollButton').innerHTML='<img src="/system/modules/bildgallery/layout/images/previous_disable.gif" />';
	document.getElementById('nextScrollButton').innerHTML='<img src="/system/modules/bildgallery/layout/images/next_disable.gif" />';
	
	//change heading  label events
	document.getElementById('slideShowHeading').innerHTML = '<a href="#" onClick="return stopSlideShow(\'Stop\');" >Slide Show:</a>';
        return false;
}

function pauseSlideShow(text){
         AutoStart(text);
	document.getElementById('play_button').style.display="inline";
	document.getElementById('pause_button').style.display="none";
        return false;
}

function stopSlideShow(text){
     AutoStart(text);
	 //disable previous image
	 document.getElementById("show_lightbox_"+current).style.display='none';
     current = 0;	
	 //show first image & it's caption
	 document.getElementById("show_lightbox_"+current).style.display='block';
	 document.getElementById("image_caption").innerHTML = document.slideform.slide[current].text;
    //document.images.show.src = document.slideform.slide[current].value;
        makeSelectedImg(current);
	//scroll automatically
		autoScroll();
	document.getElementById('play_button').style.display="inline";
	document.getElementById('pause_button').style.display="none";
	document.getElementById('stop_button').style.display="none";
	//enable next & preview scroll button
	document.getElementById('previousScrollButton').innerHTML='<a href="#" onclick="return scrollPrev()" ><img src="/system/modules/bildgallery/layout/images/previous.gif" alt="Vorherige" title="Vorherige" /></a>';
	document.getElementById('nextScrollButton').innerHTML='<a href="#" onclick="return scrollNext()"><img src="/system/modules/bildgallery/layout/images/next.gif" alt="Vorwärts" title="Vorwärts" /></a>';

	//change heading  label events
	document.getElementById('slideShowHeading').innerHTML = '<a href="#" onClick="return startSlideShow(\'Start\');" >Slide Show:</a>';
        
        return false;
}


//scrolling
var ThumbScrollPosition=0;

function putBigImg(thumb_id){
	
	if(thumb_id >= document.slideform.slide.length){
		thumb_id = document.slideform.slide.length-1;
		
	}
	/*
		//old fashion
	//change image src path
	//document.getElementById("show").src= ''+document.slideform.slide[thumb_id].value+'';
	
	//change href & tile for lighbox
	//document.getElementById('show_lightbox').href=''+document.slideform.bigslide[thumb_id].value+'';
	//document.getElementById('show_lightbox').title=''+document.slideform.bigslide[thumb_id].text+'';
	*/
	//new fashion
	//hide all
	for(i=0; i<document.slideform.slide.length; i++)
		document.getElementById("show_lightbox_"+i).style.display='none';
	//show one selected
	document.getElementById("show_lightbox_"+thumb_id).style.display='block';
	
	//change image caption
	document.getElementById("image_caption").innerHTML = document.slideform.slide[thumb_id].text;
	current = thumb_id;
	
	//find selected class & change it to opacity        
        makeSelectedImg(thumb_id);

	
	return false;
}

function scrollNext(){
	if((Math.ceil(document.slideform.slide.length/5)-1) > (ThumbScrollPosition)){ 
		ThumbScrollPosition++;
		document.getElementById("scroller").style.marginLeft=(-1*(ThumbScrollPosition*416))+"px";
		if(!isSlideShowing())
			putBigImg((ThumbScrollPosition*5));
	}
	
		
	return false;
}

function scrollPrev(){
	
	if(ThumbScrollPosition >0 ){ 
		ThumbScrollPosition--;
		document.getElementById("scroller").style.marginLeft=(-1*(ThumbScrollPosition*416))+"px";
		if(!isSlideShowing())
		putBigImg((ThumbScrollPosition*5));
	}
	return false;
}

function makeSelectedImg(thumb_id){
	//store the pointer for selected image
	document.slideform.slide.selectedIndex = thumb_id;
	
	//make all of the images to not selected (class opacity) 
	for(i=0; i<document.slideform.slide.length; i++){
		document.getElementById('thumb_img_'+i).className="scrolling_img opacity";
	}
	
	//change the class for selected image
	document.getElementById('thumb_img_'+thumb_id).className="scrolling_img selected";

	if (isSlideShowing()) {
		//scroll automatically
		autoScroll();
	}
}

/*
Scroll automatically to show the selected thumbnail image if it is in the overflow hidden window
*/
function autoScroll(){
	//if it is not the starting image
	if(current >0){
		if((current % 5) == 0){
			scrollNext();
		}
	}
	else{
		//make start image selected
		ThumbScrollPosition =0;
		document.getElementById("scroller").style.marginLeft=(-1*(ThumbScrollPosition*416))+"px";
	}
	
}
