var lynx = false;
var ActiveSearchOption = 0;

var Option1ImgOn = new Image();
var Option1ImgOff = new Image();
Option1ImgOn.src = "https://www.csusm.edu/images/search/header_search_option1_on_02.png";
Option1ImgOff.src = "https://www.csusm.edu/images/search/header_search_option1_off_02.png";

var Option2ImgOn = new Image();
var Option2ImgOff = new Image();
Option2ImgOn.src = "https://www.csusm.edu/images/search/header_search_option2_on_02.png";
Option2ImgOff.src = "https://www.csusm.edu/images/search/header_search_option2_off_02.png";

var IndexImgOn = new Image();
var IndexImgOff = new Image();
IndexImgOn.src = "https://www.csusm.edu/images/search/header_search_index_active.png";
IndexImgOff.src = "https://www.csusm.edu/images/search/header_search_index.png";

var HelpImgOn = new Image();
var HelpImgOff = new Image();
HelpImgOn.src = "https://www.csusm.edu/images/search/header_search_help_active.png";
HelpImgOff.src = "https://www.csusm.edu/images/search/header_search_help.png";

$(document).ready(
	function()
	{
		$.fn.getParam = (
			function(param)
			{
				var value = ""; //set the current value to nothing

				if (document.location.href.indexOf("?") > 0) //if the url is dynamic
				{
					var url = document.location.href.split("?"); //split the url into 2 pieces
					var variables = url[1].split("&"); //take all the dynamic variables and split them

					for(i = 0; i < variables.length; i++) //loop through the variables
					{
						var name = variables[i].split("="); //split the variable's value into 2

						if (name[0] && name[0] == param) //if the variable exisits and name is equal to the passed parameter
						{
							value = name[1]; //set the value to the value of the variable
							break; //stop the loop
						}
					}
				}

				return value; //return the value		
			}
		);

		var searchType = (!document.getElementById("type")) ? $.fn.getParam("type") : $("#type").val(); //gets the search type parameter
		var button1 = (searchType == "google" || searchType == "googlex2" || searchType == "") ? "on" : "off";
		var button2 = (searchType == "dir") ? "on" : "off"; //checked to see if selected

		//write search-box html
		var searchboxhtml = '';
		
		searchboxhtml += '<img src="https://www.csusm.edu/images/search/header_search_defaulttext.png" alt="Search Web or Directory" width="154" height="20" id="header_search_defaulttext" />'
		searchboxhtml += '<input title="Enter Search Expression" alt="Enter Search Expression" id="header_search-text" maxlength="50" name="q" size="25" autocomplete="off" type="text" />'
		searchboxhtml += '<input alt="Go - Execute Search" height="22" width="32" id="header_search-go" src="https://www.csusm.edu/images/search/search_button.png" type="image" />'
		searchboxhtml += '<img alt="Website Search Option" width="38" height="15" id="header_search_option1" src="https://www.csusm.edu/images/search/header_search_option1_'+button1+'_02.png" />'
		searchboxhtml += '<img alt="Directory Search Option" width="59" height="15" id="header_search_option2" src="https://www.csusm.edu/images/search/header_search_option2_'+button2+'_02.png" />'
		searchboxhtml += '<img alt="A-Z Index Search Option" width="32" height="10" id="header_search_index" src="https://www.csusm.edu/images/search/header_search_index.png" />'
		searchboxhtml += '<img alt="vline" width="1" height="14" id="header_search_vline" src="https://www.csusm.edu/images/search/header_search_vline.png" />'
		searchboxhtml += '<img alt="help" width="25" height="10" id="header_search_help" src="https://www.csusm.edu/images/search/header_search_help.png" />'
		searchboxhtml += '<div id="header_search_popout">'
		searchboxhtml += '<div id="popout_links">'
		searchboxhtml += '<a href="http://biblio.csusm.edu/research_portal/ask-a-librarian">> Ask a Librarian</a><br />'
		searchboxhtml += '<a href="javascript:OpenAdmissionsWin()" id="admissionspopup">> Ask the Cougar</a><br />'
		searchboxhtml += '<a href="javascript:OpenChatWindow()" id="chatpopup">> Ask an IT Technician</a><br />'
		searchboxhtml += '<a href="http://www.csusm.edu/iits/support/studenthd/index.html">> Technology Helpdesk</a><br />'
		searchboxhtml += '<a href="http://www.csusm.edu/students/cougarcentral.html">> Cougar Central</a><br />'
		searchboxhtml += '<a href="http://www.csusm.edu/about/contact.html">> Contact CSUSM</a>'
		searchboxhtml += '</div></div>'
		
		$("#header_search_frame").html(searchboxhtml);
		//$("#header_search_frame").html('<input alt="Enter Search Expression" id="header_search-text" maxlength="50" name="q" size="25" autocomplete="off" type="text"/>');

		$("#header_search-text").val($("#searchtxt").val()); //set the search value into the header search box

		$("#header_search_defaulttext").bind(
			{
				click:
					function() 
					{
						$("#header_search_defaulttext").hide();	
						document.getElementById("header_search-text").focus();
					},
				mouseover:
					function ()
					{
						$("#header_search_popout").hide();
					} 
			}
				
		);			

		$("#header_search-text").bind(
			{
				blur:
					function() 
					{
						if(document.getElementById("header_search-text").value.length==0)
						{
							$("#header_search_defaulttext").show();
						}
					},
				
				mouseover:
					function ()
					{
						$("#header_search_popout").hide();
					},
				
				keydown:
					function(event)
					{
						if (event.keyCode == 13) //keyCode 13 = enter key
							$.fn.doSearch(0); //search, lets function know we searched from the header search box
					}
			}
		);

		$("#header_search-go").hover(
			function ()
			{
				$("#header_search_popout").hide("fast");
			}
		);

		$("#header_search_index").hover(
			function ()
			{
				document.getElementById("header_search_index").src = IndexImgOn.src;
				$("#header_search_popout").hide("fast");
			}, 
			function () 
			{
				document.getElementById("header_search_index").src = IndexImgOff.src;
			}
		);

		$("#header_search_help").bind(
			{
				mouseover:
					function ()
					{
						document.getElementById("header_search_help").src = HelpImgOn.src;
						$("#header_search_popout").show("fast");

					}, 
				mouseout:
					function () 
					{
						document.getElementById("header_search_help").src = HelpImgOff.src;
						$("#header_search_popout").hide();
					}
			}
		);

		//show-hide popout
		$("#header_search_popout").hover(
			function ()
			{
				$("#header_search_popout").show();
				document.getElementById("header_search_help").src = HelpImgOn.src;
			}, 
			function () 
			{
				$("#header_search_popout").hide("fast");
				document.getElementById("header_search_help").src = HelpImgOff.src;
			}
		);

		//updates what we want to search - for website search
		$("#header_search_option1").bind("click",
			function()
			{
				document.getElementById("header_search_option1").src = Option1ImgOn.src;
				document.getElementById("header_search_option2").src = Option2ImgOff.src;
				document.getElementById("header_search-text").focus();
				ActiveSearchOption = 0;
				
				$("#header_search_defaulttext").hide();
			}
		);

		//updates what we want to search - for directory search
		$("#header_search_option2").bind("click",
			function()
			{
				document.getElementById("header_search_option1").src = Option1ImgOff.src;
				document.getElementById("header_search_option2").src = Option2ImgOn.src;
				document.getElementById("header_search-text").focus();
				ActiveSearchOption = 1;
				
				$("#header_search_defaulttext").hide();
			}
		);

		//updates what we want to search - for the site index
		$("#header_search_index").bind("click",
			function()
			{
				var prevloc = ($.fn.getParam("prevloc") == "" && !lynx) ? document.location.href : $.fn.getParam("prevloc");
				window.location = "http://lynx.csusm.edu/cascade/search/default.asp?act=search&type=index&prevloc="+prevloc;
			}
		);

		//if the "go" button is clicked on the header search box
		$("#header_search-go").bind("click",
			function()
			{
				$.fn.doSearch(0);
			}
		);
		
		//if the "search" button is clicked on the search box (dir and web)
		$("#search_button,#search_directory_button").bind("click",
			function()
			{
				$.fn.doSearch(1);									  							 
			}
		);
		
		$.fn.doSearch = (
			function(type)
			{
			
			
				var domain = document.domain; //get the current domain	
				var stype = (ActiveSearchOption == "0") ? "google" : "dir"; //deciede what type of search
				var searchText = (type == 0) ? $("#header_search-text").val() : $("#searchtxt").val(); //get the search text
				var appdomain = "";

				if (domain == "lynx.csusm.edu" || domain == "lynx" || domain == "www" || domain == "www.csusm.edu")
					appdomain = "lynx";
				else if (domain == "lynxdev.csusm.edu" || domain == "lynxdev" || domain == "www-dev" || domain == "www-dev.csusm.edu")
					appdomain = "lynxdev";

				if (domain == "lynx.csusm.edu" || domain == "lynx" || domain == "lynxdev.csusm.edu" || domain == "lynxdev") //check to see if they are coming from www/
				{
					var prevloc = $("#prevloc").val(); //get the previous location

					if (type == 0) //if the user uses the header search on the search page
					{
						window.location = "http://" + appdomain + ".csusm.edu/cascade/search/default.asp?act=search&type="+stype+"&txt="+searchText+"&prevloc="+prevloc; //redirect them to the search page
					}
					else
					{
						if (searchText.length >= 2) //check to make sure the search text is long enough
						{
							document.searchform.submit();
						}
						else
						{
							$("#search_tooshort").slideDown(); //else show error
						}
					}
				}
				else
				{
					var prevloc = document.location.href; //set the previous location as the current page
					window.location = "http://" + appdomain + ".csusm.edu/cascade/search/default.asp?act=search&type="+stype+"&txt="+searchText+"&prevloc="+prevloc; //redirect them to the search
				}		
			}
		);	

		//table Table-Alt-H function
		$(".Table-Alt-H").addClass("Table01");	
		$(".Table-Alt-H th").addClass("Table01-Header");		

		$(".Table-Alt-H tr:not([th]):nth-child(odd)").addClass("Table01-CellDark");
		$(".Table-Alt-H tr:nth-child(odd)").mouseover(function(){$(this).removeClass("Table01-CellDark"); $(this).addClass("thover");})		
		$(".Table-Alt-H tr:nth-child(odd)").mouseout(function(){$(this).removeClass("thover");$(this).addClass("Table01-CellDark");});

		$(".Table-Alt-H tr:nth-child(even)").addClass("Table01-CellLight");
		$(".Table-Alt-H tr:nth-child(even)").mouseover(function(){$(this).removeClass("Table01-CellLight"); $(this).addClass("thover");})		
		$(".Table-Alt-H tr:nth-child(even)").mouseout(function(){$(this).removeClass("thover");$(this).addClass("Table01-CellLight");});			

		//table Table-Alt-V function	
		$(".Table-Alt-V").addClass("Table01");	
		$(".Table-Alt-V th").addClass("Table01-Header");	
		$(".Table-Alt-V tr td:nth-child(even)").addClass("Table01-CellLight");
		$(".Table-Alt-V tr td:nth-child(odd)").addClass("Table01-CellDark");	

	}
);


