//Validate Search string
function validateSearchString (strSearch){
	var searchString = $.trim(strSearch);
	if(searchString == null || searchString == "" || searchString == "Search here")
	{
		alert("Please enter a valid search term.");
		return false;
	}
	return true;
}
//========================================================================

//Swaps Tabs top SEACH BOX
function searchtabs(swap){
	if (swap == 'websearch'){
		$("#websearch").show();
		$("#sitesearch").hide();
		$("#searchbox_top .google_logo").show();
		$("#web_tab").removeClass("inactive").addClass("active");
		$("#site_tab").removeClass("active").addClass("inactive");
	}else if (swap == 'sitesearch') {
		$("#websearch").hide();
		$("#sitesearch").show();
		$("#searchbox_top .google_logo").hide();
		$("#web_tab").removeClass("active").addClass("inactive");
		$("#site_tab").removeClass("inactive").addClass("active");
	}
}
//========================================================================

function preventXSSAttack (){
	var txtSearchInput = $(this).find("input[name=q]");
	var strOldSearch = txtSearchInput.val();
	if (!validateSearchString(strOldSearch)){
		txtSearchInput.val('');
		txtSearchInput.focus();
		return false;
	}
	else{
		var strNewSearch = strOldSearch.replace(/</g,"&lt;").replace(/>/g,"&gt;");
		txtSearchInput.val(strNewSearch);
		return true;
	}
}
//========================================================================

//Validate Recipe Search string
function validateRecipeSearch (strSearch) {
	var searchString = $.trim(strSearch);
	if(searchString == null || searchString == "" || searchString == "Ingredient or recipe name")
	{
		alert("Please enter a valid search term.");
		return false;
	}
	return true;
}
//========================================================================

//Truncate string
(function($){
	$.fn.TruncateString = function(MaxLength){
		return this.each(function(){
			var strHTML = $(this).html();
			var temp = cutHtmlStringNotBreakWord(strHTML,MaxLength);
			$(this).html(temp);
		});
	};
})(jQuery);
//========================================================================

//Count comments of an article
function jqgetComment(id){
	$.getJSON("/api_static/js/CommentsByArticle/" + id + "_count", function(json){
		if (parseInt(json.count) == 1)
		{
			$(".comment_count_"+id).each(function(){
				$(this).html("1 Comment");
			});
		}
		else 
		{
			$(".comment_count_"+id).each(function(){
				$(this).html(json.count+" Comments");
			});
		}
	});
}
//========================================================================

