var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"/SpryAssets/SpryMenuBarDownHover.gif", imgRight:"/SpryAssets/SpryMenuBarRightHover.gif"});
var MenuBar2 = new Spry.Widget.MenuBar("MenuBar2", {imgRight:"/SpryAssets/SpryMenuBarRightHover.gif"});
var timer;
var someData;

Shadowbox.init({overlayOpacity: 0.7});

$(function() {  //On Document Ready Function (AKA: All this gets executed onload, makes jQuery safe)
		   
	$('#content').load('home.php','',showNewContent);
	$("#submit_new_pass").click(function() {  
    	$.post('/ajax/index.php', $('#change_pass').serialize(),function(data){$('#change_pass_div').html(data);}, 'html');
  	});
   
	$("#submit_ec_info").click(function() {
    	if(checkECInfo(document.getElementById("ec_info")))
      		$.post('/ajax/index.php', $('#ec_info').serialize(),function(data){
          		$('#ec_change_confirm').html(data);
          		$('#ec_change_confirm').effect("highlight", {}, 3000);
          		}, 'html');
	  	});
  
	$('#next_btn').click(function() {
		$.galleria.next();
	    return false;
	    });

	$('#prev_btn').click(function() {
		$.galleria.prev();
		return false;
		});
	
	$("#account_accordion").accordion({ collapsible: true, active: false });
	
	
	// Check for hash value in URL  
    var hash = window.location.hash.substr(1); 
    var href = $('.nav_img_hover').each(function(){  
        var href = $(this).attr('href');  
        if(href != null && hash==href.substr(0,href.length-4)){  
            var toLoad = hash+'.php';
            $('#content').load(toLoad,'',showNewContent);
        }  
    });
	
	$('.nav_img_hover').click(function() {
			var toLoad = $(this).attr('href');
			$('#content').hide('fast',loadContent);
			window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4); 
			//TODO: Create Content Loading Indicator
			function loadContent() {
				$('#content').html('');
				$('#content').load(toLoad,'',showNewContent)  
			}
			return false;
			
		});
	$('.nav_img_hover').hover(function() {
			$(this).animate({opacity: 1}, '400'); },
			
		function() {
			$(this).animate({opacity: 0}, '100'); }
		);
	$('#login_nav_hover').hover(function() {
			$(this).animate({opacity: 1}, '400'); },
			
		function() {
			$(this).animate({opacity: 0}, '100'); }
		);
		
	$('#login_nav_hover').click(function() {
			$.post('/dev/login.php', '',function(data){someData = data;
								Shadowbox.open({
									player: 'html',
									content: someData,
									title: 'Login',
									width: '500',
									height: '200'
									
						   });}, 'html');
		});
	
});


function showNewContent() {  
				$('#content').show('normal');
				$('#content').css('display','block');
			}
function showLogin() {
	$.post('/ajax/index.php', 'action=show_login',function(data){$('#login').html(data);}, 'html');
}
function submitLogin() {
    $.post('/ajax/index.php', $('#login_form').serialize(),function(data){$('#login').html(data);history.go(0);}, 'html');
}
function forgotPass() {
	$.post('/ajax/index.php', 'action=forgot_pass',function(data){$('#login').html(data);}, 'html');
}
function forgotUsername() {
	$.post('/ajax/index.php', 'action=forgot_username',function(data){$('#login').html(data);}, 'html');
}
function resetPass() {
	var theForm = document.getElementById('forgot_pass_form');
	apos=theForm.forgot_email.value.indexOf("@");
	dotpos=theForm.forgot_email.value.lastIndexOf(".");
	if (theForm.forgot_username.value == "")
	{
		alert('You left the username field blank. Please try again.');
		theForm.forgot_username.focus()
	}
	else if(apos<1 || (dotpos-apos)<2)
	{
		alert("Sorry, the Email you entered does not appear to be a valid Email address. Plese try again."); 
		theForm.forgot_email.focus();
	}
	else
		$.post('/ajax/index.php', $('#forgot_pass_form').serialize(),function(data){$('#login').html(data);}, 'html');
}

function emailUsername() {
	var theForm = document.getElementById('forgot_username_form');
	apos=theForm.forgot_email.value.indexOf("@");
	dotpos=theForm.forgot_email.value.lastIndexOf(".");
	if(apos<1 || (dotpos-apos)<2)
	{
		alert("Sorry, the Email you entered does not appear to be a valid Email address. Plese try again."); 
		theForm.forgot_email.focus();
	}
	else
		$.post('/ajax/index.php', $('#forgot_username_form').serialize(),function(data){$('#login').html(data);}, 'html');
}
function MM_jumpMenu(targ,selObj,restore){ //v3.0
	  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0;
	}

function checkECInfo(theForm)
{
	if(theForm.ec_phone.value.length != 10)
	{
		alert("Your phone number appears to be incomplete or in the wrong format. It should be a ten-digit number with no dashes. Please try again.");
		theForm.ec_phone.focus();
		return false;
	}
	else if(isNaN(theForm.ec_phone.value))
	{
		alert("Your phone number appears to be incomplete or in the wrong format. It should be a ten-digit number with no dashes. Please try again.");
		theForm.ec_phone.focus();
		return false;
	}
	else if(theForm.uin.value.length != 9)
	{
		alert("Your UIN appears to be incomplete or in the wrong format. It should be a nine digit number with no dashes. Please try again.");
		theForm.uin.focus();
		return false;
	}
	else if(isNaN(theForm.uin.value))
	{
		alert("Your UIN appears to be incomplete or in the wrong format. It should be a nine digit number with no dashes. Please try again.");
		theForm.uin.focus();
		return false;
	}
	
	return true;
}

