/* jQuery tricks */


/* END */

var current_page = document.location.href;
var preload = [];
preload.push('./images/ajax_load.gif');
preload.push('./images/button_aboutus_pressed.jpg');
preload.push('./images/button_buynps_pressed.jpg');
preload.push('./images/button_buyitems_pressed.jpg');
preload.push('./images/button_buypets_pressed.jpg');
preload.push('./images/button_index_pressed.jpg');
preload.push('./images/button_aboutus.jpg');
preload.push('./images/button_buynps.jpg');
preload.push('./images/button_buyitems.jpg');
preload.push('./images/button_buypets.jpg');
preload.push('./images/button_index.jpg');

function pageLoad()
{
	el = document.getElementById('inner');
	el.style.height = parseInt(el.offsetHeight)+'px';
	var bufferImg = [];
	for (i = 0; i < preload.length; i++)
	{
		bufferImg.push(new Image());
		bufferImg[i].src = preload[i];
	}

	if (document.all)
	{
		
	}
	else
	{
		bufferImg[preload.length - 1].addEventListener('load', function () { bufferImg = null; }, true);
	}

	setTimeout('pageLoad()', 15000);
}

function replaceDiacritics(s)
{
	var s;

	var diacritics =[
		/[\300-\306]/g, /[\340-\346]/g, // A, a
		/[\310-\313]/g, /[\350-\353]/g, // E, e
		/[\314-\317]/g, /[\354-\357]/g, // I, i
		/[\322-\330]/g, /[\362-\370]/g, // O, o
		/[\331-\334]/g, /[\371-\374]/g,  // U, u
		/[\321]/g, /[\361]/g, // N, n
		/[\307]/g, /[\347]/g, // C, c
	];

	var chars = ['A','a','E','e','I','i','O','o','U','u','N','n','C','c'];

	for (var i = 0; i < diacritics.length; i++)
	{
		s = s.replace(diacritics[i],chars[i]);
	}

	return s;
}

