GW = {
	menuStackIEFix: function(e) {
		e = e || event;
		var t = e.target || e.srcElement, i = 0;
		t.style.zIndex = '10';
		while(t.parentNode) {
			t = t.parentNode;
			try {
				t.style.zIndex = '10';
			} catch(err) {}
		}
		document.getElementById("searchform").style.zIndex = '11';
		t = document.getElementById("searchform").getElementsByTagName('*');
		for(i = 0; i < t.length; i++) {
			t[i].style.zIndex = '11';
		}
	},
	menuStackIE: function() {
		GW.addEvent(document.getElementById("menu-primary-navigation"), 'mouseover', GW.menuStackIEFix);
	},
	fadeIn: function(el, thendo) {
		var o=0, 
			doFade = function() {
				if(o < 10) {
					el.style.opacity = o/10;
					el.style.filter = 'alpha(opacity=' +(o*10)+ ')';
					o++;
					setTimeout(doFade, 10);
				} else {
					el.style.opacity = 1;
					el.style.filter = 'alpha(opacity=100)';
					el.isfading = false;
					if(thendo) {
						if(typeof thendo === 'string') {
							el.className = thendo;
						} else {
							thendo.call();
						}
					}
				}
			};
		if(el.isfading) {
			if(el.isfading !== 'in') {
				setTimeout(function(){GW.fadeIn(el)}, 100);
			}
		} else {
			el.isfading = 'in';
			el.style.opacity = 0;
			el.style.filter = 'alpha(opacity=0)';
			doFade();
		}
	},
	fadeOut: function(el, thendo) {
		var o=10, 
			doFade = function() {
				if(o > 0) {
					el.style.opacity = o/10;
					el.style.filter = 'alpha(opacity=' +(o*10)+ ')';
					o--;
					setTimeout(doFade, 10);
				} else {
					el.style.opacity = 0;
					el.style.filter = 'alpha(opacity=0)';
					el.isfading = false;
					if(thendo) {
						if(typeof thendo === 'string') {
							el.className = thendo;
						} else {
							thendo.call();
						}
					}
				}
			};
		if(el.isfading) {
			if(el.isfading !== 'out') {
				setTimeout(function(){GW.fadeOut(el)}, 100);
			}
		} else {
			el.isfading = 'out';
			el.style.opacity = 1;
			el.style.filter = 'alpha(opacity=100)';
			doFade();
		}
	},
	showFeaturedImage: function(el) {
		if(document.getElementById(el)) {
			document.getElementById(el).className = 'visible-image';
			GW.fadeIn(document.getElementById(el));
		}
	},
	hideFeaturedImage: function(el) {
		if(document.getElementById(el)) {
			GW.fadeOut(document.getElementById(el), 'hidden-image');
		}
	},
	addEvent: function(el, evnt, fnct) {
		if(el) {
			if(window.addEventListener) {
				el.addEventListener(evnt, fnct, null);
			} else if(window.attachEvent) {
				el.attachEvent('on'+evnt, fnct);
			}
		}
	},
	findFeatureImage: function(t) {
		var c = 0, l = 0;
		if(t.nodeName.toLowerCase() !== 'li') {
			while(t.parentNode && t.parentNode.nodeName.toLowerCase() != 'ul') {
				t = t.parentNode;
			}
			if(t.parentNode && t.className.indexOf('has-feat-image') !== -1) {
				t = t.className.split(' ');
				l = t.length;
				for(c=0; c<l; c++) {
					if(t[c].indexOf('page-id-') !== -1) {
						return t[c].replace('page-id-', 'page-image-');
					}
				}
			}
		}
		return false;
	},
	testFeatureImageOn: function(e) {
		e = e || event;
		var t = e.target || e.srcElement, feat = GW.findFeatureImage(t);
		if(feat) {
			GW.showFeaturedImage(feat);
		}
	},
	testFeatureImageOff: function(e) {
		e = e || event;
		var t = e.target || e.srcElement, feat = GW.findFeatureImage(t);
		if(feat) {
			GW.hideFeaturedImage(feat);
		}
	},
	doCrossFade: function() {
		var imgs = document.getElementById('img-rotate').getElementsByTagName('img'),
			l = imgs.length, i = 0, cur = 0, nex = 0;
		for(i=0; i < l; i++) {
			if(imgs[i].className.indexOf('current') !== -1) {
				imgs[i].style.zIndex = 1;
				cur = i;
			}
		}
		if(cur+1 > l-1) {
			nex = 0;
		} else {
			nex = cur+1;
		}
		imgs[nex].style.zIndex = 2;
		imgs[nex].style.opacity = 0;
		imgs[nex].style.filter = "alpha(opacity=0)";
		imgs[nex].setAttribute('class', 'current');
		imgs[nex].className = 'current';
		GW.fadeIn(imgs[nex], function(){imgs[cur].style.zIndex=1; imgs[cur].style.opacity=0; imgs[cur].style.filter='alpha(opacity=0)'; imgs[cur].setAttribute('class', ''); imgs[cur].className = '';});
	},
	initCrossFade: function() {
		var imgs = document.getElementById('img-rotate').getElementsByTagName('img'),
			l = imgs.length, i = 0;
		document.getElementById('img-rotate').className = 'hasjs';
		for(i=0; i < l; i++) {
			if(i == 0) {
				imgs[i].className = 'current';
			}
		}
		if(l > 1) {
			setInterval(GW.doCrossFade, 10000);
		}
	},
	init: function() {
		GW.initCrossFade();
	}
};

GW.addEvent(window, 'load', GW.init);
