/**
 * @author Tyler Gaw <tylerg@arc90.com>
 *         Alex Gutierrez <alexg@arc90.com>
 *
 * Javascript document for the user Account page
 * here a user can update their information. Majors, contact info, etc
 *
 */

GradeGuru.account = {
	
	urls: {
		
		// Prod URLs
		/*
		signUp: '/sps/landingPageLoginnlu.do?method=registerRedirect',
		*/
		
		// Dev URLs
		contact: '../partials/contact.html'
		
	},

	init: function ()
	{
		// Add a major
		$('.add-major').click(this.addMajorModule);

		// Change Password Show/Hide
		$('.change-pw h3 span').toggle(function() {
			$(this).closest('.change-pw').addClass('change-pw-opened').find('div').fadeIn();
		}, function() {
			$(this).closest('.change-pw').removeClass('change-pw-opened').find('div').hide();
		});

		// Validation/Confirmation
	    $("#account #content form").submit(this.validate);

		this.bindEvents();
		
		// Uploadify Call
		if ($('#account').length) {
			$('.type-file-action').uploadify({
				'uploader'	: '/account/uploadify.swf',
				'auto'		: true, 
				'multi'		: false, 
				'cancelImg'	: '/images/cancel.png', 
				'buttonImg'	: '/images/sheet.png',
				'width'		: 135,
	            'height'	: 155
			});
		}

		// Delete Account
		$('#btn-delete-account').click(this.deleteAccount);
		
		// FAQ Sub page onLoad defaults
		$('.questions dt:first').addClass('opened').next().addClass('selected');
		$('.faq-answer').hide();
		$('.faq-answer:first').fadeIn(900);

		// Action when clicking questions
		$('.questions a').click(function() {
			$('.questions dd').removeClass('selected');
			$(this).parent().addClass('selected');
			$('.faq-answer').hide();
			
			var selectedLink = $(this).attr("href");
			$(selectedLink).fadeIn(300);
			
			return false;
		});


		// Loading of Universities
		$('#university-search-input').attr('autocomplete', 'off');

		if ($('#universities').length) {
			$.get("../api/universities/universities.json", function(data) {
				$.each(data.universities, function(i,item) {
					$('.state-universities').append('<li><a href="'+ item.url +'">'+ item.name +'</a></li>');
		
					if ( i === 30 ) {
						return false;
					}
				});
			});
		}


		// Contact modal
		$('.nav-contact').click(this.callContact);
	}, 

	callContact: function ()
	{
		$.fn.colorbox({href: GradeGuru.account.urls.contact});
	},

	deleteAccount: function ()
	{
		return GradeGuru.core.utils.confirm('Are you sure you want to delete your account? \n\nThis cannot be undone.');
	}, 
	
	addMajorModule: function ()
	{
		var majors = $(".user-major").length;

		if (majors < 3) {
			$(this).parent().append('<div class="user-major"><input type="text" class="inputbox" value="Add a new major" name="major"><a href="#" class="remove-major">Remove this</a></input>');
		}

		return false;
	}, 

	validate: function ()
	{
		// onLoad declared value
		validated = true;
		
		// Remove error class on input fields
		$(":input").removeClass('error');
		$('.form-status').remove();

		// Check for empty input fields
		$(this).find(':text, textarea, .change-pw-opened :password').each(function() {
			var getVal = $(this).val();

			if(((getVal) === '') || (getVal === 'Add a new major')) {
				$(this).addClass('error');

				validated = false;
				
			}
		});

		// Failed validation output
		if (!validated) {
			$('#account #content-main form').prepend('<p class="form-status error">Please fix the errors below and try again</p>');
			
			return false;
		}

		// Passed validation output
		if(validated) {
			GradeGuru.core.utils.alertBanner('Your updates are saved', 'confirmation');
		}

		return false;
	}, 
	
	bindEvents: function ()
	{
		// Remove a Major
		$('.remove-major').live('click', function() {
			$(this).parent().remove();
			return false;
		});

		$(document).bind('cbox_complete', 
			function () 
			{
				setTimeout(
					function ()
					{
						$("#contact-modal input[type='text']:first").focus();
					}, 
				300);
			}
		);
	}
};

$(document).ready(function ()
{
	GradeGuru.account.init();
});