//scripts by Nick Pettazzoni 2006-2007

var request;
var username;
var password;


  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request){
    alert("There was an error initializing XMLHttpRequest on your browser. You need better than IE 5 for this.");
  }


function loginscript(){
	username = document.getElementById("username").value;
	password = document.getElementById("password").value;
	document.getElementById("content").innerHTML = '<h2>Logging in<br /><br /><br />Please wait...</h2>';
	var url = "/login.php";
	request.open("POST", url, true);
	request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	var params = "username=" + escape(username) + "&password=" + escape(password);
	request.onreadystatechange = updateContent;
	request.send(params);
}

function logoutscript(){
	document.getElementById("content").innerHTML = '<h2>Logging out<br /><br /><br />Please wait...</h2>';
	var url = "/logout.php";
	request.open("GET", url, true);
	request.onreadystatechange = updateLogout;
	request.send(null);
}

function updateContent(){
     if (request.readyState == 4) {
       if (request.status == 200) {
	document.getElementById("content").innerHTML = request.responseText;
       } else {
         alert("Connection failed; status is " + request.status);
	 document.getElementById("content").innerHTML = '<h2>Login Failed</h2><form name="login" id="login"><h3><p>Username:<input name="username" type="text" id="username" value="' + username + '"></p><p>Password:<input name="password" type="password" id="password"></p><p><input name="submit" type="submit" id="submit" value="Login" onclick="loginscript()"></p></h3></form>';
       }	
     }
}

function updateLogout(){
     if (request.readyState == 4) {
       if (request.status == 200) {
	document.getElementById("content").innerHTML = request.responseText;
       } else {
         alert("Status is " + request.status);
	 document.getElementById("content").innerHTML = '<div class="inlineerror"><h2>There was an error logging you out</h2> <h3>Close your browser to ensure all sessions are closed.</h3></div>';
       }
     }
}

function showVideo(videoid){
	document.getElementById(videoid).innerHTML = '<h2>loading video</h2><img src="http://nickpettazzoni.com/images/loading.gif" />';
	document.getElementById('click' + videoid).innerHTML = "";
	var thisPage = document.url + "";
	if(thisPage.substring(25, 29) == "blog"){
		url = "../loadvideo.php?videoid=" + videoid;
	}else{
		url = "/loadvideo.php?videoid=" + videoid;
	}
	request.open("GET", url, true);
	request.onreadystatechange = function() {
		updateVideo(videoid);
	};
	request.send(null);
}

function updateVideo(videoid){
     if (request.readyState == 4) {
       if (request.status == 200) {
	document.getElementById(videoid).innerHTML = request.responseText;
       } else {
         alert("Status is " + request.status);
	 document.getElementById(videoid).innerHTML = '<div class="inlineerror"><h2>There was an error loading the video.</h2> <h2>This video is unavailable.</h2></div>';
       }
     }
}

function hideVideo(videoid){
	document.getElementById(videoid).innerHTML = '<img src="http://nickpettazzoni.com/images/qtlogo.png" height="103" width="103" alt="Quicktime video" />';
	document.getElementById('click' + videoid).innerHTML = '<a href="javascript:showVideo(\'olbermann\')">Play Video</a>';
}

function showcomments(postid){
	if(document.getElementById(postid).style.display != 'block'){
		document.getElementById(postid).style.display = 'block';
	}else{
		document.getElementById(postid).style.display = 'none';
	}
}
function showcommentform(thing){
	if(document.getElementById(thing).style.display != 'block'){
		document.getElementById(thing).style.display = 'block';
	}else{
		document.getElementById(thing).style.display = 'none';
	}
}
function envis(what){
	document.getElementById(what).style.visibility = 'visible';
}

function devis(what){
	document.getElementById(what).style.visibility = 'hidden';
}

function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent){
		while(1){
			curleft += obj.offsetLeft;
			if(!obj.offsetParent){
				break;
			}
			obj = obj.offsetParent;
		}
	}else if(obj.x){
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent){
		while(1){
			curtop += obj.offsetTop;
			if(!obj.offsetParent){
				break;
			}
			obj = obj.offsetParent;
		}
	}else if(obj.y){
		curtop += obj.y;
	}
	return curtop;
}

function showConfirm(type, whichButton, get1, get2){
	var startX = findPosX(document.getElementById(whichButton)) + 20 + "px";
	var startY = findPosY(document.getElementById(whichButton)) + 10 + "px";
	var timer = 10;
	var wide = 0;
	var high = 0;
	switch(type){
		case "ban":
			var action = "ban";
			var actionTitle = "ban this user from commenting?";
			var actionLink = "javascript:banReason(" + get1 + ", " + get2 + ")";
			break;
		case "deletecomment":
			var action = "delete";
			var actionTitle = "delete this comment?";
			var actionLink = "admin_deletecomment.php?t=news&id=" + get1 + "&comm=" + get2;
			break;
		case "deletepost":
			var action = "delete";
			var actionTitle = "delete this post?";
			var actionLink = "admin_deletepost.php?id=" + get1;
			break;
		default:
			var action = "";
			var actionTitle = "";
			var actionLink = "";
	}
	document.getElementById('confirmDialog').style.width = "\"" + wide + "px\"";
	document.getElementById('confirmDialog').style.display = 'block';
	document.getElementById('confirmDialog').style.top = startY;
	document.getElementById('confirmDialog').style.left = startX;
	document.getElementById('confirmContainer').innerHTML = "";
	while(high < 101){
		setTimeout('document.getElementById(\'confirmDialog\').style.width = ' + "\"" + wide + "px\"", timer);
		setTimeout('document.getElementById(\'confirmDialog\').style.height = ' + "\"" + high + "px\"", timer);
		timer += 2;
		wide += 3;
		high += 1;
	}
	setTimeout('drawConfirmContent(\'' + actionTitle + '\', \'' + actionLink + '\', \'' + action + '\')', (timer + 1));
}

