/* IT'S PARTY TIME!!! */
var feedTotal = 0;
var counter = 0;
var vipparty = null;
var prev = null;
/* Bouncer */
function handleTog(a) {
	switch(a.innerHTML) {
		case '+': 
			a.innerHTML = 'X';
			a.setAttribute('title','Close');
			showObj(a.parentNode.parentNode.parentNode.id+'-inside');
			checkIfCat(a);
			break;
		case 'X':
			hideObj(a.parentNode.parentNode.parentNode.id+'-inside');
			a.innerHTML = '+';
			a.setAttribute('title','Open');
			break;
		case 'Close':
			with(a.parentNode.parentNode.parentNode) {
				hideObj(id+'-inside');
				with(document.getElementById('tog-'+id.substring(4))) {
					innerHTML = '+';
					setAttribute('title','Open');
				}
			}
			break;
		default:
			with(a.parentNode.parentNode.parentNode) {
				with(document.getElementById('tog-'+id.substring(4))) {
					if(innerHTML == 'X') {
						innerHTML = '+';
						setAttribute('title','Open');
						hideObj(a.parentNode.parentNode.parentNode.id+'-inside');			
					} else {
						innerHTML = 'X';
						setAttribute('title','Close');
						showObj(a.parentNode.parentNode.parentNode.id+'-inside');
						checkIfCat(a);
					}
				}
			}
			break;
	}
};
function handleSelectall(a) {
	with (a.parentNode.parentNode.parentNode) {
		var checkboxes = getElementsByTagName('input');
		for(i=0; i<checkboxes.length; i++) {
			if(!checkboxes[i].checked && !checkboxes[i].disabled) checkboxes[i].checked = true;
		}
	}
};
function handleSelectnone(a) {
	with (a.parentNode.parentNode.parentNode) {
		var checkboxes = getElementsByTagName('input');
		for(i=0; i<checkboxes.length; i++) {
			if(checkboxes[i].checked == true) checkboxes[i].checked = false;
		}
	}
};
/* Category Coordinator */
function checkIfCat(a) {
	if(a.getAttribute('rel') == 'bmx-cat' || a.getAttribute('rel') == 'bmx-cat-title') {
		var catID = a.id.substring(4);
		if(document.getElementById('bmx-'+catID+'-inside').getAttribute('rel') != 'loaded') {
			document.getElementById('bmx-'+catID+'-inside').setAttribute('rel','loaded');
			get_categoryRSS(catID);
		}
	}
};
function get_categoryRSS(catID) {
		
	var httpObject=get_httpObject();
	if (httpObject==null) return;
	
	var url=homeskillet+'/wp-content/themes/bmx-party-time/includes/get_categoryRSS.php';
	
	var params = '';
	if (party != "no party :(") params += "party=" + party + "&";
	params += 'catID='+ catID;
	
	httpObject.onreadystatechange= function() { cat_loaded(this,catID) };
	httpObject.open("POST",url,true);
	httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpObject.setRequestHeader("Content-length", params.length);
	httpObject.setRequestHeader("Connection", "close");
	httpObject.send(params);
};
function cat_loaded (httpObject,catID) {
	with(document) {
		if (httpObject.readyState==4 || httpObject.readyState=="complete") {
			var catlinks = getElementById('bmx-'+catID+'-inside');
			
			var catlinksDIV = createElement('div');
				catlinksDIV.innerHTML = httpObject.responseText;
			
			catlinks.removeChild(getElementById('loading-links-'+catID));
			catlinks.insertBefore(catlinksDIV, catlinks.childNodes[0]);
		}
	}
};
/* Sections */
function getSection(section) {
	with(document.getElementById(section+'-inside')) {
		if(innerHTML == '') {
			innerHTML = '<h3>loading content...</h3>';
	
			var httpObject=get_httpObject();
			if (httpObject==null) return;
			
			var url=homeskillet+'/wp-content/themes/bmx-party-time/sections/'+section+'.php';
			httpObject.onreadystatechange= function() { sectionLoaded(this,section+'-inside') };
			httpObject.open("GET",url,true);
			httpObject.send(null);
		}
	}
};
function sectionLoaded(httpObject,section) {
	with(document) {
		if (httpObject.readyState==4 || httpObject.readyState=="complete") {
			document.getElementById(section).innerHTML = httpObject.responseText;
		}
	}
};
/* VIP Room */
function setupRSSResults() {
	with(document) {
		getElementById('feedresults-refresh').onclick=refreshRSS;
		getElementById('feedresults-tog').onclick=resultsTog;
		getElementById('feedresults-heading').onclick=refreshRSS;
	}
};
function resultsTog() {
	with(document) {
	
		with(getElementById('feedresults-tog')) {
			if(innerHTML == '+') {
				showObj('feedresults-inside');
				if(getElementById('feedresults-container').innerHTML == '') refreshRSS();
				getElementById('feedresults-refresh').innerHTML = 'REFRESH RESULTS';

				setAttribute('title','Close');
				innerHTML = 'X';
	
			} else {
				innerHTML = '+';
				setAttribute('title','Open');
				
				hideObj('feedresults-inside');
			}
		}
	}
};
function refreshRSS() {
	with(document) {
		
		with(getElementById('feedresults-tog')) {
			if(innerHTML = '+') {
				
				showObj('feedresults-inside');

				with(getElementById('feedresults-refresh')) {
					innerHTML = 'REFRESH RESULTS';
					setAttribute('title', 'Refresh Results');
				}

				if (getElementById('feedresults-container').innerHTML == '') getElementById('feedresults-heading').setAttribute('title', 'Refresh Results');

				innerHTML = 'X';
			}
		}

		getElementById('feedresults-container').innerHTML = "";
		
		var feedlist = 'X';
		feedTotal = 0;
		/* Split linked party into an array, only executes once. */
		if(party != "no party :(" && !vipparty) {
			vipparty = party.split("X");
		}
		/* Put active checkboxes into array */
		var feeds = getElementsByName('bmx')
				
		for(i=0; i<feeds.length; i++) {
			var feedID = feeds[i].id.substring(4);			
			if(vipparty && (vipparty[0] || vipparty[1]) && (vipparty[0] != '' || vipparty[1] != '')) {
				try { var index = vipparty.indexOf(feedID) }
				catch (e) { index = arrayIndexOf(vipparty, feedID) }
				if(index>=0) {
					try { vipparty.splice(index,1) }
					catch (e) { vipparty = removeArrayItems(vipparty,index,1)}
				}
			}
			if(feeds[i].checked) {
				if(feedlist.indexOf('X'+feedID+'X')== -1) {
					get_rssresults(feedID);
					feedlist += feedID + "X";
					feedTotal++;
				}
			}
		}
		if(vipparty && (vipparty[0] || vipparty[1]) && (vipparty[0] != '' || vipparty[1] != '')) {
			for(p=0; p<vipparty.length; p++) {	
				var feedID = vipparty[p]

				if(feedlist.indexOf('X'+feedID+'X')== -1 && feedID) {
					get_rssresults(feedID);
					feedlist += feedID + "X";
					feedTotal++;
				}
			}
		}
		generateParty(feedlist);
	}
};
function get_rssresults(feedID) {
	
	var httpObject=get_httpObject();
	if (httpObject==null) return;
	
	var url=homeskillet+'/wp-content/themes/bmx-party-time/includes/get_rrsresults.php?feedID='+feedID;
	httpObject.onreadystatechange= function() { rssFeedLoaded(this,feedID) };
	httpObject.open("GET",url,true);
	httpObject.send(null);
};
function rssFeedLoaded(httpObject,feedID) { 
	with(document) {
		if (httpObject.readyState==4 || httpObject.readyState=="complete") {
			counter++;
			if(counter == feedTotal) getElementById('loadingfeeds').innerHTML = 'Feeds are done loading (' + feedTotal + ' selected)';
			else getElementById('loadingfeeds').innerHTML = 'Loading feeds (' + counter + ' of ' + feedTotal + ')';
			
			var closeButt = createElement('a');
			closeButt.style.position = 'absolute';
			closeButt.style.right = '20px';
			closeButt.style.top = '7px';
			closeButt.style.fontSize = '9px';
			closeButt.id = 'feed-tog-'+feedID;
			closeButt.href = '#';
			closeButt.innerHTML = "MINIMIZE";
			closeButt.onclick = function(){closeFeedBox(this); return false;};
	
			var xButt = createElement('a');
			xButt.style.position = 'absolute';
			xButt.style.right = 0;
			xButt.style.top = '7px';
			xButt.style.fontSize = '9px';
			xButt.id = 'feed-x-'+feedID;
			xButt.href = '#';
			xButt.innerHTML = "(X)";
			xButt.onclick = function(){xFeedBox(this); return false;};

			var rssfeedResult = createElement('div');
			rssfeedResult.style.position = 'relative';
			rssfeedResult.id='feedbox-'+feedID;
			rssfeedResult.innerHTML = httpObject.responseText;
			rssfeedResult.appendChild(xButt);
			rssfeedResult.appendChild(closeButt);

			getElementById('feedresults-container').appendChild(rssfeedResult);
		}
	}
};
function closeFeedBox(a) {
	with(document.getElementById('feedbox-'+a.id.substring(9))) {
			if(a.innerHTML == 'MINIMIZE') {
				a.innerHTML = 'MAXIMIZE';
				style.overflow = 'hidden';
				style.height = '22px';
			} else {
				a.innerHTML = 'MINIMIZE';
				style.overflow = 'visible';
				style.height = 'auto';
			}
	}
};
function xFeedBox(a) {
	with(document.getElementById('feedbox-'+a.id.substring(7))) {
		if(prev) prev.innerHTML = '';
		innerHTML = '';
		style.overflow = 'hidden';
		style.height = '0px';
		style.visibility = 'hidden';
	}
};
function generateParty(invitelist) {
	with(document) {
		if(invitelist && invitelist != 'X') {
			counter = 0;
			var url = homeskillet+'/index.php?party='+invitelist;
			getElementById('feedresults-inside-header').innerHTML = '<img src="http://www.bmxpartytime.com/wp-content/themes/bmx-party-time/images/filter.gif"/><a href="#" id="filter-onehour" title="Only show items from the last hour" onclick="fresults(this); return false;"><img src="http://www.bmxpartytime.com/wp-content/themes/bmx-party-time/images/filter-onehour.gif"/></a><a href="#" id="filter-oneday" title="Only show items from the last 24 hours" onclick="fresults(this); return false;"><img src="http://www.bmxpartytime.com/wp-content/themes/bmx-party-time/images/filter-oneday.gif"/></a><a href="#" id="filter-twoday" title="Only show items from the last 48 hours" onclick="fresults(this); return false;"><img src="http://www.bmxpartytime.com/wp-content/themes/bmx-party-time/images/filter-twoday.gif"/></a><a href="#" id="filter-threeday" title="Only show items from the last 72 hours" onclick="fresults(this); return false;"><img src="http://www.bmxpartytime.com/wp-content/themes/bmx-party-time/images/filter-threeday.gif"/></a><a href="#" id="filter-none" title="Show all items" onclick="fresults(this); return false;"><img src="http://www.bmxpartytime.com/wp-content/themes/bmx-party-time/images/nofilter.gif"/></a><br/><input id="direct-url" name="direct-url" type="text" value="'+url+'&open=true" /><a id="partyOpened" href="'+url+'&open=true" target="_blank">AUTO-OPEN</a><h4>'+"( <a href=\""+homeskillet+"/quickie/?party="+invitelist+'&open=true" target="_blank">FASTER LOADING LINK: Auto-open feeds w/out our blog</a> )</h4><h5 id="loadingfeeds">Loading feeds (' + feedTotal + ' Total)</h5>';
		} else {
			getElementById('feedresults-inside-header').innerHTML = '<a href="http://www.bmxpartytime.com/1986/03/21/instructions/" title="Click for more details"><img src="http://www.bmxpartytime.com/wp-content/themes/bmx-party-time/images/quick-1.gif" alt="Quick Start Guide"/></a><a href="#" onclick="refreshRSS(); return false;" title="Show Results"><img src="http://www.bmxpartytime.com/wp-content/themes/bmx-party-time/images/quick-2.gif" alt="Show Results"/></a>';
		}
	}
};

