// 1.0.5b
// Changelog:
// - 1.0.5: Fixed alt and target in imgXML; adgroupsession commented out.
// - 1.0.4: Bug fixes galore.
// - 1.0.3: Improved (more portable) news XML reading; added AHAH support to newsXML; added eventscontent to newsXML; fixed missing adswap.
// - 1.0.2: Clickable nav-top.
// - 1.0.1: Replace -more- with >>> Learn More in news feed. Added scrollbar (except Gecko). Added login box.
// - 1.0.0: Init.

var siteLib = {
	clickedlink: false,
	subclickedlink: false,
	accord: false,
	accordPrev: false,
	newsElems: new Array(),
	newsList: 0,
	newsPause: 0,
	newsPos: 0,
	newsHeight: 0,
	stretchHeight: 0,

	pageLoaded: function () {
		if ($('nav-top')) {
			sgLib.navTops = new Array();
			var navFunc = function (el) {
				var childList = el.getFirst().getNext();
				if (!childList || childList.getTag() != 'ul')
					return;
				var speeds = 500;
				if (this && !isNaN(this))
					speeds = this;
				sgLib.navID++;
				var curNavID = sgLib.navID;
				if (!childList.id) childList.id = 'subnav-'+(curNavID);
				new Element('span', { 'class': 'close' }).setText('Close').injectInside(new Element('li', { 'class': 'close', 'events': { 'click': function() {
					sgLib.navSlider[this.getParent().id][0].stop();
					sgLib.navSlider[this.getParent().id][0].start(0);
				} } }).injectInside(childList));
				sgLib.navTops.push(childList.id);
				sgLib.navSlider[childList.id] = [new Fx.Style(childList, 'height', {duration: speeds, wait: false}), childList.offsetHeight];
				sgLib.navSlider[childList.id][0].set(0);
				el.getFirst().onclick = function() {
					var thissubid = this.getNext();
					if (!thissubid)
						return true;
					thissubid = thissubid.id;
					if (isNaN(sgLib.navSlider[thissubid][0].to) || sgLib.navSlider[thissubid][0].to <= 0) {
						sgLib.navTops.each(function(navtops) {
							if (navtops != thissubid) {
								sgLib.navSlider[navtops][0].stop();
								sgLib.navSlider[navtops][0].start(0);
							}
						});
						sgLib.navSlider[thissubid][0].stop();
						sgLib.navSlider[thissubid][0].start(sgLib.navSlider[thissubid][1]);
						return false;
					} else {
						return true;
					}
				}; //.bindWithEvent(childList));
				childList.style.visibility = 'visible';
			};
			$('nav-top').getElements('li[id^=topnav]').each(navFunc.bind(250));
		}

		var newsScroller = $('news-ticker');
		if (newsScroller) {
			siteLib.stretchHeight = newsScroller.offsetHeight;
			var tmpFunc = (function() {
				var contentHeight = $('content').offsetHeight - 28;
				if (siteLib.stretchHeight < contentHeight-5 || siteLib.stretchHeight > contentHeight+5) {
					if (siteLib.stretchHeight == 292 && contentHeight <= 292)
						return;
					else if (contentHeight < 292)
						contentHeight = 292;
					var tickUL = $('news-ticker');
					newsScroller.setStyle('height', (contentHeight-4)+'px');
					siteLib.stretchHeight = contentHeight;

					var j = 0, k = 0;
					var totalHeight = 0;
					siteLib.newsElems = new Array();
					siteLib.newsElems[0] = new Array();
					tickUL.getChildren().each(function(el){
						if (k > 0 && totalHeight + el.offsetHeight > siteLib.stretchHeight) {
							j++;
							k = 0;
							totalHeight = 0;
							siteLib.newsElems[j] = new Array();
						}
						siteLib.newsElems[j][k] = new Fx.Style(el, 'opacity', {wait:false, duration:400});
						if (j == 0)
							siteLib.newsElems[j][k].set(1);
						else
							siteLib.newsElems[j][k].set(0);
						el.style.top=totalHeight+'px';
						el.style.position='absolute';
						totalHeight += el.offsetHeight;
						k++;
					});
				}
			});
			tmpFunc();
			tmpFunc.periodical(1000);
			newsScroller.addEvent('mouseenter', function() {siteLib.newsPause=true;});
			newsScroller.addEvent('mouseleave', function() {siteLib.newsPause=(new Date()).getTime()+5000;});
			(function() {
				if (siteLib.newsPause == true || siteLib.newsElems.length <= 1 || (new Date()).getTime() <= siteLib.newsPause)
					return;
				for (var j = 0; siteLib.newsElems[siteLib.newsList][j]; j++) {
					siteLib.newsElems[siteLib.newsList][j].start(0);
				}
				siteLib.newsList = (siteLib.newsList+1 == siteLib.newsElems.length) ? 0 : siteLib.newsList+1;
				for (var j = 0; siteLib.newsElems[siteLib.newsList][j]; j++) {
					siteLib.newsElems[siteLib.newsList][j].start(1);
				}
			}).periodical(8000);
		}

		$('wrap').getElements('[class^=shadow]').each(function(el){
			var newWrapper = new Element('div', {'styles': {'position':'relative'}});
			var newClone = el.clone(true);
			newClone.className = 'shadowdrop';
			newClone.id = '';
			el.parentNode.insertBefore(newWrapper, el);
			newWrapper.adopt(el);
			newWrapper.adopt(newClone);
		});

		if ($('content') && $('content').hasClass('accordion')) {
			var starterDisplay = false;
			var urlFragment = document.location.hash ? (document.location.hash.charAt(0) == '#' ? document.location.hash.substring(1) : document.location.hash) : '';
			var headerLinkArray = new Array(), paragraphWrapArray = new Array();
			$('content').getElements('div[class^=post]').each(function(el){
				var accordNode, accordNext = el.getFirst();
				var accordLink = new Element('span', {'class':'more'}), accordWrap = new Element('div', {'class':'accordionWrapper'});
				var headerLink = false, paragraphWrap = false;
				accordLink.appendText('[show]');
				while (accordNode = accordNext) {
					accordNext = accordNode.getNext();
					if (accordNode.hasClass('title')) {
						if (headerLink && paragraphWrap) {
							headerLink.title = 'Toggle link';
							(accordLink.clone(true)).injectTop(headerLink);
							headerLinkArray.push(headerLink);
							if (urlFragment && urlFragment == headerLink.id)
								starterDisplay = headerLinkArray.length-1;
							paragraphWrapArray.push(paragraphWrap);
							paragraphWrap = false;
						}
						headerLink = accordNode.getFirst();
					} else if (!(/(date|metadata)/i).test(accordNode.className) || accordNode.getTag().charAt(0) != 'h') {
						if (headerLink) {
							if (!paragraphWrap) {
								paragraphWrap = accordWrap.clone(false);
								paragraphWrap.injectBefore(accordNode);
								paragraphWrap = $(paragraphWrap);
							}
							paragraphWrap.appendChild(accordNode);
						}
					}
				}
				if (headerLink && paragraphWrap) {
					headerLink.title = 'Toggle link';
					(accordLink.clone(true)).injectTop(headerLink);
					headerLinkArray.push(headerLink);
					if (urlFragment && urlFragment == headerLink.id)
						starterDisplay = headerLinkArray.length-1;
					paragraphWrapArray.push(paragraphWrap);
					paragraphWrap = false;
				}
			});
			siteLib.accord = new Fx.Accordion(headerLinkArray, paragraphWrapArray, {duration:1000,transition:Fx.Transitions.Quart.easeInOut, display:starterDisplay, opacity:false, alwaysHide:true, onStart:function(){
				if (siteLib.accordPrev != false) {
					if (this.to[siteLib.accordPrev].height > 0 && this.togglers[siteLib.accordPrev].getFirst().getText() == '[show]')
						this.togglers[siteLib.accordPrev].getFirst().setText('[hide]');
					else if (this.to[siteLib.accordPrev].height == 0)
						this.togglers[siteLib.accordPrev].getFirst().setText('[show]');
				}
				siteLib.accordPrev = this.previous;
				if (this.to[this.previous].height > 0 && this.togglers[this.previous].getFirst().getText() == '[show]')
					this.togglers[this.previous].getFirst().setText('[hide]');
				else if (this.to[this.previous].height == 0)
					this.togglers[this.previous].getFirst().setText('[show]');
			}});
			for (var m=0; headerLinkArray[m]; m++) { headerLinkArray[m].href='javascript:void(0);'; headerLinkArray[m].onclick = function(){return false;}; }
		}

		formValidator.load();
		siteLib.navLeft();
	},

	navLeft: function() {
		if ($('nav-left')) {
			$('nav-left').className = 'js';
			var children = $('nav-left').childNodes;
			var openers = [], expanders = [], startopen = -1, startslide = -1;
			var i = 0, j = 0, k = 0, childUL, childHREF;
			var subopeners = [], subopenercount = 0;
			for (i=0; children[i]; i++) {
				if (children[i].nodeName == 'LI') {
					childUL = false;
					childHREF = false;
					for (k=0; children[i].childNodes[k]; k++) {
						if (children[i].childNodes[k].nodeName == 'A')
							childHREF = children[i].childNodes[k];
						else if (children[i].childNodes[k].nodeName == 'DIV' || children[i].childNodes[k].nodeName == 'UL')
							childUL = children[i].childNodes[k];
					}
					if (!childHREF || !childUL)
						continue;
					j++;

					if (childHREF.className.match(/down/) || children[i].className.match(/down/)) {
						if (childUL.innerHTML.match(/class=.{0,1}[a-zA-Z0-9_\- ]*active[a-zA-Z0-9_\- ]*.{0,1}/))
							startopen = j-1;
						else
							startslide = j-1;
						siteLib.clickedlink = childUL;
					}
					if (!children[i].className.match(/link/)) {
						childHREF.onclick = function(){siteLib.clickedlink = this.getNext();
							if (this.href != '#' && this.href != 'javascript:void(0);' || this.getNext().style.visibility == 'visible') { this.removeEvents('click'); return true; } else return false;
						};
						//childHREF.href = "javascript:void(0);";
					}
					openers.push(childHREF);
					expanders.push(childUL);

					var schildren = childUL.childNodes;
					var sopeners = [], sexpanders = [], sstartopen = -1, sstartslide = -1;
					var si = 0, sj = 0, sk = 0, schildUL, schildHREF;
					for (si=0; schildren[si]; si++) {
						if (schildren[si].nodeName == 'LI') {
							schildUL = false;
							schildHREF = false;
							for (sk=0; schildren[si].childNodes[sk]; sk++) {
								if (schildren[si].childNodes[sk].nodeName == 'A')
									schildHREF = schildren[si].childNodes[sk];
								else if (schildren[si].childNodes[sk].nodeName == 'DIV' || schildren[si].childNodes[sk].nodeName == 'UL')
									schildUL = schildren[si].childNodes[sk];
							}
							if (!schildHREF || !schildUL)
								continue;
							sj++;

							if (schildren[si].className.match(/down/)) {
								if (schildUL.innerHTML.match(/class=.{0,1}[a-zA-Z0-9_\- ]*active[a-zA-Z0-9_\- ]*.{0,1}/))
									sstartopen = sj-1;
								else
									sstartslide = sj-1;
								siteLib.subclickedlink = schildUL;
							} else {
								if (schildUL.innerHTML.match(/class=.{0,1}[a-zA-Z0-9_\- ]*active[a-zA-Z0-9_\- ]*.{0,1}/i)) {
									sstartopen = sj-1;
									siteLib.subclickedlink = schildUL;
								} else if (schildHREF.className.match(/down/)) {
									sstartslide = sj-1;
									siteLib.subclickedlink = schildUL;
								}
							}
							if (!schildren[si].className.match(/link/)) {
								schildHREF.onclick = function(){siteLib.subclickedlink = this.getNext();
									if (this.href != '#' && this.href != 'javascript:void(0);' || this.getNext().style.visibility == 'visible') { this.removeEvents('click'); return true; } else return false;
								};
								//childHREF.href = "javascript:void(0);";
							}
							sopeners.push(schildHREF);
							sexpanders.push(schildUL);
						}
					}
					subopeners[subopenercount] = [];
					subopeners[subopenercount][0] = sopeners;
					subopeners[subopenercount][1] = sexpanders;
					subopeners[subopenercount][2] = sstartopen;
					subopeners[subopenercount][3] = sstartslide;
					subopenercount++;
				}
			}
			for (var x = 0; subopeners[x]; x++) {
				if (subopeners[x][0].length) {
					if (subopeners[x][3] >= 0)
						new Fx.Accordion(subopeners[x][0], subopeners[x][1], {display:subopeners[x][3], onComplete: function(){if (siteLib.subclickedlink.style.visibility == 'visible') siteLib.subclickedlink.style.height = 'auto'; $('foot').setStyle('top', '1px'); $('foot').setStyle('top', '0');}});
					else
						new Fx.Accordion(subopeners[x][0], subopeners[x][1], {show:subopeners[x][2], onComplete: function(){if (siteLib.subclickedlink.style.visibility == 'visible') siteLib.subclickedlink.style.height = 'auto'; $('foot').setStyle('top', '1px'); $('foot').setStyle('top', '0');}});
				}
			}
			if (openers.length) {
				if (startslide >= 0)
					new Fx.Accordion(openers, expanders, {display:startslide, alwaysHide:true, onComplete: function(){if (siteLib.clickedlink.style.visibility == 'visible') { siteLib.clickedlink.style.height = 'auto'; } $('foot').setStyle('top', '1px'); $('foot').setStyle('top', '0');}});
				else
					new Fx.Accordion(openers, expanders, {show:startopen, alwaysHide:true, onComplete: function(){if (siteLib.clickedlink.style.visibility == 'visible') { siteLib.clickedlink.style.height = 'auto'; } $('foot').setStyle('top', '1px'); $('foot').setStyle('top', '0');}});
			}
		}
	}
};

