var currentZoom = 4;
var dimensions;
var mapInitCounter = 0;
var googleMapLeft = 0;
var googleMapRight = 0;
var timePosition = 0;


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Campus welcome screen message loop
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	var messageCounter = 0;
	
	var message = new Array();
	setTimeout("setInterval('loopWriter()',5000)",200);

$(document).ready(function(){	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Autosets the width and height of the background and iFrames. Readjusts to browser resize.
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	//$('#background').height($(window).height());
	//$('#background').width($(window).width());
	
	$('body').height($(window).height());
	$('body').width($(window).width());
	
	$('.iframe').height( parseInt( $(window).height() )-parseInt( $('#header').height() )-20);
	$('.iframe').width( parseInt( $(window).width() )-( parseInt( $(window).width() )*.05));
	$('.iframe').css('left',( parseInt( $(window).width() )*.05)/2);
	
	$('.iframe iframe').height(parseInt($(window).height())-parseInt($('#header').height())-20);
	$('.iframe iframe').width( parseInt( $(window).width() )-( parseInt( $(window).width() )*.05));
	
	$('#button-close').css('right', parseInt( $(window).width() )*.05);
	
	$('#shade').height(parseInt($(window).height())-parseInt($('#header').height()));
	$('#shade').width( parseInt( $(window).width() ));
	
	$(window).resize(function() {
		//$('#background').height($(window).height());
		//$('#background').width($(window).width());
		
		$('body').height($(window).height());
		$('body').width($(window).width());
		
		$('.iframe').height( parseInt( $(window).height() )-parseInt( $('#header').height() )-20);
		$('.iframe').width( parseInt( $(window).width() )-( parseInt( $(window).width() )*.05));
		$('.iframe').css('left',( parseInt( $(window).width() )*.05)/2);
		
		$('.iframe iframe').height(parseInt($(window).height())-parseInt($('#header').height())-20);
		$('.iframe iframe').width( parseInt( $(window).width() )-( parseInt( $(window).width() )*.05));
		
		if ( $('#news').height() == 200){
			$('#button-close').css('right', parseInt( $(window).width() )*.05);
		} else {
			$('#button-close').css('right',(  $('#news').offset().left ) );
		}

		$('#shade').height(parseInt($(window).height())-parseInt($('#header').height()));
		$('#shade').width( parseInt( $(window).width() ));
		
	});


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Pops up iFrames when a header icon is clicked.
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
	$(".icon").not("#iconClass").not("#iconOrient").not("#iconStudent").not("#iconCommunity").click(function(){
		
		$('#button-close').css('right', parseInt( $(window).width() )*.05);

		$('#shade').fadeIn('fast');
		$('#button-close').fadeIn('fast');
		
		$('#news').css({'float' : 'none'});
		$('#bottomRow').css('position','static');
		$('#news .title').removeClass("newsSelected");
		$('#news').css({'height':'200px','margin-top':'20px'});
		$('#newsTitles').css({height:200});
		$('#newsStories').css({height:200});
		$('#news .story').hide();
		$('#news').css('z-index','9');
		$('#news .title:gt(4)').hide();
		
		resetLoopWriter();
		
	});

	$("#iconEmail").click(function(){
		if ( $('#iframeEmail:visible').length ){
			resetPage()
		} else {
			if ( $(".iframe:visible").length ){
				$(".iframe:visible").hide()
				$("#iframeEmail").show();
			} else {
				$("#iframeEmail").slideDown('slow');
			}
		}
	});

	
	$("#iconAid").click(function(){
		if ( $('#iframeAid:visible').length ){
			resetPage()
		} else {
			if ( $(".iframe:visible").length ){
				$(".iframe:visible").hide()
				$("#iframeAid").show();
			} else {
				$("#iframeAid").slideDown('slow');
			}
		}
	});
	
	$("#logo").click(function(){
		resetPage()
		return false;
	});
	
	$("#shade").click(function(){
		if ( $(".iframe:visible").length || $('#news .story:visible').length){	
			resetPage()	
		}
	});
	
	$("#button-close").click(function(){	
			resetPage()
	});

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Limits the amount of News Feed Titles initially displayed. 
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	$('#news .title:lt(5)').show();

	$('#news .title').click(function(){
		
		$('#news .title').removeClass("newsSelected");
		$(this).addClass("newsSelected");
		
		if ( $('#news').height() == 200){
			
			$('#news').css({'float' : 'left'});
			
			$('#bottomRow').css('position','absolute');
			
			$('#news').css({'height':'495px','margin-top':'9px'});
			$('#newsTitles').css({height:495});
			$('#newsStories').css({height:495});
			$('#news .title').show();	
			
			
			$('#shade').fadeIn('fast');
			$('#button-close').css('right',(  $('#news').offset().left ) );
			$('#button-close').fadeIn('fast');
		}
		
		var currentIndex = $(this).prevAll().length+1;
		$('#news .story').hide();
		$('#news .story:eq('+currentIndex+')').show();
		$('#news').css('z-index','108');
		return false;
	});
	
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Allows user to select a custom background
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	
	if (screen.width > screen.height){
		if (screen.width <= 1024) {
			dimensions = "1024x768";
		} else if (screen.width <= 1280){
			if (screen.height <= 800){
				dimensions = "1280x800";
			} else {
				dimensions = "1280x1024";
			}
		} else if (screen.width <= 1440){
			dimensions = "1440x900";
		} else if (screen.width <= 1680){
			dimensions = "1680x1050";
		} else if (screen.width <= 1920){
			dimensions = "1920x1200";
		} else {
			dimensions = "1920x1200";
		}
	} else {
		if (screen.height <= 768) {
			dimensions = "1024x768";
		} else if (screen.height <= 800){
			dimensions = "1280x800";
		} else if (screen.height <= 1024){
			dimensions = "1280x1024";
		} else if (screen.height <= 1200){
			dimensions = "1920x1200";
		} else {
			dimensions = "1920x1200";
		}
	}
	
	if ($.cookie("picture") == null){
		$('body').css('background-image','url("Images/backgrounds/'+backgrounds[2]+'-'+dimensions+'.jpg")');
		$.cookie("picture", 2, { expires: 30000 });
	} else {
		$('body').css('background-image','url("Images/backgrounds/'+backgrounds[ parseInt( $.cookie('picture') ) ]+'-'+dimensions+'.jpg")');
	}
	
	
	$(document).keydown(function(e){
		if (e.keyCode == 37) { //left
		
				var currentBack = parseInt($.cookie("picture"));
				currentBack--;
				
				if (currentBack < 0){
					currentBack = backgrounds.length-1;
				}
				
				$('body').css('background-image','url("Images/backgrounds/'+backgrounds[currentBack]+'-'+dimensions+'.jpg")');
				$.cookie("picture", currentBack, { expires: 30000 });

		   		return false;
		} else if (e.keyCode == 39) { //right
		
				var currentBack = parseInt($.cookie("picture"));
				currentBack++;
				
				if (currentBack >  backgrounds.length-1){
					currentBack = 0;
				}
				
				$('body').css('background-image','url("Images/backgrounds/'+backgrounds[currentBack]+'-'+dimensions+'.jpg")');
				$.cookie("picture", currentBack, { expires: 30000 });
				
		} else if (e.keyCode == 27) { //escape
			if ($('#middle:visible').length){
				$('#middle').delay(200).fadeOut('slow');
				$('.escape').fadeIn('slow');
			} else {
				$('.escape').fadeOut('slow');
				$('#middle').delay(200).fadeIn('slow');
				resetLoopWriter();

			}
		}
	});



	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Loads an xml file that populates the rotating messages
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	$.ajax({type: "GET", url: "messages.xml", dataType: "xml", success: function(xml){
			
			
			
			var counter = 0;
			
			$(xml).find('message').each(function(index){
			
				if (counter > 0){
				
					var newText = $(this).text();
	
					
					if (newText.search("\\[") > -1){
						message.push('<a href="'+newText.substring( newText.search("\\[")+1 , newText.search("\\]") )+'">'+newText.substring( newText.search("\\]")+1 )+'</a>' );
					} else {
						message.push(newText);	
					}
					
					if (newText == "Reserved for the time"){
						timePosition = index;
					}
				
				}
				
				counter++;
				
			});
			
			$('#welcomeScreen li').each(function(){
				
				var newText = $(this).text();
				
				if (newText != ""){
					
					if (newText.search("\\[") > -1){
						message.push('<a href="'+newText.substring( newText.search("\\[")+1 , newText.search("\\]") )+'">'+newText.substring( newText.search("\\]")+1 )+'</a>' );
					} else {
						message.push(newText);	
					}
	
				}
				
			});
			
	}}); 


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Resizes the google map when a button is clicked
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	$('#googleMap').hover(
		function(){
			$('.grow').show();		
		},
		function(){
			$('.grow').hide();
		}
	);
	
	$('.grow').toggle(
		function(){
			googleMapLeft = $('#googleMap').offset().left - 40;
			googleMapTop = $('#googleMap').offset().top;

			$('#googleMap .ribbon').fadeOut('fast');
			$('#googleMap').css({'position':'absolute','left':$('#googleMap').offset().left,'top':$('#googleMap').offset().top,'z-index':'106'});
			$('#googleMap').animate({
				width:$(window).width()-16,
				height:($(window).height()-$('#header').height()-8),
				top:$('#header').height(),
				left:8,
				marginLeft:'0'
			}, 1000);
			$('#map_canvas').fadeTo(0.25);
			$('#map_canvas').animate({
				width:$(window).width()-17,
				height:($(window).height()-$('#header').height()-8),
			}, 1000, function(){
				initialize();
				$('#map_canvas').fadeTo(1);
			});
			
			
			$('.grow p').text('Shrink');	
			
		},
		function(){
			$('#googleMap .ribbon').fadeIn('fast');
			$('#googleMap').animate({
				width:'415',
				height:'285',
				marginLeft:'40',
				top:googleMapTop,
				left:googleMapLeft
			}, 1000, function(){
				$('#googleMap').css({'position':'relative','z-index':'5','top':'0','left':'0'});
			});
			
			$('#map_canvas').animate({
				width:415,
				height:285,
			}, 1000);

			$('.grow p').text('Grow');	
		}	
	);

}); //<-- THIS IS THE END OF DOCUMENT.READY()


	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Loads a google map and populates with the xml file containing all student lats and longs.
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	var map;
	var loadmore = true;
	
	var image;
	var shadow;
	
	var markerXML;
	var currentMarkers = [];
		
	function initialize() {
		var myLatlng = new google.maps.LatLng(39.90973623453719, -93.8984375);
		
		if (mapInitCounter % 2 == 0) {
			var myOptions = {
				zoom: 3,
				center: myLatlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				scaleControl: false,
				navigationControl: true,
				navigationControlOptions: {
					style: google.maps.NavigationControlStyle.DEFAULT,
					position: google.maps.ControlPosition.RIGHT
				},
				mapTypeControl: false
			}
		} else {
			var myOptions = {
				zoom: 5,
				center: myLatlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				scaleControl: false,
				navigationControl: true,
				navigationControlOptions: {
					style: google.maps.NavigationControlStyle.DEFAULT,
					position: google.maps.ControlPosition.RIGHT
				},
				mapTypeControl: false
			}
			
		}
		mapInitCounter++;
		
		
		var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
		var image = new google.maps.MarkerImage('Images/pin.png',
			//Size
			new google.maps.Size(27, 16),
			//Origin
			new google.maps.Point(0,0),
			//Anchor
			new google.maps.Point(13, 16)
		);
		
		var shadow = new google.maps.MarkerImage('Images/shadow.png',
			
			new google.maps.Size(40, 16),
			new google.maps.Point(0,0),
			new google.maps.Point(15, 16)
		);
			
		

		$.ajax({type: "GET", url: "latlong.xml", dataType: "xml", success: function(xml){
		
			markerXML = xml;
			
			$(xml).find('out marker').each(function(){
						
				var marker = new google.maps.Marker({
					position: new google.maps.LatLng( parseFloat( $(this).attr("lat") ), parseFloat( $(this).attr("lng") ) ),
					title:$(this).attr("address"),
					shadow: shadow,
       				icon: image,
					map: map
				});
				
				currentMarkers.push(marker);
				
			});
			
		}}); 
		
		function deleteOverlays() {
		  if (currentMarkers) {
			for (i in currentMarkers) {
			  currentMarkers[i].setMap(null);
			}
			currentMarkers.length = 0;
		  }
		} 
		
		google.maps.event.addListener(map, 'zoom_changed', function() {
			zoom = parseInt(map.getZoom());
			
			if (zoom > 4){
				
				if (currentZoom == 5){
					deleteOverlays();
				}
				
				$(markerXML).find('in marker').each(function(){
				
					var moreMarker = new google.maps.Marker({
						position: new google.maps.LatLng( parseFloat( $(this).attr("lat") ), parseFloat( $(this).attr("lng") ) ),
						title:$(this).attr("address"),
						shadow: shadow,
						icon: image,
						map: map
					});
									
					currentMarkers.push(moreMarker);
		
				});
				
				currentZoom = zoom;
		
			} else {
			
				if (currentZoom == 4){
					deleteOverlays();
				}
				
				$(markerXML).find('out marker').each(function(){
				
					var moreMarker = new google.maps.Marker({
						position: new google.maps.LatLng( parseFloat( $(this).attr("lat") ), parseFloat( $(this).attr("lng") ) ),
						title:$(this).attr("address"),
						shadow: shadow,
						icon: image,
						map: map
					});
							
						currentMarkers.push(moreMarker);
				 
				});
				
				currentZoom = zoom;
			}
		
		});

		
		
				
	}	 	

