var curImg = 0;
var timerId = -1;
var intervalf = 3000;
var intervalm = 4500;
var intervals = 6000;
var interval = intervals;
var imgIsLoaded = false;
var bfullScreen=false;

// Added for full screen effect
var nSlideAreaWidth=600; 
var nSlideAreaHeight=400;
var nSlideConstraint;
var crossFadeDuration = 10;

function setupSlideshow(nowImage)
{
	var photoDisplay = document.getElementById('photodisplay');
	photoDisplay.style.height = nSlideAreaHeight + "px";
	curImg =   nowImage;

}

function replaceNum(myinput, token, newstr)
{
    var input = myinput;
    var output = input;
    var idx = output.indexOf(token);
    if (idx > -1) 
	{
		output = input.substring(0, idx);
		output += newstr;
		output += input.substr(idx+token.length);
    }
    return output;
}

function changeSpeed(sidx)
{
    switch (sidx) 
	{
		case 0: interval = intervalm; break;
		case 1: interval = intervals; break;
		default: interval = intervalf;
    }
    if (timerId != -1) 
	{
		window.clearInterval(timerId);
		timerId = window.setInterval("forward();", interval);
    }
}

function imgLoadNotify()
{
    imgIsLoaded = true;
}
function changeSlide()
{

	var  slideImgWidth=600;
	var  slideImgHeight=500;
    if (document.all)
	{
    	document.all.main_slideshow.style.filter="blendTrans(duration=2)";
		document.all.main_slideshow.style.filter="blendTrans(duration=crossFadeDuration)";
    	document.all.main_slideshow.filters.blendTrans.Apply();
    }
    imgIsLoaded = false;
    var htmlCont = "<center>" +
	"<img align=absmiddle src=\"" + imgPath +imageSrcArray[curImg] + "\" alt=\"\"";
	
		if(nSlideAreaWidth >= imageWidthArray[curImg] && nSlideAreaHeight >= imageHeightArray[curImg])
		{
			slideImgWidth = imageWidthArray[curImg];
			slideImgHeight = imageHeightArray[curImg];
		}else
		{
			var nSlideAreaRatio = nSlideAreaWidth/nSlideAreaHeight;
			var nPhotoRatio = imageWidthArray[curImg]/imageHeightArray[curImg];

			if(nPhotoRatio > nSlideAreaRatio)
			{
				slideImgWidth = nSlideAreaWidth;
				slideImgHeight = Math.floor((imageHeightArray[curImg]/imageWidthArray[curImg]) * nSlideAreaWidth);
			}
			else
			{
				slideImgHeight = nSlideAreaHeight-20;
				slideImgWidth  = Math.floor((imageWidthArray[curImg]/imageHeightArray[curImg]) * nSlideAreaHeight);
			}

		}
		

	     htmlCont += " width=" + slideImgWidth + " height=" + slideImgHeight;


    htmlCont += " border=0 hspace=0 vspace=0 onload=\"imgLoadNotify();\">";

	htmlCont +=	 "</center>";
	
    var pnumLine = "<small>";
    pnumLine += replaceNum(SHOWINGSTRING, "%slideNum", eval(curImg+1));
    pnumLine += "</small>";
    document.getElementById("pem").innerHTML = pnumLine;
	
	var htmlCont2 = "<span class=subject>" + imageNameArray[curImg]+"&nbsp;</span>" ;

    document.getElementById("main_slideshow").innerHTML = htmlCont;
    document.getElementById("photo_subject").innerHTML = htmlCont2;


    if (document.all) 
		document.all.main_slideshow.filters.blendTrans.Play();
}

function forward()
{
    if (imgIsLoaded) 
	{
		curImg++;
		if (curImg == numImgs)
			curImg = 0;
		changeSlide();
    }
	if (timerId == -1){
	}

}


function rewind()
{
    curImg--;
    if (curImg < 0)
		curImg = numImgs - 1;
    changeSlide();

	if (timerId == -1){ 
	}

}

function stop()
{
    window.clearInterval(timerId);
    timerId = -1;
    document.playbtn.src = buttonImgPfx + buttonOffArray[0];
    document.stopbtn.src = buttonImgPfx + buttonOnArray[1];
    imgIsLoaded = true;
	if (timerId == -1){
	}

}

function play()
{
    if (timerId == -1) 
		timerId = window.setInterval('forward();', interval);
    document.playbtn.src = buttonImgPfx + buttonOnArray[0];
    document.stopbtn.src = buttonImgPfx + buttonOffArray[1];
}
function playMain(){
    if (timerId == -1) {
		timerId = window.setInterval('forward();', interval);
	}
	imgIsLoaded=true;
}

