// Global scope

	// Minimize-Maximum content
	function toggleContent(element) {

		// Display/hide content

			// Grab tag of content to hide/display
			var content = element.attr('rel');
			content = $('#body .' + content);
			
			// If hidden, display it
			if (content.hasClass('hidden')) {
				content.removeClass('hidden');
				element.removeClass('max');
				element.attr('title', 'Minimize');
				element.find('span').text('Minimize');
			}
			else {
				content.addClass('hidden');
				element.addClass('max');
				element.attr('title', 'Maximize');
				element.find('span').text('Maximize');
			}
	}

	// Dialog positioning
	function dialogSetPosition() {

		// Determine width/height values
		var page_width = $(window).width();
		var page_height = $(window).height();
		var dialog_width = $('#dialog').width();
		var dialog_height = $('#dialog').height();
		
		// Compute width from left screen
		var left = (page_width - dialog_width) / 2;
		var top = (page_height - dialog_height) / 2;
		
		// Add positioning to dialog
		$('#dialog').css({'left' : left, 'top' : top}).removeClass('hidden');
		
		// Set event to close dialog
		$('#dialog a.close').click(function(e) {

			e.preventDefault(); // don't follow the link
			
			// Delete the dialog
			$('#dialog').remove();
		});
	}

	// Action Popup event handler
	function actionPopup(a) {

		// Grab text description
			// If picture
			if (a.hasClass('pic')) {

				var text = a.find('img').attr('alt');
			}
			else {
				var text = a.find('span').text();
			}

		// If a popup already exists, remove it
		$('#popup').remove();

		// Create popup (hidden)
		$('#footer').after('<div id="popup" class="action"><div class="main"><p>' + text + '</p></div><div class="arrow"></div></div>');

		// Calculate position
		var offset = a.offset();
		var width = a.outerWidth();
		var popup_width = $('#popup').width();
		var left = offset.left - popup_width / 2 + width / 2;
		var top = offset.top - $('#popup').height() - 10;
		var arrow = popup_width / 2 - 7;

		// Update position and set to visible
		$('#popup').css({
			'left' : left,
			'top' : top,
			'visibility' : 'visible'
		});

		// Update arrow position
		$('#popup .arrow').css({
			'left' : arrow + 'px'
		});
	}