function confirmDeny() {
							deny = confirm("Are you sure you want to deny this user and delete their information?");
							return deny;
						}
						
function checkRegistration(theForm)
{
	apos=theForm.email.value.indexOf("@");
	dotpos=theForm.email.value.lastIndexOf(".");
	if (theForm.username.value == "")
	{
		alert('You left the username field blank. Please try again.');
		theForm.username.focus()
		return false;
	}
	else if (theForm.pass.value.length < 6)
	{
		alert('Your password must be at least 6 characters. Please try again.');
		theForm.pass.focus();
		return false;
	}
	else if(theForm.f_name.value == "")
	{
		alert("You left the First Name field blank. Please try again.");
		theForm.f_name.focus();
		return false;
	}
	else if(theForm.l_name.value == "")
	{
		alert("You left the Last Name field blank. Please try again.");
		theForm.l_name.focus();
		return false;
	}
	else if(apos<1 || (dotpos-apos)<2)
	{
		alert("Sorry, the Email you entered does not appear to be a valid Email address. Plese try again."); 
		theForm.email.focus();
		return false;
	}
	else if(theForm.phone.value.length != 10)
	{
		alert("Your phone number appears to be incomplete or in the wrong format. It should be a ten-digit number with no dashes. Please try again.");
		theForm.phone.focus();
		return false;
	}
	else if(isNaN(theForm.phone.value))
	{
		alert("Your phone number appears to be incomplete or in the wrong format. It should be a ten-digit number with no dashes. Please try again.");
		theForm.phone.focus();
		return false;
	}
	else if(theForm.pledge_class.value.length != 4)
	{
		alert("The pledge class you entered is not a valid year. Be sure you are using the normal year format. I.E. 2010 ");
		theForm.pledge_class.focus();
		return false;
	}
	else if(theForm.school_class.value.length != 4)
	{
		alert("The school class you entered is not a valid year. Be sure you are using the normal year format. I.E. 2010 ");
		theForm.school_class.focus();
		return false;
	}
	else if(theForm.major.value == "")
	{
		alert("It appears you have left the Major field blank. Please try again.");
		theForm.major.focus();
		return false;
	}
	return true;
}

jQuery(function($) {
				
				$('.gallery_unstyled').addClass('gallery'); // adds new class name to maintain degradability
				
				$('ul.gallery').galleria({
					history   : true, // activates the history object for bookmarking, back-button etc.
					clickNext : true, // helper for making the image clickable
					insert    : '#main_image', // the containing selector for our main image
					onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
						
						// fade in the image & caption
						if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
							image.css('display','none').fadeIn(1000);
						}
						caption.css('display','none').fadeIn(1000);
						
						// fetch the thumbnail container
						var _li = thumb.parents('li');
						
						// fade out inactive thumbnail
						_li.siblings().children('img.selected').fadeTo(500,0.3);
						
						// fade in active thumbnail
						thumb.fadeTo('fast',1).addClass('selected');
						
						// add a title for the clickable image
						image.attr('title','Next image >>');
					},
					onThumb : function(thumb) { // thumbnail effects goes here
						
						// fetch the thumbnail container
						var _li = thumb.parents('li');
						
						// if thumbnail is active, fade all the way.
						var _fadeTo = _li.is('.active') ? '1' : '0.3';
						
						// fade in the thumbnail when finnished loading
						thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
						
						// hover effects
						thumb.hover(
							function() { thumb.fadeTo('fast',1); },
							function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
						)
					}
				});
			});

		  
function slideShow(speed) {  
  
  
    //append a LI item to the UL list for displaying caption  
    $('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><h3></h3><p></p></div></li>');  
  
    //Set the opacity of all images to 0  
    $('ul.slideshow li').css({opacity: 0.0});  
      
    //Get the first image and display it (set it to full opacity)  
    $('ul.slideshow li:first').css({opacity: 1.0});  
      
    //Get the caption of the first image from REL attribute and display it  
    $('#slideshow-caption h3').html($('ul.slideshow a:first').find('img').attr('title'));  
    $('#slideshow-caption p').html($('ul.slideshow a:first').find('img').attr('alt'));  
          
    //Display the caption  
    $('#slideshow-caption').css({opacity: 0.7, bottom:0});  
      
    //Call the gallery function to run the slideshow      
    timer = setInterval('gallery()',speed);  
      
    //pause the slideshow on mouse over  
    $('ul.slideshow').hover(  
        function () {  
            clearInterval(timer);     
        },    
        function () {  
            timer = setInterval('gallery()',speed);           
        }  
    );  
      
}  
  
function gallery() {  
  
  
    //if no IMGs have the show class, grab the first image  
    var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));  
  
    //Get next image, if it reached the end of the slideshow, rotate it back to the first image  
    var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));  
          
    //Get next image caption  
    var title = next.find('img').attr('title');   
    var desc = next.find('img').attr('alt');      
          
    //Set the fade in effect for the next image, show class has higher z-index  
    next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);  
      
    //Hide the caption first, and then set and display the caption  
    $('#slideshow-caption').animate({bottom:-70}, 300, function () {  
            //Display the content  
            $('#slideshow-caption h3').html(title);  
            $('#slideshow-caption p').html(desc);                 
            $('#slideshow-caption').animate({bottom:0}, 500);     
    });       
  
    //Hide the current image  
    current.animate({opacity: 0.0}, 1000).removeClass('show');  
  
}  

function resetSlideshow() {
	clearInterval(timer);	
}

