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;
	}
}
//========================================================================

//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);
//========================================================================

//Function get data for latest forum
function GetLatestForums(url){
	$.getJSON(url,function(json){
		for(i=0; i<json.length; i++)
		{
			var userName = '';
			if(typeof(json[i].user_name) != "undefined")
			{
				userName = json[i].user_name;
			}
			var userNameTruncated = userName.length > 7 ? (userName.substring(0,7) + "...") : userName;
			var profileLink = "/community/profile/" + userName;
			var creationDate = convertDateTimeUKWithFormat(json[i].creation_date,"dd mmm yyyy hh:MMTT");
			var postTitle = json[i].post_title;
			var titleLink = "/community/forums/thread/" + json[i].thread_id;
			var postBody = json[i].post_body.length > 104 ? cutHtmlStringNotBreakWord(json[i].post_body,104) : json[i].post_body;
			postBody = postBody.replace(/wrote:/g,'Quote:');
			if (postBody.indexOf("Quote:") >= 0)
			{
				postBody = "Quote:";
			}
			var liTag = $('#module_latest_forum .latest_forum_item').clone().removeAttr('class').appendTo('#module_latest_forum ul');
			liTag.find('.post_by').text("Posted by ");
			liTag.find('.profile_link').attr('href',profileLink);
			liTag.find('.user_name').text(userNameTruncated);
			liTag.find('.dot_divider').text(" - ");
			liTag.find('.creation_date').text(creationDate);
			liTag.find('.title_link').text(postTitle).attr('href',titleLink);
			liTag.find('.post_body').html(postBody);
		}
		$('#latest_forum_loading').remove();
		$('.latest_forum_item').remove();
	});
}
//========================================================================

//Cut string
(function($){
	$.fn.cutStrNotBreakWord = function(max_len){
		return this.each(function(){
			var strHTML = $(this).html();
			var temp = cutHtmlStringNotBreakWord(strHTML,max_len);
			$(this).html(temp);
		});
	};
})(jQuery);
//========================================================================

$(document).ready(function(){
	//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");
	
	$("#module_most_comment_popular #mostpopular li:gt(9)").remove();
	$("#module_most_comment_popular #mostpopular").show();
		
	$("#module_most_comment_popular #popular_tab").click(function(){
		$("#module_most_comment_popular #mostpopular li:gt(9)").remove();
		$("#mostcomment").hide();
		$("#mostpopular").show();
		$("#comment_tab").removeClass("active").addClass("inactive");
		$("#popular_tab").removeClass("inactive").addClass("active");
	});
	$("#module_most_comment_popular #comment_tab").click(function(){
		$("#module_most_comment_popular #mostcomment li:gt(9)").remove();
		$("#mostcomment").show();
		$("#mostpopular").hide();
		$("#comment_tab").removeClass("inactive").addClass("active");
		$("#popular_tab").removeClass("active").addClass("inactive");
	});
		
	//Get data for latest forum module
	if (document.getElementById("module_latest_forum"))
		GetLatestForums("/api/js/PostsBySite?encoding=utf-8&limit=5");
});
//========================================================================

//Get Image name from image src
function GetImageName(ImageSrc){
	var result = '';
	if (typeof ImageSrc != 'undefined') 
	{
		var arrayPath = ImageSrc.split('/');
		if (arrayPath.length > 0)
			result = arrayPath[arrayPath.length-1].replace('.jpg','').replace('-small_new','').replace('-medium_new','').replace('lg_','').replace('sm_','').replace('md_','').replace('or_','');
		else
			result = '';
	}
	return result;
}
//========================================================================

//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 = $(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");
			}
			if( numOfFrame > options.Increment) 
				$("#" + options.nextId).removeClass("inactive");
			t =  ( index < 0 ||  index >= numOfFrame  ) ? 0 : index;
			if (t > 0)
				animate("next", t);
		});
	};
})(jQuery);
