/***********Review Functions***************/
function review_product(product_id, product_name, store_id, rateby) {
  url = 'http://review.alchemydigitalmedia.com/index.php?q=products&action=showform&product_id='+product_id+'&product_name='+product_name+'&store_id='+store_id+'&rateby='+rateby;  
  //url = 'http://localhost/Projects/RentByBoater/rating/?q=products&action=showform&product_id='+product_id+'&product_name='+product_name+'&store_id='+store_id+'&rateby='+rateby;
  window.open(url, 'ReviewWindow', 'menubar=no,height=390,width=430,scrollbars=no');
}

function string_repeat(str, num) {
	var temp = '';
	for( var i = 0; i < num; i++ ) {
		temp += str;
	}
	return temp;
}

function show_review() {
	var elm = document.getElementById('user-reviews');
	var elm_recent_reviews = document.getElementById('recent-review');
	var img = document.getElementById('imgreview');
	if(elm.style.display == 'block') {
		elm.style.display = 'none';
		elm_recent_reviews.style.display = 'block';
		img.src = parseURL('/lib/inarush/btn-seeallreviews.gif');
	} else {
		elm.style.display = 'block';
		elm_recent_reviews.style.display = 'none';
		img.src = parseURL('/lib/inarush/btn-hideallreviews.gif');
	}
}

function show_average_star() {
	if(reviews.length == 0) {
		content = string_repeat("<img src=\"images/star_grey.png\" />", 5);
		document.write(content);
		return;
	}
	
	var total = 0;
	var total_review = reviews.length;
	for(var i=0; i < reviews.length; i++) {
		total += parseInt(reviews[i].overall);
	}
	
	var content = string_repeat("<img src=\"images/star_red.png\" />", Math.ceil(total/total_review));
	content += string_repeat("<img src=\"images/star_grey.png\" />", (5-Math.ceil(total/total_review)));
	document.write(content);
}

function write_review() {
	if(reviews.length == 0) {
		var c = '<table id="tbl_review" width="100%" border="0" cellspacing="10" cellpadding="0">';
		if(typeof(emptyMessage) != "undefined") {
			c += '<tr><td><td>'+ emptyMessage +'</td></tr>';
		} else {
			c += '<tr><td><td>This boat has not been reviewed yet. Be the first to review this boat.</td></tr>';	
		}
		c += '</table>';
		document.write(c);
		return;
	}
	var content = '<table id="tbl_review" width="100%" border="0" cellspacing="10" cellpadding="0">';
	for(var i=0; i<reviews.length; i++) {
		reviewDate = reviews[i].rdate.split('-');
		content += '<tr>';
		content += '<td valign="top">';
		content += '<p><b>'+ reviewDate[1] +'/'+ reviewDate[2] +'/'+ reviewDate[0] +'<br />' + reviews[i].name + '</b><br />"' + reviews[i].title + '"<br /><br />' + reviews[i].description + '</p>';
		content += '</td>';
		content += '<td width="180">';
		content += '<table width="100%" border="0" cellspacing="2" cellpadding="0">';
		content += '<tr><td width=\"110\">Overall Experience:</td><td>'+ string_repeat("<img src=\"images/star_red.png\" />",reviews[i].overall) + string_repeat("<img src=\"images/star_grey.png\" />",(5-reviews[i].overall)) +'</td></tr>';
		content += '<tr><td>'+ review_map['durability'] +':</td><td>'+ string_repeat("<img src=\"images/star_red.png\" />",reviews[i].durability) + string_repeat("<img src=\"images/star_grey.png\" />",(5-reviews[i].durability)) +'</td></tr>';
		content += '<tr><td>'+review_map['features']+':</td><td>'+ string_repeat("<img src=\"images/star_red.png\" />",reviews[i].features) + string_repeat("<img src=\"images/star_grey.png\" />",(5-reviews[i].features))  +'</td></tr>';
		content += '<tr><td>'+review_map['appearence']+':</td><td>'+ string_repeat("<img src=\"images/star_red.png\" />",reviews[i].appearence) + string_repeat("<img src=\"images/star_grey.png\" />",(5-reviews[i].appearence))  +'</td></tr>';
		content += '<tr><td>'+review_map['buy_again']+':</td><td>'+ string_repeat("<img src=\"images/star_red.png\" />",reviews[i].buy_again) + string_repeat("<img src=\"images/star_grey.png\" />",(5-reviews[i].buy_again))  +'</td></tr>';
		content += '</table>';
		content += '</td>';
		content += '</tr>';
		content += '<tr ><td colspan="2"><hr size="1" /></td></tr>';
	}
	content += '</table>';
	document.write(content);
}

function show_one_review() {
	if(reviews.length == 0) {
		var content =  '';//'<p class="CR_title" align="center">MOST RECENT REVIEW:</p>';
		content += '<p style="font-size:11px;" align="center">';
		content += 'Be the first to review this product';
		content += '</p>';
		document.write(content);
	} else {
		var content =  '<p class="CR_title" align="center">MOST RECENT REVIEW:</p>';
		content += '<p style="font-size:11px;">';
		content += '<strong>'+ reviews[0].rdate +'</strong>&nbsp;&nbsp;'+ string_repeat("<img src=\"/lib/inarush/rate-red.gif\" />",reviews[0].overall) +'<br />';
		content += '<strong>'+ reviews[0].name +'</strong>';
		content += '</p>';
		content += '<p style="font-size:11px;">'+ reviews[0].description +'</p>';
		document.write(content);
	}
}

function getAJAXAvgStar(product_id, store_id) {
	
	//var url = "http://localhost/Projects/RentByBoater/rating/?q=products&action=getavgreview&product_id="+product_id+"&store_id="+store_id;
	var url = "http://review.alchemydigitalmedia.com/index.php?q=products&action=getavgreview&product_id="+product_id+"&store_id="+store_id;
	$.getJSON(url + "&jsoncallback=?", function(count){
					var content = '';		
					var div = "#avgStardiv_"+ product_id
					count = parseInt(count.avg);
					if(count == 0) {
						content = string_repeat("<img src=\"images/star_grey.png\" />", 5);
					}
					var content = string_repeat("<img src=\"images/star_red.png\" />", count);
					content += string_repeat("<img src=\"images/star_grey.png\" />", (5-count));
					$(div).html(content);
				});
}

function openReviewWindow(id, wid, rname) {
		var url = "view_rating.php?id="+id+"&wid="+wid+"&rname="+rname;
		window.open(url, 'OwnerReviewWindow', 'menubar=no,height=450,width=430,scrollbars=yes');
}
