
var is_ie6 = (
	window.external &&
	typeof window.XMLHttpRequest == "undefined"
);

function addHistory(history){
	dhtmlHistory.add(history);
}
loadedImage = -1;
var images = new Array();
var sharedAlbum = 0;
var sortedBy = 'file_date ASC';
var viewUser = '';
var changeImage = function(shared,albumId,sortby,usr,oldimage,newimage,index){
	sortedBy = sortby;
	viewUser = usr;
	
	if(typeof(pageTracker) != "undefined")
		pageTracker._trackPageview(usr+'/'+albumId+'/'+newimage);
	//---	
	var albtype = 'album';
	if(shared==1)
		albtype='shaalbum';
	addHistory(albtype+albumId+'/asset'+newimage+'/'+index+'/'+oldimage);
	//---
	//Change the image
	replaceImage(newimage,index);
	
	//Clear comments
	var comments = document.getElementsByClassName('commentsBox');
	comments.innerHTML='';
	
	//Clear rating
	var rating = document.getElementsByClassName('ratingContainer');
	var length = rating.length;
	for(var i=0;i<length;i++){
		rating[i].style.display='none';
		rating[i].innerHTML='&nbsp;';
	}

	//Update all links.
	var links = document.getElementsByName('asset_dependant_link');
	length = links.length;
	for(var i=0;i<length;i++){
		var newhref = links[i].href
		var newhref_parts = newhref.split("#");
		newhref = newhref_parts[0];
                newhref = newhref.replace(oldimage,newimage);
		links[i].href= newhref;
	}
	
	//updateImageBar(newimage,shared,albumId,sortby,newimage,usr,index);
	//Scroll the imagebar accordingly
	scrollImageBar(newimage,index,oldimage);
	
	//Change comments
	updateComments(newimage);
	//Update file name and date.
	updateNameAndDate(newimage,shared,usr);	

	//Update rating 
	updateRatings(newimage);
	updateAvgRatings(oldimage,shared);
	//Update tags
	updateTags(newimage);
	
	//Update all links pointing to this method.. The oldimage needs updating.
	links = document.getElementsByName('changeImageLink');
	length = links.length;
	for(i=0;i<length;i++){
		links[i].href=links[i].href.replace("',"+oldimage+",","',"+newimage+",");
	}

	updatePrevAndNext(shared,albumId,sortby,usr,oldimage,newimage,index);
	loadedImage=newimage;
}

var replaceImage = function(image,index){
	imageLoading(true);
	var img = document.getElementById('showim');
	var previm = document.getElementById('previm');
	var nextim = document.getElementById('nextim');
	if(previm.name == image){
		//Image that needs loading is already in previm
		img.src = previm.src;
	}
	else{
		if(nextim.name == image){
			//Image that needs loading is already in nextim
			img.src = nextim.src;
		}
		else{
			//Image not preloaded, needs to be loaded
			img.src=img.src.replace(/id\=[0-9]*/,'id='+image);
		}
	}
	//update prev and next im. Asynchronously
	updatePrevAndNextIm(index,previm,nextim);
}
var updatePrevAndNextIm = function(index,previm,nextim){
	var nextind = (parseInt(index)+1);
	var imlength = images.length;
	if(nextind>imlength)
		nextind=0;
	nextim.src = nextim.src.replace(nextim.name,images[nextind]);
	nextim.name = images[nextind];
	var prevind = (parseInt(index)-1);
	if(prevind<0)
		prevind=imlength-1;
	previm.src = previm.src.replace(previm.name,images[prevind]);
	previm.name = images[prevind];
}

var imageBarIsLoading = 0;
var interruptImagebarLoading = 0;
function interruptImageBarLoading(){
	if(imageBarIsLoading != 0){
		//Don't interrupt IE6, it gets mad!
		if(!is_ie6){
			interruptImagebarLoading=1;
		}
		return 1;
	}

	if(imageBarIsLoading == 2)
	{
		return 2;
	}
	return 0;
}
var startAtImage=0;
var imageInProgress = images[0];

