﻿var contentDomain = CONFIG.get("CONTENT_DOMAIN");
var page = 0;

/*-- Find expert comments --*/
function getExpertBio(id) {
	jQuery.ajax({
		type: "POST",
		url: "/dailybeauty/webmethods.aspx/FindExpertBio",
		data: "{'id' : '" + id + "'}",
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		beforeSend: function () {
			//jQuery("#expert-bio").html("");
		},
		success: function (results) {
			if (results.d.length > 0) {
				expertBio.open();
				jQuery("#expert-bioContent").html(
					'<div id="eb' + id + '-bio" class="bioBox">' +
						'<div class="bioContent">' +
							'<img alt="' + results.d[0].DisplayName + '" class="bioPic" src="' + contentDomain + '/BeautyPass/Experts/' + results.d[0].ImageFile + '" />' +
							'<p>' +
								'<dl class="title">' +
								'<dt>' + results.d[0].DisplayName + '</dt>' +
								'<dd><em>' + results.d[0].Title + '</em></dd>' +
								'</dl>' +
								results.d[0].Body.replace(/\n/g, "<br />") +
							'</p>' +
						'</div>' +
					'</div>');
				jQuery("#expert-bioHead, #overlay-bio").click(function () {
					jQuery("#expert-bio").html("");
				});
			} else {
				//jQuery("#expert-bio").html("<span>No results found.</span>");
			}
		},
		failure: function (results) {
			//jQuery("#expert-bio").html("There was a problem with the search. Please try again later.");
		}
	});
}

function getExpertComments() {
	jQuery.ajax({
		type: "POST",
		url: "/dailybeauty/webmethods.aspx/FindLatestNExpertComments",
		data: "{'page' : " + page + "}",
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		beforeSend: function () { },
		success: function (results) {
			jQuery("#expert-comments").html("");
			if (results.d.length > 0) {
				jQuery.each(results.d, function (i, comment) {
					jQuery("#expert-comments").append(
						'<div class="comment">' +
							'<a class="fl" rel="display-expert-bio" href="#eb' + comment.ExpertBioId + '"><img src="' + contentDomain + '/beautypass/experts/' + comment.ExpertImage + '" alt="' + comment.ExpertName + '" /></a>' +
							'<p>' +
								'<span class="bold">' + comment.ExpertName + '</span><br />' +
								'just commented on<br />' +
								'<a class="entry bold" href="/dailybeauty/entry.aspx?id=' + comment.EntryId + '">' + comment.Headline + '</a>' +
							'</p>' +
							'<span class="cb"></span>' +
						'</div>'
					);
				});
				expertCommentsPagination();
				
				//bind getExpertBio method to the click even of all bio image links
				jQuery("a:[rel='display-expert-bio']").each(function (i, a) {
					jQuery(a).click(function () { getExpertBio(parseInt(jQuery(a).attr("href").replace(/[^\d]/g, ''), 10)) });
				});
				//send all display-expert-bio anchors into expertBios.Init()

			} else {
				jQuery("#expert-comments").html("<span>No results found.</span>");
			}
		},
		failure: function (results) {
			jQuery("#expert-comments").html("There was a problem with the search. Please try again later.");
		}
	});
}
/// <reference path="../../sitemaps/" />

function expertCommentsPagination() {
	var html = "<div class='expert-comment-pagination' style='text-align:right;'>see more";
	if (page > 0) {
		html += "<a id='expert-page-previous' href='javascript:void(0);'><img src='/dailybeauty/content/expert-comments-previous.gif' alt='previous' /></a>";
	}
	html += "<a id='expert-page-next' href='javascript:void(0);'><img src='/dailybeauty/content/expert-comments-next.gif' alt='next' /></a>";
	html += "</div>";
	jQuery("#expert-comments").append(html);
	jQuery("#expert-page-next").click(function () {
		page++;
		getExpertComments();
	});
	jQuery("#expert-page-previous").click(function () {
		page--;
		getExpertComments();
	});
}

getExpertComments();
