function keepalive(){
	new Ajax.Request("index.php?com=account&sub=live&mode=standalone", {
		onSuccess : function(transport){
			setTimeout(keepalive, 60000);
		}
	});
}
setTimeout(keepalive, 10000);

Array.prototype.multisort = function(){
	var sortArray = [];
	for(var i = 0; i < this.length; i++){
		sortArray[i] = [this[i]];
		for(var j = 0; j < arguments[0].length; j++){
			if(arguments[0][j].length != this.length) return false;
			sortArray[i][j+1] = arguments[0][j][i];
		}
	}
	sortArray.sort();
	for(var i = 0; i < sortArray.length; i++){
		this[i] = sortArray[i][0];
		for(var j = 0; j < arguments[0].length; j++){
			arguments[0][j][i] = sortArray[i][j+1];
		}
	}
	return true;
}

Array.prototype.key = function(value){
	for(var i = 0, len = this.length; i < len; ++i) if(this[i] == value) return i;
	return false;
}

function isset(obj){
	return typeof obj != "undefined" ? true : false;
}

function str_repeat(input, multiplier){
	var value = "";
	for(var i = 0; i < multiplier; ++i) value += input;
	return value;
}

function preg_match(pattern, subject){
	var regex = pattern;
	var matches = regex.exec(subject);
	return matches != null ? matches : false;
}

function OptionGrid(obj, id){
	this.headers = headers = [];
	this.cells = [];
	
	
	var __cells = [];
	var headers = [];
	
	this.orderColumns = function(){
		var caption = this.innerHTML;
		var $this = objOptionGrids[id];
		var column_id = $this.headers.key(caption);
		var items = [];
		var cells = [];
		
		for(var row_id = 0, row_len = $this.cells.length; row_id < row_len; ++row_id){
			for(var col_id = 0, col_len = $this.cells[row_id].length; col_id < col_len; ++col_id){
				if(!isset(cells[row_id])) cells[row_id] = [];
				cells[row_id][col_id] = {};
				cells[row_id][col_id]['caption'] = $this.cells[row_id][col_id].innerHTML;
				cells[row_id][col_id]['onclick'] = $this.cells[row_id][col_id].parentNode.onclick;
				cells[row_id][col_id]['onmouseover'] = $this.cells[row_id][col_id].onmouseover;
			}
		}
		for(var row_id = 0, row_len = $this.cells.length; row_id < row_len; ++row_id){
			items[row_id] = {
				'caption' : $this.cells[row_id][column_id].innerHTML,
				'id' : row_id,
				'obj' : $this.cells[row_id][column_id]
			};
		}
		var sort_str = {};
		var sort_int = [];
		for(var item_id = 0, item_len = items.length; item_id < item_len; ++item_id){
			var $item = items[item_id];
			if(preg_match(/([0-9]{1,2})-([0-9]{1,2})-([0-9]{2,4})/, $item.caption)){
				var parts = $item.caption.split("-");
				for(var part = 0, part_len = parts.length; part < part_len; ++part){
					parts[part] = str_repeat("0", 2-parts[part].length)+parts[part];
				}
				$item.caption = new String(parts[2]+parts[1]+parts[0]);
			}
			var key = new String($item.caption+"$"+item_id);
			sort_int[item_id] = key.toLowerCase();
			sort_str[key.toLowerCase()] = item_id;
		}
		sort_int.sort();
		for(var row_id = 0, row_len = sort_int.length; row_id < row_len; ++row_id){
			$this.cells[row_id][0].parentNode.onclick = cells[sort_str[sort_int[row_id]]][0]['onclick'];
			for(var col_id = 0, col_len = cells[row_id].length; col_id < col_len; ++col_id){
				$this.cells[row_id][col_id].innerHTML = cells[sort_str[sort_int[row_id]]][col_id]['caption'];
				$this.cells[row_id][col_id].onmouseover = cells[sort_str[sort_int[row_id]]][col_id]['onmouseover'];
			}
		}
	}
	var rows = obj.getElementsBySelector("tr");
	for(var row_id = 0, row_len = rows.length; row_id < row_len; ++row_id){
		var row = rows[row_id];
		var cells = row.getElementsBySelector("td");
		for(var cell_id = 0, cell_len = cells.length; cell_id < cell_len; ++cell_id){
			var cell = cells[cell_id];
			if(row_id == 0){
				headers[headers.length] = cell.innerHTML;
				cell.onclick = this.orderColumns;
			}
			else{
				__cells[row_id-1] = isset(__cells[row_id-1]) ? __cells[row_id-1] : __cells[row_id-1] = [];
				__cells[row_id-1][cell_id] = cell;
			}
		}
	}
	this.headers = headers;
	this.cells = __cells;
}

