

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false;
var suset;
var sunrise;
var hour;
var count = 0;
var nIndexList = [];
var rand_no;

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

	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	
	var currentTime = new Date();
	hour = currentTime.getHours();
	var month = currentTime.getMonth()+1;
	
	switch (month) {
		case 1:
			sunrise = 8;
			sunset = 16;
		break;
		
		case 2:
			sunrise = 7;
			sunset = 17;
		break;
		case 3:
			sunrise = 6;
			sunset = 18;
		break;
		case 4:
			sunrise = 6;
			sunset = 20;
		break;
		case 5:
			sunrise = 5;
			sunset = 21;
		break;
		case 6:
			sunrise = 5;
			sunset = 21;
		break;
		case 7:
			sunrise = 5;
			sunset = 21;
		break;
		case 8:
			sunrise = 6;
			sunset = 20;
		break;
		case 9:
			sunrise = 6;
			sunset = 19;
		break;
		case 10:
			sunrise = 7;
			sunset = 18;
		break;
		case 11:
			sunrise = 7;
			sunset = 16;
		break;
		case 12:
			sunrise = 8;
			sunset = 16;
		break;
	}
	
	
	for(i=1;i<imgs.length;i++) { imgs[i].xOpacity = 0; }
	
	if(hour >= sunrise && hour < sunset){
		//alert('a');
		imgs[0].style.display = "block";
		imgs[0].xOpacity = .99;
		current = 0;
	} else if(hour >= sunset || hour <= sunset){
		//alert('b');
		imgs[1].style.display = "block";
		imgs[1].xOpacity = .99;
		current = 1;
	}
	
	rand_no = Math.floor((13)*Math.random()) + 2;
	nIndexList[rand_no] = true;
	count++;
	//alert(rand_no);
	setTimeout(so_xfade,5000);
}

function so_xfade() {
	
	
	//alert(imgs.find('2'));
	
	cOpacity = imgs[current].xOpacity;
	//nIndex = imgs[current+1] ? current+1 : 0;
	/*if(nIndexList[rand_no]){
		so_xfade();
	} else {*/
		nIndex = rand_no;
		/*nIndexList[rand_no] = true;
		if(count >= 12){
			nIndexList = null;
			nIndexList = [];
			count = 0;
		}
		count++;
		
	}*/
	
	
	

	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) {
		imgs[current].style.display = "none";
		current = nIndex;
		generateRandom();
		setTimeout(so_xfade,5000);
	} else {
		setTimeout(so_xfade,50);
	}
	
	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 generateRandom(){
	var x = Math.floor((13)*Math.random()) + 2;
	//alert(x);
	if(nIndexList[x]){
		//alert('again');
		setTimeout(generateRandom,1);
		return;
	}
	//alert('true');
	nIndexList[x] = true;
	//alert(count);
	if(count < 11){
			//alert('false');
			count++;
			rand_no = x;
			return;
		}

    //alert('true');
	count = 0;
	nIndexList = null;
	nIndexList = [];
	nIndexList[x] = true;
	count++;
	rand_no = x;
	return;
	}

function addLoadEvent(func)
{ var oldonload = window.onload;
if ( typeof window.onload != 'function' )
{
window.onload = func;
}
else
 {
 window.onload = function()
 {
 oldunload();
 func();
 }
 }
}
addLoadEvent(so_init);