/* AJAX Calls for new-post module */

// Ajax holders //
var xmlHttp;

// Ajax functions //

// observing all starboxes
document.observe('starbox:rated', saveUserRating);

function updateRatingInfo(element, info){
	var indicator = element.down('.indicator');
	var restore = indicator.innerHTML;
	indicator.update('Rated with ' + (info.rated).toFixed(1) + ' stars...');
	window.setTimeout(function() { indicator.update(restore) }, 3000);
	//new Effect.Highlight(indicator);
}

function saveUserRating(event) {
	new Ajax.Request('ajax_star_user.php',
	{
	method:'post',
	parameters: event.memo,
	onSuccess: function(transport){
	  var response = transport.responseText;
		if(response != "FAILED"){ // custom return message				
			//Effect.Appear('post-'+str+'-media');
			//document.getElementById('contact-'+id).innerHTML = response;
		}else{
			//Effect.Appear('post-'+str+'-media');
			alert('Something went wrong...\nAre you trying to rate yourself?');
		}
	},
	onFailure: function(){ alert('Something went wrong...') }
	});
}

var galleryOffset = 0;
var currentPage = 1;
var switching = false;
function galleryRight(id) {
	if(!switching){
	  switching = true;
	  var nextPage = currentPage+1;
	  galleryOffset = galleryOffset + 10;
	  if(nextPage <= galleryPages){
		document.getElementById('galleryPage').innerHTML = nextPage;
		// turn the current page OFF
		document.getElementById('gallery-'+currentPage).style.display = 'none';
		// turn the new page ON
		document.getElementById('gallery-'+nextPage).style.display = 'block';
		
		currentPage = nextPage;
		
		// adjust arrows
		if(currentPage-1 < 1){ // no prev page available
		  document.getElementById('galleryLeftButton').style.display = 'none';
		}else{
		  Effect.Appear('galleryLeftButton');
		}
		if(currentPage >= galleryPages){ // no next page available
		  document.getElementById('galleryRightButton').style.display = 'none';
		}else{
		  Effect.Appear('galleryRightButton');
		}
		switching = false;
		/*new Ajax.Request('ajax_show_photos.php',
		{
		method:'post',
		parameters: 'q='+id+'&offset='+galleryOffset,
		onSuccess: function(transport){
		  var response = transport.responseText;
		  document.getElementById('post-gallery').innerHTML = response;
		  document.getElementById('galleryPage').innerHTML = nextPage;
		  decoGreyboxLinks();
		  currentPage = nextPage;
		  // adjust arrows
		  if(currentPage-1 < 1){ // no prev page available
			  document.getElementById('galleryLeftButton').style.display = 'none';
		  }else{
			  Effect.Appear('galleryLeftButton');
		  }
		  if(currentPage >= galleryPages){ // no next page available
			  document.getElementById('galleryRightButton').style.display = 'none';
		  }else{
			  Effect.Appear('galleryRightButton');
		  }
		  switching = false;
		},
		onFailure: function(){ alert('Something went wrong...'); switching = false;}
		}); */
	  }
	}
}

function galleryLeft(id) {
	if(!switching){
	  switching = true;
	  var nextPage = currentPage-1;
	  galleryOffset = galleryOffset - 10;
	  if(nextPage > 0){
		  document.getElementById('galleryPage').innerHTML = nextPage;
		  
		  // turn the current page OFF
		  document.getElementById('gallery-'+currentPage).style.display = 'none';
		  // turn the new page ON
		  document.getElementById('gallery-'+nextPage).style.display = 'block';
		  
		  currentPage = nextPage;
		  // adjust arrows
		  if(currentPage-1 < 1){ // no prev page available
			  document.getElementById('galleryLeftButton').style.display = 'none';
		  }else{
			  Effect.Appear('galleryLeftButton');
		  }
		  if(currentPage <= galleryPages){ // no next page available
			  Effect.Appear('galleryRightButton');
		  }else{
			  document.getElementById('galleryRightButton').style.display = 'none';
		  }
		  switching = false;
		/*
		new Ajax.Request('ajax_show_photos.php',
		{
		method:'post',
		parameters: 'q='+id+'&offset='+galleryOffset,
		onSuccess: function(transport){
		  var response = transport.responseText;
		  document.getElementById('post-gallery').innerHTML = response;
		  document.getElementById('galleryPage').innerHTML = nextPage;
		  // decoGreyboxLinks(); - creates doubles
		  currentPage = nextPage;
		  // adjust arrows
		  if(currentPage-1 < 1){ // no prev page available
			  document.getElementById('galleryLeftButton').style.display = 'none';
		  }else{
			  Effect.Appear('galleryLeftButton');
		  }
		  if(currentPage <= galleryPages){ // no next page available
			  Effect.Appear('galleryRightButton');
		  }else{
			  Effect.Fade('galleryRightButton');
		  }
		  switching = false;
		},
		onFailure: function(){ alert('Something went wrong...'); switching = false; }
		});*/
	  }
	  
	}
}

