
function ajaxLoader(url,id) {
  if (document.getElementById) {
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  }
  if (x) {
    x.onreadystatechange = function() {
      if (x.readyState == 4 && x.status == 200) {
        el = document.getElementById(id);
        el.innerHTML = x.responseText;
      }
    }
    x.open("GET", url, true);
    x.send(null);
  }
}

function editblog(action,id,headline,intro,content,catid,t,s,f,uniqueid){
	if(action == "delete"){
		statusfield = "blogstatus";
		url = "../includes/popup_editblog.php?action=delete";
		parameters = "blogid=" + id;
	};
	
	if(action == "update"){
		statusfield = "saveblogstatus";
		url = "../includes/popup_editblog.php?action=update";
		parameters = "blogid=" + id + "&headline=" + escape(headline) + "&intro=" + escape(intro) + "&content=" + escape(content);
	};
	
	var ajax;
	try{ajax = new XMLHttpRequest();}// Firefox, Opera, and the like
	catch (e){
		try
		{ajax= new ActiveXObject("Msxm12.XMLHTTP");}//Internet Exploder?
		catch (e){
			try
			{ajax = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e){
				document.getElementById("showresult").innerHTML="Fejl";}
			}
		}
		
		waitingmsg = '<div style="position:relative; top:4px;">';
		waitingmsg = waitingmsg + '<div style="float:left;"><img src="../images/loading.gif" width="24" height="24" /></div>';
		waitingmsg = waitingmsg + '<div style="position:relative; left:8px; top:2px; width:150px;">fortager ændringer ...</div></div>';
		document.getElementById(statusfield).innerHTML=waitingmsg;
		setTimeout(waitforitnow,1000);
		
		function waitforitnow(){
			ajax.open('POST', url, 'true');
			ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajax.setRequestHeader("Content-length", action.length);
			ajax.setRequestHeader("Connection", "close");
			ajax.send(parameters);
			ajax.onreadystatechange=function(){
			if(ajax.readyState == 4){
				if(ajax.responseText == "ok"){
					content = "<div style='position:relative; top:4px;'><div style='float:left;'><img src='../images/logo_small999999.png' width='28' height='28' /></div><strong style='color:#549454; position:relative; left:5px;'><em>Success,&nbsp;</em></strong> ";
					content = content + "vidrestiller ...</div>";
					document.getElementById(statusfield).innerHTML=content;
					function redirect(){
						if(action == "delete"){
							document.location.href = "http://medieskolerne.dk/?submenu=Nyheder-Blogs-og-Viden&t=t3&s=42&f=smID";
						}
						if(action == "update"){
							document.location.href = "http://medieskolerne.dk/?submenu=Nyheder-Blogs-og-Viden&catID="+catid+"&t="+t+"&s="+s+"&f="+f+"&uniqueid="+uniqueid+"&page=recent&subid="+s+"&field="+f+"";
						}
					}
					setTimeout(mclose,1000);
					setTimeout(redirect,2000);
				}else{
					document.getElementById(statusfield).innerHTML=ajax.responseText;
				}
			}
		}
	}
};