var resumeImageBarLoading = function(){
	if(imageBarIsLoading==1){
		return;
	}
	imageBarIsLoading=1;
	var length = images.length;
	for(var i=startAtImage;i<length;i++){
		var imid = images[i];
		var fsid = f_images[i];
		var thisIm = $('thumb_rl_image_'+imid);
		if(interruptImagebarLoading==1){
			imageBarIsLoading=0;
			interruptImagebarLoading=0;
			return;
		}
		//now wait until that image is loaded...
		thisIm.onload = resumeImageBarLoading;
		imageInProgress = thisIm;
		startAtImage++;
		imageBarIsLoading=2;
		thisIm.src="/asset.html?format=smallthumb&id="+imid+"&shared="+sharedAlbum+"&fid="+fsid;
		return;
	}
	imageBarIsLoading=0;
	//If we're dealing with IE6, recheck all the images. IE6 sux
	if(is_ie6){
		for(var i=0;i<length;i++){
			var imid = images[i];
			var thisIm = $('thumb_rl_image_'+imid);
			thisIm.onload = '';
			thisIm.src = thisIm.src;
		}
	}
}

var imageLoading = function(interrupt){
	interruptImageBarLoading();
	var grayer = document.getElementById('showim_loading');
	var image = document.getElementById('showim');	
	grayer.style.width =  image.width;
	grayer.style.height =  image.height;
	grayer.style.position='absolute';
	var ypos = findPosY(image);
	var xpos = findPosX(image);
	grayer.style.top = ypos;
	grayer.style.left = xpos;	
	grayer.style.display='block';
	var grayer_im = document.getElementById('showim_loading_plw');
	grayer_im.style.position='relative';
	grayer_im.style.top = (image.height/2)-grayer_im.height;
	grayer_im.style.left = (image.width/2)-grayer_im.height;
}
var firstTime = true;
var imageLoaded = function(){
	var grayer = document.getElementById('showim_loading');
	grayer.style.display='none';
	if(!firstTime && (startAtImage < images.length) && window.loaded){
		resumeImageBarLoading();
	}
	firstTime=false;
}

function atc(){
	addToContacts(document.getElementById('sharetoFreetxt').value);
	document.getElementById('sharetoFreetxt').value='';
}

function updatePrevAndNext(shared,album,sortby,usr,oldimage,newimage,index){
	var prev = document.getElementById('imb_prev_link');
	var nexts = document.getElementsByName('fixed_changeImageLink');	
	var imlength = images.length;
	var nextslength = nexts.length;
	if(parseInt(index)>0){
		prev.href='javascript:changeImage('+shared+','+album+',\''+sortby+'\',\''+usr+'\','+newimage+','+images[(parseInt(index)-1)]+','+(parseInt(index)-1)+');';
	}
	else
		prev.href='javascript:changeImage('+shared+','+album+',\''+sortby+'\',\''+usr+'\','+newimage+','+images[(imlength-1)]+','+(imlength-1)+');';
	var next_link='';
	if(parseInt(index)<(imlength-1)){
		next_link='javascript:changeImage('+shared+','+album+',\''+sortby+'\',\''+usr+'\','+newimage+','+images[(parseInt(index)+1)]+','+(parseInt(index)+1)+');';
	}
	else
		next_link='javascript:changeImage('+shared+','+album+',\''+sortby+'\',\''+usr+'\','+newimage+','+images[0]+','+0+');';	
	for(var i=0;i<nextslength;i++){
		next=nexts[i];
		next.href=next_link;
		
	}
}

document.getElementsByClassName = function(className, parentElement) {
	  if (Prototype.BrowserFeatures.XPath) {
	    var q = ".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]";
	    return document._getElementsByXPath(q, parentElement);
	  } else {
	    var children = ($(parentElement) || document.body).getElementsByTagName('*');
	    var elements = [], child;
	    for (var i = 0, length = children.length; i < length; i++) {
	      child = children[i];
	      if (Element.hasClassName(child, className))
	        elements.push(Element.extend(child));
	    }
	    return elements;
	  }
};

