var app=
{
	init:function()
	{
		if ( document.getElementById('ai') )
		{   
			xModalPopup.init();
			xModalPopup.fixCss('500', '500');
			
			
			var ul = document.getElementById('ai');
			var lis = ul.getElementsByTagName('a');
			for ( i = 0; i < lis.length; ++i )
			{
				lis[i].onclick = function(e){
					xModalPopup.clearBox();
					xModalPopup.box.onclick = function(){
						xModalPopup.hide();
						return false;
					}
					
					var img = new Image();
					img.src = this.href;
					xModalPopup.box.appendChild(img);
					xModalPopup.show();
					return false;
				}
				
			}
		}
		else if ( document.getElementById('tb') )
		{
			ajaxHelper.init(app.ajaxCompleter);
						
			xModalPopup.init();
			xModalPopup.fixCss('500', '500');
			
			
			var div = document.getElementById('tb');
			var as = div.getElementsByTagName('a');
			
			for ( i = 0; i < as.length; ++i )
			{
				as[i].onclick = app.thumbClick;
			}			
		}
		else if ( document.getElementById('tba') )
		{
				var div = document.getElementById('tba');
				var as = div.getElementsByTagName('a');
				
				for ( i = 0; i < as.length; ++i )
				{
					as[i].onclick = function(){
						window.open(this.href, "bild", "toolbar=1,menubar=1,status=1,scrollbars=1,resizable=yes,height=600width=800");
						return false;
					};
				}			
		}
		/*with ( document.getElementById('kontakt') )
		{
			var anchor = document.createElement('a');
			anchor.appendChild(document.createTextNode('kont'));
			anchor.appendChild(document.createTextNode('akt'));
			anchor.appendChild(document.createTextNode('@'));
			anchor.appendChild(document.createTextNode('roda'));
			anchor.appendChild(document.createTextNode('bonor'));
			anchor.appendChild(document.createTextNode('.se'));
			anchor.href = 'mailto:kontakt' + '@ro' + 'dabono' + 'r.se?subject=Kontakt via www.rodabonor.se';
			appendChild(anchor);

			style.position = "absolute";
			style.left = "5px";
		
		}*/
		if ( document.getElementById('new') )
		{
			var anchors = document.getElementById('new').getElementsByTagName('a');
			for ( i = 0; i < anchors.length ; ++i )
			{
				anchors[i].onclick = function(){
					window.open(this.href, "artikel");
					return false;
				}
			}
		}
	},
	ajaxCompleter:function(status, data)
	{
		if ( status != 0 )
		{
			xModalPopup.clearBox();
			
			var data = data.split('&//&');
						
			xModalPopup.fixCss(data[1], parseInt(data[2]) + 80);
			
			var file = data[3];
			
			data.reverse();
			data.pop();
			data.pop();
			data.pop();
			data.pop();
			
			var filename = new String(data);
			filename = new String(filename.match('file=[^"]+'));
            filename = filename.split('=');
            filename = filename[1];
			//alert(filename);
			
			//prompt("mojs",data);
			
			//tmp = "<div id=\"top_modal\"><a href=\"#\" onclick=\"return xModalPopup.hide();\">Close [X]</a></div>";
			var tmp = "<div id=\"top_modal\"> </div>";
			//var bigLink = "<a href=\"
			xModalPopup.box.innerHTML = tmp + data;
			xModalPopup.show();
		}
	/*	else
		{
			xModalPopup.clearBox();
			xModalPopup.show();
			xModalPopup.fixCss('250','250');
			xModalPopup.box.appendChild(document.createTextNode('Var god vänta medans bilden laddas'));
		}*/
		return false;
	},
    getTarget:function(e)
    {
        var target;
        if ( window != null && window.event ){
            target = window.event.srcElement;
        }
        else if (e){
            target = e.target;
        }
        else {
            target = null;
        }
        return target;
    },
    thumbClick:function(e)
    {
			xModalPopup.box.onclick = function(){
				xModalPopup.hide();
				return false;
			}
			var tmp = this.href.split('/');
			var arr = new Array();
			arr.push(tmp[tmp.length-1]);
			
			if ( typeof(window.innerWidth) == 'number' )
			{
				arr.push(window.innerWidth-16);
				arr.push(window.innerHeight-16);
			}
			else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
			{
				arr.push(document.documentElement.clientWidth);
				arr.push(document.documentElement.clientHeight);				
			}
			else
			{
				arr.push(document.body.clientWidth);
				arr.push(document.body.clientHeight);
			}
			
			ajaxHelper.doRequest(arr);
			return false;					
    }
}


var QuirksMode = 
{
    //findPos from quirksmode
    findPos:function(obj) {
	    var curleft = curtop = 0;
	    if (obj.offsetParent) {
		    curleft = obj.offsetLeft
		    curtop = obj.offsetTop
		    while (obj = obj.offsetParent) {
			    curleft += obj.offsetLeft
			    curtop += obj.offsetTop
		    }
	    }
	    return [curleft,curtop];
    },
    getTarget:function(e)
    {
        var target;
        if ( window != null && window.event ){
            target = window.event.srcElement;
        }
        else if (e){
            target = e.target;
        }
        else {
            target = null;
        }
        return target;
    }
}


window.onload = function(){
		app.init();
		if ( document.getElementById('ai') || document.getElementById('tb') )
		{
			window.onscroll = function()
			{
				xModalPopup.onScroll();
			}
		}
		var right = document.getElementById('right');
		var anchors = right.getElementsByTagName('a');
		
		for ( i = 1; i < anchors.length; ++i )
		{
			anchors[i].onmouseover = function(e){
				var img = new Image();
				img.src = "/gfx/mark.png";
				img.id = "mark";
				img.style.position = "absolute";
				var pos = QuirksMode.findPos(QuirksMode.getTarget(e));
				img.style.left = pos[0] + 125 + "px";
				img.style.zIndex = "999";
				img.style.top = pos[1] + "px";
				document.body.appendChild(img);
			}
			anchors[i].onfocus = function(e){
				var img = new Image();
				img.src = "/gfx/mark.png";
				img.id = "mark";
				img.style.position = "absolute";
				var pos = QuirksMode.findPos(QuirksMode.getTarget(e));
				img.style.left = pos[0] + 125 + "px";
				img.style.zIndex = "999";
				img.style.top = pos[1] + "px";
				document.body.appendChild(img);
			}
			
			anchors[i].onmouseout = function(){
				if ( document.getElementById('mark') )
				{
					var element = document.getElementById('mark');
					element.parentNode.removeChild(element);
				}
			}
			anchors[i].onblur = function(){
				if ( document.getElementById('mark') )
				{
					var element = document.getElementById('mark');
					element.parentNode.removeChild(element);
				}
			}
		}		
}
