/* Elliot Barer, ebarer [at] mac [dot] com, XX/YY/2011 */


$(document).ready(function() {

	$('.ext').click(function(){ 	//Open external links in new window
		window.open(this.href);
		return false; 
	});
	
	
/* LOGIN */
	$('#nav_login').click(function(){
		$('#login').slideDown();
		$('#nav_login').fadeOut();
	});
		
	$('#login input').focus(function(){
		$(this).siblings('label').addClass('focused');
	});
	
	$('#login input').blur(function(){
		if($(this).val() == ''){
			$(this).siblings('label').removeClass('focused');
		}
	});


/* NEWS */
	$('#edit_news input[name="subject"]').focus(function(){
		if($(this).val() == '' || $(this).val() == 'Subject...'){
			$(this).val('');
		}
	});
	
	$('#edit_news input[name="subject"]').blur(function(){
		if($(this).val() == '' || $(this).val() == ' '){
			$(this).val('Subject...');
		}
	});
	
	$('#edit_news textarea[name="content"]').focus(function(){
		if($(this).val() == '' || $(this).val() == 'Enter news here...'){
			$(this).val('');
		}
	});
	
	$('#edit_news textarea[name="content"]').blur(function(){
		if($(this).val() == '' || $(this).val() == ' '){
			$(this).val('Enter news here...');
		}
	});

	
/* QUOTES */
	$('#quotes #new_btn').click(function(){
		$('#new_quote').slideDown().animate({'top':'0'});
	});
	
	$('#quotes textarea[name="content"]').focus(function(){
		if($(this).val() == '' || $(this).val() == 'Enter quote here...'){
			$(this).val('');
		}
	});
	
	$('#quotes textarea[name="content"]').blur(function(){
		if($(this).val() == '' || $(this).val() == ' '){
			$(this).val('Enter quote here...');
		}
	});

	
/* MINUTES */	
	$('#edit_minutes input[type="text"]').blur(function(){
		if($(this).val() == '' || $(this).val() == ' '){
			$(this).val('');
		}
	});
	
	$('#edit_minutes textarea').focus(function(){ if($(this).val() == ''){ $(this).val(''); } });
	$('#edit_minutes textarea').blur(function(){ if($(this).val() == '' || $(this).val() == ' '){ $(this).val(''); } });


/* CHAPTER ROSTER */
	$('select[name="position"]').change(function(){
		var $data = $(this).closest("form").serialize();
		$.post('http://www.aepibx.com/modify_roster', $data, function(html){
			$('.message').fadeOut(function(){
				$(this).slideDown();
				$(this).html(html);
			}).fadeIn();
		});
	});
	
	$('select[name="role"]').change(function(){
		var $data = $(this).closest("form").serialize();
		var $container = '#role_' + $(this).val();
		var $brother = $(this).parents('li');
		var $added = false;
		
		$.post('http://www.aepibx.com/modify_roster', $data, function(html){
			$('.message').fadeOut(function(){
				$(this).slideDown();
				$(this).html(html);
			}).fadeIn();
		});
		
		$(this).parents('li').animate({'opacity':'0'},250).slideUp(250, function(){
			if($($container).children().length == 0){
				$($brother).appendTo($container).slideDown(500).animate({'opacity':'1'},250);
			} else {
				$('li', $container).each(function(){
					if($('.name', this).text() > $('.name', $brother).text()){
						$($brother).insertBefore($(this)).slideDown(500).animate({'opacity':'1'},250);
						$added = true;
						return false;
					}
					
					if(!$added){
						$($brother).appendTo($container).slideDown(500).animate({'opacity':'1'},250);
					}
				});
			};
		});
	});
	
	
/* RUSHEE LIST */	
	$('select[name="status"]').change(function(){
		var $data = $(this).closest("form").serialize();
		var $container = '#status_' + $(this).val();
		var $rushee = $(this).parents('li');
		var $added = false;
		
		$.post('http://www.aepibx.com/modify_rushee_list', $data, function(html){
			$('.message').fadeOut(function(){
				$(this).slideDown();
				$(this).html(html);
			}).fadeIn();
		});
		
		$(this).parents('li').animate({'opacity':'0'},250).slideUp(250, function(){
			if($($container).children().length == 0){
				$($rushee).appendTo($container).slideDown(500).animate({'opacity':'1'},250);
			} else {
				$('li', $container).each(function(){
					if($('.name', this).text() > $('.name', $rushee).text()){
						$($rushee).insertBefore($(this)).slideDown(500).animate({'opacity':'1'},250);
						$added = true;
						return false;
					}
					
					if(!$added){
						$($rushee).appendTo($container).slideDown(500).animate({'opacity':'1'},250);
					}
				});
			};
		});
	}); 



/* MISSING IMG */
	$(".hero img, .member_picture img").error(function(){
		if($(this).hasClass('pledge')){
			$(this).attr('src', '../resources/profile_pictures/pledge.jpg');		
		} else {
			$(this).attr('src', '../resources/profile_pictures/default.jpg');
		}
	});

});
