//__________________________________________________________________________________________________________________________________________________________________
// PHOTO PAGE CODE
//__________________________________________________________________________________________________________________________________________________________________	
	// VARS
	current_index					= 0				// The index in $f->photos array of the thumb clicked
	current_image					= new Object();	// Object containing data for currently seleced image
	current_image.id				= 0;			// Integer holding the index to the current image in the $f->photos array within PHP (populated by PHP when loading page)
	current_image.url				= "";
	current_image.title				= "";
	current_image.description		= "";
	current_image.owner				= "";
	current_image.comments			= "";
	current_image.comment_authors	= "";
	current_image.flickr_url		= "";
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------
	// MAJOR FUNCTIONS
	
	/**
	* Reveal the big image
	* @param i index of the image to reveal
	*/
	function revealImage(i){
	
		// If it's been passed a thumb number, then a thumb's been clicked
		if(i!=undefined){
			
			current_index=i;
		
			// Set the index of the photo in the photo_q array
			photo_q_index=current_index - start_num;
			
			// Update the current image with new vars
			current_image.id=current_index;
			current_image.url=photo_q[photo_q_index].url
			current_image.title=photo_q[photo_q_index].title
			current_image.description=photo_q[photo_q_index].description
			current_image.owner=photo_q[photo_q_index].owner
			current_image.flickr_url=photo_q[photo_q_index].flickr_url
			current_image.comments=[];
			current_image.comment_authors=[];
			for(var i=0;i<photo_q[photo_q_index].comments.length;i++){
				current_image.comments.push(photo_q[photo_q_index].comments[i])
				current_image.comment_authors.push(photo_q[photo_q_index].comment_authors[i])
			}
			
			
		}
		else{
			// We're arriving on the page for the first time, so PHP will populate current_image
		}
		
		// SET NEW IMAGE
		//alert(current_image.flickr_url)
		document.getElementById('image_holder').innerHTML="<a href='"+current_image.flickr_url+"' title='"+getReadableString(current_image.title)+" on Flickr'><img src='"+current_image.url+"' alt='' /></a>";
		
		// REVEAL INFO
		revealInfo();
		
		// PRELOAD NEXT IMAGE
		preloadNextImage()
		
	}
	
	/**
	* Load in the next image
	*/
	function preloadNextImage(){
		var next_image=new Object();
		next_image.index= current_index+1
		
		// Set the index of the photo in the photo_q array
		var photo_q_index=next_image.index - start_num;
		
		// Load in the next image into an invisible placeholder
		if(!empty(photo_q[photo_q_index])){
			next_image.url=photo_q[photo_q_index].url
			next_image.image= new Image(1,1); 
			next_image.image.src=photo_q[photo_q_index].url;
		}
			
	}
	/**
	* Reveal the comments for the big image
	* @param	saving	Boolean set to true when saving a comment
	*/
	function revealComments(saving){
	
		// Add comments
		var commentHTML="";
		var tot_comments=current_image.comments.length
		
		// Display comments
		if(tot_comments>0){
			
			// Add header
			if (document.getElementById('photo_comments_header')!=undefined){
				document.getElementById('photo_comments_header').style.display="inline";
			}
			
			// Loop through each comment
			for(var i=0;i<tot_comments;i++){
				
				// Alternate the bg colour
				if(i % 2 ==0){
					commentHTML+="<div class='comment_even'>\n";
				}else{
					commentHTML+="<div class='comment_odd'>\n";
				}
				
				// Grab a cleaned up comment
				var c=getReadableString(current_image.comments[i])
				//var c=unescape(current_image.comments[i]);
				//c=plusUnescape(c)
				
				// unescapeHTML the comment if it's just been saved (Really silly bug)
				if(saving){
					if(i<tot_comments-1){
						//c=c.unescapeHTML()
					}
					
				}else{
					//c=c.unescapeHTML()
				}
				
				c=stripslashes(c);
				
				// Get the comment author
				var author=current_image.comment_authors[i]
				author=unescape(author)
				author=plusUnescape(author)
				//author=author.unescapeHTML()
				
				if(author!="__arika__"){
					if(author!=undefined&&author!="undefined"){
						if(author==""){
							author="Somebody"
						}
						
						commentHTML+="<b>"+author+" says:</b> "
					}
				}
				// Add comment to the big string
				commentHTML+=c
				commentHTML+="\n</div>\n\n";
				
			}
			
			// Write the comments string
			document.getElementById('photo_comments').innerHTML=commentHTML;
		}
		
		// Don't display comments if there aren't any
		else{
			if (document.getElementById('photo_comments_header')!=undefined){
				document.getElementById('photo_comments_header').style.display="none";
				document.getElementById('photo_comments').innerHTML=""
			}
		}
		
	}
	
	/**
	* Transform a urlencoded string into a readable one
	* @param	str	String to process into a readable string (i.e. unescape, remove plus symbols etc)
	* @return str The transformed string
	*/
	function getReadableString(str){
		str=plusUnescape(unescape(str))
		return str
	}
	
	/**
	* Reveal all info for the big image
	* @param	saving	Boolean set to true when saving a comment
	*/
	function revealInfo(saving){
		
		// SET NEW INFO
		// Title
		document.getElementById('photo_title').innerHTML=getReadableString(current_image.title);
		
		// Description
		var d=plusUnescape(unescape(current_image.description));
		//d=d.unescapeHTML();
		
		// Add a photo credit if it's not an arika picture
		if(current_image.owner!="__arika__"){
			// Get the image owner (unescaped)
			var owner=current_image.owner
			owner=unescape(owner)
			owner=plusUnescape(owner)
			//owner=owner.unescapeHTML()
			d+=" -- uploaded by <b>"+owner+"</b>"
		}
		if (document.getElementById('photo_description')!=undefined){
			document.getElementById('photo_description').innerHTML=d;
		}
		
		revealComments(saving)
		
	}


	
	saveComment=function(){
		var comment_author="<b>";
		var photo_comment=document.getElementById('photo_comment').value;
		comment_author+=document.getElementById('comment_author').value;
		
		
		// Parse comment_author text
		if((comment_author=="")||(comment_author=="<b>Your name")){
			comment_author="<b>Somebody"
		}
		
		// Parse photo_comment text
		if((photo_comment=="")||(photo_comment=="Add comment")){
			// Show warning
			alert('Add a comment first please!');
		}
		
		else{
			comment_author+=" says:</b>  "
			var commentHTML=comment_author+photo_comment;
			commentHTML=escape(commentHTML)
			
			// Save the comment to Flickr
			//alert(current_index)
			var data={"action":"save_comment", "photo_comment":commentHTML, "current_index":current_index};
			var json_string = JSON.stringify(data);
			$.post('ajax.php', { json_string: json_string}, saveCommentCallback, "text");

			
			/*
			var data_string="action=save_comment&photo_comment="+commentHTML+"&current_index="+current_index;
			var ajax = new GLM.AJAX();
			ajax.callPage("ajax.php?"+data_string, saveCommentCallback);	
			*/
			
			//alert(commentHTML)
			// Bung the new comment into the current_image object
			current_image.comments.push(commentHTML);
			

			// Bung the new comment into the photo_q array ONLY IF THE IMAGE IS PRESENT IN THE THUMBS
			if((current_index>=start_num)&&(current_index<start_num+photo_q.length)){
				
				photo_q_index=current_index - start_num;
				photo_q[photo_q_index].comments.push(commentHTML);
			}


			revealComments(true);
			document.getElementById('photo_comment').value="Add comment"			
		}
	}

	saveCommentCallback=function(response){
		/*
		if(response){
			alert(response);
		}
		*/
	}