var SearchType = "partial"; // exact, whole, partial - Search method

// var searchDisplayType = "newpage"; // overlay = below search textbox ,
// newpage = on to the same page

var searchDisplayDivId = (searchDisplayType == "overlay") ? "search_dropdown"
		: "content";// search_dropdown = overlay below search textbox , content
					// = same page

jQuery(document).ready(function() {
	jQuery("#search_close").click(clearSearchForm);

	/* Search Related */
//	reset_highlight();

//	jQuery("#sKey").keyup(reset_highlight); // On KeyUp activate KeyHighlight

		jQuery("#sSubmit").click(submitSearchForm); // OnClick of search button
													// display the search
													// results

		// On Press Enter Key in username or password, submit the Search form
		jQuery("#sKey").keyup(function(e) {

			var key;
			if (window.event)
				key = window.event.keyCode; // IE
			else
				key = e.which; // firefox
			var sKey = jQuery("#sKey").attr("value"); // get the Keyword from search
			if(trimStr(sKey) == '')
			{
			clearSearchForm();
			}	
			else
			{
				
				if (searchDisplayType == "overlay") {
					submitSearchForm();
					return true;
				} else {
					if ((key == 13 || key == 3)) {
						// jQuery("#sSubmit").click();
						submitSearchForm();
						return false;
					} else
						return true;
				}
			}
		});

		// Close Search Results
		jQuery("#search_close").click(clearSearchForm); // Close the Search
														// Results
	});

// Submit the Search Form to search content on other pages
function submitSearchForm(clearKey) {

	// w3isEmpty("sKey","","");
	if (clearKey == "searchClearClear" && searchDisplayType == "overlay") {
		jQuery("#" + searchDisplayDivId).addClass("cms_hide");
	}

	var sKey = jQuery("#sKey").attr("value"); // get the Keyword from search
												// text box

	if (sKey == "" && clearKey != "searchClearClear") // if the keyword is
														// SearchClearClear hide
														// it
	{
		// jQuery("#"+searchDisplayDivId).html("");
		jQuery("#" + searchDisplayDivId).addClass("cms_hide");
	}
	// alert(sKey);
	
		if (globalErr == 1) {
			jQuery.ajax( {
				// url:"_includes/cmsSearch.php",
				url : siteURL + "search/searching.html/",
				type : 'POST',
				dataType : "html",
				data : {
					searchKey : sKey
				},
				success : function(msg) {
				
					if (jQuery("#" + searchDisplayDivId).hasClass("cms_hide")
							&& (clearKey != "searchClearClear"))
						jQuery("#" + searchDisplayDivId).removeClass("cms_hide");
	
					if (clearKey != "searchClearClear")
						jQuery("#" + searchDisplayDivId).html(msg);
					
					//if there are no results, hide pagination div
					if(jQuery("#overlaySearchResultId").children().hasClass("no_results"))
					{
						jQuery("#searchOverlayPageNavigation").addClass("cms_hide");
					}
					else
					{
						jQuery("#searchOverlayPageNavigation").removeClass("cms_hide");
					}
	
					// var getSearchResultHeight =
					// document.getElementById('search_dropdown').offsetHeight;
					// document.getElementById('scroller_main').style.height=(getSearchResultHeight-4)+"px";
					// document.getElementById('scroller_inner').style.height=(getSearchResultHeight-64)+"px";
//					var options = {
//						exact : SearchType, // exact, whole, partial
//						style_name_suffix : false,
//						highlight : ".highlightable",
//						keys : jQuery("#sKey").attr("value")
//					}
	
					// jQuery("#left_content").SearchHighlight(options);
//					jQuery(document).SearchHighlight(options);
	
//					reset_highlight();
//					jQuery("#sKey").keyup(reset_highlight);
					jQuery("#sSubmit").click(submitSearchForm);
	
					if (clearKey != "searchClearClear")
						searchPagination(); // Enable the search Overlay Pagination
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert('Error occured : Search Submit' + XMLHttpRequest + " \n "
							+ textStatus + "\n" + errorThrown);
				}
			});
		}

	globalErr = 1;
}

// String Highlight
function reset_highlight() { // alert(document.getElementById("sKey").value);
	var options = {
		exact : SearchType,
		style_name_suffix : false,
		highlight : ".highlightable",
		keys : jQuery("#sKey").attr("value")
	}

	// Remove the Old Hightlight
	jQuery("span[@class^=hilite]").each(function() {
		var hilite = jQuery(this);
		var txt_el = hilite[0].previousSibling;
		if (txt_el && txt_el.nodeType == 3)
			txt_el.data += hilite.text();
		else {
			hilite.before(hilite.text());
			txt_el = hilite[0].previousSibling;
		}
		if (hilite[0].nextSibling && hilite[0].nextSibling.nodeType == 3) {
			txt_el.data += hilite[0].nextSibling.data;
			$(hilite[0].nextSibling).remove();
		}
		hilite.remove();
	});

	// Seach the entire document
	jQuery("#content").SearchHighlight(options);
	// jQuery("#left_content").SearchHighlight(options);
}

// clear clearSearchForm
function clearSearchForm() {
	// Empty the Textbox
	jQuery("#sKey").attr("value", "");

	// call the search to update the search key
	submitSearchForm('searchClearClear');

	// hide the search results
	// jQuery("#search_dropdown").removeClass('hide');
	// alert(searchDisplayType);
	if (searchDisplayType == "overlay") {
		if (!jQuery("#" + searchDisplayType).hasClass("cms_hide"))
			jQuery("#" + searchDisplayType).addClass("cms_hide");
	}
}

function searchPagination() {
	// jQuery.getScript("_scripts/_JS/paging.js");
	pager1 = new Pager('overlaySearchResultId', 3);
	pager1.init();
	pager1.showPageNav('pager1', 'searchOverlayPageNavigation');
	pager1.showPage(1);
}

function showLanguageDropdown() {
	if (!jQuery("#language_dropdown").hasClass('cms_show')) {
		// document.getElementById("language_dropdown").style.display="block";
		jQuery("#language_dropdown").removeClass("cms_hide");
		jQuery("#language_dropdown").addClass("cms_show");
	}
}

function hideLanguageDropdown() {
	if (!jQuery("#language_dropdown").hasClass('cms_hide')) {
		// document.getElementById("language_dropdown").style.display="block";
		jQuery("#language_dropdown").removeClass("cms_show");
		jQuery("#language_dropdown").addClass("cms_hide");
	}
}
// This function is for stripping leading and trailing spaces
function trimStr(str) {
	if (str != null) {
		var i;
		for (i = 0; i < str.length; i++) {
			if (str.charAt(i) != " ") {
				str = str.substring(i, str.length);
				break;
			}
		}
		for (i = str.length - 1; i >= 0; i--) {
			if (str.charAt(i) != " ") {
				str = str.substring(0, i + 1);
				break;
			}
		}
		if (str.charAt(0) == " ") {
			return "";
		} else {
			return str;
		}
	}
}