var regForm = {
	form: false,
	regtype: false,
	loader: false,
	pricingArray: new Object(),
	hiderList: new Object(),
	load: function() {
		regForm.form = $('regform');
		if (regForm.form) {
				regForm.form.getElements('input').each(function(el){
					if (regForm.form['ccnumber'] && regForm.form['ccnumber'] == el) {
						regForm.form['ccnumber'].onchange = function() {
							if (!this.value.length) return;
							var cardtype = '';
							switch (this.value.charAt(0)) {
								case '3':
									if (this.value.charAt(1) == '4' || this.value.charAt(1) == '7')
										cardtype = 'Amex';
									else if (this.value.charAt(1) == '0' || this.value.charAt(1) == '6' || this.value.charAt(1) == '8')
										cardtype = 'Diners';
									else if (this.value.charAt(1) == '5')
										cardtype = 'JCB';
									break;
								case '4':
									cardtype = 'Visa';
									break;
								case '5': // 51-55 = MC, but 55 is also Diners (coop with MC)
									if (parseInt(this.value.charAt(1)) > 0 && parseInt(this.value.charAt(1)) < 6)
										cardtype = 'Mastercard';
									break;
								case '6':
									if (this.value.substring(0,4) == '6011' || this.value.charAt(1) == '5')
										cardtype = 'Discover';
									break;
							}
							$('ccnumbertype').value = cardtype;
						};
						((regForm.form['ccnumber'].onchange).bind(regForm.form['ccnumber']))();
					}
					var unhiderr = el.className.match(/unhides_([a-zA-Z0-9_\-\.]*)/i);
					if (unhiderr) {
						if (!regForm.hiderList[unhiderr])
							regForm.hiderList[unhiderr] = [el.id];
						else
							regForm.hiderList[unhiderr].push(el.id);
						el.onclick = function() {
							var unhider = this.className.match(/unhides_([a-zA-Z0-9_\-\.]*)/i);
							if (unhider && !this.checked) {
								var isit = false;
								for (var m=0; m< regForm.hiderList[unhider].length; m++) {
									if ($(regForm.hiderList[unhider][m]).checked) {
										isit = true;
										break;
									}
								}
								if (!isit)
									$(unhider[1]).style.display = 'none';
							} else if (unhider) {
								$(unhider[1]).style.display = '';
							}
						};
						if (unhiderr && !this.checked) {
							var isit = false;
							for (var m=0; m<regForm.hiderList[unhiderr].length; m++) {
								if ($(regForm.hiderList[unhiderr][m]).checked) {
									isit = true;
									break;
								}
							}
							if (!isit)
							$(unhiderr[1]).style.display = 'none';
						} else if (unhiderr) {
							$(unhiderr[1]).style.display = '';
						}
					}
				});
				regForm.pricingArray = Json.evaluate($('pricing').getText());
				var curFunc = function() {
						var totalPrice = 0;
						for (var elemID in regForm.pricingArray) {
							if (regForm.form[elemID].checked || regForm.form[elemID].selected)
								totalPrice += regForm.pricingArray[elemID];
						}
						$('total').setText('Total: $'+Math.round(totalPrice*Math.pow(10,2))/Math.pow(10,2));
				};
				var emptyReg = true;
				for (var stuffID in regForm.pricingArray) {
					emptyReg = false;
					regForm.form[stuffID].addEvent('click', curFunc);
				}
				if (!emptyReg) curFunc();
		}
		var formPoll = $('pollbox');
		if (formPoll) {
			regForm.pollselect = 0;
			regForm.polldisabled = -1;
			regForm.initialpoll = true;
			formPoll.getElements('input[name=poll]').each(function(arrayEl){
				arrayEl.addEvent('change', function() {
					if (this.checked)
						regForm.pollselect++;
					else if (!regForm.initialpoll)
						regForm.pollselect--;
					if (regForm.pollselect >= 3 && (regForm.polldisabled == false || regForm.polldisabled == -1)) {
						regForm.polldisabled = true;
						this.form.getElements('input[name=poll]').each(function(subEl) {
							if (!subEl.checked)
								subEl.disabled = true;
							else
								subEl.disabled = false;
						});
					} else if (regForm.pollselect < 3 && (regForm.polldisabled == true || regForm.polldisabled == -1)) {
						regForm.polldisabled = false;
						this.form.getElements('input[name=poll]').each(function(subEl) {
								subEl.disabled = false;
						});
					}
				});
				arrayEl.fireEvent('change');
			});
			regForm.initialpoll = false;
		}

		formValidator.extraValidation = function(i, secondaryReq) {
			if (this.elements[i].name == 'cctype') {
				for(var j=0; this.elements[i].options[j]; j++) {
					$(this.elements[i].options[j]);
					if (this.elements[i].options[j].selected && this.elements[i].options[j].value != $('ccnumbertype').value)
						return 'The selected card type does not match your credit card number.';
				}
			} else if (this.elements[i].name == 'ccnumber') {
				((this.elements[i].onchange).bind(this.elements[i]))();
			} else if (this.elements[i].id == 'i_PanelTitle' || this.elements[i].id == 'i_PanelDescription') {
				var wordcount  = this.elements[i].value.split(/\s+/g).length;
				if (this.elements[i].id == 'i_PanelDescription' && wordcount > 350)
					return 'Your description should be at most 350 words.';
			}
			return '';
		};
	}
};