function CreateXMLRequest()
{
	ua = navigator.userAgent.toLowerCase();
	if (!window.ActiveXObject)
	{
		request = new XMLHttpRequest();
	}
	else if (ua.indexOf('msie 5') == -1)
	{
		request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	else
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return request;
}

function process_form(form)
{
	if (!CreateXMLRequest()) return true;
	el = document.getElementById('inner');
	el.innerHTML = '<img src="./images/ajax_load.gif" />';
	$(el).animate({height: 40}, 300, 'swing', function () {
		objParams = $(form).formToArray();
		params = new Object();
		for (i = 0; i < objParams.length; i++)
		{
			params[objParams[i].name] = objParams[i].value;
		}
		params['ajax'] = '1';
		$.post(
			form.action,
			params,
			function (text, status)
			{
				if (status == 'success' || status == 'notmodified')
				{
					$(document.getElementById('inner')).html(text);
					scriptMatch = /<script.*?>([\s\S]+?)<\/script>/gi;
					while (regMatch = scriptMatch.exec(text))
					{
						eval(regMatch[1]);
					}
					tmpHeight = getHeight(text);
					$('#inner').animate({height: tmpHeight}, (tmpHeight > 30) ? tmpHeight + 30 : 300, 'swing', function() { /* Maybe history here */ });
					document.close();
				}
				else
				{
					alert('Ajax Request for page "'+form.action+'" failed.\r\n'+
						'Error: '+status+'\r\n'+
						'\r\n'+
						'Loading page manually.');
					location.href = form.action;
				}
			},
			'text'
		);
	});
	return false;
}

function process_ajax(page, obj)
{
	if (!CreateXMLRequest()) return true;
	$('html,body').animate({scrollTop: '0px'}, 200, 'swing');
	if (obj !== undefined)
	{
		document.getElementById('refresh').style.display = 'none';
		img = obj.getElementsByTagName('img')[0];
		mnu_imgs = document.getElementById('menubar').getElementsByTagName('img');
		for (var i = 0; i < mnu_imgs.length; i++)
		{
			if (match2 = mnu_imgs[i].parentNode.parentNode.style.backgroundImage.match(/button_([a-z]+)_pressed\.jpg/i))
			{
				mnu_imgs[i].parentNode.parentNode.style.backgroundImage = 'url(\'./images/button_'+match2[1]+'.jpg\')';
			}
		}
		match = img.parentNode.parentNode.style.backgroundImage.match(/button_([a-z]+)(_pressed)?\.jpg/i);
		img.parentNode.parentNode.style.backgroundImage = 'url(\'./images/button_'+match[1]+'_pressed.jpg\')';
	}
	else
	{
		document.getElementById('refresh').style.display = 'block';
		mnu_imgs = document.getElementById('menubar').getElementsByTagName('img');
		for (var i = 0; i < mnu_imgs.length; i++)
		{
			if (match2 = mnu_imgs[i].parentNode.parentNode.style.backgroundImage.match(/button_([a-z]+)_pressed\.jpg/i))
			{
				mnu_imgs[i].parentNode.parentNode.style.backgroundImage = 'url(\'./images/button_'+match2[1]+'.jpg\')';
			}
		}
	}
	current_page = page;
	el = document.getElementById('inner');
	$(el).css({height: el.offsetHeight+'px'});
	el.innerHTML = '<img src="./images/ajax_load.gif" />';
	$(el).animate({height: 40}, 300, 'swing', function () { ext_ajax(page); });
	return false;
}

function getHeight(txt)
{
	var el = [document.createElement('div'),
		document.createElement('div'),
		document.createElement('div')];
	el[0].className = 'contentParent';
	el[1].id = 'content';
	el[2].id = 'inner';
	el[0].appendChild(el[1]);
	el[1].appendChild(el[2]);
	el[2].innerHTML = txt;
	el[0].style.position = 'absolute';
	el[0].style.left = '-120%';
	document.body.appendChild(el[0]);
	tmp = el[2].offsetHeight;
	el[0].parentNode.removeChild(el[0]);
	return tmp;
}

function ext_ajax(page)
{
	var objIn = document.getElementById('inner');
	pageTracker._trackPageview(page.replace(location.protocol + '//' + location.hostname, ''));
	$.post(page, {ajax: 1}, function (data, state) {
		if (state == 'success' || state == 'notmodified')
		{
			objIn.innerHTML = data;
			scriptMatch = /<script.*?>([\s\S]+?)<\/script>/gi;
			while (regMatch = scriptMatch.exec(data))
			{
				eval(regMatch[1]);
			}
			tmpHeight = getHeight(data);
			$(objIn).css({height: objIn.offsetHeight});
			$(objIn).animate({height: tmpHeight}, (tmpHeight > 30) ? tmpHeight + 30 : 300, 'swing', function() { $(objIn).css({height: 'auto'}); });
			document.close();
		}
		else
		{
			alert('Ajax Request for page "'+page+'" failed.\r\n'+
				'Error: '+state+'\r\n'+
				'\r\n'+
				'Loading page manually.');
			location.href = page;
		}
	}, 'text');
	return false;
}

function fadeOut(elem) {
	if (elem.id) {
		fadeElementSetup(elem.id, 100, 0, 10);
	}
}

function fadeIn(elem) {
	if (elem.id) {
		fadeElementSetup(elem.id, 0, 100, 10);	
	}
}

// Fade: Initialize the fade function

var fadeActive = new Array();
var fadeQueue  = new Array();
var fadeTimer  = new Array();
var fadeClose  = new Array();
var fadeMode   = new Array();

function fadeElementSetup(theID, fdStart, fdEnd, fdSteps, fdClose, fdMode) {

	// alert("Fading: "+theID+" Steps: "+fdSteps+" Mode: "+fdMode);

	if (fadeActive[theID] == true) {
		// Already animating, queue up this command
		fadeQueue[theID] = new Array(theID, fdStart, fdEnd, fdSteps);
	} else {
		fadeSteps = fdSteps;
		fadeCurrent = 0;
		fadeAmount = (fdStart - fdEnd) / fadeSteps;
		fadeTimer[theID] = setInterval("fadeElement('"+theID+"', '"+fadeCurrent+"', '"+fadeAmount+"', '"+fadeSteps+"')", 15);
		fadeActive[theID] = true;
		fadeMode[theID] = fdMode;
		
		if (fdClose == 1) {
			fadeClose[theID] = true;
		} else {
			fadeClose[theID] = false;
		}
	}
}

// Fade: Do the fade. This function will call itself, modifying the parameters, so
// many instances can run concurrently. Can fade using opacity, or fade using a box-shadow.

function fadeElement(theID, fadeCurrent, fadeAmount, fadeSteps) {

	if (fadeCurrent == fadeSteps) {

		// We're done, so clear.

		clearInterval(fadeTimer[theID]);
		fadeActive[theID] = false;
		fadeTimer[theID] = false;

		// Should we close it once the fade is complete?

		if (fadeClose[theID] == true) {
			document.getElementById(theID).style.visibility = "hidden";
		}

		// Hang on.. did a command queue while we were working? If so, make it happen now

		if (fadeQueue[theID] && fadeQueue[theID] != false) {
			fadeElementSetup(fadeQueue[theID][0], fadeQueue[theID][1], fadeQueue[theID][2], fadeQueue[theID][3]);
			fadeQueue[theID] = false;
		}
	} else {

		fadeCurrent++;
		
		// Now actually do the fade adjustment.
		
		if (fadeMode[theID] == "shadow") {

			// Do a special fade on the webkit-box-shadow of the object
		
			if (fadeAmount < 0) {
				document.getElementById(theID).style.webkitBoxShadow = shadowSettings + (Math.abs(fadeCurrent * fadeAmount)) + ')';
			} else {
				document.getElementById(theID).style.webkitBoxShadow = shadowSettings + (100 - (fadeCurrent * fadeAmount)) + ')';
			}
			
		} else {
		
			// Set the opacity depending on if we're adding or subtracting (pos or neg)
			
			if (fadeAmount < 0) {
				setOpacity(Math.abs(fadeCurrent * fadeAmount), theID);
			} else {
				setOpacity(100 - (fadeCurrent * fadeAmount), theID);
			}
		}

		// Keep going, and send myself the updated variables
		clearInterval(fadeTimer[theID]);
		fadeTimer[theID] = setInterval("fadeElement('"+theID+"', '"+fadeCurrent+"', '"+fadeAmount+"', '"+fadeSteps+"')", 15);
	}
}

function setOpacity(opacity, theID) {

	var object = document.getElementById(theID).style;

	// If it's 100, set it to 99 for Firefox.

	if (navigator.userAgent.indexOf("Firefox") != -1) {
		if (opacity == 100) { opacity = 99.9999; } // This is majorly awkward
	}

	// Multi-browser opacity setting

	object.filter = "alpha(opacity=" + opacity + ")"; // IE/Win
	object.opacity = (opacity / 100);                 // Safari 1.2, Firefox+Mozilla

}

function numcheck(obj, e)
{
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	ctl_keys = [null, 0, 8, 9, 13, 27, 46];
	for (i = 0; i < ctl_keys.length; i++)
	{
		if (ctl_keys[i] == key)
			return true;
	}

	if ('0123456789'.indexOf(String.fromCharCode(key)) > -1)
		return true;
		
	return false;
}

function number_format(Value)
{
	// Separator Length. Here this is thousand separator
	var separatorLength = 3;
	var OriginalValue=Value;
	var TempValue = OriginalValue.toString();
	var NewValue = "";

	if(TempValue.length > separatorLength)
	{
		// Logic of separation
		while(TempValue.length > separatorLength)
		{
			NewValue = "," + TempValue.substr(TempValue.length - separatorLength) + NewValue;
			TempValue = TempValue.substr(0, TempValue.length - separatorLength);
		}
		NewValue = TempValue + NewValue;
	}
	else
	{
		NewValue = TempValue;
	}
	// Return Final value
	return NewValue;
}

function scrollTo(linkWithHash)
{
	var hash = linkWithHash.substr(linkWithHash.indexOf('#')+1);
	$('html,body').animate({scrollTop: $('a[name="'+hash+'"]').offset().top}, 200, 'swing');
}