function setButton(direction)
{
    imgIsLoaded = true;
    if (direction == 0) 
	{
		document.prevbtn.src = buttonImgPfx + buttonOnArray[2];
		window.setTimeout("document.prevbtn.src = buttonImgPfx + buttonOffArray[2];", 300);
    }
	else 
	{
		document.fowdbtn.src = buttonImgPfx + buttonOnArray[3];
		window.setTimeout("document.fowdbtn.src = buttonImgPfx + buttonOffArray[3];", 300);
    }
}

// added

var Opacity = 100;
var Xray = 0;
var Gray = 0;
var FlipH = 0;
var FlipV = 0;
var Inverse = 0;
var Rotation = 0;
var Emboss = 0;

var imgFilter = "";

function imgEffect(val) {

  if (val == 'emboss')
    imgEmboss();
  else if(val == 'rotate')
    imgRotate();
  else if(val == 'xray')
    imgXray();
  else if(val == 'gray')
    imgGray();
  else if(val == 'fliph')
    imgFlipH();
  else if(val == 'flipv')
    imgFlipV();
  else if(val == 'inverse')
    imgInverse();
  else if(val == 'opacityplus')
    imgOpacityPlus();
  else if(val == 'opacityminus')
    imgOpacityMinus();
  else if(val == 'cleareffect')
    imgClearEffect();
  else
    imgResize(val);
  //parent.view.imgSrc.style.filter = printFilter();
  changeSlide();
}

function imgEmboss() {
  if (Emboss == 0)
    Emboss = 1;
  else
    Emboss = 0;
}

function imgRotate() {
  if (Rotation < 3)
    Rotation += 1;
  else
    Rotation = 0;
}

function imgXray() {
  if (Xray == 0)
    Xray = 1;
  else
    Xray = 0;
}

function imgGray() {
  if (Gray == 0)
    Gray = 1;
  else
    Gray = 0;
}

function imgFlipH() {
  if (FlipH == 0)
    FlipH = 1;
  else
    FlipH = 0;
}

function imgFlipV() {
  if (FlipV == 0)
    FlipV = 1;
  else
    FlipV = 0;
}

function imgInverse() {
  if (Inverse == 0)
    Inverse = 1;
  else
    Inverse = 0;
}

function imgOpacityPlus() {
  if (Opacity > 100)
    Opacity = 100;
  else
    Opacity += 25;
}

function imgOpacityMinus() {
  if (Opacity < 0)
    Opacity = 0;
  else
    Opacity -= 25;
}

function imgResize(val) {
  Val = val;

  //parent.view.getCollectSize();
}

function printFilter() {
  imgFilter = 'alpha(opacity=100, style=3, finishopacity=' + Opacity +') ';
  imgFilter += 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + Rotation +') ';
  imgFilter += (Emboss == 1 ? 'progid:DXImageTransform.Microsoft.Emboss ':'');
  imgFilter += (Xray == 1 ? 'Xray ':'');
  imgFilter += (Gray == 1 ? 'Gray ':'');
  imgFilter += (FlipH == 1 ? 'FlipH ':'');
  imgFilter += (FlipV == 1 ? 'FlipV ':'');
  imgFilter += (Inverse == 1 ? 'Invert ':'');
  return imgFilter;
}

function imgClearEffect() {
  Opacity = 100;
  Gray = 0;
  Rotation = 0;
  FlipH = 0;
  FlipV = 0;
  Inverse = 0;
  Xray = 0;
  Emboss = 0;
 
  discheck(); 
  imgResize(100);
}

function   discheck(){
 for (var k=0; k < document.menu_box.elements.length; k++) {
    var e = document.menu_box.elements[k];
    if(e.name == 'effectCheckBox')
      e.checked = false;
  }
  return;
}



function toggleScreen()
{
  if(bfullScreen==false){
	fullscreen();
	bfullScreen=true;

  }else{
	restore();	
	bfullScreen=false;

  }
  setupSlideshow();
}

function fullscreen(){
	var hdiff;
	window.resizeTo(screen.width/2,screen.height/2)	  ;
	window.moveTo(0,10)	   ;

	hdiff=window.screenTop;
	window.moveTo(-6,-hdiff+6);
	window.resizeTo(screen.width+13,screen.height+hdiff+26)	 ;
}

function restore(){
	window.moveTo(-4,-4);
	window.resizeTo(screen.width+8,screen.availHeight+8);
}

