var QuickRate = {
	ExpandBox: function(id, nofocus)
	{
		$('#text_' + id).hide();
		$('#break_' + id).show();
		$('#save_' + id).show();
		$('#texta_' + id).css('fontSize', '12px');
		$('#texta_' + id).css('color', '#000');
		$('#texta_' + id).val('');
		if (nofocus == null || nofocus == false)
			$('#texta_' + id).focus();
	},

	MouseOver: function(id, num, clname, override)
	{
		if($('#' + id + 'div').rated && !override)
			return;
		if(clname == 'newstar')
			for (i = 1; i <= 5; i++)
				$('#' + id + 'star' + i).className = (i <= num) ? clname + '_rated' : clname;
		else if(clname == 'newfav' || clname == 'newwant')
			$('#' + id + 'obj').className = (num == 1) ? clname + '_rated' : clname;
	},

	Click: function(id, num, clname)
	{
		$('#hdn' + id).val(num);
		multiplier = (clname == '15') ? 15 : 11;
		$(".div" + id + " .star-rating .current").width(multiplier * num);
	},

	Toggle: function(id)
	{
		ok = $('#' + id).val() == 0;
		if(ok)
			$('#' + id + 'div').get(0).className += '1';
		else
			$('#' + id + 'div').get(0).className = $('#' + id + 'div').get(0).className.slice(0, -1);
		$('#' + id).val(ok ? 1 : 0);
	},

	ClickRate: function(id, num, clname, placeid, ratetype)
	{
		str = (ratetype != null) ? '&type='+ratetype : '';
		multiplier = (str == '') ? 15 : 11;
		$('#' + id + " .star-rating .current").width(multiplier * num);
		return Dialog.open($('#' + id + 'div'), {src: '/review/quickrate?placeid=' + placeid + '&rating=' + num + str, title: 'Thanks!'});
	},

	MouseOut: function(id, clname)
	{
		if($('#' + id + 'div').rated)
			return;
		$('#' + id + 'div').rating = 0;
		QuickRate.MouseOver(id, 0, clname);
	},

	MouseOutValue: function(id, clname, value)
	{
		if($('#' + id + 'div').rated)
			return;
		$('#' + id + 'div').rating = value;
		QuickRate.MouseOver(id, value, clname);
	},

	GetInt: function(val)
	{
		var a = parseInt(val);
		if(isNaN(a))
			a = 0;
		return a;
	},

	Submit: function(id)
	{
		var title = $('#title_' + id).val();
		var review = $('#texta_' + id).val();
		var rating = $('#hdnr_' + id).val();
		var empty_review = (review.replace(/\s*/g, "") == '');
		var empty_title = (title.replace(/\s*/g, "") == '');

		if(rating > 0 && empty_review)
		{
			alert("You must write a review as well. Tell the world what you loved or didn't like about this place. Your opinion matters.");
			return;
		}

		if(!empty_review && rating == 0)
		{
			alert("You must rate this place as well to post a review.");
			return;
		}

		var fav    = QuickRate.GetInt($("#f_" + id).val());
		var wantgo = QuickRate.GetInt($("#g_" + id).val());

		if(rating == 0 && empty_review && fav == 0 && wantgo == 0)
		{
			alert("You must provide a rating and review.");
			return;
		}

		$('#block_' + id).hide();
		$('#saving_' + id).show();

		$.get("/places/quickrate?placeid=" + id + "&title=" + encodeURIComponent(title) + "&review=" + encodeURIComponent(review) + "&rating=" + rating
				+ "&fav=" + fav + "&wantgo=" + wantgo, function(data){
			var result = data.split(';');

			if(result[0] == 'OK')
			{
				$('#saving_' + id).hide();

				if(review != '')
				{
					$('#saved1_'  + id).show();
					$('#review_link1_' + id).href = result[1];
					window.location = window.location + "?review_added"
				}
				else
				{
					$('#saved2_'  + id).show();
					$('#review_link2_' + id).href = result[1];
				}
			}
			else
			{
				$('#saving_' + id).hide();
				$('#error_' + id).show();
			}
		});
	},

	SubmitPic: function(id)
	{
		var params = $('#formRatePic_' + id).serialize();
		if ($('#texta_' + id).value == '')
		{
			$('#error_'+id).show();
			return false;
		}
		window.location = '/review/ratepic/?'+params;
	}
};