$optiongrids = $("content").getElementsBySelector("[class=\"optiongrid\"]");
var objOptionGrids = [];
$optiongrids.each(function($optiongrid, $optiongrid_id){
	objOptionGrids[$optiongrid_id] = new OptionGrid($optiongrid, $optiongrid_id);
});

function Menu(id, vname){
	this.obj = $(id);
	this.vname = vname;
	this._log = "";
	this.output = "";
	this.menus = 0;
	this.zi = 1000000;
	this.hiding = [];
	this.item_levels = [];
	this.indexItems = function(obj, vertical, level){
		level++;
		this.item_levels[this.obj.id+'_'+this.menus] = level;
		this.output += '<table'+(vertical ? ' onmouseover="'+this.vname+'.show(\''+this.obj.id+'_'+this.menus+'\', false);" onmouseout="'+this.vname+'.hide(\''+this.obj.id+'_'+this.menus+'\');"' : '')+' id="'+this.obj.id+'_'+this.menus+'" cellpadding="0" cellspacing="0"'+(vertical ? (level > 2 ? ' class="right_hidden"' : ' class="hidden"') : '')+'>';
		if(!vertical) this.output += '<tr>';
		var items = {};
		var num = 0;
		var $subnodes = obj.childNodes;
		for(var sn = 0, snlen = $subnodes.length; sn < snlen; ++sn){
			var subnode = $subnodes[sn];
			if(subnode.nodeName == "LI"){
				if(vertical) this.output += '<tr>';
				this.output += '<td>';
				var $innernodes = subnode.childNodes;
				for(var inn = 0, innlen = $innernodes.length; inn < innlen; ++inn){
					var innernode = $innernodes[inn];
					var href = false;
					var menu = false;
					switch(innernode.nodeName){
						case "A":
							this.output += '<a'+(innlen > 1 ? ' class="children"' : '')+' onmouseover="'+this.vname+'.show(\''+this.obj.id+'_'+(this.menus+1)+'\', true);" onmouseout="'+this.vname+'.hide(\''+this.obj.id+'_'+(this.menus+1)+'\');" href="'+innernode.href+'">'+innernode.innerHTML+'</a>';
							this.menus++;
						break;
						case "UL":
							this.indexItems(innernode, true, level);
						break;
					}
				}
				this.output += '</td>';
				if(vertical) this.output += '</tr>';
				if(vertical && sn < (snlen-2)){
					this.output += '<tr>';
					this.output += '<td class="dottedline"><div>&nbsp;</div></td>';
					this.output += '</tr>';
				}
			}
		}
		if(!vertical) this.output += '</tr>';
		if(vertical){
			this.output += '<tr>';
			this.output += '<td class="footer">&nbsp;</td>';
			this.output += '</tr>';
		}
		this.output += '</table>';
	}
	this.show = function(id, fromnil){
		if(document.getElementById(id) != null){
			clevel = typeof(this.item_levels[id]) != "undefined" ? this.item_levels[id] : 0;
			clearTimeout(this.hiding[id]);
			if($(id).className == "hidden" || $(id).className == "right_hidden"){
				if(fromnil) changeOpac(0, id);
				this.zi++;
				if(clevel > 2){
					$(id).className = "right_vertical";
				}
				else{
					$(id).className = "vertical";
				}
				$(id).setStyle({ zIndex : this.zi });
				currentOpac(id, 100, 0, 500);
			}
		}
	}
	this.hide = function(id){
		if(document.getElementById(id) != null){
			clevel = typeof(this.item_levels[id]) != "undefined" ? this.item_levels[id] : 0;
			this.hiding[id] = setTimeout(function(){
				currentOpac(id, 0, 100, 200);
				setTimeout(function(){
					if(clevel > 2){
						$(id).className = "right_hidden";
					}
					else{
						$(id).className = "hidden";
					}
				}, 200);
			}, 1000);
		}
	}
	this.startHiding = function(){
		
	}
	this.parse = function(){
		var $nodes = this.obj.childNodes;
		var items = {};
		for(var n = 0, nlen = $nodes.length; n < nlen; ++n){
			var node = $nodes[n];
			if(node.nodeName == "UL"){
				this.indexItems(node, false, 0);
			}
		}
		this.obj.innerHTML = "";
		this.obj.style.display = "block";
		this.obj.innerHTML = this.output;
	}
}


var mainmenu = new Menu("menu", "mainmenu");
mainmenu.parse();


