// JavaScript Document
<!-- <script>--> 

window.addOnloadEvent = function(addToOnload) {
    var oldOnload;
    if (window.onload) {
        oldOnload = window.onload;
        window.onload = function(){
            oldOnload();
            addToOnload();
        };
    }
    else {
        window.onload = addToOnload;
    }
} 

function getSelector(sText) 
{ 
  var S = document.styleSheets[0];
  var rules = S.rules||S.cssRules // IE ¦¦ others 
  for(var r=0;r<rules.length;r++) 
    if(rules[r].selectorText==sText) 
    return rules[r].style 
  return null; 
}

function fontAdjust()
{
	var size;
	if (getSelector('h1'))
	{
		while (document.getElementById('menu').offsetWidth > 810)
		{
			size = getSelector('#popup_div, body, p, td, li, a, #menu a, #submenu a').fontSize;
			size = (size.substr(0,(size.length - 2))) - 0.14;
			getSelector('#popup_div, body, p, td, li, a, #menu a, #submenu a').fontSize = size + 'px';
	
			size = getSelector('.galleryText, .galleryText a, #gallery_control, .text_odd, .text_even, .text_odd a, .text_even a').fontSize;
			size = (size.substr(0,(size.length - 2))) - 0.11;
			getSelector('.galleryText, .galleryText a, #gallery_control, .text_odd, .text_even, .text_odd a, .text_even a').fontSize = size + 'px';
	
			size = getSelector('h1').fontSize;
			size = (size.substr(0,(size.length - 2))) - 0.20;
			getSelector('h1').fontSize = size + 'px';
		}
	}
}

var popup_size = {
	'/gallery.php' : 300,
	'/edit.php' : 350
}

function popup_show()
{
	document.getElementById("popup_div").style.display = 'block';

	document.getElementById("popup_div").style.marginLeft = '-'+(document.getElementById("popup_div").offsetWidth/2)+'px';
}

function popup_hide()
{
	document.getElementById("popup_div").style.display='none';
	document.getElementById('popup_div').style.top = '150px';
	document.getElementById('popup_div').style.marginLeft = '0px';
	document.getElementById('popup_div').style.marginTop = '0px';
	document.getElementById("popup_div").innerHTML = '';
}

function popup_load(url,parameters,action_on_success){
   	document.getElementById('popup_div').style.width = popup_size[url] + 'px';
	
	ajax_fetch(url,parameters,'popup_div',action_on_success);		
	popup_show();
}

function gallery_load(url,parameters){
	document.getElementById('popup_div').style.top = '50px';
//	document.getElementById('popup_div').style.width = '100px';
	ajax_fetch(url,parameters,'popup_div',
		"document.getElementById('popup_div').style.marginLeft = '-'+((document.getElementById('picture').width+20)/2)+'px';" +
//		"document.getElementById('popup_div').style.marginTop = '-'+((document.getElementById('popup_div').offsetHeight)/2)+'px';" +
		"if (document.getElementById('picture').width >= 280)" + 
		"document.getElementById('popup_div').style.width = document.getElementById('picture').width+20 + 'px';" + 
		"else document.getElementById('popup_div').style.width = '300px'"
//		"document.getElementById('popup_div').style.height = document.getElementById('picture').height+90 +'px';"
	);
	if (document.getElementById("popup_div").style.display != 'block')
		popup_show();
}



function ajax_fetch(url,parameters,target,action_on_success){
	url = site_url + url;
	var xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	}

//	window.alert("Henter: "+url+"?"+parameters);
	xmlHttp.onreadystatechange=function () {ajax_statechange(xmlHttp,target,action_on_success);};

	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.send(parameters);
}

function ajax_statechange(xmlHttp,target,action_on_success){
	if (target == 'none')
	{
		if (xmlHttp.readyState==4 && action_on_success != "")
			eval(action_on_success);
	}
	if (target == '' || typeof target == 'undefined')
	{
		if (xmlHttp.readyState==4)
		{
			var result = xmlHttp.responseText;
			if (result != '')					window.alert(result);
			else if (action_on_success != "")	eval(action_on_success);
		}
	}
	else
	{
		if (xmlHttp.readyState<4 && document.getElementById(target).innerHTML == '')
		{
			if (document.getElementById(target).tagName != 'input')	document.getElementById(target).innerHTML='<div align="center"><br /><img src="'+ajax_loader_url+'"><br /><br /></div>';
		}
		if (xmlHttp.readyState==4)
		{
			if (document.getElementById(target).tagName == 'input')		document.getElementById(target).value=xmlHttp.responseText;
			else														document.getElementById(target).innerHTML=xmlHttp.responseText;
			if (action_on_success != "")								eval(action_on_success);
		}
	}
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
  return xmlHttp;
}

