﻿// Many functions/fixes depend on browser detection
var IE = document.all?true:false; 

 
 
 /*
Periodically, Microsoft releases a new version of the MSXML object.
To create an object using the latest version use this function to 
do so. It will loop through potential prog IDs until it finds a match
*/
function createLatestMSXMLObject()
{
	// Define a list of MSXML prog IDs down to the  
	// lowest that will work with your program
	var msxmlIDs = ["6.0", "5.0", "4.0", "3.0"];
	
	var objStr = "Msxml2.XMLHTTP.";

	// Loop through the IDs until you get to one that is installed
	for (var i = 0, l = msxmlIDs.length; i < l; i++) 
	{
		try 
		{
			// Create the object
			var msxmlObj = new ActiveXObject(objStr + msxmlIDs[i]);
			
			// Return it to the calling code. This will break out
			//  of the loop, ensuring no lower version is created
			return msxmlObj;
		}
		catch (e) 
		{ 
			
		}
	}	
}




function postRequest(request, serverURL) 
{
	try 
	{
		var httpRequest;

		try 
		{
			httpRequest = new XMLHttpRequest();
		} 
		catch (e)
		{	

			httpRequest = createLatestMSXMLObject();
			
		}


		httpRequest.open("POST", serverURL, false);


		httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		
		httpRequest.send(request);

		  
		return httpRequest.responseText;


	}
	catch (e) 
	{
		
		alert(e);
		return null;
	}
}
 



var oddEven = 1;

function displaySlide(slide)
{
    
    setTimeout(foo, 2000);
    
    opacity("slideImg" + oddEven, 100, 0, 1000);
    
    if(oddEven == 2)
    {
        oddEven = 1;
    }
    else
    {
        oddEven = 2;
    }
    

    var slideImg = document.getElementById("slideImg" + oddEven);
    
   //opacity('slideImg', 100, 50, 375);
    
     
     
    slideImg.src = slide;
    
   // opacity('slideImg', 50, 100, 375);
   
  // setTimeout(foo, 500);
   
   opacity("slideImg" + oddEven, 0, 100, 500);
     
}

 
 function foo()
 {
 
 }
 
 
function slideShow(dir)
{ 

    var requestStr = "dir=" + dir;

    var results = postRequest(requestStr, "directoryList.aspx");

    //var results = "3527_061402.jpg,3527_061402_432.jpg,3527_061402_543.jpg,3528_081702.jpg,3528_081702_432.jpg,3528_081702_543.jpg,3629_051701.jpg,3629_051701_432.jpg,3629_051701_543.jpg,3729_091302.jpg,3729_091302_432.jpg,3729_091302_543.jpg,3828_070502.jpg,3828_070502_432.jpg,3828_070502_543.jpg,3927_081302.jpg,3927_081302_432.jpg,3927_081302_543.jpg,4027_052901.jpg,4027_052901_432.jpg,4027_052901_543.jpg,4126_070701.jpg,4126_070701_432.jpg,4126_070701_543.jpg,4127_091202.jpg,4127_091202_432.jpg,4127_091202_543.jpg";

    var imageArray = results.split(",");

    // prepopulate one of the slide holders with last image
    var slideImg = document.getElementById("slideImg1");

    slideImg.src = "image/slides/" + imageArray[imageArray.length-1];


    /////var dump = document.getElementById("dump");


    // preload images
    for(slideNum=0; slideNum<imageArray.length; slideNum++) //
    {
         eval("var pic" + slideNum + "= new Image(1,1);");

        eval("pic" + slideNum + ".src = imageArray[slideNum]");
        
        
        /////dump.value += eval("pic" + slideNum + ".src") + "\n\n";
    }

    var increment = 0;

    var delay = 1750;

    for(loop=1; loop<100; loop++)
    {
        
 
       
        for(slideNum=0; slideNum<imageArray.length; slideNum++) //
        {
            var slide = "image/" + dir + "/" + imageArray[slideNum];
        
       
        
            var setTimeoutStatement = "displaySlide('" + slide + "')";
            
            increment += delay;

		    setTimeout(setTimeoutStatement, increment);
        }
    }
}





function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

var sEvent = "onload";

var fFunction = function(){ slideShow('slides'); };

// Check browser before attaching
if (IE)
{
	// Attach the event listener
	window.attachEvent(sEvent, fFunction);
	
}
else
{
	// If not IE, we need to translate the event names
	sEvent = sEvent.replace("on", "");

	// Attach the event listener
	window.addEventListener(sEvent, fFunction, false);
}


