
$(document).ready (function()
{

	$('#featured_images').cycle({
		fx:     'fade',
		speed:  'fast',
		timeout: 0,
		next:   '#left',
		prev:   '#right',
		after:	 onAfter
	});
	$('#hMerchant').val('1');

	$('#featured_images').css('display', 'block');

	// set up event handlers for the filter dropdowns
	$("#neighborhoodId").bind("change", handleNeighborhoodSelection);
	$("#categoryId")    .bind("change", handleCategorySelection);
	$("#subCategoryId") .bind("change", handleSubcategorySelection);

	// set up handlers for the alphabetic index links
	$(".content_list a").bind("click", handleIndexClick);

	// set up handlers for the pagination links
	$(".pagerWrapper span a").livequery ("click", handlePaginationClick);

	// truncate summary text and display the more link - for the featured summary
	setTimeout("addMoreLinkToFeatureSummarySectionIfNeeded ()", 100);
	// truncate summary text and display the more link - for each summary in list
	setTimeout("addMoreLinksToSummarySectionsWhereNeeded ()",   100);
});

function handlePaginationClick(event)
{
	event.stopPropagation();
	var search = this.href.substring (this.href.indexOf("?"));
	var url = getAjaxRequestUrl ("/travel/getFeaturedPlaceAjaxInfo.html" + search);
	$.getJSON (url, function (json)
	{
		displayPlaceIndexInfo(json);
	});

	return false;
}

function handleIndexClick (event)
{
	event.stopPropagation();
	var search = this.href.substring (this.href.indexOf("?"));
	var url = getAjaxRequestUrl ("/travel/getFeaturedPlaceAjaxInfo.html" + search);
	$.getJSON (url, function (json)
	{
		displayPlaceIndexInfo(json);
	});

	return false;
}

function onAfter(curr, next, opts)
{
	var index = opts.currSlide + 1;
	var size = opts.slideCount;
	$('#imagetag').text("Image " + index + " of " + size);
}

function addMoreLinkToFeatureSummarySectionIfNeeded ()
{
	if (placeType != "event")
	{
		var infoSection         = $(".featuredInfo.main");
		var infoSectionMoreLink = $(".more_details.moreLink");
		var heightSingleLine    = infoSectionMoreLink.height();
		var heightSixLines      = heightSingleLine * 6;
		var height;

		height = infoSection.height();
		if (height > heightSixLines)
		{
			infoSection.height(heightSixLines);
			infoSectionMoreLink.css("visibility", "visible");
		}
	}
}

function addMoreLinksToSummarySectionsWhereNeeded ()
{
	if (placeType != "event")
	{
		var summarySections         = $(".featuredInfo.featuredSummary");
		var heightSingleLine        = $(".featuredSummaryMoreLink:first").height();
		var heightThreeLines        = heightSingleLine + heightSingleLine + heightSingleLine;
		var height;

		summarySections.each (function (ndx, domElement)
		{
			height = $(domElement).height();
			if (height > heightThreeLines)
			{
				$(domElement).height(heightThreeLines);
				$(domElement).next().css("visibility", "visible");
			}
			else
			{
				$(domElement).next().css("visibility", "hidden");
			}
		});
	}
}

function addMoreLinkToSummarySections (domElement)
{
	if (placeType != "event")
	{
		var heightSingleLine        = $(".featuredSummaryMoreLink:first").height();
		var heightThreeLines        = heightSingleLine + heightSingleLine + heightSingleLine;
		var height;

		height = $(domElement).height();
		if (height > heightThreeLines)
		{
			$(domElement).height(heightThreeLines);
			$(domElement).next().css("visibility", "visible");
		}
		else
		{
			$(domElement).next().css("visibility", "hidden");
		}
	}
}

