/***************************************************************
*
*  Author: Peter Klein <peter@clioonline.dk>
*
***************************************************************/

(function( $ ){

	$.fn.loginbox = function( options ) {

		// Create some defaults, extending them with any options that were provided
		var options = $.extend( {
			'defaultSize'		: 4,
			'expandSize'		: 209,
			'expandSpeed'		: 500,
			'expandEasing'		: 'swing',
			'expandBackLink'	: true,
			'textColor'			: '#000000',
			'errorMatch'		: /fejl|failure|error/i
		}, options);

		var version = '1.2';

		return this.each(function() {

			// Main element
			var loginbox = this;

			// Button that triggers the expandable part
			var otherButton = $('.loginbox-other',this);

			// The normal loginform
			var normalForm = $('.loginbox-normalform',this);

			// The easylogon loginform
			var easyForm = $('.loginbox-easyform',this);

			// Default expandable state is false (unfolded)
			var expandedFlag = false;

			// Page to return to after successful login
			var referer = escape(document.location.pathname + document.location.search)

			// Function for unfolding sublogin
			var unfold = function(el) {
				$(otherButton).removeClass('act');
				$('.loginbox-expandable',loginbox).animate({
					height: options.defaultSize
				},options.expandSpeed,options.expandEasing, function() {
					expandedFlag = false;
					$(el).trigger('click');
				});
			}

			// Hide expandable sublogin
			$('.loginbox-expandable',this).css({
				height: options.defaultSize,
				display: 'block',
				overflow: 'hidden'
			});

			// Show login in expanded state if there's an error message
			if ($('.login-msg h3',loginbox).first().text().match(options.errorMatch)) {
				$('.loginbox-expandable',loginbox).css({
					height: options.expandSize
				});
				$('.loginbox-layer1',loginbox).css({
					display: 'none'
				});
				$('.loginbox-layer2',loginbox).css({
					display: 'block'
				});
				$(otherButton).addClass('act');
				expandedFlag = !expandedFlag;
			}

			// Hide the real password field
			$('input.pass',loginbox).css('display','none');

			// Add onfocus event on the normal login input fields
			$('.loginfield input',normalForm).focus(function() {
				$(this).unbind('focus').val('').css('color',options.textColor);
				if ($(this).hasClass('passproxy')) {
					// Remove the fake password field
					$('input.passproxy',normalForm).detach();
					// Show the real password field
					$('input.pass',normalForm).css('display','inline').focus();
				}
			}).keyup(function(event) {
				// Make form submittable by pressing return
		  		if (event.which==13) {
		  			$('.button-login',normalForm).trigger('click');
		  		}
			});

			// Button for folding/unfolding sublogin
			$(otherButton).bind('click',function(event) {
				event.preventDefault();

				$(this).toggleClass('act');

				// Fold/unfold sublogin
				var expandEnd = (expandedFlag) ? options.defaultSize : options.expandSize;
				$('.loginbox-expandable',loginbox).animate({
					height: expandEnd
				},options.expandSpeed,options.expandEasing, function() {
					expandedFlag = !expandedFlag;

					// Revert to the buttons layer if sublogin is folded/unfolded
					if (options.expandBackLink) {
						$('.loginbox-layer1',loginbox).css({
							display: 'block'
						});
						$('.loginbox-layer2',loginbox).css({
							display: 'none'
						});
					}

				})
			});

			// Button for UniC login
			$('.loginbox-unic',this).bind('click',function(event) {
				event.preventDefault();

				// If sublogin is unfolded, then fold it.
				if (expandedFlag) {
					unfold(this);
				}
				else {
					$('input[name="tx_clioeasyaccess_pi1[returnurl]"]',easyForm).val(referer);
					//alert('UniC');
					$('form',easyForm).submit();
				}
			});

			// Button for WAYF login
			$('.loginbox-wayf',this).bind('click',function(event) {
				event.preventDefault();
				// If sublogin is unfolded, then fold it.
				if (expandedFlag) {
					unfold(this);
				}
				else {
					$('input[name="mode"]',easyForm).val('wayflogin');
					$('input[name="tx_clioeasyaccess_pi1[returnurl]"]',easyForm).val(referer);
					//alert('wayf');
					$('form',easyForm).submit();
				}
			});

			// Button for Skoda login
			$('.loginbox-skoda',this).bind('click',function(event) {
				event.preventDefault();
				// If sublogin is unfolded, then fold it.
				if (expandedFlag) {
					unfold(this);
				}
				else {
					$('input[name="tx_clioeasyaccess_pi1[returnurl]"]',easyForm).val(referer);
					//alert('skoda');
					$('form',easyForm).submit();
				}
			});

			// Button for normal login form
			$('.loginbox-norm',this).bind('click',function(event) {
				event.preventDefault();

				$('.loginbox-layer1',loginbox).css({
					display: 'none'
				});
				$('.loginbox-layer2',loginbox).css({
					display: 'block'
				});
			});

			// Button for backlink on normal login form
			$('.button-backlink',normalForm).bind('click',function(event) {
				event.preventDefault();

				$('.loginbox-layer2',loginbox).css({
					display: 'none'
				});
				$('.loginbox-layer1',loginbox).css({
					display: 'block'
				});
			});

			// Button for submitting normal login form
			$('.button-login',normalForm).bind('click',function(event) {
				event.preventDefault();
				//alert('Normal');
				$('form',normalForm).submit();
			});
		});

	};
})( jQuery );