function thumbs(id,thumbType){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Sorry, Your Browser does not support HTTP Request")
		return;
	}

	if(id == '' || thumbType == '')
		return;
	
	// generate request handler
	var url="ajax_thumbs.php";
	url = url + "?id=" + id + "&thumbType=" + thumbType;
	//url = url + "&sid=" + Math.random();
	
	// do it
	xmlHttp.onreadystatechange = function stateChanged(){
		// while processing
		if(!(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){
			//Effect.Appear('working-'+str);
		}
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			// update the parent page like so: document.getElementById("txtHint").innerHTML=xmlHttp.responseText
			//Effect.Squish('working-'+str);
			if(xmlHttp.responseText != "FAILED"){ // custom return message				
				//Effect.Appear('post-'+str+'-media');
				document.getElementById('thumbs-'+id).innerHTML = xmlHttp.responseText;
			}else{
				//Effect.Appear('post-'+str+'-media');
				document.getElementById('thumbs-'+id).innerHTML = "Oops, error, can't thumb...";
			}
			// now update prices as well
		}
	}// end function
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}// end ajax function

function sendPM(user,id){
	if(id == '' || user == '')
		return;
	
	var message = tinyMCE.activeEditor.getContent();//document.getElementById('message-'+id).value;
	if(message == ''){
		alert('Please enter your message');
		return;
	}
	
	new Ajax.Request('ajax_send_pm.php',
	  {
		method:'post',
		parameters: "id=" + id + "&user=" + encodeURIComponent(user) + "&message=" + encodeURIComponent(message),
		onSuccess: function(transport){
		  var response = transport.responseText;
		  	if(response != "FAILED"){ // custom return message				
				//Effect.Appear('post-'+str+'-media');
				document.getElementById('contact-'+id).innerHTML = response;
			}else{
				//Effect.Appear('post-'+str+'-media');
				document.getElementById('contact-'+id).innerHTML = "Oops, we could not send the message for some rason. Please try agian...";
			}
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });	
}

function showMedia(str){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Sorry, Your Browser does not support HTTP Request")
		return;
	}
	
	// generate request handler
	var url="ajax_show_media.php";
	url = url + "?q=" + str;
	//url = url + "&sid=" + Math.random();
	
	// do it
	xmlHttp.onreadystatechange = function stateChanged(){
		// while processing
		if(!(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){
			Effect.Appear('working-'+str);
		}
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			// update the parent page like so: document.getElementById("txtHint").innerHTML=xmlHttp.responseText
			Effect.Squish('working-'+str);
			if(xmlHttp.responseText != "FAILED"){ // custom return message
				Effect.Appear('post-'+str+'-media');
				document.getElementById('post-'+str+'-media').innerHTML = xmlHttp.responseText;
				Nifty("div.post-images");
				//initLightbox(); // for the new images which just appeared
			}else{
				Effect.Appear('post-'+str+'-media');
				document.getElementById('post-'+str+'-media').innerHTML = "No Media Available for this post!";
			}
			// now update prices as well
		}
	}// end function
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}// end ajax function

