///// Plugin variables

playspeed = 4000;             // The playspeed determines the delay for the "Play" button in ms
dotrans = 1;                  // if value = 1 then there are transitions played in IE
transtype = 'blendTrans';     // 'blendTrans' or 'revealtrans'
transattributes = '0';        // duration=seconds,transition=#<24
imgStop = "/i/elements/accommodaties/control_pause.gif";
imgPlay = "/i/elements/accommodaties/control_play.gif";

//maxImgWitdh = 200;
//maxImgHeight = 120;

maxImgWitdh = 384;
maxImgHeight = 256;

var currentImgWitdh = 0;
var currentImgHeight = 0;
var testImage = new Image();

//#####
//key that holds where in the array currently are
i=0;


//###########################################
window.onload=function(){

	//init all
	if ( document.getElementById('slideshowholder') ) {	InitSlideShow(); }

  if ( document.getElementById("imgmapsmall") ) {
    initSmallMap();
  }


}



//###########################################
function InitSlideShow() {

	//preload images into browser
	preloadSlide();

	//set transitions
	GetTrans();

	//set the first slide
	SetSlide(0);

	//autoplay
	PlaySlide();

}
//###########################################


//###########################################
function SetSlide(num) {

	//too big
	i=num%theimage.length;
	//too small
	if(i<0)i=theimage.length-1;

	//switch the image
	if (document.all&&!window.opera&&dotrans==1) eval('document.images.imgslide.filters.'+transtype+'.Apply()')
	document.images.imgslide.src=theimage[i][0];

  testImage.src = theimage[i][0];
  currentImgWitdh = testImage.width;
  currentImgHeight = testImage.height;

  if ( (currentImgWitdh > maxImgWitdh) || (currentImgHeight > maxImgHeight) ) {

    ratioWidth = currentImgWitdh/maxImgWitdh;
    ratioHeight = currentImgHeight/maxImgHeight;

    if ( ratioWidth > ratioHeight ) {
      document.images.imgslide.width = maxImgWitdh;
      document.images.imgslide.height = currentImgHeight*(maxImgWitdh/currentImgWitdh);
    }
    else {
      document.images.imgslide.height = maxImgHeight;
      document.images.imgslide.width = currentImgWitdh*(maxImgHeight/currentImgHeight);
    }

  }
  else {
    document.images.imgslide.width = currentImgWitdh;
    document.images.imgslide.height = currentImgHeight;
  }


	//too big or too small
	if ( num > theimage.length-1 ) { num = 0; }
	if ( num < 0 ) { num = 0; }

	if ( document.getElementById("div_img_index_" + num) ) {

	  for( k=0; k<theimage.length; k++ ) {
      el_slideshow_index_id = "div_img_index_" + k;
      if ( document.getElementById(el_slideshow_index_id) ) {
        el_slideshow_index = document.getElementById(el_slideshow_index_id);
        el_slideshow_index.className = el_slideshow_index.className.replace(" div_img_index_shown", "");
      }
	  }

    el_slideshow_index_shown = document.getElementById("div_img_index_" + num);
    el_slideshow_index_shown.className += " div_img_index_shown";

  }

	if (document.all&&!window.opera&&dotrans==1) eval('document.images.imgslide.filters.'+transtype+'.Play()')

	//if they want name of current slide
	//document.getElementById('slidebox').innerHTML=theimage[i][2];

}


//###########################################


function toggleBtn( el, state ) {
  var el_id = el.id;
  if ( state == 1 ) { $(el_id).addClassName('ctrl_mo'); }
  else { $(el_id).removeClassName('ctrl_mo'); }
}


function PlaySlide() {

	if (!window.playing) {
		PlayingSlide(i+1);
		if(document.slideshow.play){
			document.slideshow.play.value="Stop";
		}
	}
	else {
		playing=clearTimeout(playing);
		if(document.slideshow.play){
			document.slideshow.play.value="Play";
		}
	}

	// if you have to change the image for the "playing" slide
  if ( $('imgPlay').hasClassName('ctrl_hidden') == false ) {
	  setTimeout("$('imgPlay').addClassName('ctrl_hidden')", 1);
	  setTimeout("$('imgPause').removeClassName('ctrl_hidden')", 1);
  }
  else {
	  setTimeout("$('imgPlay').removeClassName('ctrl_hidden')", 1);
    setTimeout("$('imgPause').addClassName('ctrl_hidden')", 1);
  }

}


//###########################################
function PlayingSlide(num) {

	playing=setTimeout('PlayingSlide(i+1);SetSlide(i+1);', playspeed);

}


//###########################################
//desc: picks the transition to apply to the images
function GetTrans() {

	//si=document.slideshow.trans.selectedIndex;

	if ((document.slideshow.trans && document.slideshow.trans.selectedIndex == 0) || (!document.slideshow.trans && dotrans==0)) {
		dotrans=0;
	}
	else if ((document.slideshow.trans && document.slideshow.trans.selectedIndex == 1) || (!document.slideshow.trans && transtype == 'blendTrans')) {
		dotrans=1;
		transtype='blendTrans';
		document.imgslide.style.filter = "blendTrans(duration=1,transition=1)";
	}
	else {
		dotrans=1;
		transtype='revealtrans';
		if(document.slideshow.trans) transattributes=document.slideshow.trans[document.slideshow.trans.selectedIndex].value;
		document.imgslide.style.filter = "revealTrans(duration=1,transition=" +transattributes+ ")";
	}

}
//###########################################
function preloadSlide() {

	for(k=0;k<theimage.length;k++) {
		theimage[k][0] = new Image().src = theimage[k][0];
	}

}


function clickImage() {

  PlaySlide();
  setTimeout("delayedPlay()", 30000);

}



function clickIndex() {

  //if ( document.images.imgPlay.src.replace("_mo","").indexOf(imgStop) != -1 ) {
  if ( $('imgPlay').hasClassName('ctrl_hidden') == false ) {
    PlaySlide();
  }
  setTimeout("delayedPlay()", 30000);

}


function delayedPlay() {

  //if ( document.images.imgPlay.src.replace("_mo","").indexOf(imgPlay) != -1 ) {
  if ( $('imgPlay').hasClassName('ctrl_hidden') == false ) {
    PlaySlide();
  }

}
