function IEContentLoaded(w, fn) {
// @w	window reference
// @fn	function reference	
	var d = w.document, done = false,
	// only fire once
	init = function () {
		if (!done) {
			done = true;
			fn();
		}
	};
	// polling for no errors
	(function () {
		try {
			// throws errors until after ondocumentready
			d.documentElement.doScroll('left');
		} catch (e) {
			setTimeout(arguments.callee, 50);
			return;
		}
		// no errors, fire
		init();
	})();
	// trying to always fire before onload
	d.onreadystatechange = function() {
		if (d.readyState == 'complete') {
			d.onreadystatechange = null;
			init();
		}
	};
}
function Features() {
	var self = this;
	var arrlen = 0;
	var interval = 6000;
	var featurearr = [];
	var boxActive = 1;
	var to;
	var isScrolling = false;
	var boxs = [];
	var preloaderstring = "<div id=\"preloaderft\" class=\"cnt\"><br/><br/><br/><br/><br/><br/><span style=\"font-size:14px\">Loading...</span><br/><img class=\"imgcnt\" src=\"/vimages/ajaxloader_black.gif\" width=\"32\" height=\"32\" alt=\"\"  /></div>";
	target = $('featuresholder');
	errbox = $('featureserrbox');
	up = $('featureup');
	down = $('featuredown');
		
	var xhr = new XMLHttpRequest();
	xhr = (xhr) ? xhr : new ActiveXObject("Msxml2.XMLHTTP");
	
	this.ftbox = function(obj, i) {
		
		var url = obj.url;
		var photo = obj.photo;
		var title = obj.title;
		var popwin = obj.popwin;
		var entdate = obj.entdate;
		var message = obj.message;
		var boxheight = 65;
		var str = "";
		if (photo != "") {
			photo = "<img src=\""+photo+"\" width=\"125\" />";
		} else {
			photo = "<div style=\"width:125px; height:120px; background:#000\"></div>";
		}
		if (url != "") {
			popwin = (popwin) ? "onclick=\"window.open(this.href); return false;\"" : "";
			str = "<a href=\""+url+"\" "+popwin+">"+photo+"</a>";
		}
		
		function stopPropagation(e) {
			e = e || event;
			e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
			
		}
		//Add in SlideOut
		box=document.createElement('div');
		box.id = "box"+i;
		box.obj = obj;
		box.int = parseFloat(i);
		box.className = "ftbox";
		box.timeout = 1;
		
		//Slideout
		slideout=document.createElement('div');
		slideout.id = "slideout"+i;
		slideout.className = "ftboxslideout";
		slideout.innerHTML = "<b>"+title+"</b><br/>"+message;
		box.slideout = slideout;
		$("banner").appendChild(slideout);

		//IE Bug fix
		if (tJS.browser.ie && tJS.browser.ieVersion() <= 8) {
			$('banner').style.width = '999px';
			$('bannerfeatures').style.width = '124px';
			target.style.width = '125px';
		}
		
		box.slideOut = function(obj, i) {
			tJS.fx.stop();
			var boxself = this;
			obj.style.top = i+"px";
			obj.style.visibility = 'visible';
			new tJS.fx.animate(obj, "left", 650, {start:876, end:555}, function() {});
		}

		box.slideIn = function(obj, i) {
			tJS.fx.stop();
			new tJS.fx.animate(obj, "left", 300, {end:876}, function() {});
		}
			
		box.onmouseover = function(e) {
			self.closedivs();
			var boxself = this;
			var slideout = boxself.slideout;
			var mousey;
			if (tJS.browser.ie && tJS.browser.ieVersion() < 8) {
				//IE 7 and less
				mousey = event.clientY + document.body.scrollTop-2;
			} else if (tJS.browser.ieVersion() == 8) {
				//IE 8
				mousey = event.clientY + document.body.scrollTop;
			} else {
				//Others, IE9
				mousey = e.pageY;
			}
			
			var top = mousey-($('banner').offsetTop);
			var topbox = boxActive;

			if (top >= 0 && top < boxheight) {
				//box1
				top = 0;
				//ACtive Down Arrow
				/*
				if (boxActive > 1) {
					up.style.visibility = "visible";
					if (!isScrolling) {
						up.onclick = function(e) {
							//stopPropagation(e);
							self.scroll("up", boxself);
						}
					}
					up.onmouseout = function() {
						this.style.visibility = "hidden";
					}
				} else {
					up.style.visibility = "hidden";
				}*/
				
			} else if (top >= boxheight && top < (boxheight*2)) {
				//box2
				top = boxheight;
			} else if (top >= (boxheight*2) && top < (boxheight*3)) {
				//box3
				top = (boxheight*2);
			} else if (top >= (boxheight*3) && top < (boxheight*4)) {
				//box4
				top = (boxheight*3);
				//ACtive Down Arrow
				/*
				if (arrlen > 4 && (boxActive+4) < arrlen) {
					down.style.visibility = "visible";
					if (!isScrolling) {
						down.onclick = function(e) {
							//stopPropagation(e);
							self.scroll("down", boxself);
						}
					}
					down.onmouseout = function() {
						this.style.visibility = "hidden";
					}
				} else {
					down.style.visibility = "hidden";
				}*/
				
			}	
			
			if (slideout.offsetLeft == 876) {
				boxself.slideOut(slideout, top);
			}
		}

		box.onmouseout = function() {
			var slideout = this.slideout;
			this.slideIn(slideout, 0);
		}
			
		box.innerHTML = str;
		return box;
	};

	this.scroll = function(direction, box) {
		isScrolling = true;
		
		if (boxActive == 1) {
			up.style.visibility = "hidden";
		}
		if ((boxActive+4) > arrlen) {
			down.style.visibility = "hidden";
		}
		var scrollmax = -(target.offsetHeight-220);
		var step = 65;
		var interval = 1500;
		var dir = direction;
		var tmp = target.offsetTop;
		
		switch (dir) {
			case "up":
				end = (tmp+step);
				boxActive--;
			break;
			case "down":
				end = (tmp-step);
				boxActive++;
			break;
		}
		
		new tJS.fx.animate(target, "top", 500, {start:tmp, end:end, noqueue:true}, function() {
			if (boxActive == 1) {
				up.style.visibility = "hidden";
			}
			if ((boxActive+4) > arrlen) {
				down.style.visibility = "hidden";
			}
			isScrolling = false;
		});
		
		
	};

	this.closedivs = function() {
		up.style.visibility = "hidden";
		down.style.visibility = "hidden";
		for(i=0; i < arrlen; i++) {
			var obj = document.getElementById('slideout'+i);
			obj.style.left = "876px";
		}
	}
	
	this.load = function() 
	{
		//alert("load features");
		target.innerHTML = preloaderstring;
		xhr.open("GET", "/features/json/feat.json", true);
		xhr.setRequestHeader("Content-Type", "application/json");
		xhr.onreadystatechange = function() 
		{
			if (this.readyState == 4 && this.status==200) 
			{
				response = this.responseText;
				response = eval("("+response+")");
				if (typeof response == "object") {
					target.innerHTML = "";
					featurearr = response.Features;
					arrlen = featurearr.length;
					for(i=0; i < arrlen; i++) {
						var featureobj = featurearr[i];
						boxs[i] = self.ftbox(featureobj, i);
						var box = boxs[i];
						if (box) {
							target.appendChild(box);
							new tJS.fx.animate(box, "opacity", 400, {start:0, end:100, easing:'linear'});
						}
					}
					$('bannerfeatures').onmouseover = function() {
						clearTimeout(to);
					}
					$('bannerfeatures').onmouseout = function() {
						clearTimeout(to);
						//self.activate();
					}
					//Done Loading Everything
					//self.activate();
				}

			}
		}
		xhr.send();
	};
}



