//GLOBALS
hotTopicCurrent = 0; //temp variable to record current hottopic array index
highlightItemsPerBlock = 1; //number of highllight items to move at once
currentHighlightItem = 0; //temp variable to record current highlight that is moved item moved
///




$(document).ready(function() {
	
	//iphone detect script - iphonedetect.js
	iphoneDetect(); 
	
	//hide textOnlyForNonJSBrowsers
	$('div#textOnlyForNonJSBrowsers').css({'display':'none'});
	
	//switch to first hottopic
	switchHotTopic(); 
	//then infinite loop it
	setInterval("switchHotTopic()",5555);
	

	//show announcements
	showannouncements();
	//get data from highlight items array and display them
	showHighlights();
	//start hilighttems auto scroll
	startAutoScroller();
	
	
	//onfocus highlight input box
	//when input box is focused inputboxFocus class is swapped, when blured inputbox class is swapped
	$(".inputbox").focus(function() {
			$(this).removeClass("inputbox");
			$(this).addClass("inputboxFocus");
	}).blur(function() {
			$(this).removeClass("inputboxFocus");
			$(this).addClass("inputbox");
	});
	
	
	//jquery.00Cookie.js
	//check for theme cookie
	var c = new $.cookie();
	c.get();
	if(c.homePageTheme != '') themeSwitch(c.homePageTheme);	//if present switch to that theme, else default dark theme will stay
	//end of theme cookie

});



//Place a random image in slideshow area
//This is not a typical slideshow. image changes only on page reload.
//This is called right after slideshow img tag. It replaces the default slideshowimage with randomNumber.jpg
function loadSlideshowImage()
{
	var randomnumber=Math.floor(Math.random()*15); //set the number of pictures here
	randomnumber = randomnumber + 1; 
	$("div#slideShow img:last").css({'display':'none'})
							   .attr({'src':'images/slides/'+randomnumber+'.jpg'})
							   .css({'display':'inline'});
}


//load announcements
//get announcements from announcements array and load it
function showannouncements()
{
	for (var i in announcements)
	{
		$('#announcementItems').html(decodeURIComponent(announcements[i]));
	}
}



//this function switches hottopics
//get the current hottopic 
//show hottopic at that index
//increment current hottopic
function switchHotTopic()
{	
	$("div#hotTopics p").css({opacity:1}).animate({opacity:0.1}, function()
					{
						$("div#hotTopics p").html(decodeURIComponent(hottopics[hotTopicCurrent])).css({opacity:0}).animate({opacity:1});
						
						if(hottopics.length!=hotTopicCurrent+1) hotTopicCurrent++;
						else hotTopicCurrent=0;
					});
}


//show hightlights boxes
//using jquery createdomnodes plugin
//This adds an image, title and description to the highlightitem
function showHighlights()
{
	var context = $("div#scroller");
	var item = context._span();
	
	for (var i in HightlightsTitle)
	{
		var item = context._div({"class": "highlightItem"});
		
		var image = item._a({"class": "highlightItem-image"}).attr({'href':decodeURIComponent(HightlightsLink[i])});
		image._img().attr({'src':decodeURIComponent(HightlightsImage[i])});
		
		var info = item._div({"class": "highlightItem-infoArea"});
		
		var title = info._a({"class": "highlightItem-title"}).attr({'href':decodeURIComponent(HightlightsLink[i])})
		title.html(decodeURIComponent(HightlightsTitle[i]));
		
		var description = info._a({"class": "highlightItem-description"}).attr({'href':decodeURIComponent(HightlightsLink[i])})
		description.html(decodeURIComponent(HightlightsDescription[i]));
		
		item.click(function(){
						window.location = decodeURIComponent(HightlightsLink[$(this).prevAll().length]);
				});
	}
	
	//get the width of each highlight bar including margin
	var itemWidth = $($("div.highlightItem")[0]).outerWidth(true);
	
	//set the width of the visible scroller width to width of each highhlight item X number of visible items 
	$('div#scroller-container').css({'width':(itemWidth*5)+'px'});
	
	//set the main scroller-container width to the number of items X size of each item //added +5 as a fix for ie6
	context.css({'width':((HightlightsTitle.length)*itemWidth)+5+'px'}); 
}