var formTitler = {
	load: function(textVal, fieldId, fillIt) {
		var field = $(fieldId);
		if (!field)
			return;
		if (field.value == '' && fillIt)
			field.value = textVal;
		field.addEvent('focus', function() { this.value = this.value == textVal ? '' : this.value; });
		field.addEvent('blur', function() { this.value = this.value == '' ? textVal : this.value; });
	}
};

function switcherLoader() {
	var abtswitcher  = $('about-switcher');
	if (!abtswitcher)
		return;
	/*var switcherSize = abtswitcher.scrollHeight;
	var navSize      = abtswitcher.getElement('ul[class$=switcher-nav]').scrollHeight;
	if (navSize > switcherSize)
		switcherSize = navSize;
	abtswitcher.setStyle('height', switcherSize);*/
	var Switcher = function(elem) {
		this.id       = elem.id;
		this.fx       = new Fx.Scroll(elem.getElement('div[class$=switchwrap]'), {x: 120, y: 0, wheelStops: false, duration: 1000, wait: false});
		
		this.scrollTo = function(id) {
			this.fx.toSwitcher = id;
			this.fx.stop();
			this.fx.scrollTo($(id).offsetLeft, 0);
		};

		var navlist = elem.getElement('ul[class$=switcher-nav]').getElementsByTagName('a');
		for (var j = 0; j < navlist.length; j++) {
			var matcher = navlist[j].href.match(/#(.*?)$/);
			if (matcher && matcher[1])
				navlist[j].onclick = function(toLink) {
					this.scrollTo(toLink);
					return false;
				}.bind(this, matcher[1]);
		}
		delete navlist;
	};

	var switchers = $('col-right').getElementsByClassName('switcher');
	for (var i = 0; i < switchers.length; i++) {
		new Switcher(switchers[i]);
	}
	delete switchers;
}

var newsXML = function(elemID, xmlURL) {
	this.elem = $(elemID);
	if (!this.elem)
		return false;

	this.elem.addClass('loading');
	this.success = function(responseText, responseXML) {
		var fxgo = new Fx.Style(this.elem, 'opacity', {wait:false, duration:1000});
		fxgo.start(0);

		if (/<\?xml/.test(responseText)) {
			var newsItems = responseXML.getElementsByTagName('item');
		(function() {
			var textSet = '<ul>';
			for (var i = 0; i < newsItems.length; i++) {
				textSet += '<li>';
					textSet += '<h5>' + newsItems[i].getElementsByTagName('title')[0].firstChild.nodeValue + '</h5>';
	
					var elemChild = newsItems[i].getElementsByTagName('description')[0].firstChild;
					do {
						if (/<div.*?>(.*?)<\/div>/.test(elemChild.nodeValue)) {
							textSet += elemChild.nodeValue.match(/<div.*?>(.*?)<\/div>/)[1].replace(/-(<a.*?>)more<\/a>-/i, '$1&gt;&gt;&gt; Learn More</a>');
							break;
				}
					} while (elemChild = elemChild.nextSibling);
	
				textSet += '</li>';
			}
			textSet += '</ul>';
			this.elem.setHTML(textSet);
				contentScrollbar.unload(this.elem.id);
				contentScrollbar.load(this.elem.id);
			fxgo.start(1);
			this.elem.removeClass('loading');
		}).bind(this).delay(1000);
		} else {
			(function() {
				this.elem.setHTML(responseText);
				contentScrollbar.unload(this.elem.id);
				contentScrollbar.load(this.elem.id);
				fxgo.start(1);
				this.elem.removeClass('loading');
			}).bind(this).delay(1000);
		}
	};
	this.failure = function() {
		this.elem.removeClass('loading');
		this.elem.setHTML('<h5>Sorry, there was an error loading the news feed.</h5>');
	};

	(function(){
		new XHR({ timeout: 15000, method: 'get', onSuccess: this.success.bind(this), onFailure: this.failure.bind(this) }).send(xmlURL+(xmlURL.indexOf('?')>-1?'&':'?')+'random='+Math.random()*10);
	}).bind(this).delay(1);

	return this;
};

var imgXML = function(xmlURL) {
	this.ads = new Object();

	this.swapFirst = function(imgID, rotationDelay, imgList, curImg) {
		this.ads[imgID].wrap.addClass('loading');
		if (this.ads[imgID].link) {
			this.ads[imgID].link.href = imgList[curImg].getAttribute('href');
			this.ads[imgID].link.target = imgList[curImg].getAttribute('target') ? imgList[curImg].getAttribute('target') : '';
		}
		//this.ads[imgID].img.src = imgList[curImg].getAttribute('src');
		//this.ads[imgID].img.onload = (function() {
		var blah = new Image();
		blah.onload = (function() {
			this.ads[imgID].img.src = imgList[curImg].getAttribute('src');
			this.ads[imgID].img.alt = imgList[curImg].getAttribute('alt') ? imgList[curImg].getAttribute('alt') : '';
			new Fx.Style(this.ads[imgID].img, 'opacity', { wait:false, duration:500, onComplete: (function() {
				this.ads[imgID].wrap.removeClass('loading');
				this.ads[imgID].img.onload = function(){};
			}).bind(this) }).set(0).start(1);
		}).bind(this);
		//if (this.ads[imgID].img.complete)
		//	this.ads[imgID].img.onload();
		blah.src = imgList[curImg].getAttribute('src');
		if (rotationDelay) {
			this.swap.pass([imgID, rotationDelay, imgList, (curImg+1 >= imgList.length ? 0 : curImg+1)], this).delay(rotationDelay);
		}
	};
	this.swap = function(imgID, rotationDelay, imgList, curImg) {
		this.ads[imgID].wrap.addClass('loading');
		var fxGo = new Fx.Style(this.ads[imgID].img, 'opacity', { wait:false, duration:500, onComplete: (function() {
			if (this.ads[imgID].link) {
				this.ads[imgID].link.href = imgList[curImg].getAttribute('href');
				this.ads[imgID].link.target = imgList[curImg].getAttribute('target') ? imgList[curImg].getAttribute('target') : '';
			}
			//this.ads[imgID].img.src = imgList[curImg].getAttribute('src');
			//this.ads[imgID].img.onload = (function() {
			var blah = new Image();
			blah.onload = (function() {
				this.ads[imgID].img.src = imgList[curImg].getAttribute('src');
				this.ads[imgID].img.alt = imgList[curImg].getAttribute('alt') ? imgList[curImg].getAttribute('alt') : '';
				fxGo.stop();
				new Fx.Style(this.ads[imgID].img, 'opacity', { wait:false, duration:500, onComplete: (function() {
					this.ads[imgID].wrap.removeClass('loading');
					//this.ads[imgID].img.onload = function(){};
				}).bind(this) }).start(1);
			}).bind(this);
			//if (this.ads[imgID].img.complete)
			//	this.ads[imgID].img.onload();
			blah.src = imgList[curImg].getAttribute('src');
		}).bind(this) }).start(0);
		if (rotationDelay) {
			this.swap.pass([imgID, rotationDelay, imgList, (curImg+1 >= imgList.length ? 0 : curImg+1)], this).delay(rotationDelay);
		}
	};
	this.success = function(responseText, responseXML) {
		var groupItems = responseXML.getElementsByTagName('group');
		var curGroup = Cookie.get('adGroupSession');
		if (!curGroup)
			curGroup = parseInt(Cookie.get('adGroup'));
		else
			curGroup = parseInt(curGroup);

		if (isNaN(curGroup) || curGroup < 1 || curGroup >= groupItems.length) {
			curGroup = 0;
		}
		Cookie.set('adGroup', curGroup+1, {duration: 30});
		// temp commented to make session change on every page load as requested
		//Cookie.set('adGroupSession', curGroup);

		var imgItems = groupItems[curGroup].getElementsByTagName('img');
		for (var i = 0; i < imgItems.length; i++) {
			var swapItems = imgItems[i].getElementsByTagName('link');
			if (swapItems.length < 1) {
				continue;
			} else if (typeof this.ads[imgItems[i].getAttribute('id')] != 'undefined') {
				if (swapItems.length == 1 && swapItems[0].getAttribute('src') != this.ads[imgItems[i].getAttribute('id')].img.src) {
					this.swapFirst(imgItems[i].getAttribute('id'), false, swapItems, 0);
				} else if (swapItems[0].getAttribute('src') == this.ads[imgItems[i].getAttribute('id')].img.src) {
					this.swapFirst.pass([imgItems[i].getAttribute('id'), parseInt(imgItems[i].getAttribute('delay')), swapItems, 1], this).delay(parseInt(imgItems[i].getAttribute('delay')));
				} else {
					this.swapFirst(imgItems[i].getAttribute('id'), parseInt(imgItems[i].getAttribute('delay')), swapItems, 0);
				}
			}
		}
	};
	this.failure = function() {
	};

	var adItems = $A($('wrap').getElementsByClassName('adswap'));
	var elemCopy = new Element('div');
	for (var i = 0; i < adItems.length; i++) {
		adItems[i].setStyle('visibility', 'hidden');
		if (adItems[i].getParent().getTag() == 'a') {
			this.ads[adItems[i].className.match(/adswap([0-9]+)/)[1]] = {
				img: adItems[i],
				link: adItems[i].getParent(),
				wrap: elemCopy.clone(true).injectBefore(adItems[i]).adopt(adItems[i])
			};
		} else {
			this.ads[adItems[i].className.match(/adswap([0-9]+)/)[1]] = {
				img: adItems[i],
				link: false,
				wrap: elemCopy.clone(true).injectBefore(adItems[i]).adopt(adItems[i])
			};
		}
	}

	(function(){
		new XHR({ timeout: 15000, method: 'get', onSuccess: this.success.bind(this), onFailure: this.failure.bind(this) }).send(xmlURL+(xmlURL.indexOf('?')>-1?'&':'?')+'random='+Math.random()*10);
	}).bind(this).delay(1);

	return this;
};

var contentScrollbar = {
	fx: new Array(),
	scrolling: new Array(),
	pos: new Array(),
	scrollDir: function(event) {
		event = new Event(event);

		if (event.type == 'mouseup') {
			for (index in contentScrollbar.scrolling) {
				if (typeof contentScrollbar.scrolling[index] == 'number') {
					$clear(contentScrollbar.scrolling[index]);
					delete contentScrollbar.scrolling[index];
				}
			};
			return;
		}

		evID = event.id ? event : $(event.target.parentNode).getPrevious();
		if (!evID || !evID.id) return;
		evID = evID.id;
		if (contentScrollbar.scrolling[evID])
			$clear(contentScrollbar.scrolling[evID]);

		if (event.wheel || event.type == 'mousedown') {
			contentScrollbar.scrolling[evID] = (function(){
				var newpos    = contentScrollbar.pos[evID];
				var offsetter = (contentScrollbar.fx[evID].options.steps * 0.02 < 1 ? 1 : Math.floor(contentScrollbar.fx[evID].options.steps * 0.02)) * (event.wheel ? (event.wheel <= 1 && event.wheel >= -1 ? event.wheel * 2 : event.wheel) : 1);
				if (event.wheel || /scroll\-top$/.test(this))
					newpos -= offsetter;
				else
					newpos += offsetter;
				if (newpos > contentScrollbar.fx[evID].options.steps && contentScrollbar.pos[evID] != contentScrollbar.fx[evID].options.steps)
					newpos = contentScrollbar.fx[evID].options.steps;
				else if (newpos < 0 && contentScrollbar.pos[evID] != 0)
					newpos = 0;
				if (newpos > contentScrollbar.fx[evID].options.steps || newpos < 0)
					$clear(contentScrollbar.scrolling[evID]);
				else
					contentScrollbar.fx[evID].set(newpos);
				contentScrollbar.pos[evID] = newpos;
			});
			if (event.wheel) {
				contentScrollbar.scrolling[evID]();
				delete contentScrollbar.scrolling[evID];
				event.stop();
			} else {
				contentScrollbar.scrolling[evID](this.id);
				contentScrollbar.scrolling[evID] = contentScrollbar.scrolling[evID].bind(this.id).periodical(40);
			}
		}
	},
	load: function(elemName) {
		if (window.gecko) return false;
		var tempElem = $(elemName);
		if (tempElem) {
			if (typeof contentScrollbar.fx[elemName] == 'undefined') {
				contentScrollbar.fx[elemName] = false;
				contentScrollbar.scrolling[elemName] = false;
				contentScrollbar.pos[elemName] = 0;
			}
			var contentwrap = $(elemName+'wrap');
			if (!contentwrap) {
				contentwrap = new Element('div', {'id':elemName+'wrap'});
				contentwrap.injectBefore(tempElem);
				contentwrap.adopt(tempElem);
				tempElem = $(elemName);
			}

			var wrapperHeight  = $(elemName+'wrap').offsetHeight;
			var contentHeight  = tempElem.scrollHeight;
			var barHeight      = wrapperHeight/contentHeight;
			var boxHeading = tempElem.getFirst();
			var offsetTop = 10;
			if (boxHeading.className == 'boxhead' || boxHeading.id == 'newshead') {
				offsetTop = boxHeading.getSize().scrollSize.y+10;
			}
			var scrollbar      = new Element('div', {'id':elemName+'scrollbar', 'class':'scrollbar', 'styles':{'height':(wrapperHeight-10-offsetTop)+'px', 'top':offsetTop}});
			var scrollbarinner = new Element('div', {'id':elemName+'scrollbarinner', 'class':'scrollbarinner', 'styles':{'height':(wrapperHeight-10-offsetTop-8)+'px'}});

			document.addEvent('mouseup', contentScrollbar.scrollDir);
			$(elemName+'wrap').addEvent('mousewheel', contentScrollbar.scrollDir);
			scrollbar.adopt(new Element('div', {'id':elemName+'scroll-top', 'class':'scroll-top', 'events':{'mousedown':contentScrollbar.scrollDir}}));
			contentwrap.adopt(scrollbar);
			scrollbar.adopt(scrollbarinner);
			scrollbar.adopt(new Element('div', {'id':elemName+'scroll-bot', 'class':'scroll-bot', 'events':{'mousedown':contentScrollbar.scrollDir}}));

			barHeight = barHeight > 1 ? 1 : barHeight;
			barHeight = (barHeight*(scrollbarinner.offsetHeight-15) < 21 ? 20 : Math.floor(barHeight*(scrollbarinner.offsetHeight-15)));
			if (barHeight >= scrollbarinner.offsetHeight-16 && barHeight <= scrollbarinner.offsetHeight+16) {
				contentScrollbar.unload(elemName);
			} else {
				$(elemName+'wrap').removeClass('noscrollbar');
				var knob = new Element('div', {'id':elemName+'knob', 'class':'knob'});
				knob.adopt(new Element('div', {'id':elemName+'knob-mid', 'class':'knob-mid', 'styles':{'height':barHeight+'px'}}));
				knob.adopt(new Element('div', {'id':elemName+'knob-bot', 'class':'knob-bot'}));
				scrollbarinner.adopt(knob);

				contentScrollbar.fx[elemName] = new Slider(scrollbarinner, knob, {
					offset: 0,
					mode: 'vertical',
					steps: contentHeight-wrapperHeight+(typeof(window.opera) == 'undefined' ? 0 : 15),
					onChange: function(step) {
						if (!step)
							step = 0;
						else
							step = step > 0 ? '-'+step : step;
						contentScrollbar.pos[elemName] = step * -1;
						tempElem.setStyle('top', step+'px');
						this.knob.addClass('hover');
					},
					onComplete: function() {
						this.knob.removeClass('hover');
					}
				}).set(0);
			}
		}
	},
	unload: function(elemName) {
		if (window.gecko) return false;
		if (contentScrollbar.scrolling[elemName]) {
			$clear(contentScrollbar.scrolling[elemName]);
			delete contentScrollbar.scrolling[elemName];
		}
		if ($(elemName+'scrollbar')) {
			$(elemName+'scrollbar').remove();
			$(elemName+'wrap').addClass('noscrollbar');
		}
	}
};

var loginLinker = {
	fx: false,
	load: function() {
		var link = $('loginlink'), box = $('loginbox');
		if (!link || !box) return false;
		new Element('a', { 'href': 'javascript:loginLinker.close();', 'class': 'close' }).setText('Close').injectInside($('login-password').getParent());
		box.setStyle('top',  -1 * box.scrollHeight-20);
		box.setStyle('visibility', 'visible');
		this.fx = new Fx.Style(box, 'top', {wait:false, duration:1000, transition:Fx.Transitions.Quart.easeInOut});

		link.onclick = (function() {
			loginLinker.fx.stop();
			if (loginLinker.fx.element.getStyle('top').toInt() > 0) {
				loginLinker.fx.start(-1 * loginLinker.fx.element.scrollHeight-20);
			} else {
				loginLinker.fx.start(this.scrollHeight);
			}
			return false;
		});
	},
	close: function() {
		loginLinker.fx.start(-1 * loginLinker.fx.element.scrollHeight-20);
		this.blur();
	}
};

siteLib.pageLoaded();
regForm.load();
switcherLoader();
var newser = new newsXML('news', '/xml.xml');
var imger  = new imgXML('/linkswap.xml');
contentScrollbar.unload('eventscontent');
contentScrollbar.load('eventscontent');
