
(function ($) {
	$().ready(function () {
		$('#Header').rotateBanners({images: '#BannerImages img'});
		
		var closerTimeout = null;
		var currentId = 0;
		
		$('.itemRating').hover(
			function () {
				var id = $(this).attr('rel').split('-');
				var type = id[0];
				id = id[1];

				var ratingType = $(this).hasClass('providerInfo') ? 'ProviderCriteria' : 'ProductCriteria';
				
				if (closerTimeout) {
					clearTimeout(closerTimeout);
					closerTimeout = null;
				}
				
				if (id != currentId) {
					currentId = id;
					$('#ItemAjaxLoader').show();
					$('#ItemCriteriaDetails').hide();
					$.get('providerdata/criteriadetails', {ID: id, Type: type}, function (data) {
						if (data) {
							var res = $.parseJSON(data);
							
							$('#ItemCriteriaDetails h3').text(res.title);
							
							$('#ItemCriteriaDetails div').hide();
							$('p.noCriteria').hide();
							$('p.hasCriteria').hide();
							$('p.providerWarning').hide();
							
							if (res.description) {
								$('#ItemCriteriaDetails div.description').html('<p>'+res.description+'</p>').show();
							}

							if (res['criteria'].length) {
								$('#' + ratingType + ' ul').empty();
								$.each (res['criteria'], function (ind) {
									$('<li>').appendTo('#' + ratingType + ' ul').text(this.Title);
								})
								$('#' + ratingType + ' ul').show();
								
								$('#' + ratingType + ' p.hasCriteria').show();
							} else {
								$('#' + ratingType + ' ul').hide();
								$('#' + ratingType + ' p.noCriteria').show();
							}
							
//							if (res.url) {
//								if (!(res.url.indexOf('://') > 0)) {
//									res.url = 'http://' + res.url;
//								}
//								$('a.providerUrl').attr('href', res.url);
//							} else {
//								$('a.providerUrl').attr('href', location.href);
//							}

							$('p.providerWarning').show();
							$('#ItemAjaxLoader').hide();
							$('#' + ratingType).show();
							$('#ItemCriteriaDetails').show();
						}
					})
				}
				
				$('#ItemRating').css({
					top: ($(this).position().top - 50) + "px",
					left: ($(this).position().left + 200) + "px"
				}).show().hover(function () {
					if (closerTimeout) {
						clearTimeout(closerTimeout);
						closerTimeout = null;
					}
				});
			}, // this is still part of the hover fn!
			// on hover finish
			function () {
				closerTimeout = setTimeout(function () {
					$('#ItemRating').hide();
				}, 2000);
			}
		);
			
		$('a.showProducts').toggle(function (e) {
			e.preventDefault();
			$(this).text('Hide').closest('dd').find('div.productListing').fadeIn();
		},
		function (e) {
			e.preventDefault();
			$(this).text('Show products').closest('dd').find('div.productListing').fadeOut();
		});
	});
})(jQuery);