$(function(){

// HEADER ACCOUNT MENU
	$('#header ul li.account a').click(function(e) {

		// Fetch parent li
		var parent = $(this).parents('li:first');

		// If menu is open
		if (parent.hasClass('menu')) {

			parent.removeClass('menu');
			$('#account-menu').remove();
		}
		else {

			// Create menu
			$('#footer').after('<ul id="account-menu"><li><a href="#">Edit Profile</a></li><li><a href="#">Privacy</a></li><li><a href="/login.php?action=logout">Log Out</a></li></ul>');

			// Calculate position
			var offset = $(this).offset();
			var left = offset.left - 1;

			// Update position and set to visible
			$('#account-menu').css({
				'left' : left,
				'visibility' : 'visible'
			});
			parent.addClass('menu');

			// Event handlers
			
				e.stopPropagation();

				$(document.body).click(function() {

					parent.removeClass('menu');
					$('#account-menu').remove();
				});

				$('#account-menu').click(function(e) {

					e.stopPropagation();
				});


		}
	});

// MINIMIZE/MAXIMIZE SECTIONS
	$('#body a.min-max').click(function(e) {

		e.preventDefault(); // don't follow the link

		// Call toggleContent function
		toggleContent($(this));
	});

// EMULATE :focus behavior for IE7
	$('input[type=text]:not(:disabled), input[type=password]:not(:disabled), textarea:not(:disabled), select:not(:disabled)').focus(function(){
	
		$(this).addClass('focus');

	}).blur(function(){
	
		$(this).removeClass('focus');
		
	})

// LOADER

	var loaderTimer = 0;

	// LINK LOADER AND POPUP
	var loader_link = {    
		over: loader_link_on, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out: loader_link_off // function = onMouseOut callback (REQUIRED)    
	};

	$('a[rel=popup]:not(.pic)').hoverIntent(loader_link);

		function loader_link_on() {

			// If loader already exists, remove it
			$('#loader').remove();

			// Calculate position
			var offset = $(this).offset();
			var left = offset.left + $(this).width() - 1;
			var top = offset.top - 3;

			// If loader already exists, delete it
			$('#loader').remove();

			// Create loader
			$('#footer').after('<div id="loader" class="link" style="left: ' + left + 'px; top: ' + top + 'px;"></div>');

			// Animate loader
			var i = 1;

			loaderTimer = setInterval(function () {

				if (i < 10) {
				
					$('#loader').css('backgroundPosition', '0 -' + i*16 + 'px');
					i++;
				}
				else {

					clearInterval(loaderTimer);
				}

			}, 100);
		}

		function loader_link_off() {

			// Stop animation
			clearInterval(loaderTimer);

			// Delete element
			$('#loader').remove();
			
		}

	// LINK LOADER AND POPUP
	var loader_pic = {    
		over: loader_pic_on, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out: loader_pic_off // function = onMouseOut callback (REQUIRED)    
	};

	$('a.pic[rel=popup]').hoverIntent(loader_pic);

		function loader_pic_on() {

			// If loader already exists, remove it
			$('#loader').remove();

			// Calculate position
			var offset = $(this).offset();
			var left = offset.left + $(this).width() + 2;
			var top = offset.top - 8;

			// If loader already exists, delete it
			$('#loader').remove();

			// Create loader
			$('#footer').after('<div id="loader" class="pic" style="left: ' + left + 'px; top: ' + top + 'px;"></div>');

			// Animate loader
			var i = 1;

			loaderTimer = setInterval(function () {

				if (i < 11) {
				
					$('#loader').css('backgroundPosition', '0 -' + i*16 + 'px');
					i++;
				}
				else {

					clearInterval(loaderTimer);
				}

			}, 100);
		}

		function loader_pic_off() {

			// Stop animation
			clearInterval(loaderTimer);

			// Delete element
			$('#loader').remove();
			
		}

// POPUPS

	// ACTION POPUP
	$('a.action, a.close, a.move, ul.pics li a').mouseover(function(e) {

		actionPopup($(this));

	});
	$('a.action, a.close, a.move, ul.pics li a').mouseout(function(e) {
 
		$('#popup').remove();
	});

	// HELPER
	$('form label a, a.helper, .user .top ul.showcase a').hoverIntent(function(e) {

		// Fetch contents
		var contents = $(this).find('span').html();

		// If a popup already exists, remove it
		$('#popup').remove();

		// Create popup (hidden)
		$('#footer').after('<div id="popup" class="helper"><div class="main"><p>' + contents + '</p></div><div class="arrow"></div></div>');

		// Calculate position
		
			var offset = $(this).offset();
			var height = $('#popup').outerHeight(); // popup height
			var link_height = $(this).outerHeight(); // link height

			// Left/Right
				// Left
				if (offset.left < ($(document).width() / 2)) {
					var left = offset.left + $(this).outerWidth() / 2 - 38;
				}
				// Right
				else {
					var left = offset.left + $(this).outerWidth() / 2 - $('#popup').outerWidth() + 38;
					$('#popup .arrow').addClass('right');
				}

			// Top/Bottom
				// Top
				if ((offset.top - $(window).scrollTop()) > (height + 16)) {
					var top = offset.top - height - 16;
				}
				// Bottom
				else {
					var top = offset.top + link_height + 16;
					$('#popup .arrow').addClass('top');
				}

		// Update position and set to visible
		$('#popup').css({
			'left' : left,
			'top' : top,
			'visibility' : 'visible'
		});

	}, function() {
 
		$('#popup').remove();
	});

// FORMS

	// GAIN FOCUS ON DIV CLICK
	$('form div').click(function(e) {

		var sender = e.target.nodeName;
		if (sender == 'DIV') {
			$(this).find('input, select, textarea').focus();
		}
	});

	// CLEAR TEXTINPUT WHEN clearOnFocus class exists
	$('input.clearOnFocus').focus(function(e) {

		// If clearOnFocus class still exists
		if ($(this).hasClass('clearOnFocus')) {
			$(this).removeClass('clearOnFocus');
			$(this).val('');
		}
	});
});