function getAjaxRequestUrl (url)
{
	var neighborhoodSelection = $('#neighborhoodId').val();
	var categorySelection     = $('#categoryId')    .val();
	var subCategorySelection  = $('#subCategoryId') .val();

	if ((neighborhoodSelection != -1) || (categorySelection != -1))
	{
		if (neighborhoodSelection != -1 && neighborhoodSelection != null)
		{
			url += (url.indexOf("?") == -1) ? "?" : "&";
			url += "neighborhoodId=" + neighborhoodSelection;
		}
		if (categorySelection != -1 && categorySelection != null)
		{
			url += (url.indexOf("?") == -1) ? "?" : "&";
			url += "categoryId=" + categorySelection;
		}
		if (subCategorySelection != -1 && subCategorySelection != null)
		{
			url += (url.indexOf("?") == -1) ? "?" : "&";
			url += "subCategoryId=" + subCategorySelection;
		}
	}
	if (url.indexOf("placeType") == -1)
	{
		url += (url.indexOf("?") == -1) ? "?" : "&";
		url += "placeType=" + placeType;
	}
	if (url.indexOf("cityName") == -1)
	{
		url += (url.indexOf("?") == -1) ? "?" : "&";
		url += "cityName=" + encodedCityName;

	}
	return (url);
}

function handleNeighborhoodSelection ()
{
	$("#categoryId")   .val(-1);
	$("#subCategoryId").val(-1);
	var url = getAjaxRequestUrl ("/travel/getFeaturedPlaceAjaxInfo.html");
	$.getJSON (url, function (json)
	{
		displayPlaceIndexInfo(json);
		repopulateCategories (json, "#categoryId");
		$("#subCategoryId").css("visibility", "hidden");
	});
}

function handleCategorySelection ()
{
	$("#subCategoryId").val(-1);
	var url = getAjaxRequestUrl ("/travel/getFeaturedPlaceAjaxInfo.html");
	$.getJSON (url, function (json)
	{
		displayPlaceIndexInfo(json);
		//repopulateCategories (json, "#subCategoryId");
		if ((json.categories) && (json.categories.length > 0))
		{
			$("#subCategoryId").css("visibility", "visible").focus();
		}
		else
		{
			$("#subCategoryId").css("visibility", "hidden");
		}

		if($("#categoryId").val() == -1 ){
			$("#subCategoryId").css("visibility", "hidden");
		}
	});                            
}

function handleSubcategorySelection ()
{
	var url = getAjaxRequestUrl ("/travel/getFeaturedPlaceAjaxInfo.html");
	$.getJSON (url, function (json)
	{
		displayPlaceIndexInfo(json);
	});
}

function repopulateCategories (json, selectBoxId) {
	var selectBox = $(selectBoxId);
	selectBox[0].options.length = 0;
	if (selectBox[0].name == 'categoryId')
		selectBox.append ("<option value='-1'>Select Category</option>");
	else
		selectBox.append ("<option value='-1'>Select Subcategory</option>");
	if (json.categories)
	{
		for (var ndx = 0; ndx < json.categories.length; ndx++)
		{
			var category = json.categories[ndx];
			selectBox.append ("<option value='" + category.id + "'>"  + category.name + "</option>");
		}
	}
}

