// Pop-Up Embedder Script by David Battino, www.batmosphere.com
// Version 2006-05-31 
// OK to use if this notice is included
   
function initializeAudio(filepath) {
   // Get Operating System 
   var isWin = navigator.userAgent.toLowerCase().indexOf("windows") !=-1
   if (document.all) { // Use MIME type = "application/x-mplayer2"

      visitorOS="Windows";
   } 
   else { // Use MIME type = "audio/mpeg"; // or audio/x-wav or audio/x-ms-wma, etc.
      visitorOS="Other";
   }

   // Get the MIME type of the audio file from its extension (for non-Windows browsers)
   var mimeType = "audio/mpeg"; // assume MP3/M3U
   var objTypeTag = "application/x-mplayer2"; // The Windows MIME type to load the WMP plug-in in Firefox, etc.
   var theExtension = filepath.substr(filepath.lastIndexOf('.')+1, 3); // truncates .aiff to aif
   if (theExtension.toLowerCase() == "wav") { mimeType = "audio/x-wav"};
   if (theExtension.toLowerCase() == "aif") { mimeType = "audio/x-aiff"};    
   if (theExtension.toLowerCase() == "wma") { mimeType = "audio/x-ms-wma"};
   if (theExtension.toLowerCase() == "mid") { mimeType = "audio/mid"};
   // Add additional MIME types as desired
   
   if (visitorOS != "Windows") { objTypeTag = mimeType; // audio/mpeg, audio/x-wav, audio/x-ms-wma, etc.

     PlayerContents = "";
      PlayerContents += "<div style='visibility: hidden'>";
      PlayerContents += "<embed src ='" + filepath + "' type='" + objTypeTag + "' name='mediaplayer' id='mediaplayer' autoplay='false' controller='true' showstatusbar='0' enablejavascript='true'>";
      PlayerContents += "</embed></div>";
      }
      else{
           PlayerContents = "";
      PlayerContents += '<div style="visibility: hidden"><object id="MediaPlayer" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112">';
      PlayerContents += "<param name='url' value='" + filepath + "'>";
      PlayerContents += "<param name='autostart' value='true'>";
      PlayerContents += '<param name="uiMode" value="none">';  
      PlayerContents += "</object></div>";


      }
      
 audioPlayer = addControls('audioplayerDiv',galleryDiv);
 audioPlayer.innerHTML = PlayerContents;
 mediaPlayerObj = document.getElementById('mediaplayer');
 StartAudio();



}

function ControlSpeed(anObj,spd){
	
		
	
}

function StopAudio() {


if(visitorOS=="Windows"){
		// check if method is available
		if (mediaPlayerObj.controls.isAvailable('Pause')){
		// pause the movie
			mediaPlayerObj.controls.pause();
		}
		}
		else
		{
// alert(visitorOS);
		mediaPlayerObj.Stop();
		
	}
		
}

function StartAudio() {
if(visitorOS=="Windows"){

		// check if method is available
		if (mediaPlayerObj.controls.isAvailable('Play')){
		// pause the movie
			mediaPlayerObj.controls.play();
		}}
		else
		{

		mediaPlayerObj.Play();
	}
		
}
galleryInitiated=false;


   function initGallery() {
if (galleryInitiated==false){
galleryInitiated=true;
 
 playing = true;
 muted = false;
direction = "forward";
 galleryDiv=document.getElementById('photogallery');
 galleryImages=galleryDiv.getElementsByTagName('img');

if(randomize){
currentImage = Math.floor(Math.random()*parseInt(galleryImages.length)) 
// currentImage = parseInt(galleryImages.length) -1;
}
else{
currentImage = parseInt(galleryImages.length) -1;
}
 previousImage = currentImage -1;
 if(previousImage < 0){previousImage = parseInt(galleryImages.length) -1}
 // alert(previousImage + " " + currentImage);
 addGalleryControls();
 addGalleryEvents();

galleryImages[currentImage].style.visibility="visible";
transparency=0;
doFade();
playingGallery = window.setTimeout(autoPlayGallery,5000);

// autoPlayGallery();
 if(useAudio){
 initializeAudio(audioSource);
 }
 
 }
 
   }

   /* for Mozilla */
   if (document.addEventListener) {
       document.addEventListener("DOMContentLoaded", initGallery, false);
   }

   /* for Internet Explorer */
   /*@cc_on @*/
   /*@if (@_win32)
       document.write("<script defer src=ie_onload.js><"+"/script>");
   /*@end @*/

   /* for other browsers */
   window.onload = initGallery;

function addControls(divId,appendTo){
var newdiv = document.createElement('div');
newdiv.setAttribute('id',divId);
appendTo.appendChild(newdiv);

return document.getElementById(divId);
}

function addGalleryControls(){
if(useControls){
galleryControls = addControls('galleryControlsDiv',galleryDiv);
backButton = addControls('backButtonImg',galleryControls);
pausePlaying = addControls('pausePlayImg',galleryControls);
forwardButton = addControls('forwardButtonImg',galleryControls);
}
 if(useAudio){muteButton = addControls('muteImg',galleryDiv);}
test = addControls('testDiv',galleryDiv);
test.innerHTML = "test";

}

function addEvent(element,event,functionObj){
if (element.addEventListener) {
//	alert('1');
element.addEventListener (event,functionObj,false);
} else if (element.attachEvent) {
//	alert('2');
element.attachEvent ("on"+ event,functionObj);
} else  {
//	alert('3');
var event = "on" + event;
element.event = functionObj;
}
}

