	/*
	* File: dive_operators.js
	* Custom logic for the page: travels_dive_operator
	*/

	$(document).ready(function () {

		// show reviews tab
		$("#openReviews").click(function () {
			$("#diveOperatorsTabs a").removeClass("on");
			$("#diveOperatorsTabsContent").find(".tab_content").hide();
			$("#tabReviews").addClass("on");
			$("#tabReviewsContent").show();
			window.location='#postReview';
			return true;
		});

		// tabs
		$("#diveOperatorsTabs a").click(function () {
			var tabID = $(this).attr("id");
			$("#diveOperatorsTabs a").removeClass("on");
			$(this).addClass("on");

			$("#diveOperatorsTabsContent").find(".tab_content").hide();
			$("#" + tabID + "Content").show();
		});

		// dive operator registration tabs
		/*
		$("#diveOperatorRegistrationTabs a").click(function () {
			var tabID = $(this).attr("id");
			var currentStep = $("#" + tabID + "Step").val();
			$("#diveOperatorRegistrationTabs a").removeClass("on");
			$(this).addClass("on");
			$("#step").val(currentStep);

			$("#diveOperatorRegistrationTabsContent").find(".tab_content").hide();
			$("#" + tabID + "Content").show();
		});
		*/

		// submit review
		$("#btnSubmitReview").click(function () {
			var html = "";
			var operatorID = $("#operator_id").val();
			var review = $("#c_comments").val();
			var rating = parseInt($("#rating").val());
			var rate = parseFloat($("#rate").val());
			var votes = parseInt($("#votes").val());

			var now = new Date();

			if (review == "")
			{
				alert ("Please enter your review");
				return false;
			}

			if (rating == "")
			{
				alert ("Please rate this operator");
				return false;
			}

			$.post("/ajax/dive_operator_reviews",
				{
					operator_id: operatorID,
					review: review,
					rating: rating,
					rate: rate,
					votes: votes
				},
	  		function(data)
	  		{
	    		if (data == "SUCCESS")
	    		{
						$("#be_the_first").hide();

						html += '<div class="ar_item">';
						html += '	<div class="ar_image"><a href="/user/' + username + '"><img src="' + user_photo + '" width="56" alt="' + full_name + '" title="' + full_name + '" /></a></div>';
						html += '	<div class="ar_body">';
						html += '		<div class="ar_rating">';
						html += '			Rating:<br />';
						for (i=1; i<=5; i++)
						{
							html += '<img src="/img/stars/star_small_' + ((i <= rating) ? "full" : "empty") + '.gif" width="10" height="11" />';
						}
						html += '		</div>';
						html += '		<div class="ar_date">' + dateFormat(now, "mmm d, yyyy, h:MM TT") + '</div>';
						html += '		<div class="ar_desc">';
						html += '			<a href="/user/' + username + '">' + full_name + '</a> reviewed this operator:';
						html += '			<div class="c" style="height: 5px;"></div>';
						html += '			<div style="padding-left: 20px;">';
						html += 				review.replace(/\n/g,"<br />");
						html += '			</div>';
						html += '		</div>';
						html += '	</div>';
						html += '	<div class="c"></div>';
						html += '</div>';
						html += '<div class="c"></div>';
						html += '<div class="a_item_delim"></div>';

						$("#dive_operator_reviews").prepend(html);
						$("#c_comments").val("");
						$("#rating").val("");

						var newVotes = votes + 1;
						var newRate = (rate * votes + rating) / newVotes;

						$("#rate").val(newRate.toFixed(2));
						$("#votes").val(newVotes);

						displayStars($("#rate").val());
	    		}
	    		else
	    		{
						//error
	    		}
	  		},
	  		"text"
	  	);
		});

	});


	function displayStars(r1)
	{
		var rate = parseFloat(r1);

		var rate1 = parseInt(rate);
		var rate2 = rate1 + 0.5;
		if(rate2 <= rate)
		{
			rate1 = rate2;
			rate2 = rate1 + 0.5;
		}

		rate = (rate - rate1 <= rate2 - rate) ? rate1 : rate2;

		for(i = 1; i <= 5; i++)
		{
			if(i - 0.5 == rate)
			{
				$("#star" + i).attr("src", s[0.5]);
			}
			else
			{
				$("#star" + i).attr("src", (i <= rate) ? s[1] : s[0]);
			}
		}
		return false;
	}