function displayPlaceIndexInfo (json) {
	var placeIndexInfo = json.placeIndexInfo;
	var ndxFirst       = swa.sitelife.RatingWidget.widgetCounter;
	var html           = "";
	var url;
	var info;
	$(".pagerContainer").html("");

	if (placeIndexInfo.length != 0)
	{
		for (var ndx = 0; ndx < placeIndexInfo.length; ndx++)
		{
			info = placeIndexInfo[ndx];
			html += '<div class="placeInfoWrapper">';

			if (info.image)
			{
				html += '<a href="' + baseUrl + info.placeWCitiesId + '">'
				     +  '<img src="' + info.image + '" width="150" height="125" class="featuredListImage"/>'
				     +  '</a>';
			}

			html += '<div class="featuredTitle featuredList">'
			     +  '<a href="' + baseUrl + info.placeWCitiesId + '">' + info.name + '</a>'
			     +  '</div>';

			if (info.image)
				html +=  '<div class="featuredWrapper" style="width:390px; padding-left:0px">';
			else
				html +=  '<div class="featuredWrapper" style="width:560px; padding-left:0px">';


			if (placeType != "event")
			{
				html += '<div class="featuredAddress list" style="width:200px" id="featuredAddress_' + ndx + '"></div>'
				     +  '<script>$("#featuredAddress_' + ndx + '").load("' + info.address + '");</script>';
			}

			if (placeType != "event")
			{
				html += '<div class="featuredRating">'
				     +  '<div id="tempId"></div>'
				     +  '<div id="tempScriptId"></div>'
				     +  '<script>'
				     +  '$("#tempId").attr("id", "sl_RatingWidget_' + (ndxFirst + ndx) + '");'
				     +  '$("#tempScriptId").attr("id", "script_' + (ndxFirst + ndx) + '");'
				     +  '</script>'
				     +  '</div>';
			}

			if (placeType != "event")
			{
			html +=  '<div class="featuredInfo featuredSummary" id="featuredInfo_' + ndx + '"></div>'
			     +  '<script>$("#featuredInfo_' + ndx + '").load("' + info.summary + '", function(){addMoreLinkToSummarySections ("#featuredInfo_' + ndx + '")});</script>';
			}
			else
			{
				html +=  '<div class="featuredInfo featuredSummary" id="featuredInfo_' + ndx + '">';
				if (info.image)
				{
					html += '<div class="container image"></div>';
				}
				else
				{
					html += '<div class="container noImage"></div>';
				}
				html += '</div>'
			             +  '<script>$("#featuredInfo_' + ndx + ' .container").load("' + info.summary + '", function(){addMoreLinkToSummarySections ("#featuredInfo_' + ndx + '")});</script>';

			}

			if (placeType != "event")
			{
				html +=  '<div class="featuredSummaryMoreLinkWrapper">'
				     +  '<div class="more_details featuredSummaryMoreLink">'
				     +  '... <a href="' + baseUrl + info.placeWCitiesId + '">read more</a>'
				     +  '</div>'
				     +  '</div>';
			}

			html += '</div>'
			     +  '<div class="featuredPlaceAdditionalLinksWrapper list">'
			     +  '<div class="featuredPlaceAdditionalLinks list">'
			     +  '<div class="featuredPlaceAdditionalLink">'
			     +  '<a href="' + baseUrl + info.placeWCitiesId + '#gmap_container"> View Map </a>'
			     +  '</div>'
			     +  '<div class="featuredPlaceAdditionalLink">'
			     +  '| <a href="' + baseUrl + info.placeWCitiesId + '"> More Details </a>'
			     +  '</div>';

			if (placeType == "hotel")
			{
				html += '<div class="featuredPlaceAdditionalLink">'
				     +  '| <a href="' + baseUrl + info.placeWCitiesId + '#hotel_detail_booking_widget"> Search Rates </a>'
				     +  '</div>';
			}

			html += '</div>'
			     +  '</div>'
			     +  '<div style="clear:both">&nbsp;</div>'
			     +  '</div>';
		}
		$("#placeIndexInfo").html (html);

		for (var ndx = 0; ndx < placeIndexInfo.length; ndx++)
		{
			info = placeIndexInfo[ndx];
			html = '<script>var closeit = {notifyOfAddReviewSuccess : function() {'
					 + 'ratingWidget.modalOff();'
					 +  '}};'
					 +  'var ratingWidget = new swa.sitelife.RatingWidget(new ArticleKey("' + info.placeWCitiesId + '"), "ratingWidget", "template_DisplayRatingWidget", "' + baseUrl + info.placeWCitiesId + '")'
					 + '</script>';
			$("#script_" + (ndxFirst + ndx)).html(html);
		}
	}
	else
	{
		var selectedNeighborhoodText = $('#neighborhoodId :selected').text();
		var selectedCategoryText     = $('#categoryId     :selected').text();
		var selectedSubCategoryText  = $('#subCategoryId  :selected').text();

		html = "There are no results available";
		if (selectedNeighborhoodText != "Select Neighborhood")
		{
			html += " in " + selectedNeighborhoodText;
		}

		if (selectedSubCategoryText != "Select Subcategory")
		{
			html += " for " + selectedSubCategoryText;
		}
		else
		{
			if (selectedCategoryText != "Select Category")
			{
				html += " for " + selectedCategoryText;
			}
		}

		if (json.firstLetterOfName)
		{
			html += " that begin with " + json.firstLetterOfName;
		}

		html += ".";
		$("#placeIndexInfo").html (html);
	}

	if (json.numPages > 1)
	{
		url = "/travel/featuredPager.jsp?numPages=" + json.numPages + "&page=" + json.page;
		if (json.firstLetterOfName)
		{
			url += "&firstLetterOfName=" + json.firstLetterOfName;
		}
		$(".pagerContainer.header").load(url);
		$(".pagerContainer.footer").load(url);
	}
}
