function scroll_to(id){
$('html,body').animate({scrollTop: $(id).offset().top},'slow');
} 

function link_images() {
	if($('.post-holder.link_image').length > 0) {
		$('.post-holder.link_image').each( function() {
			var url = $('h2 a', this).attr('href');
			if ($('.post-pic a:not(h3 a)', this).length === 0) {
				//create the link and wrap image
				$('.post-pic img', this).wrap("<a href='" + url + "'></a>");
			}
			//$('a.get_link', this).attr('href', url);
		});
	}
}

function getWebAppIDFromHref(url) {
		//get web app ID so that we can use it for the search
		var id = "0";
		if(url.indexOf("/_") === -1) {
			id = url.split("?ID=")[1];
		} else {
			id = url.split("_")[2].split("/")[0];
		}
		
		return id;
}

function unslug(str) {
	//convert slug into pretty string
	return str.split('-').join(" ");
}

function showMenu(menu, button, hover_class) {
	//shows the various drop down menus on the site
	button.addClass(hover_class);
	$("img", button).attr('src', '/Images/sitedesignimages/2010/icon/cat-icon-up.jpg');
	menu.show();
	//menu.stop(true, true).show(1);
	return false;
}

function hideMenu(menu, button, hover_class) {
	//hides the various drop down menus on the site
	// menu.stop(true, true).delay(300).hide(1, function () {
	// 		button.removeClass(hover_class);
	// 	});
	menu.hide();
	$("img", button).attr('src', '/Images/sitedesignimages/2010/icon/cat-icon.jpg');
	button.removeClass(hover_class);
	return false;
}

function initDropDown(menu, hit_target, button, hover_class) {
	//used for the directory categories drop down menu if we are not on the directory home page. If we are on the directory home page, the categries are displayed in the main
	//page content and the drop down is not required. On other directory pages, the categories need to be hidden away inside the drop down.
	menu.hide();
	
	// hit_target.click(function () {
	// 		$(menu).toggle();
	// 		return false;
	// 	});
	
	hit_target.toggle(function() {
	  showMenu(menu, button, hover_class);
	}, function() {
	  hideMenu(menu, button, hover_class);
	});
	
	
	
	
	// hit_target.hoverIntent({
	// 		over : function() {
	// 			showMenu(menu, button, hover_class);
	// 		}, 
	// 		out : function() {
	// 			hideMenu(menu, button, hover_class);
	// 		}
	// 	});

	// $(document).click(function () {
	// 		hideMenu(menu, button, hover_class);
	// 	});
	
	// hit_target.click(function (event) {
	// 		//prevent clicking in the menu closing it
	// 		//event.stopPropagation();
	// 	});
}

function correctCufonFontSizes() {
	$('div.top-navbar h4 a').addClass("loaded");
}

function toggleLogin(onOrOff) {
	if(onOrOff == "1") {
		$('#member-account-controls').show();
		$('#member-login-controls').hide();
	} else {
		$('#member-account-controls').hide();
		$('#member-login-controls').show();
	}
}

function toggleForgottenPassword() {
	if($('a.forgotten_password_toggle').length > 0) {
		if($('.forgotton_password_container').length > 0) {
			$('.forgotton_password_container').hide();
			$('a.forgotten_password_toggle').bind('click', function () {
				$(".forgotton_password_container").slideToggle();
				return false;
			});
		}
	}
}

function hideEmpty() {
	$('.optional').each(function () {
		var optional, optional_html, optional_data, hide;
		optional_html = $(this).html();
		//optional data span or img. if img, don't hide. if optional data span, check text inside optional data
		if (optional_html !== "") {
			hide = false;
			if ($('.optional_data', this).length > 0) {
				optional_data = $.trim($('.optional_data', this).text());
				hide = optional_data === "" ? true : false;
			}
		} else {
			optional = $.trim($(this).text());
			hide = optional === "" ? true : false;
		}
		if (hide) { $(this).hide(); }
	});
}

function convertToSlug(t) {
    return t.toLowerCase().replace(/ |&+/g, '_').replace('___', '_');
}