/* Show All Items */
function showAllItems(a) {
	
	document.getElementById('feedbox-'+a.rel.substring(4)).innerHTML = '<h3 class="result-tit"><a href="#">PLEASE WAIT: Loading all available feed items.</a></h3>';
	
	var httpObject=get_httpObject();
	if (httpObject==null) return;
	
	var url=homeskillet+'/wp-content/themes/bmx-party-time/includes/get_rrsresults.php?feedID='+a.rel.substring(4)+'&max=25';
	httpObject.onreadystatechange= function() { allItemsLoaded(this,a.rel.substring(4)) };
	httpObject.open("GET",url,true);
	httpObject.send(null);
};
function allItemsLoaded(httpObject,feedID) { 
	with(document) {
		if (httpObject.readyState==4 || httpObject.readyState=="complete") {
			
			var closeButt = createElement('a');
			closeButt.style.position = 'absolute';
			closeButt.style.right = '20px';
			closeButt.style.top = '7px';
			closeButt.style.fontSize = '9px';
			closeButt.id = 'feed-tog-'+feedID;
			closeButt.href = '#';
			closeButt.innerHTML = "MINIMIZE";
			closeButt.onclick = function(){closeFeedBox(this); return false;};
	
			var xButt = createElement('a');
			xButt.style.position = 'absolute';
			xButt.style.right = 0;
			xButt.style.top = '7px';
			xButt.style.fontSize = '9px';
			xButt.id = 'feed-x-'+feedID;
			xButt.href = '#';
			xButt.innerHTML = "(X)";
			xButt.onclick = function(){xFeedBox(this); return false;};

			with(getElementById('feedbox-'+feedID)) {	
				innerHTML = httpObject.responseText;
				appendChild(xButt);
				appendChild(closeButt);
			}
		}
	}
};
/* Filter Results */
function fresults(a) {
	with(document.getElementById('feedresults-container')) {
		var fresults=getElementsByTagName('li');
		for(fr=0; fr<fresults.length; fr++) {
			fresults[fr].style.overflow = 'visible';
			fresults[fr].style.height = 'auto';
			fresults[fr].style.fontSize = '10px';
			fresults[fr].style.visibility = 'visible';
			switch(a.id.substring(7)) {
				case "none": break;
				case "threeday":
					if (fresults[fr].childNodes[0].name == "threeday") break;
				case "twoday":
					if (fresults[fr].childNodes[0].name == "twoday") break;
				case "oneday":
					if (fresults[fr].childNodes[0].name == "oneday") break;
				case "onehour":
					if (fresults[fr].childNodes[0].name == "onehour") break;
				default:
					fresults[fr].style.overflow = 'hidden';
					fresults[fr].style.height = '0px';
					fresults[fr].style.fontSize = '0px';
					fresults[fr].style.visibility = 'hidden';
					break;
			}
		}
	}
};

