var RATINGS = function(){
	var _P = {
		init : function( params ) {
			_P.params = params;
			_P.loadXml();
		},
		params : null,
		data : null,
		loadXml : function() {
			$.ajax({
				type : "GET",
				url : _P.params.xmlPath,
				dataType : "xml",
				success : function( data ) {
					_P.data = data;
					_P.max = _P.params.perView;
					_P.count = $( "rating", data ).length;
					_P.preloadRatings();
					_P.browseRatings();
				}
			});
		},
		first : 0,
		max : 0,
		count : 0,
		preloadRatings : function() {
			$( "ul", "#ratings" ).empty();
			$( "rating", _P.data ).each(function( i ) {
				
				/*
					<overall_rating><![CDATA[Excellent]]></overall_rating>
					<location><![CDATA[Kansas City, KS]</location>
					<comment><![CDATA[Best customer service]</comment>
					<product_code>tt24</product_code>
					<image width="95" height="115">
						<src><![CDATA[images/ratings/id.jpg]]></src>
					</image>
					<shipping_method><![CDATA[Express]</shipping_method>
					<average_rating>4.5</average_rating>
					<price_rating>5</price_rating>
					<shipping_options_rating>5</shipping_options_rating>
					<delivery_rating>5</delivery_rating>
					<ease_of_purchase_rating>5</ease_of_purchase_rating>
					<customer_service_rating>5</customer_service_rating>
				*/
				
				
				var overall_label 			= $.trim( $( "overall_label", this ).text() );
				var overall_rating 			= $.trim( $( "overall_rating", this ).text() );
				var location 				= $.trim( $( "location", this ).text() );
				var author					= $.trim( $( "author", this ).text() );
				var comment					= $.trim( $( "comment", this ).text() );
				var product_code 			= $.trim( $( "product_code", this ).text() );
				var img_src 				= $.trim( $( "image > source", this ).text() );
				var shipping_method_rating	= $.trim( $( "shipping_method_rating", this ).text() );
				var price_rating 			= $.trim( $( "price_rating", this ).text() );
				var shipping_options_rating = $.trim( $( "shipping_options_rating", this ).text() );
				var delivery_rating 		= $.trim( $( "delivery_rating", this ).text() );
				var ease_of_purchase_rating = $.trim( $( "ease_of_purchase_rating", this ).text() );
				var customer_service_rating = $.trim( $( "customer_service_rating", this ).text() );
				
				
				// Brief rating view
				$( "ul", "#ratings" ).append([
					"<li><div style='width: 125px;'><img src='images/stars_",
					overall_rating,
					".png'><br><br><b>",
					overall_label,
					"</b><br>",
				//	author,
				//	"<br>",
				//	location,
					"<br>",
					"&quot;",
					comment.substring(0,100),
					"... &quot; <br><a href='javascript:void(0)' class='info'>View Full Rating</a></div></li>" ].join( "" ));
				
				// Hidden detailed information
				$( "body" ).append([
					"<div class='ratings_tool_tip' id='ratings_tool_tip_",
					i,
					"'><div class='ratings_pointer_left'><!-- ratings pointer --></div><div class='inner'><p><img src='images/stars_",
					overall_rating,
					".png'> <b>",
					overall_label,
					" Overall Rating</b><br><br>Component Ratings (5 is best)<br><img src='images/dash_",
					customer_service_rating,
					".png'> Customer Service<br><img src='images/dash_",
					shipping_options_rating,
					".png'> Shipping Options<br><img src='images/dash_",
					price_rating,
					".png'> Price<br><img src='images/dash_",
					ease_of_purchase_rating,
					".png'> Ease of Purchase<br><img src='images/dash_",
					delivery_rating,
					".png'> Delivery by Shipping Carrier (UPS or US Postal Service)<br><br>",
					comment,
				//	"<br> (by <em>",
				//	$.trim( $( "author", this ).text() ),
				//	" in ",
				//	location,
				//	"</em>)",
					"</p></div></div>" ].join( "" ));
			});
			$( ".info", "#ratings" ).hover(function( e ) {
				_P.tooltip.show( e, $( "#ratings_tool_tip_" + $( "a.info", "#ratings" ).index( this ) ) );
			}, function( e ) {
				_P.tooltip.hide( e, $( "#ratings_tool_tip_" + $( "a.info", "#ratings" ).index( this ) ) );
			});
			$( "#ratings .prev" ).click(function() {
				_P.browseRatings( "prev" );
				return false;
			});
			$( "#ratings .next" ).click(function() {
				_P.browseRatings( "next" );
				return false;
			});
		},
		browseRatings : function( browse ) {
			if ( browse == "prev" ) {
				if ( _P.first == _P.count && ( _P.count % _P.max > 0 ) ) {
					_P.first = _P.first - ( ( _P.count % _P.max ) + _P.max );
				} else {
					_P.first = _P.first - ( _P.max * 2 );
				}
			}
			var range = _P.first + _P.max;
			var start = 1;
			if ( range > _P.max ) {
				start = ( ( range - _P.max ) + 1 );
			}
			if ( _P.first == 0 ) {
				$( "#ratings .prev" ).css( "visibility", "hidden" );
			} else {
				$( "#ratings .prev" ).css( "visibility", "visible" );
			}
			if ( range < _P.count ) {
				$( "#ratings .next" ).css( "visibility", "visible" );
			} else if ( range >= _P.count ) {
				range = _P.count;
				$( "#ratings .next" ).css( "visibility", "hidden" );
			}
			$( "rating", _P.data ).each(function( i ) {
				if ( i >= _P.first && i < range ) {
					$( "#ratings li:eq(" + i + ")" ).fadeIn( "slow" );
				} else {
					$( "#ratings li:eq(" + i + ")" ).css( "display", "none" );
				}
			});
			_P.first = range;
			$( "#ratings .showing" ).html([
				"</a>Viewing Independent Customer Ratings <strong>",
				start,
				" - ",
				range,
				"</strong> of <strong>",
				_P.count,
				"</strong>" ].join( "" ));
		},
		tooltip : {
			show : function( e, $o ) {
				var v = _P.tooltip.getViewport();
				var pageX = _P.tooltip.getMouseCoord( v, e )[0] + 15;
				var pageY = _P.tooltip.getMouseCoord( v, e )[1];
				$o.find( ".ratings_pointer_right" ).addClass( "ratings_pointer_left" ).removeClass( "ratings_pointer_right" );
				if ( pageX + $o.width() > v.innerWidth + v.pageXOffset ) {
					pageX = pageX - $o.width() - 30;
					$o.find( ".inner" ).addClass( "inner_right" );
					$o.find( ".ratings_pointer_left" ).addClass( "ratings_pointer_right" ).removeClass( "ratings_pointer_left" );
				}
				$o.css( "left", pageX ).css( "top", pageY ).css( "display", "block" );
			},
			hide : function( e, $o ) {
				$o.css( "display", "none" );
			},
			getMouseCoord : function( v, e ) {
				( !e ) ? e = window.event : e = e;
				( e.pageX ) ? v.pageX = e.pageX : v.pageX = e.clientX + v.scrollLeft;
				( e.pageY ) ? v.pageY = e.pageY : v.pageY = e.clientY + v.scrollTop;
				return [ e.pageX, e.pageY ];
			},
			getViewport : function() {
				var viewport = {}
				if ( self.innerHeight ) {
					viewport.pageYOffset = self.pageYOffset;
					viewport.pageXOffset = self.pageXOffset;
					viewport.innerHeight = self.innerHeight;
					viewport.innerWidth = self.innerWidth;
				} else if ( document.documentElement && document.documentElement.clientHeight ) {
					viewport.pageYOffset = document.documentElement.scrollTop;
					viewport.pageXOffset = document.documentElement.scrollLeft;
					viewport.innerHeight = document.documentElement.clientHeight;
					viewport.innerWidth = document.documentElement.clientWidth;
				}
				return viewport;
			}
		}
	};
	return {
		init : function( params ) {
			_P.init( params );
		}
	};
}();