function scrollImageBar(newimage,index,oldimage){
	var imageBarDiv = document.getElementById('imageBarContainer');
	var imageDiv = document.getElementById('imageContainerDiv'+newimage);
	//Remove the frame about the previously selected image

	var oldImageTds = document.getElementsByClassName('specific_album_imagerowTD_Active');

	if(oldImageTds != null){
		var length = oldImageTds.length;
		for(i=0;i<length;i++){
			var oldImageTd = oldImageTds[i];
			oldImageTd.className = 'specific_album_imagerowTD';	
			oldImageTd.name = 'not_highlighted';
		}
	}
	//Highlight the new one.
	var newImageTd = document.getElementById('tblCol'+newimage);
	if(newImageTd != null){
		newImageTd.className = 'specific_album_imagerowTD_Active';
		newImageTd.name = 'highlighted';
	}
	scrollInDiv(imageBarDiv,imageDiv);
}
function scrollInDiv(container, element){
	if(container == null || element == null)
		return false;
  var container_x = findPosX(container);
  var element_x = findPosX(element);
  var offset =350;
  new Effect.Scroll(container, {x:(element_x-container_x-offset), y:0});
  return false;
}
function extendOneMonth(uid){
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var url=host+"/popup_actions/api.html";
	var params = new Array(new Array('func','extendonemonth'), new Array('uid',uid));
	showPopup(url, params);
}
function showSimplePopup(text){
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var url=host+"/popup_actions/simplePopup.html";
	showPopup(url,new Array(new Array('text',text)));
}
function addToContacts(contact)
{

	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	if(thirdslashpos>0)
		host = host.substr(0,thirdslashpos);
	else
		host = 'http://'+host+'/';
	var page = host+'popup_actions/api.html?func=addtocontacts&contact_name=&contact_email='+contact;
	//Get which checkboxes are checked currently.
	var elems = document.getElementsByTagName('input');
	for(var i=0; i<elems.length;i++){
		var name = elems[i].name;
		if(name.substr(0,3)=='chk'){
			if(elems[i].checked==true){
				page += '&checkThese[]='+name;
			}
		}
	}

	var r_args=new Array();
	sendCall(page,reposFillDivsAndShow,r_args);
}
function updateComments(imid){
	//Get the comments for this image.
	$comments=getComments(imid);
}
function updateTags(imid){
	//Get the comments for this image.
	$comments=getTags(imid);
}

function highlightRating(rateval){
	var rateImgs = document.getElementsByClassName('rate_image');
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	for(var i = 0; i<rateImgs.length;i++){
		var rateImgNumber = rateImgs[i].id.replace(/rate_image/,'');
		if(rateImgNumber<=rateval){
			rateImgs[i].src=host+'/images/blue_star.png';;
		}
		else{
			rateImgs[i].src=host+'/images/blank_star.png';
		}
	}
}
//Reloads all img tags with name 
function reloadImages(imid){
	var imgs = document.getElementsByName('rl_image_'+imid);
	for(var i=0;i<imgs.length;i++){
		var oldsrc=imgs[i].src;
		imgs[i].src='';
		//remove any rl= req var
		if(oldsrc.indexOf('&rl=')!=-1)
			oldsrc=oldsrc.replace(/&rl=[0-9]*/,'&rl='+(new Date()).getTime());
		else
			oldsrc=oldsrc+'&rl='+(new Date()).getTime();
		imgs[i].src=oldsrc;
	}
}

function swapImage(im,url){
	im.src=url;
}
function parseHash(){
	var hash = window.location.hash;
	var loc = window.location;
	if(hash != null){
//		alert(loc + ' '+hash);		
		//Strip hash from location.
		//replace the current url with one pointing to the hash values. strip everyting after username (with @) and add hash after that.
		
	}
}