function resetPage(){
	
		$('#news').css({'float' : 'none'});
		$('#bottomRow').css('position','static');
		$('#news .title').removeClass("newsSelected");
		$(".iframe").slideUp('slow');
		$('#news').css({'height':'200px','margin-top':'20px'});
		$('#newsTitles').css({height:200});
		$('#newsStories').css({height:200});
		$('#news .story').hide();
		$('#news').css('z-index','9');
		$('#news .title:gt(4)').hide();
		$('#shade').fadeOut('fast');
		$('#welcomeScreen').show();
		$("#button-close").fadeOut('fast');
		
		resetLoopWriter();
}

	
function loopWriter(){
		
		
		var time = new Date();
		var hours = 0;
		var minutes = 0;
		var ampm = "am";
		
		if (time.getHours() > 12){
			hours = time.getHours() - 12;
			ampm = "pm";
		} else {
			hours = time.getHours();
		}
	
		if (time.getMinutes() < 10){
			minutes = "0"+time.getMinutes();
		} else {
			minutes = time.getMinutes();
		}

		if (timePosition == 0){
			message.splice ( -1, 0, "It's now "+hours+":"+minutes+' '+ampm);
			timePosition++;
		} else {
			message[message.length-2] = "It's now "+hours+":"+minutes+' '+ampm;
		}
				
		$("#welcomeScreen li").eq(0).fadeOut(1000,function(){ 
		
			$("#welcomeScreen li").eq(0).remove(); 

		});
		
		$("#welcomeScreen li").eq(1).animate({
			opacity: 0.20,
			top: '10',
			color:"#fff"
		  }, 1050);
		$("#welcomeScreen li").eq(1).find('a').animate({
			color:"#fff"
		  }, 1050);
		  
		$("#welcomeScreen li").eq(2).animate({
			opacity: 0.40,
			top: '70',
			color:"#fff"
		  }, 1050);
		$("#welcomeScreen li").eq(2).find('a').animate({
			color:"#fff"
		 }, 1050);

		$("#welcomeScreen ul").append("<li style='top:220px'>"+message[messageCounter]+"</li>");
	
		var textLength = message[messageCounter].replace(/<.*?>/g, '');

		if (textLength.length > 38){
			$("#welcomeScreen li").eq(3).addClass('long');
		} else {
			$("#welcomeScreen ul").eq(3).removeClass('long');
		}
		
		$("#welcomeScreen li").eq(3).animate({
			top: '130',
		  }, 1050);
		
		if (messageCounter >= message.length-1){
			messageCounter = 0;
		} else {
			messageCounter++;
		}
	
}
	
function resetLoopWriter(){
	
	$('#welcomeScreen li').text('');
	$('#welcomeScreen li').eq(2).text('Welcome to campus');	
	
}
	
	

