viewerrating_box_height = 150;//increase the height of the content div by this amount to accommodate the extra height added by the ratings box
viewerrating_adjust_height = viewerrating_box_height;

/*
Show and Hide Voting functions are used to display the user login state

There are 2 stages the voting gallery goes through.
The first stage is loading of the image.
The second stage is loading of the rating through ajax.
Either stage can occur first because of the asynchronous nature.  This asynchronous nature causes a problem with displaying the correct state.

showVoteWrappers() and hideVoteWrappers() functions are controlled by the first stage.
showVote() and hideVote() functions are controled by the second stage.
Only when both show or both hide functions are executed the proper states will display
*/
function showVoteSystem(){
   var viewervotewrapper = document.getElementById( 'viewervotewrapper' );
   viewervotewrapper.style.display = 'block';
}

function showRating(){
   var viewerratingexternalwrapper = document.getElementById( 'viewerratingexternalwrapper' );
   viewerratingexternalwrapper.style.display = 'block';
}

function showVoteWrappers(){
   //showVoteSystem();
   showRating();
}

function hideVoteSystem(){
   var viewervotewrapper = document.getElementById( 'viewervotewrapper' );
   viewervotewrapper.style.display = 'none';
}

function hideVoteWrappers(){
   
   hideVoteSystem();

   var viewerratingexternalwrapper = document.getElementById( 'viewerratingexternalwrapper' );
   viewerratingexternalwrapper.style.display = 'none';
}

//user is logged in
function showVote(){
   var viewervote = document.getElementById( 'viewervote' );
   var viewerlogin = document.getElementById( 'viewerlogin' );
   var viewerratingwrapper = document.getElementById( 'viewerratingwrapper' );
   viewervote.style.display = 'block';
   viewerratingwrapper.style.display = 'block';
   viewerlogin.style.display = 'none';//hide login text
   viewerrating_adjust_height = viewerrating_box_height;
}

//user is logged out
function hideVote(){
   var viewerrating = document.getElementById ( 'viewerrating' );
   var viewervote = document.getElementById( 'viewervote' );
   var viewerlogin = document.getElementById( 'viewerlogin' );
   var viewercontent = document.getElementById( 'viewercontent' );
   var viewerratingwrapper = document.getElementById( 'viewerratingwrapper' );
   viewerrating.innerHTML = '';//prevent residual data from showing up when another image is selected
   viewerratingwrapper.style.display = 'none';
   viewervote.style.display = 'none';
   viewerlogin.style.display = 'block';//show login text

   //no rating box is displayed, reset height adjustment
   viewerrating_adjust_height = 60;
   viewercontent.style.height = '';
}

//retrieve ratings average
function getViewerVote(g_ary,g_index,image_suffix){
   var image = g_ary[g_index];
   var img_rate_prefix = 'rate_';

   //set onclick properties for rating anchors
   for(var i=1; i<=5; i++){
      eval('var '+img_rate_prefix+i+' = document.getElementById("'+img_rate_prefix+i+'")');
	  eval(img_rate_prefix+i+'.onclick = function(){rateFunction('+i+',g_ary,g_index,image_suffix);}');
   }

   //displays image rating average
   ajaxFunction('vote.php?output=viewerrating&img='+image+'&g_index='+g_index,'viewerrating');
}

//execute rating function through ajax
function rateFunction(rate,gallery_ary,index,image_suffix){
   var image = gallery_ary[index];
   ajaxFunction('vote.php?rate='+rate+'&img='+image);
   if( gallery_ary[index+1] ){
      show_pic( gallery_ary, index+1, image_suffix, true);
   }else{
	  hidepic();
   }
}

function ajaxFunction(file,thisdiv){
   var ajaxRequest;  // The variable that makes Ajax possible!
   var str;
   var obj = document.getElementById(thisdiv);

   try{
	  // Opera 8.0+, Firefox, Safari
	  ajaxRequest = new XMLHttpRequest();
   } catch (e){
	  // Internet Explorer Browsers
	  try{
		 ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
		 try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		 } catch (e){
			// Something went wrong
			alert("Please upgrade your browser!");
			return false;
		 }
	  }
   }

   // Create a function that will receive data sent from the server
   ajaxRequest.onreadystatechange = function(){
	  if(ajaxRequest.readyState == 4){
		 str = ajaxRequest.responseText;
		 //check if user is logged in
		 if(str.match('false')){
		    hideVote();
		 }else{
		    showVote();
			var lastIndex = str.lastIndexOf("_true");
			var new_str = '';

			//test if the user has already voted for this image this session
			if(lastIndex>-1){
				new_str = str.substr(0,lastIndex);
				hideVoteSystem();
				str = new_str;
			}else{
				showVoteSystem();
			}
			if(obj){
	           obj.innerHTML = str;
		    }
		 }
	  }
   }

   ajaxRequest.open("GET", file, true);
   ajaxRequest.send(null);
}