function minimizeSlide(){
	setSlideSize(400,400);
	document.getElementById('minmaxlbl').innerHTML='Maximize';
	document.getElementById('minmaxA').href='javascript:maximizeSlide();';		 	
}
function maximizeSlide(){
	     var x,y;
         var test1 = document.body.scrollHeight;
         var test2 = document.body.offsetHeight
         if (test1 > test2) // all but Explorer Mac
      	 {
			x = document.body.clientWidth;
            y =document.body.clientHeight
         }
         else // Explorer Mac;
         {
	         x = document.body.offsetWidth;
             y = document.body.offsetHeight;
         }
	     //To have room for close bar
	     y=y-50;
	     x=x-10;
		 setSlideSize(x,y);
		 document.getElementById('minmaxlbl').innerHTML='Minimize';
		 document.getElementById('minmaxA').href='javascript:minimizeSlide();';		 
}
function setSlideSize(width,height){
	var frm = window.frames['slideshow_iframe'];

	var flash_arr = document.getElementsByName('kabFlash');
	for(var i=0;i<flash_arr.length;i++){
		var flash=flash_arr[i];
		flash.setAttribute("width", width);
		flash.setAttribute("height", height);		
	}
	var scc=document.getElementById('popupContent');
	scc.style.display='none';
	scc.style.width=width;
	scc.style.height=height;		
	
	var slideFrm=document.getElementById('slideshow_iframe');
	slideFrm.style.width=width;
	slideFrm.style.height=height;	
	
	//Reposition slide to middle of window.
	centerObject('popupContent');
	scc.style.display='block';	
}
function centerObject(objid){

	var obj=document.getElementById(objid.replace(/^\s+|\s+$/g, ''));
	var height = obj.offsetHeight;
	var width = obj.offsetWidth;
	//get the upperleft offset of the current scrollposistion
	var uy = document.body.scrollTop;
	var rx = document.body.scrollLeft;
	var w_width= document.body.clientWidth;
	var w_height = document.body.clientHeight;
	//So i need position w_width-(width/2)+rx
	var new_x = Math.max(0,((w_width/2)-(width/2)+rx));
	var new_y = Math.max(0,((w_height/2)-(height/2)+uy));	
	obj.style.top=new_y;
	obj.style.left=new_x;	
}

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 myAlbums_image_mouseover(imid){
	
	//Show an overlay with share, view and some other button;
	imdiv=document.getElementById('imdiv'+imid);
	var image = document.getElementById('td_imdiv'+imid);
	
	imdiv.style.position='absolute';
	imdiv.style.top=findPosY(image);
	imdiv.style.left=findPosX(image);
	imdiv.style.width=image.style.width;
	imdiv.style.height=image.style.height;
	
	//Transparentify another div
	makeTransparent('bg_imdiv'+imid,0.2);
	
	imdiv.style.visibility='visible';
	imdiv.style.display='block';	
}