//Theme image slideshow
$(function($)
{
	//set interval to change first image
	var interval = $(".active").attr("interval");
	var $active;
	var $next;
	var $activeIndex;
	var $nextIndex;
	var timer;
	timer = setTimeout("$.fn.slideSwitch(0,0)", interval);

	$(".imageIndex").click(
		function()
		{
			clearTimeout(timer);
			$.fn.slideSwitch(1,$(this).attr('imageIndex'));
		 }
	);

	$.fn.slideSwitch = (
		function(clicked,imageIndex)
		{
			//get active image
			$active = $('#theme_image_ss IMG.active');

			if($active.length==0) 
				$active = $('#theme_image_ss IMG:last');

			//check to see if the index has been clicked and set next image
			if (!clicked)
			{
				//force timoutclear
				clearTimeout(timer);
				imageIndex = 9;
				$next =  ($active.next().length) ? $active.next() : $('#theme_image_ss IMG:first');
			}
			else
			{
				$next = $("#theme_image_ss IMG:eq("+imageIndex+")");
			}

			$nextIndex = $next.attr('imageIndex');
			$activeIndex = $active.attr('imageIndex');

			if (imageIndex !=$activeIndex) 
			{
				//switch image and thumbs		
				$("#theme_slideshow IMG:eq("+$activeIndex+")").attr({"src":"box.gif"});
				$("#theme_slideshow IMG:eq("+$nextIndex+")").attr({"src":"box_fill.gif"});
				$active.addClass('last-active');

				$next.css({opacity: 0.0})
				.addClass('active')

				.animate({opacity: 1.0}, 1000, 
					function()
					{
						$active.removeClass('active last-active');
						//get interval for new active image
						interval = $(".active").attr("interval");
						//repeat loop

						if (!clicked) 
							timer = setTimeout("$.fn.slideSwitch()", interval);
					}
				);			
			}
		}
	);

});


function OpenChatWindow()
{
	window.open('https://chat.csusm.edu:9091/webchat/userinfo.jsp?chatID=7zX4OheD9z&workgroup=iits-callcenter@workgroup.chat.csusm.edu', 'chatwindow', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=520,height=520,left = 700,top = 280');
}

function OpenAdmissionsWin()
{
	window.open('http://csusm.askadmissions.net/ask.aspx?iname=testing', 'admissionswin', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=630,height=600,left = 700,top = 280');
}

function AskCougarPopup(form, winName)
{
	if(!window.focus)
		return true;
	window.open('', winName, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=630,height=600,left = 700,top = 280');
	form.target = winName;
	return true;
}

function GetXmlHttpObject()
{
    var objXMLHttp = null;
    
    if (window.XMLHttpRequest)
	{
        try{
            objXMLHttp = new XMLHttpRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.createRequest){
        try{
            objXMLHttp = new window.createRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.ActiveXObject){
        try {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try {
                objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                objXMLHttp = false;
            }
        }
    }
    
    return objXMLHttp;
}
