/**
 * General JavaScript Functions
 *
 * @author Gabriel Lemonde-Labrecque
 * @copyright 2010, SchoolSuccess inc.
 */
$(document).ready(function() {
	TC.data.load();
	
	// find the div.fade elements and hook the hover event
	$('div.fade').hover(function() {
		// on hovering over, find the element we want to fade *up*
		var fade = $('> div', this);
		
		// if the element is currently being animated (to a fadeOut)...
		if (fade.is(':animated')) {
			// ...take it's current opacity back up to 1
			fade.stop().fadeTo(250, 1);
		}
		else {
			// fade in quickly
			fade.fadeIn(250);
		}
	}, function () {
		// on hovering out, fade the element out
		var fade = $('> div', this);
		if (fade.is(':animated')) {
			fade.stop().fadeTo(9000, 0);
		} else {
			// fade away slowly
			fade.fadeOut(900);
		}
	});
	
	// Fixes for IE7 and IE8
	if ($.browser.msie && $.browser.version < 9) {
		$('.no-submenu .containerSearch').css({ border: '1px solid lightGray' });
		
		// Rounded Corners
		if ($.fn.corner) {
			$('#header ul.tabs li').corner('tl,tr 10px'); // Top tabs
			$('.authentication .become-tutor').corner('10px'); // Become a tutor button
			
			$('.roundedHalfTop').corner('tl,tr 5px');
			$('.roundedDefault').corner('5px'); //individual configuration
			$('.alert').corner('10px');
			$('.alert.promo').corner('5px');
			$('.content').corner('br,bl,tr keep 5px');
			$('#navi li a:not(.selected)').corner('10px');
			$('#wrapper .menu li a').corner('10px');
			$('.circle').corner('25px');
		}
	}
	
	// Initialize tipsy tool tips
	if ($.fn.tipsy) {
		$('a.tooltip.left').tipsy({gravity: 'w'});
		$('a.tooltip.right').tipsy({gravity: 'e'});
		$('a.tooltip.top').tipsy({gravity: 'n'});
		$('a.tooltip.bottom').tipsy({gravity: 's'});
	}
	
	// Prevent window scrolling top when click on tool tip
	$('a.tooltip').click(function(event) { event.preventDefault(); });
	
	// Assign action to close button of alerts
	$('.alert').each(function() {
		var alertObject = $(this);
		var buttonClose = $(this).find('.buttonClose');
		
		buttonClose.click(function() {
			var scope = alertObject.find('input[name=scope]').val();
			
			switch (scope)
			{
				case 'once':
					alertCloseInClient(alertObject);
					break;
				case 'conditional':
				case 'permanent':
				case 'session':
					alertCloseOnServer(alertObject);
					break;
				default:
			}
		});
	});
	
	/**
	 * Close an alert in client page with no notification to the server
	 */
	function alertCloseInClient($alert) {
		$alert.remove();
	}
	
	/**
	 * Close an alert in client page and notify server that it's been closed
	 */
	function alertCloseOnServer($alert) {
		$form = $alert.find('form');
		
		TC.ajax.getJson('/alert/close/', $form.serialize(),
			function(response) {
				if (response.code == 'ok') {
					$alert.remove();
				}
			}
		);
	}
	
	// Pressing enter in a form object submits the parent form
	$('form.submitOnEnter').each(function() {
		var $form = $(this);
		
		$form.find('input, select').each(function() {
			var $input = $(this);
			
			$input.enter(function() {
				if ( ! $input.is('.ac_input') || ! $('.ac_results').is(':visible'))
					$form.submit();
			});
		})
	});
	
	function fixImageRatio($images) {
		$images.each(function() {
			var $this = $(this);
			
			$this.data('width', $this.width());
			$this.css('width', 'auto');
		});
		
		$images.each(function() {
			var $this = $(this);
			
			if ($this.width() > $this.height()) {
				$this.css('height', 'auto');
				$this.width($this.data('width'));
			}
		});
	}
	
	if ($.fn.center) {
		$('img.avatar.small').center({ width: 35, height: 35 });
		$('img.avatar.medium').center({ width: 85, height: 85 });
		$('img.avatar.big').center({ width: 128, height: 128 });
	}
	
	if ($.fn.uniform) {
		$("select:not(.notUniform), input:checkbox:not(.notUniform), input:radio:not(.star), input:file").uniform({ fileDefaultText: TC.lang.get('fileInputNoFileText'), fileBtnText: TC.lang.get('fileInputButtonText') });
	}
	
	var $footerContainer = $('#footer');
	
	// Show a "back to top" box at right corner
	var divBackToTop = $('#divBackToTop');
	var isVisible = true;
	
	function updateBackToTopLinkVisibility(animate) {
		if (typeof animate == 'undefined')
			animate = true;
		
		var windowScrollTop = $(window).scrollTop();
		
		if ($footerContainer.length == 0)
		{
			divBackToTop.stop().css({opacity: 0});
			return;
		}
		
		var shouldBeVisible = (windowScrollTop >= 200) ? true : false;
		
		if (shouldBeVisible && ! isVisible) {
			if (animate)
				divBackToTop.stop().animate({opacity: 1}, 300);
			else
				divBackToTop.stop().css({opacity: 1});
			
			isVisible = true;
		}
		else if ( ! shouldBeVisible && isVisible) {
			if (animate)
				divBackToTop.stop().animate({opacity: 0}, 300);
			else
				divBackToTop.stop().css({opacity: 0});
			
			isVisible = false;
		}
		
		if (isVisible) {
			$('#divBackToTop').css('bottom', '15px');
		}
	}
	
	$(window).scroll(function() {
		updateBackToTopLinkVisibility();
	});
	
	$('#linkBackToTop').click(function(event) {
		event.preventDefault();
		$(window).scrollTop(0);
	});
	
	// Add a vertical separator line if there is a submenu
	if ($('.containerSearch .submenu').length)
		$('#middle').addClass('containsSubmenu');
	
	// Unhide the page once the Global JavaScript is all initialized
	$('#wrapper').removeClass('hidden');
	$footerContainer.removeClass('hidden');
	
	updateBackToTopLinkVisibility(false);
	
	$('#divBackToTop').removeClass('hidden');
	
	$('.alert_request_search .close').click(function(){
		$('.alert_request_search').remove();
	});
	$('header .submenu li').click(function(event) {
		event.preventDefault();
		window.location = $(this).find('a').attr('href');
	});
});

