// Application JavaScript
$(document).ready(function(){
	$("#viewAll").click(function(){
		var display = $("#subCategories").css('display');
		if (display != 'block')
		{
			$("#subCategories").slideDown('slow');
		}
		else
		{
			$("#subCategories").slideUp('slow');
		}
		return false;
	});
	
	$(".confirm").click(function(){
		return confirm('Are you sure?');
	});
	
	$(".imageGrid .thumbnail a").lightBox();
	$("a.lightbox").lightBox();
	
	$('.dhtmlTabs a').click(function(){return showTab($(this))});
	
})

// DHTML Tabs
function showTab(clickedElement)
{
	tabPanelId = clickedElement.attr('rel');
	if (tabPanelId != '')
	{
		$(".tabPanel").attr('class', 'tabPanel');
		$(".dhtmlTabs a").attr('class', '');
		$("#"+tabPanelId).attr('class', 'tabPanel tabCurrent');
		clickedElement.attr('class', 'selected');
		clickedElement.blur();
	}
	return false;
}

// DHTML Comment Posting
function postComment(profileId)
{
	var comment = $('#txtComment').val();
	if (comment != '')
	{
		// Comment was entered
		var url = BASE_HREF+'/comments/post';
		var data = {
			'profile_id'	: profileId,
			'body'			: comment
		};
		$.post(url, data, commentPosted);
	}
	else
	{
		// No comment entered
		alert('You must enter a comment first.');
	}
}

// Comment post ajax response
function commentPosted(data, textStatus)
{
	if (data == '1')
	{
		// Success
		$('#txtComment').attr('disabled', true);
		$('#btnPostComment').attr('disabled', true);
		$('#btnPostComment').hide();
		$('#posted').show();
	}
	else
	{
		// Fail
		alert('Error saving comment. Please try again later.');
	}
}


// DHTML Comment Posting
function postThreadedComment(profileId, commentId)
{
	var comment = $('#txtComment_'+commentId).val();
	if (comment != '')
	{
		// Comment was entered
		var url = BASE_HREF+'/comments/post';
		var data = {
			'profile_id'	: profileId,
			'body'			: comment,
			'comment_id'	: commentId
		};
		$.post(url, data, 
			function(response)
				{
					if (response == '1')
					{
						// Success
						$('#txtComment_'+commentId).attr('disabled', true);
						$('#btnPostComment_'+commentId).attr('disabled', true);
						$('#btnPostComment_'+commentId).text('Comment Posted');
					}
					else
					{
						// Fail
						alert('Error saving comment. Please try again later.');
					}
				}
			);
	}
	else
	{
		// No comment entered
		alert('You must enter a reply first.');
	}
}

// Comment post ajax response
function threadedCommentPosted(data, textStatus)
{
	if (data == '1')
	{
		// Success
		$('#txtComment_'+data['comment_id']).attr('disabled', true);
		$('#txtComment_'+data['comment_id']).text('Comment Posted!');
		$('#btnPostComment_'+data['comment_id']).attr('disabled', true);
		//$('#btnPostComment_'+data['comment_id']).text('Comment Posted');
	}
	else
	{
		// Fail
		alert('Error saving comment. Please try again later.');
	}
}


// DHTML Review Posting
function postReview(eventId)
{
	var review = $('#txtReview').val();
	var rating = $('.addReview .title input').val();
	if (rating == 0)
	{
	   var response = confirm("\"Never Again!\"\n\nThis will negatively count against this event.  Please confirm your 0 star rating.");   
	}
	if (response || rating != 0)
	{
    	if (review != '' && rating != '')
    	{
    	    //alert("event_id: "+ eventId + "\nrating: "+rating+"\nbody: "+review);
    		var url = BASE_HREF+'/reviews/post';
    		var data = {
    			'event_id'	: eventId,
    			'rating'	: rating,
    			'body'		: review
    		};
    		$.post(url, data, reviewPosted);
    	}
    	else
    	{
    		alert('You must enter a comment and/or a rating in order to post.');
    	}
	}
}
// Review Posted
function reviewPosted(data, textStatus)
{
	if (data == '1')
	{
		// Success
		$('#txtReview').attr('disabled', true);
		$('.star').attr('disabled', true);
		$('#btnPostReview').attr('disabled', true);
		$('#btnPostReview').text('Review Posted');
		$('#btnPostReview').hide();
		$('#reviewposted').show();
		
	}
	else
	{
		// Fail
		alert('Error saving review. Please try again later.');
	}
}

