// JavaScript Document

window.addOnloadEvent = function(addToOnload) {
	document.observe('dom:loaded',function() {addToOnload();});
}

/*window.addOnloadEvent(function() {
	if (getSelector('#menu ul ul'))
		getSelector('#menu ul ul').display = 'none';
});

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

function popup_show(id)
{
	$(id).style.display = 'block';
	$(id).style.marginLeft = '-'+($("popup_div").getWidth() / 2)+'px';
}

function popup_hide()
{
	removePopup('popup_div');
}

function popup_load(url,parameters,action_on_success){
	insertPopup('popup_div');
	
	new Ajax.Updater('popup_div',site_dir + url,{
					 method: 'post',
					 parameters: parameters,
					 onComplete: function (transport) {
						 if (action_on_success)		eval(action_on_success);
						 
						 popup_show('popup_div');
					 }
					 });
}

function gallery_load(url,parameters){
	insertPopup('gallery_popup');
	
//	$('gallery_popup').setStyle({'top' : '10px' , 'width' : '100px'});

	new Ajax.Updater('gallery_popup',site_dir+url,{
				 method: 'post',
				 parameters: parameters,
				 onComplete: function (transport) {
					 $('gallery_popup').style.marginLeft = '-'+(($('gallery_popup_picture').getWidth() + 20)/2)+'px';
					 if ($('gallery_popup_picture').getWidth() >= 280)
					 	$('gallery_popup').style.width = $('gallery_popup_picture').getWidth() + 20 + 'px';
					 else
						$('gallery_popup').style.width = '300px';	
				 }
				 });
}

function insertPopup(id)
{
	if (typeof id == "undefined")	id = 'popup_div';
	
	if (!$(id))		$(document.body).insert('<div id="'+id+'"></div>');
//	$(id).update('');
}
function removePopup(id)
{
	if (typeof id == "undefined")	id = 'popup_div';
	
	if ($(id)) 		$(id).remove();
}


// Gammel AJAX

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

	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;
}