function changepwd(pwd,rpwd){
	var ajax;
	try{ajax = new XMLHttpRequest();}// Firefox, Opera, and the like
	catch (e){
		try
		{ajax= new ActiveXObject("Msxm12.XMLHTTP");}//Internet Exploder?
		catch (e){
			try
			{ajax = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (e){
				document.getElementById("showresult").innerHTML="Fejl";}
			}
		}
		
		pwd = escape(pwd);
		rpwd = escape(rpwd);
		
		document.getElementById('pwd').disabled = true;
		document.getElementById('rpwd').disabled = true;
		document.getElementById('pwdsubmit').disabled = true;
		
		waitingmsg = '<img src="../images/loading.gif" width="24" height="24" /><br />';
		waitingmsg = waitingmsg + 'Vent venligst<br />Kodeord ændres';
		document.getElementById("pwdinfo").innerHTML=waitingmsg;
		setTimeout(waitforitnow,1000);
		
		function waitforitnow(){
			ajax.open('POST', '../includes/changepwd.php?pending=1', 'true');
			ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajax.setRequestHeader("Content-length", pwd.length);
			ajax.setRequestHeader("Connection", "close");
			ajax.send("pwd=" + pwd + "&rpwd=" + rpwd);
			ajax.onreadystatechange=function(){
			if(ajax.readyState == 4){
				if(ajax.responseText == "ok"){
					content = "<br /><strong style='color:#549454;'><em>Success</em></strong><br />";
					content = content + "Dit kodeord er nu blevet ændret.";
					document.getElementById("pwdinfo").innerHTML=content;
					setTimeout('mclose()',3000);
				}else{
					document.getElementById('pwd').disabled = false;
					document.getElementById('rpwd').disabled = false;
					document.getElementById('pwdsubmit').disabled = false;
					document.getElementById("pwdinfo").innerHTML=ajax.responseText;
				}
			}
		}
	}
};

//file upload
function fileUpload(form, action_url, div_id){
	
	// Create the iframe...
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","upload_iframe");
	iframe.setAttribute("name","upload_iframe");
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");
	
	// Add to document...
	form.parentNode.appendChild(iframe);
	window.frames['upload_iframe'].name="upload_iframe";
	
	iframeId = document.getElementById("upload_iframe");
	
	// Add event...
	var eventHandler = function()  {
		if (iframeId.detachEvent)
			iframeId.detachEvent("onload", eventHandler);
		else
			iframeId.removeEventListener("load", eventHandler, false);
		
		// Message from server...
		if (iframeId.contentDocument) {
			content = iframeId.contentDocument.body.innerHTML;
		} else if (iframeId.contentWindow) {
			content = iframeId.contentWindow.document.body.innerHTML;
		} else if (iframeId.document) {
			content = iframeId.document.body.innerHTML;
		}
		
		if(content == '<div id="loginmessageGreen">login ok...</div>'){
			document.getElementById(div_id).innerHTML = content;
			if(readCookie('deeplink')){
				url = readCookie('deeplink');
			}else{
				url = "http://medieskolerne.dk/"
			}
			setTimeout("window.top.location = url",1000);
		}else{
			document.getElementById(div_id).innerHTML = content;
		};
		
		// Del the iframe...
		setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
	}
	
	if (iframeId.addEventListener)
		iframeId.addEventListener("load", eventHandler, true);
	if (iframeId.attachEvent)
		iframeId.attachEvent("onload", eventHandler);
	
	// Set properties of form...
	form.setAttribute("target","upload_iframe");
	form.setAttribute("action", action_url);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	
	// Submit the form...
	form.submit();
	
	document.getElementById(div_id).innerHTML = "<img src='../images/loading.gif' width='24' height='24' />";
}

//UniqueID,NormalBGcolor,HoverBGcolor,NormalFontColor,HoverFontColortext,text,link
function RoundCorners(thisId,NormalColor,OverColor,NormalFontColor,HoverFontColor,id1,id2,id3,id4,NormalClass,OverClass,OverOut){
	if(OverOut == 'over'){
		if(OverColor != ''){
			ClassName = OverClass;
			document.getElementById(thisId).style.background="#"+OverColor;
			if(HoverFontColor != ''){
				document.getElementById(thisId).style.color = '#'+HoverFontColor;
			};
		}else{
			ClassName = NormalClass;
		};
	}else if(OverOut == 'out'){
	
		ClassName = NormalClass;
		document.getElementById(thisId).style.background="#"+NormalColor;
		document.getElementById(thisId).style.color = '#'+NormalFontColor;
	};
	document.getElementById(id1).className = "result_"+ClassName+' result_TL';
	document.getElementById(id2).className = "result_"+ClassName+' result_TR';
	document.getElementById(id3).className = "result_"+ClassName+' result_BL';
	document.getElementById(id4).className = "result_"+ClassName+' result_BR';
};

//function to show templateid
function getVersionID(name,subid,field){
	alert('Template: '+name+'\nSubid: '+subid+'\nField: '+field);
};

//functions to set js cookies
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//function to read querystring values
function getParam(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
}; 

//default form text function
function defaultText(field,text){
	if(document.getElementById(field).value == text){
		document.getElementById(field).value = '';
	};
	
	if(document.getElementById(field).value == ''){
		document.getElementById(field).value = text;
	}
};

//dim background function
$(document).ready(function(){

	$("#dim").css("height", $(document).height()+3000);
	
	$(".alert").click(function(){
		$("#dim").show();
		return false;
	});
	
	$(".close").click(function(){
		$("#dim").hide();
		return false;
	});
});

$(window).bind("resize", function(){
	$("#dim").css("height", $(window).height());
});

//leftmenu
ddaccordion.init({
	headerclass: "submenuheader", //Shared CSS class name of headers group
	contentclass: "submenu", //Shared CSS class name of contents group
	
	//Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	revealtype: "click",
	
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: false, //persist state of opened contents within browser session?
	
	//Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	toggleclass: ["",""],
	
	//Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"]
	togglehtml: ["prefix", "<span style='position:absolute; left:8px;'>+</span>", "<span style='position:absolute; left:8px;'>-</span>"],
	
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do not highlight leftmenu1 if this is a deep link
		if(!getParam('t')){
			if(document.getElementById('leftmenu1')){
				changeme1(document.getElementById('leftmenu1'));
			};
		};
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})

//nifty corners
window.onload=function(){
	Nifty("div#nav a","all small transparent fixed-height");
	Nifty("div.dropdowntext","all small transparent fixed-height");
	//Nifty("#showresult#resultlist","all small transparent fixed-height");
	Nifty("div.glossymenu a","all small transparent fixed-height");Nifty("div.greybox","all small transparent fixed-height");}

//border images
//leftmenu color change
var oActiveA2 = null;
function outline(oA2){
    if (oActiveA2) {
        oActiveA2.style.background = '#ebebeb';
		if(document.getElementById(readCookie('isactive'))) document.getElementById(readCookie('isactive')).style.background = '#ebebeb';
    }
    oActiveA2 = oA2;
	if(oActiveA2 != 'veryfirstid'){
		if(document.getElementById('veryfirstid')) document.getElementById('veryfirstid').style.background = '#ebebeb';
		if(document.getElementById(readCookie('isactive'))) document.getElementById(readCookie('isactive')).style.background = '#ebebeb';
	};
    oActiveA2.style.background = '#6f6f6f';
}

var oActiveA21 = null;
function outlineimage(oA21){
    if (oActiveA21) {
        oActiveA21.style.color = '#333333';
		oActiveA21.style.background = '#ebebeb';
		
		if(document.getElementById(readCookie('isactive'))) document.getElementById(readCookie('isactive')).style.background = '#ebebeb';
		if(document.getElementById(readCookie('isactive'))) document.getElementById(readCookie('isactive')).style.color = '#333333';
    }
    oActiveA21 = oA21;
	if(oActiveA21 != 'veryfirstid'){
		if(document.getElementById('veryfirstid')) document.getElementById('veryfirstid').style.background = '#ebebeb'; 
		if(document.getElementById('veryfirstid')) document.getElementById('veryfirstid').style.color = '#333333';
		if(document.getElementById(readCookie('isactive'))) document.getElementById(readCookie('isactive')).style.background = '#ebebeb';
		if(document.getElementById(readCookie('isactive'))) document.getElementById(readCookie('isactive')).style.color = '#333333';
	};
    oActiveA21.style.background = '#6f6f6f';
	oActiveA21.style.color = '#ffffff';
}

//leftmenu color change
var oActiveA = null;
function changeme1(oA){
    if (oActiveA) {
        oActiveA.style.background = '#ebebeb';
        oActiveA.style.color = '#333';
		oActiveA.style.fontWeight = 'normal';
    }
    oActiveA = oA;
    oActiveA.style.background = '#6f6f6f';
    oActiveA.style.color = '#fff';
	oActiveA.style.fontWeight = 'bold';
}

//leftmenu submenu color change
var oActiveA1 = null;
function changeme2(oA1){
    if (oActiveA1) {
        oActiveA1.style.color = '#333';
		oActiveA1.style.fontWeight = 'normal';
    }
    oActiveA1 = oA1;
    oActiveA1.style.color = '#6f6f6f';
	oActiveA1.style.fontWeight = 'bold';
}

//breadcrumb
var array = new Array();
var count = 0;

function pushcrumb(newCrumb,allowedArrayLength,itemId,itemLabel){
	
	if(allowedArrayLength == 2 && itemId == 36 || itemId == 41 || itemId == 44){
		array.push(newCrumb);
		array.push(itemLabel);
		count=2;
	}else if(allowedArrayLength == 1 && count > 0){
		array.splice(allowedArrayLength-1,allowedArrayLength);
		array.push(newCrumb);
		count=1;
	}else if(allowedArrayLength == 2 && count > 1){
		array.splice(allowedArrayLength-1,allowedArrayLength);
		array.push(newCrumb);
		count=2;
	}else if(allowedArrayLength == 3 && count > 2){
		array.splice(allowedArrayLength-1,allowedArrayLength);
		array.push(newCrumb);
		count=3;
	}else{
		array.push(newCrumb);
		count++;
	}
	
	showcrumb(newCrumb,allowedArrayLength);
}

function showcrumb(newCrumb,allowedArrayLength) {
	var string;
	for(i = 0; i < count; i++) {
		if(i == 0){
			string = " > "+array[i];
		}else{
			if(newCrumb == ''){
				string = string;
			}else{
				string = string + " > "+array[i];
			}
		}
	}
	
	//if in a sub submenu replace last > with a :
	if(allowedArrayLength == 3){
		String.prototype.reverse = function(){
			splitext = this.split("");
			revertext = splitext.reverse();
			reversed = revertext.join("");
			return reversed;
		}	
	
		newstring = string.reverse();
		newstring = newstring.replace(">",":");
		newstring = newstring.reverse();
		string = newstring;
	}
	
	document.getElementById('crumb').innerHTML = string;
}

$.ctrl = function(key, callback, args) {
    var isCtrl = false;
    $(document).keydown(function(e) {
        if(!args) args=[]; // IE barks when args is null

        if(e.ctrlKey) isCtrl = true;
        if(e.keyCode == key.charCodeAt(0) && isCtrl) {
            callback.apply(this, args);
            return false;
        }
    }).keyup(function(e) {
        if(e.ctrlKey) isCtrl = false;
    });
};

$.ctrl('Q', function() {
    alert("Dette site er udviklet af Mike Johnsen (mike@suncodes.com)\ntil Medieskolerne i Viborg");
});
