var app = {
	site: {uri:null},
	onload: function() {
		var bgchanger = new headerimgbgchanger();
		var filmslider = new bgslider('filmstreifenani', 68 * 78, 900, 1);
		var sponsorslider = new bgslider('sponsorsani', 11 * 303, 909, -1);

		if(abCrosshair != null){
			abCrosshair.init();
		}
		if(location.href.indexOf('fan_voting.html') != -1 && location.href.indexOf('country=') != -1) {
			var links = document.getElementsByTagName('A');
			for(i = 0; i < links.length; i++) {
				if(links[i].className.indexOf('lnshoot') == -1 || links[i].href.indexOf('javascript') != -1) {
					continue;
				}

				var lnk = links[i].href.replace(/&amp;/ig, '&').replace(/#.*$/i, '');
				links[i].href = 'javascript:void(0);';
				lnk = lnk.replace(/&shoot=[1-5]/, '');

				links[i].lnk = lnk +='&action=vote';

				var clickfunc = function(e){
					if(e == null) {e = window.event;}
					var el = (e.target) ? e.target : e.srcElement;
					app.voting.vote(el.lnk);
				}
				observe(links[i], 'click', clickfunc);
			}
		}
	},

	ajax: {
		submit : function(uri, onreadystatechange) {
			var xmlhttp = new XMLHttpRequest();
			uri += (uri.match(/\?/) ? '&' : '?') + 'nocache=' + (Math.random());

			if(typeof onreadystatechange == "function") {
				var callback = {
					process: function() {
						if (xmlhttp.readyState == 4) {
							if (xmlhttp.status == 200) {
							  onreadystatechange(xmlhttp);
							}
						}
					}
				};
				xmlhttp.onreadystatechange = callback.process;
			}
		    xmlhttp.open('GET', uri, true);
		    xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		    xmlhttp.send();

		    return true;
		}
	},

	voting: {
		vote: function(lnk) {
			document.getElementById("ssound").innerHTML="<embed src=\"/images/schuss.wav\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
			app.ajax.submit(lnk, app.voting.shoot);
		},
		shoot: function(xmlhttp) {
			var res = xmlhttp.responseText;
			if(res.match(/^[0-9]+,[1-5],[1-5]$/)) {
				var v = res.split(',');
				var el=document.getElementById('target'+v[0]+'_'+v[1]);
				el.style.backgroundPosition='left bottom';
			} else if(res.match(/\.html/)) {
				location.href = res;
			} else {
				alert("Fehler: " +res);
			}
		}

	}
}
function construct() {
	app.onload();
}


function observe(obj, type, fn) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, true );
	} else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = fn;
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function stopObserve(obj, type, fn) {
	if (obj.removeEventListener) {
		obj.removeEventListener( type, fn, false );
	} else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