function myAlbums_image_mouseout(imid){
	//Show an overlay with share, view and some other button;
	imdiv=document.getElementById('imdiv'+imid);
	imdiv.style.visibility='hidden';
	imdiv.style.display='none';
	makeTransparent('bg_imdiv'+imid,1);	
}
function makeTransparent(divid,opacity){
		div=document.getElementById(divid);
		div.style.opacity=opacity;
		div.style.MozOpacity=opacity;
		div.style.filter = "alpha(opacity=" + (opacity*100) + ")";
		div.style.KhtmlOpacity = (opacity / 100); 
}
function makeAllImagesTransparent(imid,opacity,include_showim){
		if(include_showim){
				makeTransparent('showim',opacity);
		}
		var imgs = document.getElementsByName('rl_image_'+imid);
		for(var i=0;i<imgs.length;i++){
			makeTransparent(imgs[i].id,opacity);
		}
}
function setBG(element,color){
		document.getElementById(element).style.bgColor=color;
}
function showPopup(page,r_vars){
	var sco=document.getElementById('popupContainer');
	sco.style.visibility='visible';
	sco.style.display='block';
	var scn=document.getElementById('popupContent');
/*
	scn.style.visibility='hidden';
	scn.style.display='none';
*/
	scn.style.visibility='visible';
	scn.style.display='block';
	scn.innerHTML='<img src="/images/plw.gif" />';
	centerObject('popupContent');

	//Resize the background to fill the page.
	sco.style.height=document.body.scrollHeight;
	sco.style.width=document.body.scrollWidth;
	//Make the url to our page
	var params = '';
	if(r_vars.length>0)
		params += ''+r_vars[0][0]+'='+r_vars[0][1];
	for(var i=1;i<r_vars.length;i++){
		params += '&'+r_vars[i][0]+'='+r_vars[i][1];
	}
	if(r_vars.length>0)
		params += '&';
	var loc = window.location;
	params += 'backTo='+loc;
	
	var r_args=new Array();
	r_args[0]='popupContent';
	sendPost(page,reposFillDivsAndShow,r_args,params);
}
function showRequestPasswordPrompt(user){
	var url = '/popup_actions/api.html?func=reqpassprompt&usr='+user;
	var sco=document.getElementById('popupContainer');
	sco.style.visibility='visible';
	sco.style.display='block';
	var scn=document.getElementById('popupContent');
	scn.style.visibility='hidden';
	scn.style.display='none';
	//Resize the background to fill the page.
	sco.style.height=document.body.scrollHeight;
	sco.style.width=document.body.scrollWidth;
	//Make the url to our page
	
	var r_args=new Array();
	r_args[0]='popupContent';
	sendPost(url,reposFillDivsAndShow,r_args,'');
}
function showSigninWithReturn(text,imid){
	var backToUrl = window.location.toString();
	backToUrl = backToUrl.replace(/#/,'');
	if(imid!=''){
		if(backToUrl.search(/asset[0-9]*/)>0){
			backToUrl = backToUrl.replace(/asset[0-9]*/,'asset'+imid);
		}
		else{
			backToUrl += '/asset'+imid
		}
	}
	var url = '/popup_actions/signinfirst.html?wt=1&text='+text+'&backTo='+backToUrl;
	
	var sco=document.getElementById('popupContainer');
	sco.style.visibility='visible';
	sco.style.display='block';
	var scn=document.getElementById('popupContent');
	scn.style.visibility='hidden';
	scn.style.display='none';
	//Resize the background to fill the page.
	sco.style.height=document.body.scrollHeight;
	sco.style.width=document.body.scrollWidth;
	//Make the url to our page
	
	var r_args=new Array();
	r_args[0]='popupContent';
	sendPost(url,reposFillDivsAndShow,r_args,'');
}
function hidePopup(){
	sco=document.getElementById('popupContainer');
	scn=document.getElementById('popupContent');	
	sco.style.visibility='hidden';
	sco.style.display='none';
	scn.style.visibility='hidden';
	scn.style.display='none';
	scn.style.top='0px';
	scn.style.left='0px';	
}
function resizePopup(width,height){
	scn=document.getElementById('popupContent');	
	scn.style.width=width+'px';
	scn.style.height=height+'px';
}
function getWindowSize(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else
		if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )){
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
	}
	else
		if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
	return new Array(myWidth,myHeight);
}

/* AJAX related functionality */
function getHTTPRequestObject() {

  var xmlHttpRequest;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (exception1) {
      try {
        xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (exception2) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttpRequest = false;
  @end @*/
 
  if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlHttpRequest = new XMLHttpRequest();
    } catch (exception) {
      xmlHttpRequest = false;
    }
  }
  return xmlHttpRequest;
}