function submitTheComment(user_id, post_id, username, notify, comment){
	comment = comment.replace(/\n/g, "<br />"); // add new line to comments since they dissapear when sent by GET
	Effect.Appear('working');
	
	new Ajax.Request('ajax_submit_comment.php',
	  {
		method:'post',
		parameters: "user_id=" + user_id + "&post_id=" + post_id + "&username=" + encodeURIComponent(username) + "&notify=" + notify + "&comment=" + encodeURIComponent(comment),
		onSuccess: function(transport){
			var response = transport.responseText;
			// update the parent page like so: document.getElementById("txtHint").innerHTML=xmlHttp.responseText
			Effect.Squish('working');
			if(response != "FAILED"){ // custom return message
				// create new comment div and insert it above
				var newCommentWrapper = document.createElement("div");
				newCommentWrapper.className = "post-comment-wrapper";
				var tmp_id = Math.random();
				newCommentWrapper.id = 'comment-' + tmp_id.toString();
				newCommentWrapper.style.display = "none";
				newCommentWrapper.innerHTML = response;
				document.getElementById('new-post-comment-holder').appendChild(newCommentWrapper);								
				NiftifyThePage(); // round corners of new comment
				Effect.BlindDown('comment-' + tmp_id.toString());
				// remove the new comment form
				Effect.SwitchOff('newCommentBox');
				// remove the button too
				Effect.SwitchOff('commentButton');
				// detailing effect - update the # of comments to + 1
				var comments_start = parseInt(document.getElementById('total-comments-number').innerHTML);
				var comments_end = comments_start + 1;
				document.getElementById('total-comments-number').innerHTML = comments_end.toString();
				document.getElementById('newPostCommentNumber').innerHTML = comments_end.toString();
				if(comments_end != 1)
					document.getElementById('total-comments-string').innerHTML = "comments";
				else
					document.getElementById('total-comments-string').innerHTML = "comment";
				//Effect.Pulsate('total-comments', {pulses:2});
			}else{
				alert('System error ... Can\'t seem be able to send your comment now.\nPlease try again later...');
			}
		},
		onFailure: function(){ alert('Something went wrong...') }
	  });	
}

function addPostToFavorites(str){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Sorry, Your Browser does not support HTTP Request")
		return;
	}
	
	// generate request handler
	var url="ajax_add_to_favorites.php";
	url = url + "?q=" + str;
	//url = url + "&sid=" + Math.random();

	// do it
	xmlHttp.onreadystatechange = function stateChanged(){
		// while processing
		if(!(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){
			// nothing really
		}
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			if(xmlHttp.responseText == "FAILED"){
				alert('Oops, can\'t do that now ...');	
			}else{
				if(xmlHttp.responseText == "LOGIN"){
					alert('You must sign in first');
				}else{
					if(xmlHttp.responseText == "INFAVS"){
						alert('The post is already in your favorites :)');	
					}else{ // OK ADD IT
						Effect.Puff('add-favorites-'+str);
						Effect.Appear('added-favorites-'+str);
						Effect.Puff('post-interact-add-faves-'+str);
						Effect.Appear('post-interact-remove-faves-'+str);
						
						// detailing effect - update the # of comments to + 1
						var favs_start = parseInt(document.getElementById('topFavCountNumber').innerHTML);
						var favs_end = favs_start + 1;
						document.getElementById('topFavCountNumber').innerHTML = favs_end.toString();
						if(favs_end != 1)
							document.getElementById('topFavCount').innerHTML = "posts";
						else
							document.getElementById('topFavCount').innerHTML = "post";						
					}
				}
			}
		}
	}// end function
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}// end ajax function

function removePostFromFavorites(str){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Sorry, Your Browser does not support HTTP Request")
		return;
	}
	
	// generate request handler
	var url="ajax_remove_from_favorites.php";
	url = url + "?q=" + str;
	//url = url + "&sid=" + Math.random();

	// do it
	xmlHttp.onreadystatechange = function stateChanged(){
		// while processing
		if(!(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){
			// nothing really
		}
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			if(xmlHttp.responseText == "FAILED"){
				alert('Oops, can\'t do that now ...');	
			}else{
				if(xmlHttp.responseText == "LOGIN"){
					alert('You must sign in first');
				}else{ // OK
					Effect.Puff('added-favorites-'+str);
					Effect.Appear('add-favorites-'+str);
					Effect.Puff('post-interact-remove-faves-'+str);
					Effect.Appear('post-interact-add-faves-'+str);

					// detailing effect - update the # of comments to + 1
					var favs_start = parseInt(document.getElementById('topFavCountNumber').innerHTML);
					var favs_end = favs_start - 1;
					document.getElementById('topFavCountNumber').innerHTML = favs_end.toString();
					if(favs_end != 1)
						document.getElementById('topFavCount').innerHTML = "posts";
					else
						document.getElementById('topFavCount').innerHTML = "post";
				}
			}
		}
	}// end function
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}// end ajax function

// Main Ajax GetXML function
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch(e){
		//Internet Explorer
		 try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 }
		 catch(e){
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		 }
	}
	return xmlHttp;
}// end function