function convertToElementIDName(t) {
    return t.toLowerCase().replace(/ [|&;$%@"<>()+,] /g, "-").replace(" ", "-");
}

function revealComment() {
	//toggle comments form
	$(".commentReveal").hide();
	$("a.toggle-comment").bind('click', function () {
		$(".commentReveal").slideToggle();
		return false;
	});
}

function initCommentLinkThrough() {	
	//loop through article lists and populate the comment count href with article URL.
	if ($('.postDetail H2 a').length > 0) {
		$('.postDetail').each(function () {
			if ($('a.link-to-comments', this).length > 0) {
				var url = $('H2 a', this).attr("href") + "#comments";
				$('a.link-to-comments', this).each( function() {
					$(this).attr("href", url);
				});
			}
		});
	}
}

function getBCThumbnailSource(i, w, h, c) {
	var width = w || "150", height = h || "150", constrain = c || "1", imgsrc = i || "/Images/sitedesignimages/2010/event-no-image-large.jpg";
	return "/Utilities/ShowThumbnail.aspx?USM=1&amp;W=" + width + "&amp;H=" + height + "&amp;R=" + constrain + "&amp;Img=" + imgsrc
}

function replaceImages() {
	
	$(".replace_image").each(function() {
		//alert($("img", this).length);
		if ($("img", this).length === 0) {
			$(this).append("<img src='" + getBCThumbnailSource() + "' />");
			link_images();
		}
	});
	
	$('.post-pic img').error(function() {
		//alert("error");
		$(this).attr('src', '/Images/sitedesignimages/2010/event-no-image-large.jpg');
	});
}

function chooseFillerImage(img) {
	var w = $(img).css("width");
	var wInt = parseInt(w.replace("px", ""));
	var missing_img_src = "/Images/sitedesignimages/2010/event-no-image-small.png"
	if(wInt !== NaN && wInt !== "undefined") {
		if(wInt > 100) {
			missing_img_src = "/Images/sitedesignimages/2010/event-no-image-large.jpg";
		}
	}
	return missing_img_src;
}

function corners() {
	if ($('h3#action-category-drop').length > 0) {
		$('h3#action-category-drop').corner("top 5px");
	}
	if ($('#category_menu_list').length > 0) {
		$('#category_menu_list').corner("bottom 5px tl 5px");
	}
	
	if ($('#directory_category_menu_list').length > 0) {
		$('#directory_category_menu_list').corner("bottom 5px tl 5px");
	}
}

function displayCompetitionsOLD() {	
	if ($("div#com-box").size() > 0) {
		var links;
		$("div#com-box .competition").each(function (i) {
			//var compImage = $(".compimage", this ).get(0);
			var title, loc, img, src;
			title = $("h3", this).text();
			loc = $("a", this).attr("href");
			//
			//$(this).append(compImage);
			img = $("img", this).get(0);
			src = $(img).attr("src");
			$(img).attr("src", "/Utilities/ShowThumbnail.aspx?USM=1&R=1&W=137&H=137&Img=" + src);
			$(img).attr("title", title);
			$(img).attr("alt", title);
			$(img).attr("class", "dynamic_comp_image");
			$(this).wrap("<a href='" + loc + "' class='dynamic_comp_image_link'></a>");
			$(this).replaceWith(img);
		});	
		links = $(".dynamic_comp_image_link");
		$("div#com-box .com-box-content").replaceWith("<div class='com-box-content'></div>");
		$("div#com-box .com-box-content").append(links);
		$("div#com-box .dynamic_comp_image").last().attr("class", "nm");
	}
}


function displayCompetitions() {	
	if ($("div#com-box").size() > 0) {
		var links;
		$("div#com-box .post-holder").each(function (i) {
			//var compImage = $(".compimage", this ).get(0);
			var title, loc, img, src;
			title = $("h2", this).text();
			loc = $("h2 a", this).attr("href");
			//$(this).append(compImage);
			img = $("img", this).get(0);
			src = $(img).attr("src");
			$(img).attr("src", "/Utilities/ShowThumbnail.aspx?USM=1&R=1&W=137&H=137&Img=" + src);
			$(img).attr("title", title);
			$(img).attr("alt", title);
			$(img).attr("class", "dynamic_comp_image");
			$(this).wrap("<a href='" + loc + "' class='dynamic_comp_image_link'></a>");
			$(this).replaceWith(img);
		});	
		links = $(".dynamic_comp_image_link");
		$("div#com-box .com-box-content").replaceWith("<div class='com-box-content'></div>");
		$("div#com-box .com-box-content").append(links);
		$("div#com-box .dynamic_comp_image").last().attr("class", "nm");
		$("div#com-box").show();
	}
}

function truncateText(element,length) {
	$(element).truncate({
		max_length: length
	});
}

function loadFooter() {
	$('#footer-latest-articles, #footer-latest-holiday-guides').hide();
	$('#footer-latest-articles').load('/ajax/footer-articles #fetch', function() {
	  	$('#footer-latest-holiday-guides').load('/ajax/latest-holiday-guides #fetch', function() {
	  		ajaxLoadCallback();
		});
	});
}

function ajaxLoadCallback() {
	//Cufon.replace('#footer-latest-articles h2, #footer-latest-holiday-guides h2', { fontFamily: 'Myriad',  hover: true });
	//Cufon.replace('#footer-latest-articles h3, #footer-latest-holiday-guides h3', { fontFamily: 'Myriad',  hover: true });
	truncateText("#footer-latest-articles .foot-list h3 .preview", 50);
	truncateText("#footer-latest-holiday-guides .foot-list h3 .preview", 50);
	$('#footer-latest-articles, #footer-latest-holiday-guides').show();
}

$(document).ready(function () {
	loadFooter();
	truncateText(".postBlock.narrow .preview", 80, "...");
	corners();
	//correctCufonFontSizes();
	displayCompetitions();
	toggleLogin(typeof(loggedin) != 'undefined' ? loggedin : "0");
	initCommentLinkThrough();
	hideEmpty();
	revealComment();
	replaceImages();
	toggleForgottenPassword();
	//link_images();
if ($('a.scroll_to').length > 0) {
$('a.scroll_to').click(function () {
scroll_to($(this).attr('href'));
return false;
});
} 
});