function sendCall(URL,response_function,arguments){
	//Get the contents to fill the popup with
	var httpReq = getHTTPRequestObject(); 
	httpReq.open("POST", URL, true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=latin1");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			//Get the arguments from the html returned. The text should be argument1#|#argument2#|#...#|#argumentX
			var txt = httpReq.responseText;
			var args=txt.split("#|#");
			response_function(args);
		}
	};
    httpReq.send(null);
}
function sendPost(URL,response_function,arguments,params){
	//If URL if http but the page we're viewing if https it won't work, so we need to fix that
	var http = true;
	if(window.location.toString().indexOf('https://') != -1)
		http=false;
	if(URL.indexOf('https://') != -1 && http){
		//URL is https but page is http
		URL = URL.replace('https:','http:');
	}
	if(URL.indexOf('https://') == -1 && !http){
		//URL is http but page is https
		URL = URL.replace('http:','https:');
	}
	
	var httpReq = getHTTPRequestObject(); 
	httpReq.open("POST", URL, true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=latin1");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			//Get the arguments from the html returned. The text should be argument1#|#argument2#|#...#|#argumentX
			var txt = httpReq.responseText;
			var args=txt.split("#|#");
			response_function(args);
		}
	};
    httpReq.send(params);
}
function updateImageBar(focusedimage,sharedAlbum,albumId,sortby,imid,usr,index){
	//The the new contents of the div with ajax.
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	httpReq.open("POST", host+"/popup_actions/imagebar.html?sharedAlbum="+sharedAlbum+"&aid="+albumId+"&sortby="+sortby+"&imid="+imid+"&usr="+usr+"&index="+index, true);
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			var txt = httpReq.responseText;
			if(txt=='login'){
				showSigninWithReturn('This album is password protected, please login to see it:',imid);
			}
			else{
				//Get the arguments from the html returned. The text should be argument1#|#argument2#|#...#|#argumentX
				var div = document.getElementById('imagebar_container');
				div.innerHTML=txt;
			}
		}
	};
    httpReq.send(null);
}
function reposFillDivsAndShow(arguments){
	fillDivsAndShow(arguments);
	arguments[1] = arguments[1].replace(/^\s+|\s+$/g, '');
	centerObject(arguments[1]);
	document.getElementById(arguments[1]).style.display='block';
	document.getElementById(arguments[1]).style.visibility='visible';	
}
function fillDivsAndShowInline(arguments){
	//Assumes arguments is setup as: divcontents|divid
	var div = document.getElementById(arguments[1]);
	div.innerHTML=arguments[0];
	if(arguments.length>2)
		div.style.width=arguments[2];
	if(arguments.length>3)		
		div.style.height=arguments[3];	
	div.style.visibility='visible';
	div.style.display='inline';
}

function fillDivsAndShow(arguments){
	//Assumes arguments is setup as: divcontents|divid
	var div = document.getElementById(arguments[1].replace(/^\s+|\s+$/g, ''));
	div.innerHTML=arguments[0];
	if(arguments.length>2)
		div.style.width=arguments[2];
	if(arguments.length>3)		
		div.style.height=arguments[3];	
	div.style.visibility='visible';
	div.style.display='block';
}
function saveComment(imid){
	//Get the comment body
	var cbody=document.getElementById('commentBody').value;
	
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var params="imid="+imid+"&commentBody="+escape(cbody);

	httpReq.open("POST", host+"/popup_actions/saveComment.html", true);
	httpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1");
	httpReq.setRequestHeader("Accept-Charset","iso-8859-1");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			var txt = httpReq.responseText;
			if(txt=='login'){
				showSigninWithReturn('Sign in to save comments:',imid);
			}
			else{
				updateComments(imid);
				document.getElementById('commentBody').value='';
			}
		}
	};
    httpReq.send(params);

}
function getComments(imid){
	//Get the comment body
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var params="imid="+imid;
	httpReq.open("POST", host+"/popup_actions/getComments.html", true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			var txt = httpReq.responseText;
			var div = document.getElementById('allComments');
			div.innerHTML=txt;
		}
	};
    httpReq.send(params);
}
function getTags(imid){
	//Get the comment body
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var params="imid="+imid;
	httpReq.open("POST", host+"/popup_actions/getTags.html", true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			var txt = httpReq.responseText;
			var div = document.getElementById('allTags');
			div.innerHTML=txt;
		}
	};
    httpReq.send(params);
}

