if(window.attachEvent) {
	var doh = true;
	if($ && parseInt($.browser.version) > 6) {
		doh = false;
	}
	if(doh) window.attachEvent("onload",ieHover);
	window.attachEvent("onload",init);
} else if(window.addEventListener) {
	window.addEventListener("load",init,false);
}

function init() {
	if(typeof($) == "undefined" ) return;
	if(typeof(app) == "undefined") app = {};
	for(var n in app) {
		var fn = window[n];
		if(typeof(fn) == "function") fn(app[n]);
	}
}

function map(d) {
	var holder = $(d.ident);
	var info = $(d.info);
	if(!holder || !info) return;
	
	holder.css("position","relative");

	app.map.img = $("#map_image")[0];
	app.map.inf = info;
	app.map.infoUp = false;
	app.map.iv = 0;
	
	info.css("backgroundColor","#ffffff");
	info.hover( 
		function() {
			app.map.cli();
			if(app.map.infoUp) app.map.tip.show();
		}, 
		function() {
			app.map.cli();
			app.map.iv = setTimeout(function() {
				app.map.tip.hide();
				app.map.inf.html("&nbsp;");
				app.map.infoUp = false;
			},1500);
		}
	);
	
	app.map.show = function(idx) {
		this.cli();
		this.tip.hide();
		this.inf.html("&nbsp;");
		var c = this.views[this.current];
		c.icon.src = c.icon.up;
		c.layer.hide();
		var v = this.views[idx];
		this.current = idx;
		v.icon.src = v.icon.over;
		v.layer.show();
		this.img.src = v.image;
		this.mapTitle.text(v.title);
	}
	app.map.cli = function() {
		if(this.iv > 0) clearTimeout(this.iv);
		this.iv = 0;
	}

	var div = document.createElement("div");
	div.id = "map_buttons";
	holder.append(div);
	var ov = $(div);
	ov.css({position:"absolute",top:190,left:10});
	
	//map views
	for(var i=0;i<d.views.length;i++) {
		var v = d.views[i];
		v.icon = new Image();
		v.icon.up = v.buttons[0];
		v.icon.over = v.buttons[1];
		v.icon.src = v.icon.up;
		v.icon.idx = i;
		ov.append(v.icon);
		var ic = $(v.icon);
		ic.css({display:"block",marginBottom:"10px",cursor:"pointer"});
		if(!v.online || !d.showButtons) ic.css({display:"none"});
		ic.hover(
			function() { this.src = this.over },
			function() { if(app.map.current != this.idx) this.src = this.up }
		);
		ic.click(function() { app.map.show(this.idx) });

		div = document.createElement("div");
		div.id = "locations_"+i;
		div.idx = i;
		holder.append(div);
		var ly = $(div);
		v.layer = ly;
		ly.css({position:"absolute",top:0,left:0});
		ly.hide();
		//Map-Points
		for(var j=0;j<v.locations.length;j++) {
			var loc = v.locations[j];
			var dv = document.createElement("div");
			dv.id = "loc_"+i+"_"+j;
			dv.data = loc;
			dv.selected = false;
			ly.append(dv);
			var dot = $(dv);
			dot.css({
				position:"absolute",
				width:8,
				height:8,
				left:loc.x,
				top:loc.y,
				cursor:"pointer",
				backgroundColor:"#c4112f"}
			);
			//show / hide on hover
			dot.hover(
				function() {
					app.map.cli();
					var d = this.data;
					var t = app.map.tip;
					t.html(d.name);
					t.css({top:d.y,left:d.x+15,border:"1px solid #ffffff"});
					t.show();
					app.map.inf.html("<h3>"+d.name+"<br>"+d.country+"</h3><div>"+d.info+"</div>");
					app.map.infoUp = true;
					},
				function() {
					app.map.cli();
					app.map.iv = setTimeout(
						function() {
							app.map.inf.html("&nbsp;");
							app.map.infoUp = false;
							app.map.tip.hide();
					},1500);
				}
			);
		}
	}
	div = document.createElement("div");
	div.id = "map_tip";
	holder.append(div);
	var tip = $(div);
	tip.css({
		position:"absolute",
		height:"14px",
		padding:"2px 4px 0px 4px",
		border:"1px solid #888888",
		lineHeight:"1em",
		backgroundColor:"#ffffff"});
	tip.hide();
	app.map.tip = tip;
	
	div = document.createElement("div");
	div.id = "map_title";
	div.className = "imageTag";
	holder.append(div);
	var t = $(div);
	app.map.mapTitle = t;

	app.map.show(d.current);

}
function rolloverImages(d) {
	var t = $(d.target);
	var img = new Image();
	var wait = new Image();
	
	wait.src = "template/layout/wait.gif";
	
	$(wait).css({position:"absolute",top:156,left:240});
	$(img).css({position:"absolute",top:0,left:0});
	
	t.prepend(img);
	t.prepend(wait);
	
	rolloverImages.timer = 0;
	
	rolloverImages.titleTag = $(d.target+" "+d.title);
	rolloverImages.stdTitle = rolloverImages.titleTag.text();
	rolloverImages.titleText = rolloverImages.stdTitle;
	
	rolloverImages.image = $(img);
	rolloverImages.wait = $(wait);
	
	rolloverImages.image.hide();
	rolloverImages.wait.hide();
	
	rolloverImages.image.load(function() {
		$(this).show();
		rolloverImages.wait.hide();
		rolloverImages.titleTag.html(rolloverImages.titleText);
	});
	
	$(d.ident).css("cursor","pointer").hover(function() {
		if(rolloverImages.timer > 0) {
			clearTimeout(rolloverImages.timer);
			rolloverImages.timer = 0;
		}
		rolloverImages.wait.show();
		rolloverImages.image.attr("src",null);
		rolloverImages.image.attr("src",this.name);
		var n = $(this).attr("rel");
		if(n=="") n = rolloverImages.stdTitle;
		rolloverImages.titleText = n;
	}, function() {
		rolloverImages.wait.hide();
		if(rolloverImages.timer > 0) clearTimeout(rolloverImages.timer);
		rolloverImages.timer = setTimeout( function() { 
			rolloverImages.image.hide();
			rolloverImages.titleTag.html(rolloverImages.stdTitle);
		},100);

	});
}
function contentPanes(d) {
	app.contentPanes.current = null;
	app.contentPanes.currentItem = null;
	var p = $(d.ident+":first");
	if(p.length < 1) return;
	app.contentPanes.current = p[0];

	$(d.link).each(function() {
		var p = this.href.split("#",2);
		var d = $("#"+p[1]);
		this.pane = d[0];
		this.pane.via = this;
		if(this.pane.id == app.contentPanes.current.id) {
			this.className = "selected";
		}
	}).click(function() {
		var c = app.contentPanes.current;
		$c = $(c);
		if(this.pane.id == c.id) return;
		c.via.className = "";
		$c.hide();
		$c.css("position","absolute");
		$(this.pane).show();
		$(this.pane).css("position","relative");
		//$(c).fadeOut("fast");
		//$(this.pane).fadeIn("fast");

		app.contentPanes.current = this.pane;
		this.className ="selected";
		this.pane.via = this;
		return false;
	});
}
function msieFlyout(d) {
	if($.browser.msie && d.ident) {
		$(d.ident+" ul").css(d.css);
	}
}
function externalLinks(d) {
	for(var i=0;i<d.length;i++) {
		var lnk = d[i];
		$("a[href*='article_id="+lnk.id+"']").each(function() {
			this.link = lnk;
		}).click(function() {
			var w = window.open(this.link.url,"_blank",this.link.param);
			if(w) w.focus();
			return false;
		});
	}
}