function postThreadedReview(eventId, reviewId)
{
	//alert(eventId +' '+ reviewId);
	var review = $('#txtReview_'+reviewId).val();
	if (review != '')
	{
		// Comment was entered
		var url = BASE_HREF+'/reviews/post';
		var data = {
			'event_id'		: eventId,
			'body'			: review,
			'review_id'		: reviewId
		};
		$.post(url, data, 
			function(response)
				{
					if (response == '1')
					{
						// Success
						$('#txtReview_'+reviewId).attr('disabled', true);
						$('#btnPostReview_'+reviewId).attr('disabled', true);
						$('#btnPostReview_'+reviewId).text('Comment Posted');
					}
					else
					{
						// Fail
						alert('Error saving comment. Please try again later.');
					}
				}
			);
	}
	else
	{
		// No comment entered
		alert('You must enter a reply first.');
	}
}

// Comment post ajax response
function threadedReviewPosted(data, textStatus)
{
	if (data == '1')
	{
		// Success
		$('#txtReview_'+data['review_id']).attr('disabled', true);
		$('#btnPostReview_'+data['review_id']).attr('disabled', true);
		$('#btnPostReview_'+data['review_id']).text('Comment Posted');
	}
	else
	{
		// Fail
		alert('Error saving comment. Please try again later.');
	}
}


function showImageUpload()
{
	$("#imageUploadFrame").toggle();
}

function refreshImageGrid()
{
	if (EVENT_ID)
	{
		var url = BASE_HREF+'/events/'+EVENT_ID+'/images';
		$.get(url, null, refreshImageGrid_callback);
	}
}

function refreshImageGrid_callback(data, textStatus)
{
	$("#imageGrid").html(data);
	$(".imageGrid .thumbnail a").lightBox();
	$("a.lightbox").lightBox();
}

function deleteSelectedImages()
{
	var selected = $(".imageGrid input[checked=true]");
	if (selected.length > 0)
	{
		if (confirm('Are you sure?'))
		{
			var image_ids = new Array();
			$.each(selected, function(){image_ids.push($(this).val())});
			var data = 
			{
				'event_id'		: EVENT_ID,
				'image_ids[]'		: image_ids
			};
			var url = BASE_HREF + '/images/delete';
			$.post(url, data, refreshImageGrid);
		}
	}
	else
	{
		alert('You must select at least one image first.');
	}
}

function postEventComment(eventId)
{
	var comment = $('#txtComment').val();
	if (comment != '')
	{
		// Comment was entered
		var url = BASE_HREF+'/comments/post';
		var data = {
			'event_id'		: eventId,
			'body'			: comment
		};
		$.post(url, data, eventCommentPosted);
	}
	else
	{
		// No comment entered
		alert('You must enter a comment first.');
	}
}

// Comment post ajax response
function eventCommentPosted(data, textStatus)
{
	if (data == '1')
	{
		// Success
		$('#txtComment').attr('disabled', true);
		$('#btnPostComment').attr('disabled', true);
		$('#btnPostComment').text('Comment Posted');
		$('#btnPostComment').hide();
		$('#posted').show();
	}
	else
	{
		// Fail
		alert('Error saving comment. Please try again later.');
	}
}


// DHTML Comment Posting
function postThreadedEventComment(eventId, commentId)
{
	var comment = $('#txtEventComment_'+commentId).val();
	if (comment != '')
	{
		// Comment was entered
		var url = BASE_HREF+'/comments/post';
		var data = {
			'event_id'		: eventId,
			'body'			: comment,
			'comment_id'	: commentId
		};
		$.post(url, data, 
			function(response)
				{
					if (response == '1')
					{
						// Success
						$('#txtEventComment_'+commentId).attr('disabled', true);
						$('#btnPostEventComment_'+commentId).attr('disabled', true);
						$('#btnPostEventComment_'+commentId).text('Comment Posted');
					}
					else
					{
						// Fail
						alert('Error saving comment. Please try again later.');
					}
				}
			);
	}
	else
	{
		// No comment entered
		alert('You must enter a reply first.');
	}
}
