﻿$(document).ready(function(){
	$('#submit-review').live('click',function(){
		// ctl00_ContentMain_ProductReviews_val1
		$.ajax({
			type: "POST",
			url: "/productreviews/insert.ashx",
			data: "{'p':'" + $('#ctl00_ContentMain_val1').val()
					+ "','c':'" + $('#ctl00_ContentMain_val2').val()
					+ "','title':'" + $('#title').val()
					+ "','review':'" + $('#reviewText').val() + "'}",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function (msg) {
				$('#review-response').html('Review saved.<br />May take up to 72 hours to post.');
				$('#review-response').fadeIn(800).animate({opacity:1.0},5000).fadeOut(1500);
			},
			error: function (error) {
				alert(error);
			}
		});
	});
	$('#create-review').live('click',function(){
		$('#review-box').slideDown(800);
	});
	$('#submit-review-cancel').live( 'click', function(){
		$('#review-box').slideUp(800);
	});
	
	var voteAjaxDefaultBeforeSendReviews = function(buttonAnchor, buttonContainer, messageContainer) {
		messageContainer.html('<div style="margin-top:5px;float:left;" class="spinner"><img src="/i/spinner.gif" alt="" />&nbsp;&nbsp;<span>Sending feedback ...</span></div>'); 
		buttonContainer.hide(); 
		messageContainer.show();
	};
	var voteAjaxDefaultSuccessReviews = function(aData, aStatus, buttonAnchor, buttonContainer, messageContainer) { 
		/*
		if (aData.redirect == 1) {
			return window.location.href=buttonAnchor.href;
		}
		*/
		if (aData.error) {
			messageContainer.html('Oops! System error.');
			buttonContainer.show();
		} else {
			messageContainer.html('Feedback received!'); 
		}
	};
	var voteAjaxDefaultErrorReviews = function(aStatus, aError, buttonAnchor, buttonContainer, messageContainer) { 
		messageContainer.html('Oops! System error.');
		buttonContainer.show();
	};

	$('.votingButton').each(function(){
		$(this).unbind('click.vote.Reviews');
		$(this).bind('click.vote.Reviews', function(){
			var buttonAnchor = this;
			var buttonContainer = $(this).parent();
			var messageContainer = $(buttonContainer).next('.votingMessage');
			$.ajax({
			type: 'GET',
			dataType: 'json',
			ajaxTimeout: 10000,
			cache: false,
			beforeSend: function(){ 
				voteAjaxDefaultBeforeSendReviews(buttonAnchor, buttonContainer, messageContainer); 
			},
			success: function(data, textStatus){ 
				voteAjaxDefaultSuccessReviews(data, textStatus, buttonAnchor, buttonContainer, messageContainer); 
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				voteAjaxDefaultErrorReviews(textStatus, errorThrown, buttonAnchor, buttonContainer, messageContainer);
			},
			url: buttonAnchor.href
			});
			return false;
		});
	});
	
	$('.abuseButton').each(function(){
		$(this).unbind('click.abuse.Reviews');
		$(this).bind('click.abuse.Reviews', function(){
			var buttonAnchor = this;
			var buttonContainer = $(this).parent();
			$.ajax({
				type: 'GET',
				dataType: 'json',
				ajaxTimeout: 10000,
				cache: false,
				beforeSend: function(){ 
					buttonContainer.html('<div style="margin-top:5px;float:left;" class="spinner"><img src="/i/spinner.gif" alt="" />&nbsp;&nbsp;<span>Reporting Review ...</span></div>'); 
				},
				success: function(data, textStatus){ 
					if (data.error) {
						buttonContainer.html('Oops! System error.');
					} else {
						buttonContainer.html('<div style="color:#990000;">Report received!</div>'); 
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
					buttonContainer.html('Oops! System error.');
				},
				url: buttonAnchor.href
			});
			return false;
		});
	});
});