/* Get RSS Content */
function get_rsscontent(a) {
	with(document) {
		if(!prev) {
			prev = createElement('div');
			prev.id = 'preview';
		}
	
		prev.innerHTML = '<a class="preview">PLEASE WAIT: FETCHING FEED DATA.</a>';
		getElementById('feedbox-'+a.id.substring(12)).appendChild(prev);
	
		var httpObject=get_httpObject();
		if (httpObject==null) return;
		
		var url=homeskillet+'/wp-content/themes/bmx-party-time/includes/get_rrscontent.php?feedID='+a.id.substring(12)+'&i='+a.rel;
		httpObject.onreadystatechange= function() { rssContentLoaded(this) };
		httpObject.open("GET",url,true);
		httpObject.send(null);
	}
};
function rssContentLoaded(httpObject) { 
	with(document) {
		if (httpObject.readyState==4 || httpObject.readyState=="complete") prev.innerHTML = httpObject.responseText;
	}
};
function hideRSSContent() {
	with(document.getElementById('preview')) {
		innerHTML = '<small>THANK YOU FOR VIEWING</small>';
	}
};

/* VIP Pass */
function get_httpObject() {
	var httpObject=null;
	try {
		 /* Firefox, Opera 8.0+, Safari */
		 httpObject=new XMLHttpRequest();
	}
	catch (e) {
		/* Internet Explorer */
		try {
			httpObject=new ActiveXObject("Msxml2.httpObject");
		}
		catch (e) {
			httpObject=new ActiveXObject("Microsoft.httpObject");
		}
	}
	if (httpObject==null) {
		alert ("Browser does not support HTTP Request.\nGet FireFox if you wanna party!");
		return;
	} else { return httpObject }
};
/* Hide */
function hideObj(id) {
	obj = document.getElementById(id);
	with(obj.style) {
		visibility = 'hidden';
		display = 'none';
		height = 0;
		overflow = 'hidden';
	}
};
function showObj(id) {
	obj = document.getElementById(id);
	with(obj.style) {
		visibility = 'visible';
		display = 'block';
		height = 'auto';
		overflow = 'visible';
	}	
};
/* For automatic feed selected links in IE. */
function removeArrayItems(arr, index, items) {

	var aHead = arr.slice(0, index);
	var aTail = arr.slice(index + items);

	var newArray = aHead.concat(aTail);

	return newArray;
};
function arrayIndexOf(arr, obj) {
	for(var i=0; i<arr.length; i++){
		if(arr[i]==obj){
			return i;
		}
	}
	return -1;
};