function drawConfirmContent(actionTitle, actionLink, action){
	document.getElementById('confirmDialog').style.padding = 20 + "px";
	document.getElementById('confirmContainer').innerHTML = "<h2>Are you sure you want to " + actionTitle + "</h2>";
	document.getElementById('confirmContainer').innerHTML += "<a href=\"" + actionLink + "\"><img src=\"images/" + action + "_small.png\" border=\"0\" /></a> <a href=\"javascript:hideConfirm()\"><img src=\"images/cancel_small.png\" border=\"0\" alt=\"Cancel\" /></a>";
}

function hideConfirm(){
	document.getElementById('confirmContainer').style.display = 'block';
	document.getElementById('confirmContainer').innerHTML = "";
	document.getElementById('confirmForm').style.display = 'none';
	document.getElementById('confirmDialog').style.padding = 0 + "px";
	var timer = 0;
	var high = 100;
	var wide = 300;
	while(high >= 0){
		setTimeout('document.getElementById(\'confirmDialog\').style.width = ' + "\"" + wide + "px\"", timer);
		setTimeout('document.getElementById(\'confirmDialog\').style.height = ' + "\"" + high + "px\"", timer);
		timer += 1;
		wide -= 3;
		high -= 1;
	}
	setTimeout('document.getElementById(\'confirmDialog\').style.display = \'none\';', (timer + 1));
}

function banReason(get1, get2){
	document.getElementById('confirmContainer').innerHTML = '';
	document.getElementById('confirmContainer').style.display = 'none';
	document.getElementById('confirmForm').style.display = 'block';
	document.banForm.newsid.value = get1;
	document.banForm.comm.value = get2;
}

function banSubmit(){
	document.banForm.submit();	
}

function rollover(who){
	document.getElementById(who).src = 'images/' + who + '_over.png';
}

function rollback(who){
	document.getElementById(who).src = 'images/' + who + '.png';
}


//code for Dashboardish

var opaque = 0;
var zoom = 500;
var windowWidth = 0;
var windowHeight = 0;

	function init(){
		resizeShade();


	}
	function resizeShade(){
		getWindowSize(false);
		var newWidthStr = "" + windowWidth + "px";
		var newHeightStr = "" + windowHeight + "px";
		document.getElementById('shade').style.width = newWidthStr;
		document.getElementById('shade').style.height = newHeightStr;		
	}
	
	//function getWindowSize
	//original script modified by Nick Pettazzoni
	//thanks to http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	function getWindowSize(alertSwitch){
		if(typeof(window.innerWidth) == 'number'){
			//not Internet Explorer
			windowWidth = window.innerWidth;
			windowHeight = window.innerHeight;
		}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
			//Internet Explorer 6+ in 'standards compliant mode'
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
			//IE 4 compatible
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		
		//alerts width and height if told to do so
		if(alertSwitch){
			window.alert('Width = ' + windowWidth);
			window.alert('Height = ' + windowHeight);
		}
	}
	
	function dimmer(){
		document.getElementById('shade').style.display = 'block';
		var timer = 10;
		while(opaque < 0.8){
			setTimeout('document.getElementById(\'shade\').style.opacity = ' + opaque, timer);
			setTimeout('document.getElementById(\'shade\').style.filter = "alpha(opacity = ' + (opaque * 100) + ')"', timer);
			//setTimeout('document.getElementById(\'abox\').style.zoom = "' + zoom + '%"', timer);
			timer += 5;
			opaque += 0.01;
			zoom -= 100;
		}
	}
	
	function lighter(){
		var timer = 10;
		while(opaque >= 0){
			setTimeout('document.getElementById(\'shade\').style.opacity = ' + opaque, timer);
			setTimeout('document.getElementById(\'shade\').style.filter = "alpha(opacity = ' + (opaque * 100) + ')"', timer);
			//setTimeout('document.getElementById(\'abox\').style.zoom = "' + zoom + '%"', timer);
			timer += 5;
			opaque -= 0.01;
			zoom += 100;
		}
		setTimeout('document.getElementById(\'shade\').style.display = "none"', timer);
	}
	
	function humanToTime(){
		var humDate = new Date(Date.UTC(document.h2u.inYear.value,
			(stripLeadingZeroes(document.h2u.inMon.value)-1),
			stripLeadingZeroes(document.h2u.inDay.value),
			stripLeadingZeroes(document.h2u.inHr.value),
			stripLeadingZeroes(document.h2u.inMin.value),
			stripLeadingZeroes(document.h2u.inSec.value)));
		document.h2u.result.value = (humDate.getTime()/1000.0);
	}
	
	function stripLeadingZeroes(input){
	if((input.length > 1) && (input.substr(0,1) == "0"))
	  return input.substr(1);
	else
	  return input;
	}