var Type = 'Z';
var statusUpdateTime = 2000;

var 
SYMBOL_RATE_QAM64A_NTSC_188			= 5056900,
SYMBOL_RATE_QAM64A_NTSC_204			= 4660400,
SYMBOL_RATE_QAM64A_PAL_188			= 6742500,
SYMBOL_RATE_QAM64A_PAL_204			= 6213400,
SYMBOL_RATE_QAM256A_NTSC_188		= 5264000,
SYMBOL_RATE_QAM256A_NTSC_204		= 4851000,
SYMBOL_RATE_QAM256A_PAL_188			= 7147400,
SYMBOL_RATE_QAM256A_PAL_204			= 6586700,

SYMBOL_RATE_QAM64B_NTSC_188			= 5245000,
SYMBOL_RATE_QAM64B_NTSC_204			= 4831000,
SYMBOL_RATE_QAM64B_PAL_188			= 6742500,
SYMBOL_RATE_QAM64B_PAL_204			= 6213400,
SYMBOL_RATE_QAM256B_NTSC_188		= 5361000,
SYMBOL_RATE_QAM256B_NTSC_204		= 4940000,
SYMBOL_RATE_QAM256B_PAL_188			= 7147400,
SYMBOL_RATE_QAM256B_PAL_204			= 6586700,

QAM64_188_FACTOR_A					= 0.18085,
QAM64_204_FACTOR_A					= 0.16667,
QAM256_188_FACTOR_A					= 0.13564,
QAM256_204_FACTOR_A					= 0.12500,

QAM64_188_FACTOR_B					= 0.18750,
QAM64_204_FACTOR_B					= 0.17279,
QAM256_188_FACTOR_B					= 0.13812,
QAM256_204_FACTOR_B					= 0.12729,

CLOCK_RATE_QAM64_NTSC_188_A			= (SYMBOL_RATE_QAM64A_NTSC_188/QAM64_188_FACTOR_A),
CLOCK_RATE_QAM64_NTSC_204_A			= (SYMBOL_RATE_QAM64A_NTSC_204/QAM64_204_FACTOR_A),
CLOCK_RATE_QAM64_PAL_188_A			= (SYMBOL_RATE_QAM64A_PAL_188/QAM64_188_FACTOR_A),
CLOCK_RATE_QAM64_PAL_204_A			= (SYMBOL_RATE_QAM64A_PAL_204/QAM64_204_FACTOR_A),
CLOCK_RATE_QAM256_NTSC_188_A		= (SYMBOL_RATE_QAM256A_NTSC_188/QAM256_188_FACTOR_A),
CLOCK_RATE_QAM256_NTSC_204_A		= (SYMBOL_RATE_QAM256A_NTSC_204/QAM256_204_FACTOR_A),
CLOCK_RATE_QAM256_PAL_188_A			= (SYMBOL_RATE_QAM256A_PAL_188/QAM256_188_FACTOR_A),
CLOCK_RATE_QAM256_PAL_204_A			= (SYMBOL_RATE_QAM256A_PAL_204/QAM256_204_FACTOR_A),

CLOCK_RATE_QAM64_NTSC_188_B			= (SYMBOL_RATE_QAM64B_NTSC_188/QAM64_188_FACTOR_B),
CLOCK_RATE_QAM64_NTSC_204_B			= (SYMBOL_RATE_QAM64B_NTSC_204/QAM64_204_FACTOR_B),
CLOCK_RATE_QAM64_PAL_188_B			= (SYMBOL_RATE_QAM64B_PAL_188/QAM64_188_FACTOR_B),
CLOCK_RATE_QAM64_PAL_204_B			= (SYMBOL_RATE_QAM64B_PAL_204/QAM64_204_FACTOR_B),
CLOCK_RATE_QAM256_NTSC_188_B		= (SYMBOL_RATE_QAM256B_NTSC_188/QAM256_188_FACTOR_B),
CLOCK_RATE_QAM256_NTSC_204_B		= (SYMBOL_RATE_QAM256B_NTSC_204/QAM256_204_FACTOR_B),
CLOCK_RATE_QAM256_PAL_188_B			= (SYMBOL_RATE_QAM256B_PAL_188/QAM256_188_FACTOR_B),
CLOCK_RATE_QAM256_PAL_204_B			= (SYMBOL_RATE_QAM256B_PAL_204/QAM256_204_FACTOR_B),
CLOCK_RATE_MANUAL					= (0),
CLOCK_RATE_8PSK_188					= (0),
CLOCK_RATE_8PSK_204					= (0);