//move the scroller, depending upon the direction
//This technique uses (-ve) margin property of the scroller - to create the effect
//at the end of each animation a timeout timer is called, which again calls this function back. That way it is a loop
//This technique is used to remove the user button on click bug - which occurs if we use a settimer 
function scrollerMove(direction)
{	
	var currentMargin = $('div#scroller').css('margin-left');
	var highlightItemWidth = $($("div.highlightItem")[0]).outerWidth(true);
	
	if(direction=='left')
	{
		currentHighlightItem = currentHighlightItem + highlightItemsPerBlock;
		
		//if not 1st block move 
		if(currentHighlightItem != highlightItemsPerBlock)
		{
			var newMargin = (currentHighlightItem) * highlightItemWidth;
			$('div#scroller').stop().animate({'marginLeft':newMargin}, function(){startAutoScroller()});
		}
		else
		{//else make the current item back to 0;
			currentHighlightItem = 0;
		}
	}
	
	
	if(direction=='right')
	{
		currentHighlightItem = currentHighlightItem - highlightItemsPerBlock;

		//if last block move to 1st block
		if(currentHighlightItem == -1 * (HightlightsTitle.length-5+highlightItemsPerBlock)) currentHighlightItem = 0;
		
		var newMargin = (currentHighlightItem) * highlightItemWidth;
		$('div#scroller').stop().animate({'marginLeft':newMargin}, function(){startAutoScroller()});
	}
	
	
}


//When user clicks on left/right button, after the transition, this will be called.
//so, that it move the timer 55555 seconds ahead
function startAutoScroller()
{
	clearTimeout(window['scrollerTimer']);
	window['scrollerTimer'] = setTimeout("scrollerMove('right')",15000);
}

//clears the auto scroll timer
function stopAutoScroller()
{
	clearTimeout(window['scrollerTimer']);
}


//popup webmail form
function showWebmailForm()	
{
		
	var offset = $('#webmail').offset();
	$('div#webmailForm').css({'left':offset.left, 'top':offset.top, 'display':'block', 'opacity': 0, 'width':0, 'height':0})
						.animate({'opacity':1, 'width':'240px', 'height':'170px'},
								function()
								{
									setTimeout("$(\"[name='username']\").focus()",200);
								}
						);

}

//close popup webmail form
function hideWebmailForm()
{
	$('div#webmailForm').animate({'width':0, 'height':0, 'opacity':0},500,function(){
													$(this).css({'display':'none'});
												});
}



//search between site and directory
//This function gets the searchterm value and depending upon the selection (dir/search) redirects it to that page
function doSearch()
{
	
	var term = $($("[name='searchTerm']")[0]).attr('value');
	var searchUrl = "http://www.uis.edu/search/google.html?cx=013341582354443854947%3Auycnnhx_mey&cof=FORID%3A11&sa=Search&q=" + escape(term);
	var directoryUrl = "http://uisapp.uis.edu/directory/Default.aspx?search_type=all&search=" + escape(term);
	
	
	var current = $($("[name='siteOrDirectory']:checked")[0]).attr('value');
	
	if(current == 'Site')
	{
		window.location = searchUrl;
	}
	else if(current == 'Directory')
	{
		window.location = directoryUrl;
	}
	
}



//Switches the theme
//if selectorUpdate is false or null, updates the theme and theme selector
//if selectorUpdate is true, just updates the theme selecor
function themeSwitch(name)
{
	var link = $($("link[rel=stylesheet]")[1]);
	
	switch(name)
	{
		case 'light':
					link.attr({href : "css/light.css"});
					$('#lightTheme').css({'fontSize':'14px'}); //switch font size
					$('#darkTheme').css({'fontSize':'10px'}); //switch font size
					$('#title').attr({'src':'images/themes/light/wordmarkmatte.gif'});
					break;
		case 'dark':
					link.attr({href : "css/dark.css"});
					$('#lightTheme').css({'fontSize':'10px'}); //switch font size
					$('#darkTheme').css({'fontSize':'14px'}); //switch font size
					$('#title').attr({'src':'images/themes/dark/wordmarkmatte.gif'});
					break;
	}
	
	//set cookie
	var c = new $.cookie();
	c.homePageTheme = name;
	c.set();
}




//fix for webmail form submit
function webmailSubmit(e)
{
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) // IE
    {
  		keynum = e.keyCode;
  	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	if(keynum == 13) 
		 document.logonForm.submit();
}