// JavaScript Document

$(document).ready(function(){
	
	$(".allReviews a").click(function(event){
		event.preventDefault();
		var allReviewsURL = $(this).attr("href").replace( /\/page\/FF\/PROD\//, "").replace( /http:\/\/www.favorfavor.com\/page\/FF\/PROD/, "" ).replace( /#reviews/, "" );
		allReviewsURL = "http://www.favorfavorbaby.com/pscripts/reviews/viewAll.php?code=" + allReviewsURL;
		if ( $("#allReviews").dialog( "option", "height" ) ) {
			$("#allReviews").dialog( "destroy" );
		} else {
			$("head").append("<link rel=\"stylesheet\" href=\"/jquery/css/datatables/demos.css\" type=\"text/css\" />");
		}
		$("#allReviews").dialog({
			width: 675,
			height: 545,
			bgiframe: true,
			modal: true,
			close: function(event,ui){ $(this).html("<p style=\"text-align: center;\"><img src=\"/images/misc/loadingAnimation.gif\" height=\"13\" width=\"208\"></p>"); }
		}).load( allReviewsURL, function(){
			$(document).ready(function(){
				$("#allReviewsTable").dataTable({
					"bPaginate": false,
					"bFilter": false,
					"aaSorting": [[1,'desc']],
					"aoColumns": [
						null,
						{ "iDataSort": 2 },
						{ "bVisible": false },
						{ "bSortable": false }
					]
				});
			});
			
		});
		return false;
	});
	
	$(".reviewImage").live( "click", function(event){
		event.preventDefault();
		//pageTracker._trackEvent('Products', 'Review Image Viewed', prodID );
		if ( $("#reviewImages").dialog( "option", "height" ) ) {
			$("#reviewImages").dialog( "destroy" );
		}
		$("#reviewImages").dialog({
			bgiframe: true,
			height: 500,
			width: 600,
			modal: true,
			close: function(event,ui){ $(this).html("<p style=\"text-align: center;\"><img src=\"/images/misc/loadingAnimation.gif\" height=\"13\" width=\"208\"></p>"); }
		}).load( $(this).attr("href") );
		return false;
	});

	$(".sendReview").click(function(event){
		event.preventDefault();
		if ( !$("#sendReview").length ) {
			$("body").append("<div id=\"sendReview\" title=\"Sending Reviews &amp; Pictures to Favor Favor Baby\"><p>To send us pictures and fill out a review of your favors, <b>keep your eyes peeled for a reminder email from us about a month after you place you order</b>. We'll send you a list of the favors you ordered, with a link to review each one and send us your pictures.</p><p>Or if you want to fill out a review right away, simply <b>browse our website and find your favors. On the product page, click the link reading \"Write a review\".</b></p>");
			$(document).ready(function(){
				$("#sendReview").dialog({ width: 550, height: 200, modal: true });
			});
		} else {
			$("#sendReview").dialog( "open" );
		}		
		return false;
	});
	$("#reviewsTable h3 a + a").click(function(event){
		event.preventDefault();
		var itemCode = $(this).attr("href").split( "-" )[1].split( "." )[0];
		if ( !$("#allReviewsDialog").length ) {
			$("body").append("<div id=\"allReviewsDialog\" class=\"hideMe\" title=\"All Reviews\"><p style=\"text-align: center;\"><img src=\"/images/misc/loadingAnimation.gif\"></p></div>");
			$(document).ready(function(){
				$("#allReviewsDialog").dialog({
					bgiframe: true,
					height: 545,
					width: 675,
					modal: true
				}).load( "//www.favorfavorbaby.com/pscripts/reviews/viewAll.php?code=" + itemCode, function(){
					$(document).ready(function(){
						$("head").append("<link rel=\"stylesheet\" href=\"/jquery/css/datatables/demos.css\" type=\"text/css\" />");
						$("#allReviewsTable").dataTable({
							"bPaginate": false,
							"bFilter": false,
							"aaSorting": [[1,'desc']],
							"aoColumns": [
								null,
								{ "iDataSort": 2 },
								{ "bVisible": false },
								{ "bSortable": false }
							]
						});
						if ( testSetAttributeIgnoresNameAttribute() ) {
							// IE <= 7 looks like garbage, so we need to apply the fixes below
							//$("body").append("<br>this is IE7 or less");
							$("#allReviewsTable_wrapper").css( "height", "95%" );
							$("#allReviewsTable_wrapper").css( "overflow", "scroll" );
							$("#allReviewsDialog").css( "overflow", "hidden" );
						}
					});
				});
			});
		} else {
			$("#allReviewsDialog").dialog( "open")
			.html("<p style=\"text-align: center;\"><img src=\"/images/misc/loadingAnimation.gif\"></p>")
			.load( "//www.favorfavorbaby.com/pscripts/reviews/viewAll.php?code=" + itemCode, function(){
					$(document).ready(function(){
						$("#allReviewsTable").dataTable({
							"bPaginate": false,
							"bFilter": false,
							"aaSorting": [[1,'desc']],
							"aoColumns": [
								null,
								{ "iDataSort": 2 },
								{ "bVisible": false },
								{ "bSortable": false }
							]
						});
						if ( testSetAttributeIgnoresNameAttribute() ) {
							// IE <= 7 looks like garbage, so we need to apply the fixes below
							//$("body").append("<br>this is IE7 or less");
							$("#allReviewsTable_wrapper").css( "height", "95%" );
							$("#allReviewsTable_wrapper").css( "overflow", "scroll" );
							$("#allReviewsDialog").css( "overflow", "hidden" );
						}
					});
				});
		}
		return false;
	});





});