function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "','down')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "','up')",(timer * speed));
			timer++;
		}
	}
}

function opaciton(id,start,end,time,delay) {
  var X = "opacity('"+id+"',"+start+","+end+","+time+")";
  setTimeout(X, delay);
}

//change the opacity for different browsers
function changeOpac(opacity, id, direction) {
	var object = document.getElementById(id).style; 
	if( opacity == 1 & direction == "down") {
   	document.getElementById(id).style.display = "none";
	}
	
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
	
	if ( opacity == 1 & direction == "up" ) {
  	document.getElementById(id).style.display = "inline";
	}
}

function overOn(i) {
  if (i.substr((i.length)-1,i.length) != "o") {
    document.getElementById(i).src = "images/" + i.substr(1,i.length) + ".png";
  }
} 
function overOff(i) {
  if (i.substr((i.length)-1,i.length) != "o") {
    document.getElementById(i).src = "images/" + i.substr(1,i.length) + "o.png";    
  }        
}
function singlePhoto(height,width,img,id) {
  document.getElementById("floatactphoto").innerHTML = "<img src='" + img + "' alt='' />";
  document.getElementById("photoid").innerHTML = img.substr(10,(img.length)-14);
  document.getElementById("floatphoto").style.width = width+300 + "px";
  document.getElementById("floatphoto").style.height = height+184 + "px";
  centerDiv('floatphoto',height+164,width+300);
  document.form1.commentid.value = id;
  sendRequest(id);
  document.getElementById("floatphoto").style.display = "inline";
  changeOpac(20,'supercont','down');
}
function retId() {
  return id;  
}
function closeDiv(div) {
  document.getElementById(div).style.display = "none";
  document.getElementById("yesno").style.display = "none";
  if (document.getElementById("floatphoto").style.display != "inline") {
    changeOpac(100,'supercont','up');  
  } else {  
    changeOpac(100,'floatphoto','up');  
  }
}
function scrollOffsetTop() {
  var y;
  if (self.pageYOffset) // all except Explorer
  {
  	y = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
  	// Explorer 6 Strict
  {
  	y = document.documentElement.scrollTop;
  }
  else if (document.body) // all other Explorers
  {
  	y = document.body.scrollTop;
  }  
  return y;
}
function scrollOffsetLeft() {
  var x;
  if (self.pageYOffset) // all except Explorer
  {
  	x = self.pageXOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
  	// Explorer 6 Strict
  {
  	x = document.documentElement.scrollLeft;
  }
  else if (document.body) // all other Explorers
  {
  	x = document.body.scrollLeft;
  }  
  return x;
}  
function pageHeight() {  
  var y;
  var test1 = document.body.scrollHeight;
  var test2 = document.body.offsetHeight
  if (test1 > test2) // all but Explorer Mac
  {
  	y = document.body.scrollHeight;
  }
  else // Explorer Mac;
       //would also work in Explorer 6 Strict, Mozilla and Safari
  {
  	y = document.body.offsetHeight;
  }
  return y;
}    
function pageWidth() {  
  var x;
  var test1 = document.body.scrollHeight;
  var test2 = document.body.offsetHeight
  if (test1 > test2) // all but Explorer Mac
  {
  	x = document.body.scrollWidth;
  }
  else // Explorer Mac;
       //would also work in Explorer 6 Strict, Mozilla and Safari
  {
  	x = document.body.offsetWidth;
  }
  return x;
}    
function windowHeight() {
  var y;
  if (self.innerHeight) // all except Explorer
  {
  	y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
  	// Explorer 6 Strict Mode
  {
  	y = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
  	y = document.body.clientHeight;
  }
  return y;     
}
function windowWidth() {
  var x;
  if (self.innerHeight) // all except Explorer
  {
  	x = self.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
  	// Explorer 6 Strict Mode
  {
  	x = document.documentElement.clientWidth;
  }
  else if (document.body) // other Explorers
  {
  	x = document.body.clientWidth;
  }    
  return x;     
}

// Ajax requests
// Need to make an object of XMLHttpRequest Type.
function createRequestObject() {
    var ro;
    if (navigator.appName == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }
    return ro;
}
var http = createRequestObject();
var http2 = createRequestObject();
var httpfav = createRequestObject();

function sendRequest(id) {
    http.open('get', 'pages/comments.inc.php?a=getcomments&id=' + encodeURIComponent(id));
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function sendComment() {
    var commentid = document.form1.commentid.value;
    var comment = document.form1.comment.value;
    var poster = document.form1.poster.value;
    http2.open('get', 'pages/comments.inc.php?a=addcomment&commentid=' + encodeURIComponent(commentid) + '&comment=' + encodeURIComponent(comment) + '&poster=' + encodeURIComponent(poster));
    http2.onreadystatechange = handleResponse2;
    http2.send(null);
}

function sendFavourites(id) {
    httpfav.open('get', 'pages/comments.inc.php?a=favourites&id=' + encodeURIComponent(id));
    httpfav.onreadystatechange = handleFavourites;
    httpfav.send(null);
}

function handleResponse() {
    if(http.readyState == 4){
        document.getElementById('commentdiv').innerHTML = http.responseText;
    }
}

function handleResponse2() {
    document.form1.poster.value = "";
    document.form1.comment.value = "";
}

function handleFavourites() {
  if(httpfav.readyState == 4){
    createCookie('fav',httpfav.responseText,'300');
    centerDiv('confirm',200,100);
    changeOpac(20,'floatphoto','down');
    document.getElementById("confirm").style.display = "inline";
    document.getElementById("confirmtext").innerHTML = "Added!<br /><br /><img src='images/ok.jpg' alt='Ok' class='cursor' style='float:right' onclick=closeDiv('confirm') />";
  }
}

function createCookie(name,value,days) {
  if (readCookie(name) == null) {
    value = value;
  } else {
    value = value + readCookie(name);
  }
	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=/";
	updateFavourites(name);
}

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);
	updateFavourites(name);
}

function updateFavourites(name) {
  if (readCookie(name) == null) {
	   document.getElementById("supercont").display = "none";
     createCookie('fav','','300');
  }
  document.getElementById('favourites').innerHTML = readCookie(name); 
}

function centerDiv(name,height,width) {
  document.getElementById(name).style.top = (scrollOffsetTop()+(windowHeight()-(height))/2) + "px";
  document.getElementById(name).style.left = ((pageWidth()/2)-(width)/2) + "px";   
}

function eraseFav() {
  changeOpac(20,'supercont','down');
  centerDiv('confirm',200,100);
  document.getElementById("confirm").style.display = "inline";
  document.getElementById("yesno").style.display = "inline";
  document.getElementById("confirmtext").innerHTML = "Are you sure you want to erase your favourites?";  
}



