icon_list_closed= new Image();
icon_list_closed.src = "/images/icon_list_closed.gif";
icon_list_open = new Image();
icon_list_open.src = "/images/icon_list_open.gif";

function get_xmlhttp_request() {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, IE 7.0
		http_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // IE 6.0
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
			}
		}
    }
	if (http_request)
		http_request.onreadystatechange = function() { rec_http_events(http_request); };
		
	return http_request;
}

function req_http_events(thedate) {
	url = "/js/ajax_day_events.php?date="+thedate;
	if (!document.http_request)
		document.http_request = get_xmlhttp_request();
	document.http_request.open( "GET", url, true );
	document.http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=ISO-8859-1");
	document.http_request.send('');
}

function rec_http_events(http_request) {
	if (http_request.readyState==4) {
		try {
			status = http_request.status;
		}
		catch (e) {
		}
		if (status && status == 200) {
			content = http_request.responseText.split("\n");
			lines=0;
			for(n=0; n<content.length; n++) {
				if (content[n] != "\n") {
				//	alert(content[n]);
					day = content[n].split("\t");
					if (day.length != 1) {
					if (!document.events[day[0]])
						document.events[day[0]] = Array();
					document.events[day[0]][lines] = [day[1], day[2]];
					var contdiv = document.getElementById("calendar_mover_cont");
					contdiv.innerHTML = "<h4>"+day[0]+"</h4>";
					contdiv.innerHTML += "<p>"+document.events[day[0]][lines][0]+"<br />"+document.events[day[0]][lines][1]+"</p>";
					lines++;
					}
					else {
					if (!document.events[day])
						document.events[day] = Array();
					}
				}
				if (lines>0) {
					var infodiv = document.getElementById("calendar_mover");
					infodiv.style.display = "block";
				}				
			}
		}
	}
}

function init_page() {
	hide_matches();
	init_latest_clicks();
	init_sponsors();
	document.http_request = get_xmlhttp_request();
}

	
function showhideobj(objname) {
	obj = document.getElementById(objname);
	if (obj) {
		obj.style.display = (obj.style.display == 'none') ? 'block' : 'none';
	}
}

function showhidelist(objname) {
	obj = document.getElementById(objname);
	if (obj)	{
		if (obj.style.display == "none") {
			obj.style.display = "block";
			
			document.images [objname+"_icon"].src = icon_list_open.src;
		}
		else {
			obj.style.display = "none";
			document.images [objname+"_icon"].src = icon_list_closed.src;
		}
	}
}

function mouseX(evt) {
	if (!evt) evt = window.event;
	if (evt.pageX)
		return evt.pageX;
	else if (evt.clientX)
		return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	else return 0;
}

function mouseY(evt) {
	if (!evt) evt = window.event;
	if (evt.pageY)
		return evt.pageY;
	else if (evt.clientY)
		return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else return 0;
	}

function follow(evt) {
	if (document.getElementById) {
		var obj = document.getElementById("calendar_mover").style;
		obj.left = (parseInt(mouseX(evt))+15) + 'px';
		obj.top = (parseInt(mouseY(evt))+5) + 'px';
		}
	}

function displaytoday(thedate) {
	var infodiv = document.getElementById("calendar_mover");
	var contdiv = document.getElementById("calendar_mover_cont");
	if (events && events[thedate] && events[thedate].length) {
		document.onmousemove = follow;
		contdiv.innerHTML = "";
	//	contdiv.innerHTML = "<h4>"+thedate+"</h4>";
		for (var n=0; n<events[thedate].length; n++) {
			contdiv.innerHTML += "<p>"+events[thedate][n][0]+"<br/>"+events[thedate][n][1]+"</p>";
		}
		infodiv.style.visibility = "visible";
	}
	/* Ajax:
	if (!document.events)
		document.events = Array();
	if (document.events[thedate]) {
		contdiv.innerHTML = "";
		contdiv.innerHTML = "<h4>"+thedate+"</h4>";
		for(n=0; n<document.events[thedate].length; n++)
			contdiv.innerHTML += "<p>"+document.events[thedate][n][0]+"<br />"+document.events[thedate][n][1]+"</p>";
		infodiv.style.display = "block";
	}
	else {
		req_http_events(thedate);
	}	
	*/
}

function hidetoday() {
	document.onmousemove = null;
	var infodiv = document.getElementById('calendar_mover');
	infodiv.style.visibility = "hidden";
}

function hide_matches() {
	lists = document.getElementsByTagName('dl');
	for (var n=0; n<lists.length; n++) {
		id = lists[n].id;
		if (id.match(/^series_/)) {
			lists[id].style.display = "none";
		}
	}
}
	
function show_dropdown(list_name) {
	obj = document.getElementById(list_name);
	if (obj) {
		obj.style.visibility = 'visible';					
	}
}

function hide_dropdown(list_name) {
	obj = document.getElementById(list_name);
	if (obj) {
		obj.style.visibility = 'hidden';					
	}
}


function init_latest_clicks() {
	tags = document.getElementsByTagName('a');
	if (tags) {
		for (n=0; n<tags.length; n++) {
			if (tags[n].className) {
				if (tags[n].className.match("latest_click"))
					tags[n].onclick = function() {
						return true;
					};
			}
		}
	}
}

function init_sponsors() {
	for(n=1; n<=4; n++) {
		if (sponsors[n] && sponsors[n].length>0) {
			sgroup = sponsors[n];
			pos_1_0 = Math.floor(Math.random()*(sgroup.length));
			set_sponsor("sponsor_"+n+"_0", sgroup[pos_1_0]);			
			if (n<3 && sgroup.length>1) {
				pos_1_1 = null;
				while(pos_1_1 == null || pos_1_1==pos_1_0) {
					pos_1_1=Math.floor(Math.random()*(sgroup.length));
				}
				set_sponsor("sponsor_"+n+"_1", sgroup[pos_1_1]);
			}
		}
	}
	setTimeout("init_sponsors()",5000);
}

function set_sponsor(holder, sponsor) {
			obj = document.getElementById(holder);
			if (obj) {

					img = document.createElement('img');
					img.setAttribute('src', '/images/sponsors/'+sponsor["image"]);
					img.setAttribute('alt', sponsor["name"]);

				while(obj.childNodes[0])
					obj.removeChild(obj.childNodes[0]);

				if (sponsor["url"] != "") {

					a = document.createElement('a');
					a.setAttribute('href', sponsor["url"]);
					a.setAttribute('target', '_blank');
					a.appendChild(img);
					obj.appendChild(a);
					
//					obj.innerHTML = "<a href=\""+spo["url"]+"\" target=\"_blank\"><img src=\"/images/sponsors/"+spo["image"]+"\" alt=\""+spo["name"]+"\" /></a>";
				}	
				else {
					obj.appendChild(img);
				}

				obj=null;
			}
}

function change_cup_form_origin(obj) {
	obj_origin = document.getElementById('ort_annan');
	if(obj_origin) {
		
		if (obj.options[obj.selectedIndex].text=='Annan...') {
			obj_origin.style.display = "inline";
			obj_origin.disabled = false;
		}
		else {
			obj_origin.style.display = "none"; 
			obj_origin.disabled = "disabled";			
		}
	}
}

