//Validate Search string, if empty searches are not executed
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;
	}
}
//========================================================================

$(document).ready(function(){
	$("#promoPlayer").css('visibility','visible');
	$("#promo_player_preload").animate({"opacity": "-0.001"}, 500, function(){$(this).css("display", "none")});
	$("#most_comment_popular_content #most_comment li:gt(9)").remove();
	var h1 = $("#most_comment_popular_content #most_comment").height() + 44;
	var h2 = $("#most_comment_popular_content #most_popular").height() + 44;
	$("#most_comment_popular_tab #tab1").attr("hei",h1 + "px");
	$("#most_comment_popular_tab #tab2").attr("hei",h2 + "px");
	$("#most_comment_popular_content").css("height", h1);// the first always is "tab1"
	var justClicked = false;
	$("ul#most_comment_popular_tab li").hover(function(){
		var active_tab = $("ul#most_comment_popular_tab li.active");
		if($(this).attr("id")!=active_tab.attr("id")) 
			$(this).stop().addClass("hover");
	},
	function(){$(this).stop().removeClass("hover");});

	$("ul#most_comment_popular_tab li").click(function(){
		var active_tab = $("ul#most_comment_popular_tab li.active");
		if(($(this).attr("id") != active_tab.attr("id")) && !justClicked)
		{
			justClicked = true;
			$("ul#most_comment_popular_tab li").stop().removeClass("active");
			$(this).stop().addClass("active");
			
			$("#most_comment_popular_content #jsframe").animate({"left": "+=" + $(this).stop().attr("pos")}, "slow",function(){justClicked = false;});
			$("#most_comment_popular_content").animate({height: $(this).stop().attr("hei")}, "slow");
		}
	});
	//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");
});
//========================================================================

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

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

//Get data for latest forum
function GetLatestForums(url){
	$.getJSON(url,function(json){
		for(i=0; i<json.length; i++)
		{
			var 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 > 50 ? cutHtmlStringNotBreakWord(json[i].post_body,50) : json[i].post_body;
			postBody = postBody.replace(/wrote:/g,'Quote:');
			if (postBody.indexOf("Quote:") >= 0)
			{
				postBody = "Quote:";
			}
			var liTag = $('#module_latest .latest_forum_item').clone().removeAttr('class').appendTo('.content_latest 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').text(postBody);
		}
		$('#latest_forum_loading').remove();
		$('.latest_forum_item').remove();
	});
}
//========================================================================

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