// PARSING
dojo.addOnLoad(function(){
	dojo.require("dijit.form.Button");
	dojo.require("dijit.Dialog");
	dojo.require("dijit.Tooltip");
	dojo.require("dojo._base.fx");
	
	
	var box = document.getElementsByTagName("div");
	for(var i=0; i<box.length; i++)
	{
		var child = box[i];
		if(child.className == "blogg_innlegg11")
		{
			var parent = child.parentNode;
			if(child.style.display == "none")
				parent.style.padding = "0px";
		}
	}
});




// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  
  return true
}

function show_hide_blogg(id, arrowid)
{
	var divNode = dojo.byId(id);
	var parentNode = divNode.parentNode;
	var arrowNode = dojo.byId(arrowid);
	
	if(divNode.style.display == "" || divNode.style.display == "block")
	{
		divNode.style.display = "none";
		parentNode.style.padding = "0px";
		arrowNode.src = "images/blogg_arrow_closed.gif";
	}
	else
	{
		divNode.style.display = "block";
		parentNode.style.padding = "10px";
		arrowNode.src = "images/blogg_arrow_open.gif";
	}
	
	return false;
}


function blogg_comment_show(id, divid)
{
	var comments = document.getElementsByTagName("div");
	for(var i=0; i<comments.length; i++)
	{
		if(comments[i].className == "container_comment")
		{
			comments[i].style.display = "none";
		}
	}
	var coords = dojo.byId(divid);
	coords = dojo.coords(coords);
	
	var div = dojo.byId(id);
	div.style.opacity = "0";
	div.style.top = (tempY-100) + "px";
	div.style.left = (tempX-500) + "px";
	div.style.display = "block";
	
	var foo = dojo.fadeIn({node: id, duration: 200});
	foo.play();
	
	
	return false;
}

function blogg_comment_hide(id)
{
	var foo = dojo.fadeOut({node: id, duration: 200});
	dojo.connect(foo, "onEnd", function(){ dojo.byId(id).style.display = "none"; });
	foo.play();
	
	return false;
}

function commentAdd(name, message, bloggid, parent)
{
	// Getting the values
	var nameNode = dojo.byId(name);
	var messageNode = dojo.byId(message);
	var bloggidNode = dojo.byId(bloggid);
	
	var nameValue = nameNode.value;
	var messageValue = messageNode.value;
	var bloggidValue = bloggidNode.value;
	
	// Check
	if(nameValue != "" && messageValue != "")
	{
		// Replacing
		messageValue = messageValue.replace(/\r\n/g, "<br>");
		messageValue = messageValue.replace(/\r/g, "<br>");
		messageValue = messageValue.replace(/\n/g, "<br>");
		
		// OPPRETTER NYTT XHR
		var xhr = httpRequest();
		
		// SJEKKER
		if(xhr)
		{
			// OPEN
			xhr.open("POST", "admin/ajax/commentsAdd.php?name=" + nameValue + "&message=" + messageValue + "&bloggid=" + bloggidValue, true);
			
			// ONREADYSTATECHANGE
			xhr.onreadystatechange = function()
			{
				// SJEKKER STATUSEN
				if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304))
				{
					// ENDRER ANTALL INNLEGG
					commentChangeAmount(bloggidValue, "admin/");
					
					// OPPRETTER DIVEN
					var resultat = document.createElement("div");
					var id = "new_comment" + Math.floor(Math.random()*111111111111111111111111111);
					resultat.id = id;
					resultat.innerHTML = xhr.responseText;
					resultat.style.opacity = "0";
					
					// Appending
					var parentNode = dojo.byId(parent);
					parentNode.insertBefore(resultat, parentNode.firstChild);
					
					
					// Fading in
					var foo = dojo.fadeIn({node: id, duration: 200});
					foo.play();
					
					// Empty the containers
					nameNode.value = "";
					messageNode.value= "";
				}
			};
			
			// SENDING
			xhr.send(null);
		}				
	}
	
	// Returning
	return false;
}

function commentDelete(id, bloggid)
{
	// CHECKING
	if(confirm("Er du sikker på at du vil slette?"))
	{
		// VERDIER
		var thisid = "comment_comment" + id;
		var thisNode = dojo.byId(thisid);
		var parentNode = thisNode.parentNode;
		
		// OPPRETTER NYTT XHR
		var xhr = httpRequest();
		
		// SJEKKER
		if(xhr)
		{
			// OPEN
			xhr.open("POST", "ajax/commentsDelete.php?bloggid=" + id, true);
			
			// ONREADYSTATECHANGE
			xhr.onreadystatechange = function()
			{
				// SJEKKER STATUSEN
				if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304))
				{
					// ENDRER ANTALL INNLEGG
					commentChangeAmount(bloggid, "");
					
					// Fading out
					var foo = dojo.fadeOut({node: thisid, duration: 200});
					dojo.connect(foo, "onEnd", function(){
						parentNode.removeChild(thisNode);
					})
					foo.play();
				}
			};
			
			// SENDING
			xhr.send(null);
		}
	}
	
	return false;
}

function commentChangeAmount(bloggid, admin)
{
	// OPPRETTER NYTT XHR
	var xhr = httpRequest();
	
	// SJEKKER
	if(xhr)
	{
		// OPEN
		xhr.open("POST", admin + "ajax/commentsChangeAmount.php?bloggid=" + bloggid, true);
		
		// ONREADYSTATECHANGE
		xhr.onreadystatechange = function()
		{
			// SJEKKER STATUSEN
			if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 304))
			{
				var number = xhr.responseText;
				dojo.byId("comment_amount" + bloggid).innerHTML = "Dette innlegget har " + number + " kommentarer";
			}
		};
		
		// SENDING
		xhr.send(null);
	}				
}


// FUNKSJONEN SOM SENDER DEG EN XHR
function httpRequest()
{
	// OPPRETTER VARIABEL
	var xhr = false;
	
	// SJEKKER
	if(window.XMLHttpRequest)
	{
		// OPPRETTER
		xhr = new XMLHttpRequest();
	}
	else
	{
		// SJEKKER
		if(window.ActiveXObject)
		{
			// PRØVER
			try
			{
				// SETTER
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				
			}
		}
	}
	
	// RETURNERER
	return xhr;
}
