/**
 * $Id$
 */
function select_idx(id, index) {
	var obj = document.getElementById(id);
	if (obj)
		obj.selectedIndex = index;
	else
		log("sorry but the tag: " + id + " could not be found =/");

}
function select_val(id, val) {
	var obj = document.getElementById(id);
	var opt = null;
	var i = 0;
	if (obj)
		for (i = 0; i < obj.options.length; i++) {
			if (obj.options[i].value == ("" + val))
				obj.options[i].selected = true;
		}
	else
		log("sorry but the tag: " + id + " could not be found =/");
}
// countdown
var count_handles = new Array();
var count_offset = new Array();
var count_duration = new Array();
var count_to = 0;
var count_numHandles = 0;
function countdown(id, duration) {
	if (count_to == 0) {
		count_to = setInterval("refreshTimes()", 100);
	}
	var handle = document.getElementById(id);
	if (handle) {
		var now = new Date();
		count_handles[count_numHandles] = handle;
		count_offset[count_numHandles] = now.getTime();
		count_duration[count_numHandles] = duration;// parseInt(handle.innerHTML,
													// 0);
		count_numHandles++;
		refreshTimes();
	}
}
function refreshTimes() {
	for ( var i = 0; i < count_numHandles; i++) {
		var now = new Date();
		tmp = Math.floor(count_duration[i]
				- ((now.getTime() - count_offset[i]) / 1000));
		if ((tmp <= 0) && (tmp > -2)) {
			window.location.reload(true);
		}
		if (tmp < -2) {
			count_handles[i].innerHTML = "-";
		} else {
			count_handles[i].innerHTML = formatDuration(tmp);
		}
	}
}

function formatDuration(seconds) {
	var sec = seconds % 60;
	var min = ((seconds - sec) / 60) % 60;
	var hours = ((seconds - sec - (min * 60)) / 3600) % 24;
	var days = ((seconds - sec - (min * 60) - (hours * 3600))) / 86400;
	return (days > 0 ? days + "T " : "") + (hours < 10 ? "0" : "") + hours
			+ ":" + (min < 10 ? "0" : "") + min + ":" + (sec < 10 ? "0" : "")
			+ sec;
}

function calcSpeed(ele, secs) {
	if (secs != '' && secs != 0	&& document.getElementById('army_attack_arrival'))
		document.getElementById('army_attack_arrival').innerHTML = formatDuration(Math.floor(secs / ele[ele.selectedIndex].value));
}


/* log to Firebug console if available */
function log(msg) {
	$.log.apply(this, arguments);
}

/* Show or Hide msg fav bar */
function favbar(name) {
	var name2 = 'link' + name;
	if (document.getElementsByName(name)[0].style.display != 'none')
	{
		document.getElementsByName(name)[0].style.display = 'none';
		document.getElementsByName(name2)[0].innerHTML = 'Favoriten einblenden';
	}
	else {
	document.getElementsByName(name2)[0].innerHTML = 'Favoriten ausblenden';
	document.getElementsByName(name)[0].style.display = '';
	}
}
/* show hide a element by name */
/* deprecated use $(selection).hide(); */
function hideElement(name) {
	if (document.getElementsByName(name)[0].style.display != 'none') {
		document.getElementsByName(name)[0].style.display = 'none';
	} else {
		document.getElementsByName(name)[0].style.display = '';
	}
}

/* Insert reciver in msg Form */

function insert_rec(name, empf) {
	document.getElementsByName(name)[0].value += empf + ',';
}