function rotateImage(imid,rotation){
	//overlay the image with something moving here!
	makeAllImagesTransparent(imid,0.5,true);
	//Send ajax request to make the rotation
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var params="imid="+imid+"&rotation="+rotation;
	httpReq.open("POST", host+"/popup_actions/rotateImage.html", true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			var img = document.getElementById('showim');
			img.src=img.src+'&clrcache=1';
			//Also reload any other examples of this image that may be on the current page.
			reloadImages(imid);
			img.onLoad=makeAllImagesTransparent(imid,1,true);
		}
	};
    httpReq.send(params);
}
function voteRating(imid,rating){
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var params="imid="+imid+"&rating="+rating;
	httpReq.open("POST", host+"/popup_actions/rateImage.html", true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			var nxt = document.getElementById('next_link');
			var txt = httpReq.responseText;
			if(txt=='login'){
				showSigninWithReturn('Sign in to rate images:',imid);
			}
			else{
				if(nxt!=null){
					nxt = nxt.href;
					eval(nxt);
				}
				else{
					updateRatings(imid);
				}
			}
		}
	};
    httpReq.send(params);
}
function updateRatings(image){
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var url=host+"/popup_actions/updateRatings.html?imid="+image;
	sendCall(url,fillDivsAndShowInline, new Array());
}
function updateNameAndDate(image,shared,usr){
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var url=host+"/popup_actions/api.html?func=getNameAndDate&usr="+usr+"&imid="+image+"&shared="+shared;
	sendCall(url,fillDivsAndShowInline, new Array());
}
function getHost(){
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);	
	return host;
}
function updateAvgRatings(image,shared){
	if(image != -1 )
	{
		var host = window.location.hostname;
		var thirdslashpos = host.indexOf("/",10);
		host = host.substr(0,thirdslashpos);
		var url=host+"/popup_actions/updateAvgRatings.html?shared="+shared+"&imid="+image;
		sendCall(url,fillDivsAndShow, new Array());
	}
	else
	{
		document.getElementById('avgRatingContent').style.display='none';
	}
}
function addTag(imid){
	//Get the tag body
	var tbody=document.getElementById('tag').value;
	if(tbody=='')
		return;
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var params="imid="+imid+"&tagBody="+tbody;
	httpReq.open("POST", host+"/popup_actions/saveTag.html", true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			//Get the arguments from the html returned. The text should be argument1#|#argument2#|#...#|#argumentX
			updateTags(imid);
			document.getElementById('tag').value='';
		}
	};
    httpReq.send(params);
}
function removeTag(imid,tagname){
	//Get the tag body
	var tbody=tagname;
	if(tbody=='')
		return;
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var params="imid="+imid+"&tagBody="+tbody;
	httpReq.open("POST", host+"/popup_actions/deleteTag.html", true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			//Get the arguments from the html returned. The text should be argument1#|#argument2#|#...#|#argumentX
			updateTags(imid);
		}
	};
    httpReq.send(params);
}
function togglePublic(aid,shrd){
	var httpReq = getHTTPRequestObject(); 
	var host = window.location.hostname;
	var thirdslashpos = host.indexOf("/",10);
	host = host.substr(0,thirdslashpos);
	var params="func=togglePublic&albumId="+aid+'&sha='+shrd;
	httpReq.open("POST", host+"/popup_actions/api.html", true);
	httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpReq.setRequestHeader("Content-length", params.length);
	httpReq.setRequestHeader("Connection", "close");
    httpReq.onreadystatechange = function () {
		if (httpReq.readyState == 4) {
			var retval = httpReq.responseText;
			var img = document.getElementById('pubpriv'+aid);
			if(img != null)
			{
				var txt = document.getElementById('pubprivtxt'+aid);
				if(retval==1){
					txt.innerHTML='Make<br />Private';
				}
				else{
					txt.innerHTML='Make<br />Public';
				}
			}
			else{
				var img = document.getElementById('pubpriv_ikon');
				if(retval==1){
					img.src='/images/private_icon.jpg'
					img.title="Set private";
					img.alt="Set private";
				}
				else{
					img.src='/images/public_icon.jpg'
					img.title="Set public";
					img.alt="Set public";
				}
			}
		}
	};
    httpReq.send(params);
}

/* END AJAX */

function checkEnter(e){ 
	var characterCode;
	if(e && e.which){ 
		e = e;
		characterCode = e.which;
	}
	else{
		e = event;
		characterCode = e.keyCode;
	}

	if(characterCode == 13){
		return true;
	}
	return false;
}

