// JavaScript Document
/*****

Image Cross Fade Redux
Version 1.0
Last revision: 02.15.2006
steve@slayeroffice.com

Please leave this notice intact. 

Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html


*****/


window.addEventListener?window.addEventListener("load",so_init,false):window.attachEvent("onload",so_init);

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false;

function so_init() {
	if(!d.getElementById || !d.createElement)return;

	// DON'T FORGET TO GRAB THIS FILE AND PLACE IT ON YOUR SERVER IN THE SAME DIRECTORY AS THE JAVASCRIPT!
	// http://slayeroffice.com/code/imageCrossFade/xfade2.css
	css = d.createElement("link");
	css.setAttribute("href","xfade.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	d.getElementsByTagName("head")[0].appendChild(css);

	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;	
	var startNum;
	startNum = Get_Cookie('imgIndex');
	if (startNum != null){		      
	   startNum = Number(startNum);	   
	   if (startNum + 1 >= imgs.length){
	        startNum = 0;
	   }
	   imgs[startNum + 1].style.display = "block";
	   imgs[startNum + 1].xOpacity = .99;
	   current = startNum + 1;
	   setCookie("imgIndex", current, 30);
	} else {
	    current = getIndex();
	    imgs[current].style.display = "block";
	    imgs[current].xOpacity = .99;  
	}	
	
	setTimeout(so_xfade,4000);
}
function getIndex(){
    var index = Math.floor((imgs.length - 1)*Math.random());
	
	return Number(index);
	
}

function so_xfade() {
    //alert('test');
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;

	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(cOpacity<=0 || nOpacity >= 1) {
		imgs[current].style.display = "none";
		current = nIndex;
		setTimeout(so_xfade,4000);
	} else {
		setTimeout(so_xfade,50);
	}
	setCookie("imgIndex", nIndex, 30);
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
	
}
function Get_Cookie( check_name ) {
	        // first we'll split this cookie up into name/value pairs
	        // note: document.cookie only returns name=value, not the other components
	        var a_all_cookies = document.cookie.split( ';' );
	        var a_temp_cookie = '';
	        var cookie_name = '';
	        var cookie_value = '';
	        var b_cookie_found = false; // set boolean t/f default f
        	
	        for ( i = 0; i < a_all_cookies.length; i++ )
	        {
		        // now we'll split apart each name=value pair
		        a_temp_cookie = a_all_cookies[i].split( '=' );
        		
        		
		        // and trim left/right whitespace while we're at it
		        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
        	
		        // if the extracted name matches passed check_name
		        if ( cookie_name == check_name )
		        {
			        b_cookie_found = true;
			        // we need to handle case where cookie has no value but exists (no = sign, that is):
			        if ( a_temp_cookie.length > 1 )
			        {
				        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			        }
			        // note that in cases where cookie is initialized but no value, null is returned
			        return cookie_value;
			        break;
		        }
		        a_temp_cookie = null;
		        cookie_name = '';
	        }
	        if ( !b_cookie_found )
	        {
		        return null;
	        }
        }
        function setCookie(cookie_name, value, expiredays){            
            var exdate = new Date();            
            exdate.setDate(exdate.getDate() + expiredays);            
            document.cookie = cookie_name + "=" + escape(value) + ((expiredays==null) ? "" : "; expires=" + exdate.toGMTString());        
            }     
            function Get_Cookie( check_name ) {
	        // first we'll split this cookie up into name/value pairs
	        // note: document.cookie only returns name=value, not the other components
	        var a_all_cookies = document.cookie.split( ';' );
	        var a_temp_cookie = '';
	        var cookie_name = '';
	        var cookie_value = '';
	        var b_cookie_found = false; // set boolean t/f default f
        	
	        for ( i = 0; i < a_all_cookies.length; i++ )
	        {
		        // now we'll split apart each name=value pair
		        a_temp_cookie = a_all_cookies[i].split( '=' );
        		
        		
		        // and trim left/right whitespace while we're at it
		        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
        	
		        // if the extracted name matches passed check_name
		        if ( cookie_name == check_name )
		        {
			        b_cookie_found = true;
			        // we need to handle case where cookie has no value but exists (no = sign, that is):
			        if ( a_temp_cookie.length > 1 )
			        {
				        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			        }
			        // note that in cases where cookie is initialized but no value, null is returned
			        return cookie_value;
			        break;
		        }
		        a_temp_cookie = null;
		        cookie_name = '';
	        }
	        if ( !b_cookie_found )
	        {
		        return null;
	        }
        }   