jQuery(function($) {
    $('ul.mainmenu li').ahover();
	submenu_now();
});
function sound() {
	document.getElementById('bgsound').innerHTML = '<embed src="inc/captcha/securimage_play.php" width="0" height="0" autostart="true" loop="false" />';
}
function refresh(){
	document.getElementById('secimage').src = 'inc/captcha/securimage_show.php?sid=' + Math.random();
}
function getCookie(name){
 if (document.cookie.length > 0){
  start=document.cookie.indexOf(name + "=");
  if (start != -1){
   start=start + name.length+1;
   end=document.cookie.indexOf(";",start);
   if (end == -1) end=document.cookie.length;
   return unescape(document.cookie.substring(start,end));
  }
 }
 return "";
}
function setCookie(c_name,value,expiredays){
 var exdate=new Date();
 exdate.setDate(exdate.getDate()+expiredays);
 document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
}
function generate_menu(){
	var length_m = menu.length;
	var length_s = subm.length;
	var menu_new = "";
	for (i=0;i<length_m;i++){
		if(subm[i][0] == '1') onclk="onClick=go(\'"+subm[i][1]+"\');"; else onclk="onClick=submenu('"+i+"')";
		menu_new = menu_new+"<li id='menu"+i+"'"+onclk+"><a><b>"+menu[i]+"</b></a></li>";
		document.getElementById('mainmenu').innerHTML = menu_new;
	}
}
function submenu(id){
	if(!id) id = 0;
	var data 	= id;
	var subm_new= "";
	var less	= Math.floor(subm[id].length/2);
	setCookie('menu_info',data,365);
	for (i=0;i<less;i++){
		if(subm[id][0] == '1') ii = 2*i+1; else ii = 2*i;
		if(i == less-1) end = ""; else end = " | ";
		subm_new = subm_new+"<a class='submenuitem' href="+subm[id][ii+1]+">"+subm[id][ii]+"</a>"+end;
		document.getElementById('submenuitems').innerHTML = subm_new;
	}
	document.getElementById('submenuitems').style.padding='5px';
	for (i=0; i<menu.length; i++) {
		document.getElementById('menu'+i).style.background='';
	}
	document.getElementById('menu'+id).style.background='url(\'/template/hvn/img/50proc.png\')';
}
function submenu_now(){
	var data;
	data = getCookie('menu_info');
	submenu(data);
}
function go(page,title_new,method,value){
	document.location.href=page;
}
var TICKER = {
	options: {
		'nat1':{
			ALARM_MIN: 30,
			END_HOUR: 13,
			END_MIN: 0,
			END_SEC: 0,
			START_HOUR: 12,
			START_MIN: 00,
			START_SEC: 0,
			INGAME_MSG: "",
			WARNING_SIZE: 16,
			START_MSG: 'now!',
			UNITS: 4,
			GMT: +2,
			msgID: 'msg_nw'
		},
        'nat2':{
			ALARM_MIN: 30,
			END_HOUR: 21,
			END_MIN: 0,
			END_SEC: 0,
			START_HOUR: 20,
			START_MIN: 0,
			START_SEC: 0,
			INGAME_MSG: "",
			WARNING_SIZE: 16,
			START_MSG: 'now!',
			UNITS: 4,
			GMT: +2,
			msgID: 'msg_nw'
		}
	},
	addOption: function(o,obj){
		this.options[o] = obj;
		for (var i in this.options['default']){
			if (this.options[o][i.toString()] == undefined) {this.options[o][i.toString()] = this.options['default'][i.toString()];}
		}

	},
	printMSG: function(o,msg){
		$("#"+this.options[o].msgID).html(msg);
	},
	countDown: function(){
        var dnow = new Date();
        dnow = new Date(dnow.getTime());
		var currHour = dnow.getHours();
		var currMin = dnow.getMinutes();
		var currSec = dnow.getSeconds();
		var currMill = dnow.getMilliseconds() % 100;


        if(currHour*60+currMin <= this.options['nat1'].END_HOUR*60){
            o = 'nat1';
        } else if(currHour*60+currMin > this.options['nat1'].END_HOUR*60 && currHour*60+currMin <= this.options['nat2'].END_HOUR*60){
            o = 'nat2';
        } else if(currHour*60+currMin > this.options['nat2'].END_HOUR*60){
            o = 'nat1';
        }
        var option = this.options[o];
        var targetDate;
		var diffHour = -1;
		var diffMin = -1;
		var diffSec = -1;
		var diffMill = -1;



		var time = 100; //used for calling this function time milliseconds later.. used in last line of this function
		//1. ENDTIME <-> (STARTTIME - ALARM_MIN) : just tick away..

		if(currHour >= option.END_HOUR && currHour < 23 && currMin > 0 && currMin > option.END_MIN)
		{	targetDate = new Date(dnow.getFullYear(), dnow.getMonth(), dnow.getDate()+1, option.START_HOUR, option.START_MIN, option.START_SEC);}
		else
		{	targetDate = new Date(dnow.getFullYear(), dnow.getMonth(), dnow.getDate(), option.START_HOUR, option.START_MIN, option.START_SEC); }

		var diffDate = targetDate.getTime() - dnow.getTime();

		diffHour = Math.floor((parseInt(diffDate) / (1000*60*60))%24);
		diffMin = Math.floor((parseInt(diffDate) / (1000*60))%60);
		diffSec = Math.floor((parseInt(diffDate) / 1000)%60);
		diffMill = Math.floor((parseInt(diffDate) % 1000) / 11);
		var diffTime =
		    ((diffHour < 10) ? "0" : "") + diffHour + ":"
		    + ((diffMin < 10) ? "0" : "") + diffMin + ":"
		    + ((diffSec < 10) ? "0" : "") + diffSec + ":"
		    + ((diffMill < 10) ? "0" :"") + diffMill;

		    switch(option.UNITS){//filter out display by UNITS - hr:min:sec:millisec, AND reduce resource usage by setting revoke interval(time) via UNITS
		    case 3:
			    diffTime = diffTime.substr(0,diffTime.length-3);
			    time = 1000;//1 seconds
			    break;
		    case 2:
			    diffTime = diffTime.substr(0,diffTime.length-6);
			    time = 60000;//1 minute
			    break;
		    case 1:
			    diffTime = diffTime.substr(0,diffTime.length-9);
			    break;
		    default:
			    break;
		    }

		if (diffDate < option.ALARM_MIN * 1000 * 60 && diffDate > 0)//if difference of time is within alarm range!! BLINK BLINK !!
		{
			time = 100;
			if (dnow.getMilliseconds() % 500 > 250) {$("#"+option.msgID).css("fontSize", (option.WARNING_SIZE+2).toString() + 'px');}
				else {$("#"+option.msgID).css("fontSize", (option.WARNING_SIZE).toString() + 'px');}
		}
		else if (diffDate < 0)
		{	diffTime = option.START_MSG;}

		if (option.END_HOUR < option.START_HOUR) {diffTime = "INVALID END_HOUR";}
		else if (option.END_HOUR == option.START_HOUR && option.END_MIN <= option.START_MIN) {diffTime = "INVALID END_MIN";}

		this.printMSG(o, diffTime);

		setTimeout("TICKER.countDown('"+o+"')",time);
	}
};
