/**********************************************************
Author: Ritchie A.
Date: 02/03/07
Description:
- Photo Animation
- Open new browser windows (externalLinks)
- Back Button (externalLinks)
- sfHover for IE
- HideSelects for IE 5 & 6

CSS Photo Shuffler v1.0 by
Carl Camera
http://iamacamera.org 
SetOpacity Function and inpiration from Photo Fade by
Richard Rutter
http://clagnut.com
License: Creative Commons Attribution 2.5  License
http://creativecommons.org/licenses/by/2.5/
**********************************************************/	

/*Animation Script
---------------------------------------------------------*/
var gblPhotoShufflerDivId = "animate";
var gblPhotoShufflerImgId = "photoimg"; 
//Add your images here
var gblImg = new Array( 
    "img/home01.jpg",
    "img/home02.jpg",
    "img/home03.jpg",
    "img/home04.jpg",
    "img/home05.jpg"	
    );
var gblPauseSeconds = 4;
var gblFadeSeconds = 1;
var gblRotations = 100;

// End Customization section
var gblDeckSize = gblImg.length;
var gblOpacity = 100;
var gblOnDeck = 0;
var gblStartImg;
var gblImageRotations = gblDeckSize * (gblRotations+1);
	
function preloadImage() {//preload images
  	for(j=0; j < gblImg.length; j++) {
		gblImg[j]=new Image();
		gblImg[j].src=gblImg[j];
	}
}
addLoadEvent(photoShufflerLaunch);//begin loading images

function photoShufflerLaunch() {
	if (!document.getElementById(gblPhotoShufflerImgId)) return;
	var theimg = document.getElementById(gblPhotoShufflerImgId);
	gblStartImg = theimg.src; // save away to show as final image
	document.getElementById(gblPhotoShufflerDivId).style.backgroundImage='url(' + gblImg[gblOnDeck] + ')';
	setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
}

function photoShufflerFade(){
	var theimg = document.getElementById(gblPhotoShufflerImgId);
  	// determine delta based on number of fade seconds
	// the slower the fade the more increments needed
	var fadeDelta = 100 / (30 * gblFadeSeconds);

	// fade top out to reveal bottom image
	if (gblOpacity < 2*fadeDelta ) {
		gblOpacity = 100;
		// stop the rotation if we're done
		if (gblImageRotations < 1) return;
		photoShufflerShuffle();
		// pause before next fade
		setTimeout("photoShufflerFade()",gblPauseSeconds*1000);
	}
	else {
		gblOpacity -= fadeDelta;
		setOpacity(theimg,gblOpacity);
		setTimeout("photoShufflerFade()",30);  // 1/30th of a second
	}
}

function photoShufflerShuffle() {
	var thediv = document.getElementById(gblPhotoShufflerDivId);
	var theimg = document.getElementById(gblPhotoShufflerImgId);
	
	// copy div background-image to img.src
	theimg.src = gblImg[gblOnDeck];
	// set img opacity to 100
	setOpacity(theimg,100);

    // shuffle the deck
	gblOnDeck = ++gblOnDeck % gblDeckSize;
	
	// decrement rotation counter
	if (--gblImageRotations < 1) {
		// insert start/final image if we're done
		gblImg[gblOnDeck] = gblStartImg;
	}

	// slide next image underneath
	thediv.style.backgroundImage='url(' + gblImg[gblOnDeck] + ')';
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
    
    // IE/Win
    obj.style.filter = "alpha(opacity:"+opacity+")";
    
    // Safari<1.2, Konqueror
    obj.style.KHTMLOpacity = opacity/100;

    // Older Mozilla and Firefox
    obj.style.MozOpacity = opacity/100;

    // Safari 1.2, newer Firefox and Mozilla, CSS3
    obj.style.opacity = opacity/100;
}

/*Add Events
---------------------------------------------------------*/
// addLoadEvent, for queuing window.onload init functions
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		};
	};
};

/*Open New Window
---------------------------------------------------------*/
function externalLinks() {
	if (!document.getElementsByTagName) return;
 		var anchors = document.getElementsByTagName("a");
 		for (var i=0; i<anchors.length; i++) {
  	 	var anchor = anchors[i];
  	 if (anchor.getAttribute("href") &&
       	anchor.getAttribute("rel") == "external") //Opens New Window based on rel name
     	anchor.target = "_blank";
 		}
	}
addLoadEvent(externalLinks);


/*Back Button
---------------------------------------------------------*/
function backLink(){
	var p=document.getElementById('back');//change this to whatever div id name you like
		if(p && document.all ){
			var newa=document.createElement('a');
			newa.setAttribute('href','#');
			newa.appendChild(document.createTextNode('back to previous page'));
			newa.onclick=function(){history.back();return false} //for ie
		    p.replaceChild(newa,p.firstChild);
		  	}
		else if (p && document.getElementById ){
			var newa=document.createElement('a');
			newa.setAttribute('href','#');
			newa.appendChild(document.createTextNode('back to previous page'));
			newa.onclick=function(){window.back();return false} //for gecko based browsers
		    p.replaceChild(newa,p.firstChild);	
		}
}
addLoadEvent(backLink);

 