function Photos() {
	var self = this;
	var arrlen = 0;
	var interval = 6000;
	var firstimgflag = true;
	var photoarr = [];
	var photofiles = [];
	var preloaderstring = "<div id=\"preloaderbanner\" class=\"cnt\"><br/><br/><br/><br/><br/><br/><span style=\"font-size:14px\">Loading...</span><br/><img class=\"imgcnt\" src=\"/vimages/ajax-loader.gif\" width=\"32\" height=\"32\" alt=\"\"  /></div>";
	var target = $('bannerphotos');
	var errbox = $('bannererrbox');
	var xhr = new XMLHttpRequest();
	xhr = (xhr) ? xhr : new ActiveXObject("Msxml2.XMLHTTP");
	var loadcheckInterval = null;
	
	this.load = function() 
	{
		target.innerHTML = preloaderstring;
		xhr.open("GET", "/features/json/bannerphotos.json?"+Math.random(), true);
		xhr.setRequestHeader("Content-Type", "application/json");
		xhr.onreadystatechange = function() 
		{
			if (xhr.readyState == 4 && xhr.status==200) 
			{
				response = xhr.responseText;
				text = eval("("+response+")");
				if (typeof text == "object") {
					photoarr = text.Photos;
					arrlen = photoarr.length;
					for(i=0; i < arrlen; i++) {
				       var img = photoarr[i];
					   var imgurl = photoarr[i].photourl;
				       img = new Image();
				       img.src = imgurl;
					   photofiles[i] = img;
					   photofiles[i].title = photoarr[i].title;
					   photofiles[i].desc = photoarr[i].description;
					   photofiles[i].url = photoarr[i].url;
					   photofiles[i].popwin = photoarr[i].popwin;
					   photofiles[i].style.position = "absolute";
					   //photofiles[i].style.opacity = 0;
					   //photofiles[i].style.filter = 'alpha(opacity=0)';
					   photofiles[i].style.display = 'none';
					   target.innerHTML = "";
				}
					self.play(Math.floor(Math.random()*arrlen));
				} else {
					target.innerHTML = "Error Parsing JSON";
				}
			} else {
				target.innerHTML = preloaderstring;
			}
		};
		xhr.send(null);
	}; //load
	
	this.next = function(i) {
		var int = i;
		setTimeout(function() {
			self.play(int+1);
		}, interval);
	}
	
	this.play = function(i) {

		if (document.getElementById('descdiv')) {
			target.removeChild(document.getElementById('descdiv'));
		}
		
		clearTimeout(loadcheckInterval);
		if (arrlen > 0) {
			if (i > arrlen-1) {
				i = 0;
			}
			var img = photofiles[i];
			var title = img.title;
			var url = img.url;
			var popwin = img.popwin;
			var titlestr = "";
			if (popwin) {
				popwin = "onclick=\"window.open(this.href); return false;\"";
			} else {
				popwin = "";
			}
			
			if (img.complete) 
			{
				if (firstimgflag) {
					//First Load
					img.style.display = 'block';
					target.appendChild(img);
					self.next(i);
				} else {
					//Recurring Loads
					
					//IE Bug fix
					//if (tJS.browser.ie && tJS.browser.ieVersion() <= 8) {
						//img.style.height = '220px';
					//}
					img.style.position = 'absolute';
					img.style.zIndex = 0;
					img.style.display = 'block';
					target.appendChild(img);
					
					new tJS.fx.animate(img, "opacity", 900, {start:0, end:100, noqueue:true, easing:'easeOutQuad'}, 		function() { self.next(i); });
				}
				firstimgflag = false;
				
				//Attach Details
				if (title != "") {
					descdiv=document.createElement('div');
					descdiv.setAttribute('id', 'descdiv');
					if (url != "") {
						titlestr = ">&nbsp;&nbsp;<a href=\""+url+"\" "+popwin+">"+title+"</a>";
					} else {
						titlestr = title;
					}
					descdiv.innerHTML = titlestr;
					target.appendChild(descdiv);
				}
				
			} else {
				//quickly check for next ready image
				loadcheckInterval = setTimeout(function() { 
					self.play(i);
				}, 125);
			}

			
			
		} else {
			errbox = "No Photos Currently Available";
		}
	}
}

function loadJSON() {
	var pshow = new Photos();
	pshow.load();
	var ft = new Features();
	ft.load();
}

//Onload
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", loadJSON, false);
} else {
	//For IE DOMContentLoaded Emulation
	IEContentLoaded(window, loadJSON);
}
