var LI = document.getElementsByTagName('li');
var DIV = document.getElementsByTagName('div');
var SPAN = document.getElementsByTagName('span');
var blurTabs = true;
var targetElement;
var targ;
var okdtarg;
var tabclicked;
var Ajax;

function loadDefaultAjax() {
	// loop through each one to find the ajax loaders
	for (var i = 0; i < LI.length; ++i) {
		// check if the tab is default
		splat = LI[i].id.split('_');

		// if default is true, then load ajax for this tab
		if(splat[4] == 'd') {
			// get proper vars from the ID
			if(splat[2]) var3 = splat[2];

			// since this is the initial load, only do ajax when the div is empty
			if(document.getElementById(var3).innerHTML == '') {
				// set the target ID
				targ = LI[i].id;
				// simulate onClick
				onClick();
			}
		}
	}
}

function setNoFocus() {
	var browser = navigator.appName;

	// to hell with IE
	if(browser != 'Microsoft Internet Explorer') {
		if(blurTabs == true) {
			for (var i = 0; i < LI.length; ++i) {
				splat = LI[i].id.split('_');

				// set nofocus
				if(splat[0] == 'tab') if(LI[i].childNodes[0]) LI[i].childNodes[0].onfocus = blurLink;
				if(splat[0] == 'nav') if(LI[i].childNodes[1]) LI[i].childNodes[1].onfocus = blurLink;
			}
			for (var i = 0; i < SPAN.length; ++i) {
				splater = SPAN[i].id.split('_');

				// set nofocus
				if(splater[0] == 'link') if(SPAN[i].childNodes[0]) SPAN[i].childNodes[0].onfocus = blurLink;
			}
		}
	}
}

// Utility to help setNoFocus()
function blurLink() {
	if(this.blur) this.blur();
}

function onClick(e) {
	// get the ID of affected element
	if(!targ) {
		if (!e) var e = window.event;
		if (e.target) targ = e.target.parentNode.id;
		else if (e.srcElement) targ = e.srcElement.parentNode.id;
		if(targ) if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode.id;
		tabclicked = true;
	}
	else tabclicked = false;
	//alert(targ);
	// get the element of targeted ID
	if(targ) targetElement = document.getElementById(targ);

	// check if element exists (hence valid request)
	if(targetElement != '' && targ) {

		// implode the ID to get some values
		splat = targ.split('_');

		// get proper vars from the ID
		if(splat[0]) var var1 = splat[0]; //type (eg: tab)
		if(splat[1]) var var2 = splat[1]; //option (eg: ajax)
		if(splat[2]) var var3 = splat[2]; //div id
		if(splat[3]) var var4 = splat[3]; //call page
		if(splat[4]) var var5 = splat[4]; //default flag

		// unset targ (not being used after this line), targ is only useful for initial ajax loads
		if(targ) targ = '';

		// if element is tab, then do the necessary tabbing
		if(var1 == 'tab') {
			// untoggle each of the list that's in the same var3 as clicked item
			for (var i = 0; i < LI.length; ++i) {
				// check if the list is same var3 as the clicked tab
				splat = LI[i].id.split('_');
				// if same var3, untoggle the tabs
				if(splat[2] == var3) LI[i].className = "";
			}

			// set the style of targetted element to toggled
			targetElement.className = "toggled";
		}

		// check if it's an ajax request, run ajax if true
		if(var2 == 'ajax') {
			// check if page value available, and pass as condition
			ajax(var1, var3, var4); // ajax(type, div, page)
			return false;
		}

		// unset vars and return false
		if(var1) {
			// unset them!
			var1 = '';
			var2 = '';
			var3 = '';
			var4 = '';
			var5 = '';
			action = '';
		}
		
		if(var2) {
			return false;
		}
		
		targetElement = '';
	}
}

function onPageLoad() {
	// detect if any onclick happened
	document.onclick = onClick;

	// load default ajax sections on pageload
	loadDefaultAjax();

	// grab default stuff
	//ajax('get_messages', 'getPMCount', '', 'noload');
	//ajax('get_friendrequests', 'getFriendRequests', '', 'noload');
	//ajax('shoutbox', 'getShoutbox', '', 'noload');
}

window.onload = function() {
	if(window.onPageLoad) onPageLoad();
	if(window.setNoFocus) setNoFocus();
	//var myinterval = window.setInterval('setUploadTitle()',50); // check msg every x seconds
}

function ajax(type, div, page) {
	// check if any empty parameter
	if(type != '' && div != '' && page != '') {
		var params = '';
		var actionURL = 'ajax/' + page + '.php';
		// do the ajax thing
		
		new Ajax.Updater(div, actionURL, {
			asynchronous:true,
			method:'post',
			parameters: params,
			onLoading: function() {
				//alert('1111');
				showLoader('preloader_'+div, div);				
			},
			onComplete: function() {
				//alert('222');
				hideLoader('preloader_'+div, div);
			}
		})
	}
	
	return false;
}

function showLoader(loader, div) {
   	new Effect.Appear($(loader), {duration: 0.5});
	$(div).style.display = "none";
}
function hideLoader(loader, div) {
   	new Effect.BlindUp($(loader), {duration: 0.5});
	new Effect.Appear($(div), {duration: 1});
}