function fixPoorlyFormattedLinks(){
	// update poorly formatted link
	var pattern =  /(^\/)|(^www.)|(^http:\/\/)|(^https:\/\/)|(^javascript)|(^#)/
	$(".secondaryContent a, #xs_print_content a, #fb_maincontent a, #content_flipbook_v2 a, .blogBody a, #openArticleContent a").each(function(){
		var src = jQuery.trim($(this).attr("href"));
		if(src != '' && !src.match(pattern)){
			$(this).attr("href","/" + src);
		}
	});
	$(".secondaryContent img, #xs_print_content img, #fb_maincontent img, #content_flipbook_v2 img, .blogBody img, #openArticleContent img").each(function(){
		var src = jQuery.trim($(this).attr("src"));
		if(src != '' && !src.match(pattern)){
			$(this).attr("src","/" + src);
		}
	});
}
//========================================================================

$(document).ready(function(){
	$("#promo_player").show();
	
	//prevent XSS attack
	$("#sitesearch .search_btn").click(function(){
		$("form[name='result_search']").submit();
	});
	
	$("form[name='result_search']").submit(preventXSSAttack);
	$("form[name='cse-search-box']").submit(preventXSSAttack);
	
	//Site search
	 $("#sitesearch #gen_search").clearDefault().addClass("search_here").val("Search here");
	// Web search 
	$("#websearch .search_input").clearDefault().addClass("search_here").val("Search here");
	
	//Script for login box on header: globalHeader.tmpl 
	
	if(mag_user && mag_user.logged_in == 1){
		$("#mainHeader .loginBox").addClass("logout");
		$('#headerTop .loginBox .loginLogo').html('<img src="/cm/allaboutyou/site_images/header/loginBoxLogo.gif" alt="All About You" title="All About You"/>');
	}
	$(".sign_in_link").click(function(){
		HEARST.tempUtils.writeCookie('nexturl', document.location.href, 1);
	});
	
	//Script for moduleMostPopular
	
	$("#moduleMostPopular #mostComment li:gt(9)").remove();
	$("#moduleMostPopular #mostPopular li:gt(9)").remove();
	var justClicked = false;
	$("ul#moduleMostPopularTab li").click(function(){
		var active_tab = $("ul#moduleMostPopularTab li.active");
		if(($(this).attr("id") != active_tab.attr("id")) && !justClicked)
		{
			justClicked = true;
			$("ul#moduleMostPopularTab li").stop().removeClass("active");
			$(this).stop().addClass("active");

			if($("ul#moduleMostPopularTab li:first").hasClass("active"))
				$("#moduleMostPopularTab").removeClass("moduleMostRecentTab");
			else
				$("#moduleMostPopularTab").addClass("moduleMostRecentTab");
			$("#moduleMostPopularContent #jsframe").animate({
				"left": "+=" + $(this).stop().attr("pos")}, "slow",function(){justClicked = false;});
		}
	});
});
//========================================================================

//Add default text to input when it lost focus.
(function($){
	var defaults = {
		TextDefault: "Search here"
	};

	$.fn.clearDefault = function(setting){
		return this.each(function(){
			var opts = $.extend({},defaults,setting);
			var default_value = defaults.TextDefault;
			$(this).focus(function(){
				if ($(this).val() == default_value)
					$(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "")
					$(this).val(default_value);
			});
		});
	};
})(jQuery);
//========================================================================

//jquery plugin for Tool Slider
(function($) {
	$.fn.easySlider = function(options,index){
		// default configuration properties
		var defaults = {
			prevId: 'btnPrevious', prevText: '',
			nextId: 'btnNext', nextText: '',
			speed: 500,
			Increment: 3
		};
		var options = $.extend(defaults, options);
		return this.each(function() 
		{
			obj = $(this);
			var numOfFrame = $("li", obj).show().length;
			var widthOfFrame = 13 + $(obj).width(); var h = $(obj).height();
			var ts = numOfFrame - 1;
			var t = 0;
			$("ul", obj).css('width', numOfFrame * widthOfFrame);
			var $nextButton = $("#" + options.nextId).addClass("inactive");
			var $prevButton = $("#" + options.prevId).addClass("inactive");
			$nextButton.click(function(){
				animate("next");
			});

			$prevButton.click(function(){
				animate("prev");
			});

			function animate(dir, f)
			{
				if (f  !== undefined && f !== null && f > 0)
				{
					t = Math.floor(f/options.Increment);
				}
				else 
				{
					if(dir == "next")
						t = (t >= Math.floor (ts/ options.Increment)) ? Math.floor (ts/ options.Increment) : t+ 1;
					else
						t = ( t<= 0) ? 0 : t - 1;
				}

				var p = (t * widthOfFrame * (-1));
				$("ul",obj).animate( { marginLeft: p },  options.speed );

				//for next
				if (t >= Math.floor(ts/ options.Increment))
					$nextButton.addClass("inactive");
				else
					$nextButton.removeClass("inactive");
				//$prevButton.removeClass("inactive");

				//for previous
				if (t <= 0)
					$prevButton.addClass("inactive");
				else
					$prevButton.removeClass("inactive");
				//$nextButton.removeClass("inactive");
			}

			if( numOfFrame > options.Increment)
				$("#" + options.nextId).removeClass("inactive");

			t =( index < 0 ||  index >= numOfFrame  ) ? 0 : index;
			if (t > 0)
				animate("next", t);
		});
	};
})(jQuery);
