// Preload Images
		
	imagesToLoad = ["images/mtnBanner50.png", 
	                "images/churchBanner.png",
	                "images/churchoutsmall.png", 
	                "images/SandH2.jpg",
	                "images/csSentinel1.jpg"];
	                
	var imgObj = new Image(0,0);
	for (var i=0; i < imagesToLoad.length; i++) {
		imgObj.src = imagesToLoad[i];
	}
	
	$(document).ready(function() {	
		
		// Setup Login events

		//select all the a tag with name equal to modal
		$('a[name=modal]').click(function(e) {
			//Cancel the link behavior
			e.preventDefault();
			

			// Hide Gif Spinning Rotator
			$('#ajax_loading').hide();  
			
			//Get the A tag
			var dest = $(this).attr('href');
			
			// Add destination to form
			$('#login input[name="destination"]').val(dest);
		
			//Get the screen height and width
			var maskHeight = $(document).height();
			var maskWidth = $(window).width();
		
			//Set height and width to mask to fill up the whole screen
			$('#mask').css({'width':maskWidth,'height':maskHeight});
			
			//transition effect		
			$('#mask').fadeIn(1000);	
			$('#mask').fadeTo("slow",0.8);	
		
			//Get the window height and width
			var winH = $(window).height();
			var winW = $(window).width();
			
			var id = "#login";
	              
			//Set the popup window to center
			$(id).css('top',  winH/2-$(id).height()/2);
			$(id).css('left', winW/2-$(id).width()/2);
		
			//transition effect
			$(id).fadeIn(2000); 
		
		});
		
		//if close button is clicked
		$('.window .close').click(function (e) {
			//Cancel the link behavior
			e.preventDefault();
			
			$('#mask').hide();
			$('.window').hide();
		});		
		
		//if mask is clicked
		$('#mask').click(function () {
			$(this).hide();
			$('.window').hide();
		});	
		
		// When the form is submitted
		$("#loginForm").submit(function(){  
			
			// Show Gif Spinning Rotator
			$('#ajax_loading').show();
			
			// 'this' refers to the current submitted form  
			var str = $(this).serialize();  

			// -- Start AJAX Call --

			$.ajax({  
			    type: "POST",

			    url: "loginValidate.php",  // Send the login info to this page
			    data: str,  
			    success: function(msg){  
			   				
					$("#status").ajaxComplete(function(event, request, settings){  
		
						// Hide Gif Spinning Rotator
						$('#ajax_loading').hide();  
						
						if(msg.substring(0,2) == 'OK')  {   // LOGIN OK?
													
							$('#loginForm').hide();
												
							
							var login_response = '<div id="logged_in">' +
							 '<div style="width: 350px; float: left; margin-left: 70px;">' + 
							 '<div style="width: 40px; float: left;">' +
							 '<img style="margin: 10px 0px 10px 0px;" align="absmiddle" src="images/ajax-loader.gif">' +
							 '</div>' +
							 '<div style="margin: 10px 0px 0px 10px; float: right; width: 300px; font-weight: bold;">'+ 
							 "You are successfully logged in! <br /> Please wait while you're redirected...</div></div>";  
							
							
							 $(this).html(login_response); // Refers to 'status' 
							 					
												
							// After 3 seconds redirect the 
							setTimeout('go_to_destination()', 3000); 
						  
						 }  else   {  // ERROR?
		   
							 var login_response = msg;
							 $('#login_response').html(login_response);
						 }  
	     
					});
				}
			
			});
			
			return false;
							
		});
				
		// Create Rounded Corners for older browsers
		
		function hasBorderRadius(){
			  var element = document.documentElement;
			  var style = element.style;
			  if (style){
			    return typeof style.borderRadius == "string" ||
			      typeof style.MozBorderRadius == "string" ||
			      typeof style.WebkitBorderRadius == "string" ||
			      typeof style.KhtmlBorderRadius == "string";
			  }
			  return null;
		}
		
		$(function(){
			  if(!hasBorderRadius()){
			    $("#secContainer").corners("round 10px");
			  }
		});
		
		
		// Setup Drop Down
		
		$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
	      
        $("#navbar ul li span").click(function() { //When trigger is clicked...  
      
            //Following events are applied to the subnav itself (moving subnav up and down)  
            $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
      
           $(this).parent().hover(function() {  
           }, function(){  
               $(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up  
           });  
     
           //Following events are applied to the trigger (Hover events for the trigger)  
           }).hover(function() {  
               $(this).addClass("subhover"); //On hover over, add class "subhover"  
           }, function(){  //On Hover Out  
               $(this).removeClass("subhover"); //On hover out, remove class "subhover"  
       });  
		
		
		
	});  // End document Ready
	
	

	function go_to_destination() {		
		window.location = 'eventEntry.php'; 
	}
	    
     function getEvents() {
    	 $.ajax({
    		 type:		'GET',
    		 url:		'getEvents.php',
    		 dataType:	'json',
    		 success:	updatePage,
    		 error:		getEventsError
    	 }); 	 
    	 
     }
         
    var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
     
    function updatePage(jsonData, status, request) {

            var events = jsonData.newevents;
            
            for(var j in events){
                    
	              var eventTitle = events[j].title;
	              var eventContent = events[j].content; 
	              var locationName = events[j].locationName;
	              var locationAddr1 = events[j].locationAddr1;
	              var locationAddr2 = events[j].locationAddr2;
	              var locationCity = events[j].locationCity;
	              var locationState = events[j].locationState;
	              var locationZip = events[j].locationZip;
	              var link = events[j].link;
	              var label = events[j].label; 
	              var eventDate = events[j].eventDate;
	              var dateWork = eventDate.split("-");
	              var currMonth = parseInt(dateWork[1], 10) - 1;
	                      
	              var eventTime = events[j].eventTime;  
	              var timeWork = eventTime.split(":");   
	              var currHour = parseInt(timeWork[0]);
	              var amPm = "pm";
              
	              if(currHour >12){
	                currHour -= 12;
	                amPm = "pm";
	              }else if (currHour == 12){
	                amPm = "pm";
	              }else{
	                amPm = "am";
	              }
                       
	              var eventDiv = document.getElementById("newEvents");
	              var existingHTML = eventDiv.innerHTML;
	              var newEvent = 
	                  "<dt>" + eventTitle + "</dt>"+
	                  "<dd>" + m_names[currMonth] + " " + dateWork[2] + ", " + dateWork[0] + "  " + 
	                  "<em>" + currHour + ":" + timeWork[1] + " " + amPm + "</em></dd> ";
		                  
	              if(locationName) {
	                  newEvent += "<dd>" + locationName + "</dd> ";
	              }        
	              if(locationAddr1) {
	                  newEvent += "<dd>" + locationAddr1 + "</dd> ";
	              }
	              if(locationAddr2) {
	                  newEvent += "<dd>" + locationAddr2 + "</dd> ";
	              }
	              if(locationCity && locationState && locationZip) {
	                  newEvent += "<dd>" + locationCity + ", " +locationState+ " " +locationZip+ " </dd> ";
	              }  
	              
	              newEvent += "<dd class='eventContent'>" + eventContent + "</dd>";
	              
	              if (link && label) {
	                  newEvent += "<dd><a href='" + link + "'>" + label + "</a></dd>";
	              }   
	              
	              eventDiv.innerHTML = existingHTML + newEvent;
	                    

            } // end for loop

    } // end function updatePage
  
    
	 function getEventsError(request, textStatus, errorThrown) {	 
		    alert("Error! Status is: " + textStatus + " HTTP status is: " + errorThrown );	
	 }
	 
	 function getThumbNailEvents() {
    	 $.ajax({
    		 type:		'GET',
    		 url:		'getEvents.php',
    		 dataType:	'json',
    		 success:	updateSide,
    		 error:		getEventsError
    	 }); 	 
    	 
     }
	 
	 function updateSide(jsonData, status, request) {

         var events = jsonData.newevents;
         var allEvents = "";
         var eventDiv = document.getElementById("sideEvents");
         for(var j in events){
                 
	              var eventTitle = events[j].title;
	              var locationName = events[j].locationName;
	              var eventDate = events[j].eventDate;
	              var dateWork = eventDate.split("-");
	              var currMonth = parseInt(dateWork[1], 10) - 1;
	              	             
	              var newEvent = 
	                  "<dt>" + eventTitle + "</dt>"+
	                  "<dd>" + m_names[currMonth] + " " + dateWork[2] + ", " + dateWork[0] + "</dd> ";
	              if(locationName) {
	                  newEvent += "<dd>" + locationName + "</dd> ";
	              }               
	              
	              allEvents += newEvent ;
	              
         } // end for loop
         
         if(allEvents) {     
        	 eventDiv.innerHTML = "<a href='events.php'>" + allEvents + "</a>";
         }else{
        	 eventDiv.innerHTML = "<dt>No current Events</dt>";
         }
        	 

	 } // end function updateSide
	 
		 	 

    
		
		
		
		

		
		
			