function pageOverlays(d) {
	var ps = $(d.ident);
	if(ps.length < 1) return;
	$("body").append("<div id='overlayWindow'><img id='overlayShadow' src='template/layout/dropshadow.png'><div id='overlayFrame'><div id='overlayCloseButton'>X</div><div id='overlayContent'><div id='overlayBody'>&nbsp;<div style='clear:both'></div></div><br style='clear:both;line-height:0px;font-size:0px;height:0px' /></div></div></div>");

	app.pageOverlays = {
		outer: $("#overlayWindow"),
		inner: $("#overlayFrame"),
		body: $("#overlayBody"),
		current: 0,
		hide:function() {
			this.outer.hide();
			this.current = "";
		},
		show:function(p,id,el) {
			if(this.current == id) return this.hide();
			var sz = __getPageSize();
			var w = p.width();
			var h = p.height();
			this.body.html(p.html());
			//fixed size for now
			var nw = 400;
			var nh = 400;
			var dw = 54;
			var dh = 54;
			this.outer.width(nw+dw);
			this.outer.height(nh+dh);
			this.inner.width(nw);
			this.inner.height(nh);
			this.inner.css({left:"17px",top:"17px"});
			//center
			var iw = Math.floor( (sz.pW-(nw+dw)) /2 );
		
			var tp = $(el.parentNode).offset();
			var y = tp.top-60;
			if(y+nh > sz.pH) y = sz.pH-nh-20;
		
			this.outer.css({left:iw,top:y});
			this.outer.show();

			this.current = id;
	
			$("body").one("click",null,function() { app.pageOverlays.hide() } );
		}

	}
	app.pageOverlays.hide();

	$("#overlayCloseButton").click(function() {
		app.pageOverlays.hide();
	});
	$(d.link).click(function() {
		var p = this.href.split("#",2);
		var h = "#"+p[1];
		var d = $(h);
		if(d) app.pageOverlays.show(d,p[1],this);
		return false;
	});
}
function newsTicker(d) {
	var c = $(d.ident);
	if(!c || d.text == "" || d.text == "off") return;
	var sp = d.speed;
	var st = d.step;
	if(st == undefined) st = -1;
	if(sp == undefined) sp = 59;

	c.show();
	c.append("<div id='ticktext'>"+d.text+"</div>");

	var tt = $("#ticktext");
	tt.css({position:"absolute",whiteSpace:"nowrap",paddingTop:"3px",cursor:"default"});
	var iw = Math.max(10,tt.width());

	var inner = tt[0];
	var ow = c.width();
	var nw = iw;
	while(nw <= ow) {
		inner.innerHTML += d.text;
		nw += iw;
	}
	inner.innerHTML += inner.innerHTML;
	inner.hw = (tt.width()/2)*(st > 0 ? 1 : -1);
	inner.step = st;
	app.newsTicker.element = inner;
	if(st > 0) inner.style.left = -(inner.hw)+"px";
		
	app.newsTicker.iv = setInterval(function() {
		var el = app.newsTicker.element;
		var st = el.step;
		var x = el.offsetLeft+st;
		if(st > 0) {
			if(x >= 0) x = -el.hw+st;
		} else {
			if(x <= el.hw) x = 0;
		}
		el.style.left = x+"px";
		},sp);
}
function ieHover() {
	if(!document.getElementById || !document.getElementsByTagName) return;
	var ul = document.getElementById('menu');
	if(!ul) return;
	var lis = ul.getElementsByTagName("li");
	for(var i=0;i<lis.length;i++) {
		var li = lis[i];
		if(li.className == "flyout") {
			var m = li.getElementsByTagName("ul")[0];
			li._menu = m;
			li.onmouseover = function() { 
				this._menu.style.display = "block";
				this.style.height = "21px";
			};
			li.onmouseout = function() { this._menu.style.display = "none" }
		}
	}
}

/* Size and Scroll-Calculations by quirksmode.com */
function __getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return {pW:pageWidth,pH:pageHeight,wW:windowWidth,wH:windowHeight};
}
function __getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		return {x:xScroll,y:yScroll};
}

function trace(s) {
	var dbg = document.getElementById("debug");
	if(!dbg) return;
	var o = s ? s.toString().replace(/</g,"&lt;") : '';
	if(s=='') dbg.innerHTML = s;
	else dbg.innerHTML += o+"<br>";
}
function dump(o) {
	if(typeof(o) == "object") {
		for(var n in o) {
			try {
				var e = o[n];
				if(typeof(e)=="function") trace("function "+n+"()");
				else trace(n+": "+e);
			} catch(err) {
				trace(n+": n.a.");
			}
		}
	} else {
		trace(o);
	}
	trace("---------------");
}
function clear() {
	trace('');
}
