/**********************************************************
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/anim01.jpg",
    "img/anim02.html",
    "img/anim03.html",
    "img/anim04.html",
    "img/anim05.html"	
    );
var gblPauseSeconds = 7.25;
var gblFadeSeconds = .85;
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);


/*SFHover
---------------------------------------------------------*/
sfHover = function() {	
	if ( (document.getElementById("menuList")) && ((navigator.userAgent.toLowerCase().indexOf('5.0') != -1) || (navigator.userAgent.toLowerCase().indexOf('6.0') != -1)) ) { /* browser detection for IE5/IE6*/
	var sfEls = document.getElementById("menuList").getElementsByTagName("LI")
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}	
}
addLoadEvent(sfHover);

/*Hide/Show Select
---------------------------------------------------------*/
hideShow={
	triggerClass:'hide', /*'hide' is the class name you place on your menu links. This is the hook that will hide the select field. This class can be changed to your liking.*/
	init:function(){
		/*Check to see if DOM = IE*/
	if ( (document.getElementById("menuList")) && ((navigator.userAgent.toLowerCase().indexOf('5.0') != -1) || (navigator.userAgent.toLowerCase().indexOf('6.0') != -1)) ) { /* browser detection for IE5/IE6*/
		/*Find Nav ID*/
		var menu = document.getElementById("menuList")
		/*Find a href*/
		var allLinks = menu.getElementsByTagName('a');
		for(var i=0;i<allLinks.length;i++){
		/*if trigglerclass=hide then active the functions below*/
		if(!cssjs('check',allLinks[i],hideShow.triggerClass)){continue;} 
		allLinks[i].onmouseover=function() { 
			hideSelects();
			}
		allLinks[i].onmouseout=function() { 
			showSelects();
		}
		}
	}
	}
	}
addLoadEvent(hideShow.init);	

//DOM checker	
init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
	}
//CSS Checker
function cssjs(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!DOMhelp.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				var found=false;
				var temparray=o.className.split(' ');
				for(var i=0;i<temparray.length;i++){
					if(temparray[i]==c1){found=true;}
				}
				return found;
			break;
		}
	}
//Hide Select Field
 function hideSelects() {
	if ((document.all)&&(navigator.userAgent.toLowerCase().indexOf('5.0') != -1)||(navigator.userAgent.toLowerCase().indexOf('6.0') != -1)) { /* browser detection for IE5/IE6*/
	var selects = document.getElementsByTagName("SELECT");
	for (var i=0; i<selects.length; i++) {
	var oneSelect = selects[i];
	oneSelect.style.visibility = 'hidden';
  	}
  }
 }
//Show select field
  function showSelects() {
  if ((document.all)&&(navigator.userAgent.toLowerCase().indexOf('5.0') != -1)||(navigator.userAgent.toLowerCase().indexOf('6.0') != -1)) { /* browser detection for IE5/IE6*/
   var selects = document.getElementsByTagName("SELECT");
   for (var i=0; i<selects.length; i++) {
    var oneSelect = selects[i];
    oneSelect.style.visibility = 'visible';
   }
  }
  }
 
 