function addGalleryEvents(){
if(useControls){
addEvent(galleryDiv,"mouseover",showGalleryControls);
addEvent(galleryDiv,"mouseout",hideGalleryControls);

addEvent(backButton,"mouseover",hiliteBack);
addEvent(backButton,"mouseout",unhiliteBack);
addEvent(backButton,"click",goBack);


addEvent(forwardButton,"mouseover",hiliteForward);
addEvent(forwardButton,"mouseout",unhiliteForward);
addEvent(forwardButton,"click",goForward);

addEvent(pausePlaying,"mouseover",hilitePause);
addEvent(pausePlaying,"mouseout",unhilitePause);
addEvent(pausePlaying,"click",togglePlay);
}
 if(useAudio){
addEvent(muteButton,"mouseover",hiliteMute);
addEvent(muteButton,"mouseout",unhiliteMute);
addEvent(muteButton,"click",toggleMute);
}

}

function togglePlay(){
	
if(playing){
playing=false;
pausePlaying.style.backgroundPosition = "0px 0px";
window.clearTimeout(playingGallery);
}
else{
playing=true;
pausePlaying.style.backgroundPosition = "-24px 0px";
playingGallery = window.setTimeout(autoPlayGallery,5000);
}
}

function toggleMute(){
if(muted){
muted=false;
muteButton.style.backgroundPosition = "0px 0px";
StartAudio();
}
else{
muted=true;
muteButton.style.backgroundPosition = "0px -24px";
StopAudio();
}
}

function showGalleryControls(){
galleryControls.style.visibility ="visible";
}

function hideGalleryControls(){
galleryControls.style.visibility ="hidden";
}

function hiliteBack(){
backButton.style.backgroundPosition = "0px 0px";
}

function unhiliteBack(){

backButton.style.backgroundPosition = "0px 0px";
}

function hiliteForward(){
forwardButton.style.backgroundPosition = "0px 0px";
}

function unhiliteForward(){
forwardButton.style.backgroundPosition = "0px 0px";
}

function hilitePause(){
if(playing){
pausePlaying.style.backgroundPosition = "-24px 0px";
}
else{
pausePlaying.style.backgroundPosition = "0px 0px";
}
}

function unhilitePause(){
if(playing){
pausePlaying.style.backgroundPosition = "-24px 0px";
// autoPlayGallery();
}
else{
pausePlaying.style.backgroundPosition = "0px 0px";
}
}

function hiliteMute(){
if(muted){
muteButton.style.backgroundPosition = "0px 0px";
}
else{
muteButton.style.backgroundPosition = "0px 0px";
}
}

function unhiliteMute(){
if(muted){
muteButton.style.backgroundPosition = "0px 0px";

}
else{
muteButton.style.backgroundPosition = "0px 0px";
}
}

function goBack(){
window.clearTimeout(playingGallery);
direction = "back";
incrementImage();
test.innerHTML="back";
playingGallery = window.setTimeout(autoPlayGallery,5000);
}

function goForward(){
window.clearTimeout(playingGallery);
direction = "forward";
incrementImage();
test.innerHTML="forward";
playingGallery = window.setTimeout(autoPlayGallery,5000);
}

function autoPlayGallery(){
if(playing){
direction = "forward";
incrementImage();

playingGallery = window.setTimeout(autoPlayGallery,5000);
}
else{
window.clearTimeout(playingGallery);
}
}

function incrementImage(){

if (direction=="forward"){
currentImage=currentImage-1;
if(currentImage<0){currentImage=(galleryImages.length-1)}
}
else{
currentImage = currentImage+1;
if(currentImage>=galleryImages.length){currentImage=0}
}

test.innerHTML = currentImage + " " + direction + " " + previousImage + " " + galleryImages.length;
// currentImage = 1;
for (i=0;i<galleryImages.length;i++){
if(i==currentImage && (currentImage <= previousImage)){
galleryImages[i].style.visibility="visible";
transparency=1;

}
else if(i==previousImage && (currentImage <= previousImage)){
galleryImages[i].style.visibility="visible";
transparency=1;
doFade();
}

else if(i==currentImage && (currentImage > previousImage)){
galleryImages[i].style.visibility="visible";
transparency=0;
doFade();

}
else if(i==previousImage && (currentImage > previousImage)){
galleryImages[i].style.visibility="visible";
transparency=1;
}

else {
galleryImages[i].style.visibility="hidden";
}
}

}

function doFade(){
if (transparency > 0 && (currentImage <= previousImage)){

	 fadeControl(previousImage,transparency);
	transparency = transparency - 0.1;
	fadeLoop = setTimeout(doFade,60);
}
else if (transparency < 1 && (currentImage > previousImage)){

	 fadeControl(currentImage,transparency);
	transparency = transparency + 0.1;
	fadeLoop = setTimeout(doFade,60);
}
else{
try{clearTimeout(fadeLoop);}catch(e){}
galleryImages[previousImage].style.visibility="hidden";
galleryImages[currentImage].style.visibility="visible";
fadeControl(previousImage,1);
fadeControl(currentImage,1)
previousImage = currentImage;
test.innerHTML = currentImage + " test " + direction + " " + previousImage;

}
}
function fadeControl(thisObject,transparency){
			if(document.all){
			galleryImages[thisObject].style.filter='progid:DXImageTransform.Microsoft.BasicImage(opacity='+transparency+')'

			}
			else if(navigator.userAgent.indexOf("Firefox")!=-1){
			galleryImages[thisObject].style.MozOpacity=transparency;
			}
			else if(navigator.userAgent.indexOf("KHTML")!=-1){
			galleryImages[thisObject].style.KhtmlOpacity=transparency;
			}
}