function initiate_selectdate(){
	$_selectdate = $("content").getElementsBySelector("[class=\"selectdate\"]");
	$_selectdate.each(function($selectdate){
		var $_names = $selectdate.id.split("_");
		$names = [];
		$_names.each(function($_name, $name_id){
			if($name_id > 0) $names[$name_id] = $_name;
		});
		$name = $names.join("_").substr(1);
		$selectdate.callName = $name;
		changeOpac(0, "selectdatecalender_"+$selectdate.callName);
		$selectdate.active = false;
		new Ajax.Request("index.php?com=form&sub=sa_selectdate&mode=standalone&name="+$selectdate.callName+"&date="+$("selectdateinput_"+$selectdate.callName).value, {
			onSuccess:function(transport){
				$("selectdateinnercalender_"+$selectdate.callName).update(transport.responseText);
			}
		});
		$selectdate.onclick = function(){
			if($selectdate.active == false){
				$selectdate.active = true;
				$("selectdatecalender_"+$selectdate.callName).setStyle({ display : 'block' });
				currentOpac("selectdatecalender_"+$selectdate.callName, 100, 0, 500);
			}
			else{
				$selectdate.active = false;
				currentOpac("selectdatecalender_"+$selectdate.callName, 0, 100, 500);
				setTimeout(function(){
					$("selectdatecalender_"+$selectdate.callName).setStyle({ display : 'none' });
				}, 500);
			}
		};
	});
}
initiate_selectdate();
function set_selectdate(name, date){
	var dates = date.split("-");
	$("selectdate_"+name).update(date);
	$("selectdateinput_"+name).value = dates[2]+"-"+dates[1]+"-"+dates[0];
	$("selectdate_"+name).active = false;
	currentOpac("selectdatecalender_"+name, 0, 100, 500);
	setTimeout(function(){
		$("selectdatecalender_"+name).setStyle({ display : 'none' });
	}, 500);
}
function changeMonth(name, dir){
	var date = $(name).value;
	var $_names = name.split("_");
	$names = [];
	$_names.each(function($_name, $name_id){
		if($name_id > 0) $names[$name_id] = $_name;
	});
	var $name = $names.join("_").substr(1);
	$_date = date.split("-");
	$year = $_date[0];
	$month = $_date[1];
	$day = $_date[2];
	if(dir == "next"){
		if($month == 12){
			$month = 1;
			$year++;
		}
		else $month++;
	}
	else{
		if($month == 1){
			$month = 12;
			$year--;
		}
		else $month--;
	}
	$month = new String($month);
	if($month.length < 2) $month = "0"+$month;
	$(name).value = $year+"-"+$month+"-"+$day;
	new Ajax.Request("index.php?com=form&sub=sa_selectdate&mode=standalone&name="+$name+"&date="+$(name).value, {
		onSuccess:function(transport){
			$("selectdateinnercalender_"+$name).update(transport.responseText);
		}
	});
}
function mktime(hours, minutes, seconds, month, year, day){
	var date = new Date();
	date.setFullYear(year, month, day);
	return Math.ceil(date.getTime()/1000);
}


function timeselect(){
	this.mouseover = function(){
		$(this.id).setStyle({
			backgroundColor : '#006699',
			color : '#ECEDF2'
		});
	};
	this.mouseout = function(){
		if(this.selected){
			$(this.id).setStyle({
				backgroundColor : '#006699',
				color : '#ECEDF2'
			});
		}
		else{
			$(this.id).setStyle({
				backgroundColor : this.std_backgroundColor,
				color : '#006699'			
			});
		}
	};
	this.click = function(){
		var id = this.id;id = id.split("_");id = id[1];
		var $time = this.id.split("_");
		var time = $time[1];
		var quart = $time[3];
		var $id = this.parentNode.parentNode.parentNode.id;$id = $id.split("_");$id = $id[1];
		var celltype = this.parentNode.id.substr(0, 15) != "timeselecthours" ? "quarter" : "hour";
		var time_cells = $("timeselecttable_"+$id).getElementsByTagName("td");
		if(this.selected){
			this.selected = selected = false;
			this.setStyle({
				backgroundColor : this.std_backgroundColor,
				color : '#006699'
			});
			if(celltype == "hour"){
				for(var i = 0; i <= 3; i++){
					var cc = document.getElementById(this.id+"_"+i);
					cc.selected = false;
					cc.style.backgroundColor = this.std_backgroundColor;
					cc.style.color = "#006699";
				}
			}
			else{
				var hour_id = this.id;hour_id = hour_id.split("_");hour_id = hour_id[0]+"_"+hour_id[1]+"_"+hour_id[2];
				time_cells[hour_id].selected = false;
				time_cells[hour_id].style.backgroundColor = this.std_backgroundColor;
				time_cells[hour_id].style.color = "#006699";
			}
		}
		else{
			this.selected = selected = true;
			this.setStyle({
				backgroundColor : '#006699',
				color : '#ECEDF2'
			});
			if(celltype == "hour"){
				for(var i = 0; i <= 3; i++){
					var cc = document.getElementById(this.id+"_"+i);
					cc.selected = true;
					cc.style.backgroundColor = "#006699";
					cc.style.color = "#ECEDF2";
				}
			}
		}
		var count = 0;
		var quarters = "";
		for(var index = 0, len = time_cells.length; index < len; ++index){
			if(time_cells[index].parentNode.id.substr(0, 18) == "timeselectquarters" && time_cells[index].selected){
				count += 15;
				var cCells = time_cells[index].id.split("_");
				var cCellHour = cCells[1];
				var cCellQuarter = cCells[3];
				quarters += ","+cCellHour+";"+cCellQuarter;
			}
		}
		var hours = new String(Math.floor(count/60));
		var minutes = new String(((count/60)-Math.floor(count/60))*60);
		hours = hours.length < 2 ? "0"+hours : hours;
		minutes = minutes.length < 2 ? "0"+minutes : minutes;
		$("timeselect_"+$id).update(hours+":"+minutes);
		var $items = $("content").getElementsBySelector("[name=\""+$id+"\"]");
		$items[0].value = quarters.substr(1);
	};
}

