/**
 * @copyright  2009 Ian Atkin
 * @license    http://www.ianatkin.info/
 */

// ajax functions
function ajaxFunc(mode, items) {
	var ajaxRequest; // The variable that makes ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}

	// functions that will receive data sent from the server

	// GET only: we add this param to prevent fetched content being brought from the cache
	var rnd = Math.random()*10000;

	if (mode == 'loadPage') {
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				var response = ajaxRequest.responseText;

				if (response != '') {
					setHTML('page', response);

					// set volume
					setVolume(readCookie('tg_vv'));

					// set mute state
					if (readCookie('tg_vm') == '1') {
						mute();
					}
				}

				showHideBusy();

			}
		}

		showHideBusy(true);

		var postVal = "mode=loadPage&page=" + items[0];

		ajaxRequest.open("POST", "/includes/video.ajax.php", true);
		ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ajaxRequest.send(postVal);
	}

	if (mode == 'logError') {
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				return;
			}
		}

		var postVal = "mode=logError&vars=" + items[0] + "&comment=" + items[1];

		ajaxRequest.open("POST", "/includes/client.ajax.php", true);
		ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ajaxRequest.send(postVal);
	}
}