function Float() {
	if (Type == 'A') {
		document.all.AdFloater.style.pixelTop = document.documentElement ? document.documentElement.scrollTop : document.body.scrollTop;
	} else if (Type == 'B') {
		document.AdFloater.top = window.pageYOffset;
	} else if (Type == 'C') {
		document.getElementById('AdFloater').style.top = window.pageYOffset + 'px';
	}
}

function getAjaxSuccess() {
	return function (data) {
		document.location = document.getElementById("reloadAdminLink").getAttribute('href');
	};
}

function getAjaxError() {
	return function (xmlHttpRequest, textStatus, errorThrown) {
		if (xmlHttpRequest.readyState != "0" && xmlHttpRequest.status != "0") {
			alert("An ajax error has occurred.");
		}
	};
}

function loadPage(page, params, callback) {
	if (params === undefined) {
		params = "";
	}
	if (callback === undefined) {
		callback = {
			success: getAjaxSuccess(),
			failure: getAjaxError()
		};
	}
	$.ajax({
		url: page,
		data: params,
		success: function (data) {
			callback.success(data);
		},
		error: function(data, status) {
			callback.failure(data);
		}
	});
}

var reloadAdmin = function (o) {
	var callback = {
		success: function (data) {
			if (data == "True") {
				document.location = document.getElementById("reloadAdminLink").getAttribute('href');
			} else {
				setTimeout(reloadAdmin, 1000);
			}
		},
		failure: getAjaxError()
	};
	loadPage("/asimux/reloadAdmin.py", "", callback);
};

var reloadStatus = function (o) {
	var callback = {
			success: function (data) {
				if (data != "") {
					var div = $("#servermessageadmin");
					if (div.length == "0") {
						div = $("#servermessagereadonly");
					}
					var oldData = div.html();
					div.css("display", "none");
					div.html(data);
					if ($("#statusTable", div).length === 0) {
						div.html(oldData);
					}
					div.css("display", "");
				}
				setTimeout(reloadStatus, statusUpdateTime);
			},
			failure: getAjaxError()
	};
	loadPage("/asimux/system.py", "action=getStatus", callback);
};

function StartFloat() {
	if(document.all) {
		document.all.AdFloater.style.pixelRight = document.body.clientWidth - document.all.AdFloater.offsetWidth;
		document.all.AdFloater.style.visibility = 'visible';
		Type = 'A';
	} else if(document.layers) {
		document.AdFloater.left = window.innerWidth - document.AdFloater.clip.width - 16;
		document.AdFloater.visibility = 'show';
		Type = 'B';
	} else if(document.getElementById) {
		document.getElementById('AdFloater').style.right = (window.innerWidth - 20) + 'px';
		document.getElementById('AdFloater').style.visibility = 'visible';
		Type = 'C';
	}
	if (document.all) {
		window.onscroll = Float();
	} else {
		setInterval(Float, 100);
	}
	
	if (!(document.getElementById("loadingBar") === null || document.getElementById("loadingBar") === undefined)) {
		setTimeout(reloadAdmin, 5000);
	}
	setTimeout(reloadStatus, statusUpdateTime);
}

function hexToDec(hexVal) {
	return parseInt(hexVal, 16);
}

function updateDefaultBase() {
	var params = "action=update_base&defaultBase=" + $("input[name=defaultBase]:checked").val();
	var callback = {
		success: function (data) {
		},
		failure: function (data) {
		}
	};
	loadPage("/asimux/system.py", params, callback);
}

function startsWith(needle, haystack) {
	return haystack.substr(0, needle.length) == needle;
}