//$

var timeselect = new timeselect();
$_timeselect = $("content").getElementsBySelector("[class=\"timeselect\"]");

function closetimeselect(name){
	$("timeselect_"+name).active = false;
	//obj.active = false;
	currentOpac("timeselectdisplay_"+name, 0, 100, 500);
	setTimeout(function(){
		$("timeselectdisplay_"+name).setStyle({ display : 'none' });
	}, 500);
}

$_timeselect.each(function($timeselect){
	var $_names = $timeselect.id.split("_");
	$names = [];
	$_names.each(function($_name, $name_id){
		if($name_id > 0) $names[$name_id] = $_name;
	});
	$name = $names.join("_").substr(1);
	$timeselect.callName = $name;
	
	changeOpac(0, "timeselectdisplay_"+$timeselect.callName);
	$timeselect.active = false;
	$timeselect.onclick = function(){
		if($timeselect.active == false){
			$timeselect.active = true;
			$("timeselectdisplay_"+$timeselect.callName).setStyle({ display : 'block' });
			currentOpac("timeselectdisplay_"+$timeselect.callName, 100, 0, 500);
		}
		else{
			$timeselect.active = false;
			currentOpac("timeselectdisplay_"+$timeselect.callName, 0, 100, 500);
			setTimeout(function(){
				$("timeselectdisplay_"+$timeselect.callName).setStyle({ display : 'none' });
			}, 500);
		}
	};
	var time_cells = $("timeselecttable_"+$timeselect.callName).getElementsByTagName("td");
	for(var index = 0, len = time_cells.length; index < len; ++index){
		if(time_cells[index].parentNode.id.substr(0, 13) != "timeselectsep"){
			//time_cells[index].id = "timeselectcell_"+index;
			
			var $time = time_cells[index].id.split("_");
			var time = $time[1];
			var quart = $time[3];
			
			time_cells[index].std_backgroundColor = "#ECEDF2";
			time_cells[index].selected = time_cells[index].scope != 1 ? false : true;
			time_cells[index].onmouseover = timeselect.mouseover;
			time_cells[index].onmouseout = timeselect.mouseout;
			time_cells[index].onclick = timeselect.click;

			if(time > 8 && time <= 17 && (time < 17 || quart < 2)){
				time_cells[index].std_backgroundColor = "#d9dbe6";
				time_cells[index].setStyle({
					backgroundColor : '#d9dbe6'
				});
			}
			if(time_cells[index].scope == "1"){
				time_cells[index].setStyle({
					backgroundColor : '#006699',
					color : '#ECEDF2'
				});
			}
			else{
				switch(time_cells[index].parentNode.id.substr(0, 15)){
					case "timeselecthours":
						time_cells[index].setStyle({
							height : "14px"
							
						});
					break;
					default:
						time_cells[index].setStyle({
							fontSize : "6pt",
							fontFamily : "Arial",
							height : "14px",
							width : "14px"
							
						});
					break;
				}
			}
			//alert(ss);
			//time_cells[index].onload = timeselect.click;
			
		}
		else{
			time_cells[index].style.backgroundColor = "#ffffff";
			time_cells[index].style.cursor = "default";
			time_cells[index].style.height = "2px";
			time_cells[index].style.fontSize = "1px";
		}
	}
});


