/**
 * Documentation dlam.js for all Info.nl programmed JavaScript for Dalte Lloyd Asset Management.
 * Date of change can be found in Subversion
 * 
 * @author Arjan Eising <arjan@info.nl>
 */


/**
 * Delta Lloyd Asset Management
 *
 * @namespace DLAM
 * @requires jQuery <http://jquery.com>
 */
var DLAM = {
	/** @config {string} cookieName Name of the cookie that contains the preferred language*/
	cookieName: 'dlamlang',
	

	/**
	 * Checks what page is called, and decides what subclasses it calls.
	 */
	init: function() {
		var bodyElm = $('body').addClass('js');
		
		if (bodyElm.hasClass('languagesplash'))
			DLAM.languagesplash.init();
		
		if (bodyElm.hasClass('fundspage'))
			DLAM.funds.init();
		
		if (bodyElm.hasClass('disclaimer'))
			DLAM.disclaimer.init();
	},
	
	
	/**
	 * Allows the language option to be clicked on the whole DIV, instead of only the link.
	 *
	 * @class languagesplash
	 */
	languagesplash: {
		/** @config {string} activeClass Class of an hovered/focussed box */
		activeClass: 'activebox',
		
		/** @config {string} boxClass Class a box */
		boxClass: 'language',
		
		
		/**
		 * Finds all language panes, and appends handlers on them.
		 * Also checks if a cookie is set.
		 */
		init: function() {
			// Read the cookie
			var ca = document.cookie.split(';');
			
			for (var i=0; i < ca.length; i++) {
				var c = ca[i];
				while (c.charAt(0)==' ')
					c = c.substring(1, c.length);
				if (c.indexOf(DLAM.cookieName) == 0) {
					var prefLang = c.substring(DLAM.cookieName.length + 1, c.length);
					break;
				}
			}
			
			// If a coockie is set with valid content, go to the URL
			if (prefLang) {
				//location.href = prefLang;
				//return;
			}
			
			var langOptions = $('#content-center .' + DLAM.languagesplash.boxClass);
			
			langOptions.mouseover(DLAM.languagesplash.over);
			langOptions.mouseout(DLAM.languagesplash.out);
			langOptions.click(DLAM.languagesplash.click);
			
			langOptions.find('.block-more a').focus(DLAM.languagesplash.over);
			langOptions.find('.block-more a').blur(DLAM.languagesplash.out);
		},
		
		
		/**
		 * Called when a user hovers / focusses a box
		 */
		over: function() {
			hovered = $(this);
			
			while (!hovered.hasClass(DLAM.languagesplash.boxClass))
				hovered = hovered.parent();
			
			hovered.addClass(DLAM.languagesplash.activeClass);
		},


		/**
		 * Called when a user unhovers / unfocusses a box
		 */
		out: function() {
			hovered = $(this);
			
			while (!hovered.hasClass(DLAM.languagesplash.boxClass))
				hovered = hovered.parent();
			
			hovered.removeClass(DLAM.languagesplash.activeClass);
		},
		
		
		/**
		 * Called when a user activates a box
		 */
		click: function() {
			var newLocation = $(this).find('.h1-wrapper a').eq(0).attr('href');
			
			if (newLocation.length) {
				var expireDate = new Date();
				expireDate.setTime(expireDate.getTime()+(365*24*60*60*1000));

				document.cookie = DLAM.cookieName + '=' + newLocation + '; expires=' + expireDate.toGMTString() + '; path=/';
				
				location.href = newLocation;
			}
		}
	},
	
	
	
	/**
	 * Can set a cookie when the user clicks the link on the disclaimer page.
	 */
	disclaimer: {
		/** @config {string} linkContainerClass The class of the container, that contains the accept link */
		linkContainerClass: 'accept',
		
		
		/**
		 * Set the click event to the acceptlink
		 */
		init: function() {
			var acceptLink = $('.' + DLAM.disclaimer.linkContainerClass + ' a').eq(0);
			
			
			if (acceptLink) {
				acceptLink.click(DLAM.disclaimer.accept)
			}
		},
		
		
		/**
		 * Saves or sets the new cookie.
		 */
		accept: function() {
			var newLocation = $(this).attr('href');
			
			if (newLocation.length) {
				var expireDate = new Date();
				expireDate.setTime(expireDate.getTime()+(365*24*60*60*1000));

				document.cookie = DLAM.cookieName + '=' + newLocation + '; expires=' + expireDate.toGMTString() + '; path=/';
			}
			
			return true;
		}
	},


	/**
	 * For the page 'funds'. Contains functions to open and close panes.
	 *
	 * @class funds
	 * @Requires jQuery easing plugin <http://gsgd.co.uk/sandbox/jquery/easing/>.
	 */
	funds: {
		/** @config {integer} config.closedPaneWidth Width in PX of a closed pane */
		closedPaneWidth: 158,
		
		/** @config {integer} config.openedPaneWidth Width in PX of a opened pane */
		openedPaneWidth: 530,
		
		/** @config {integer} config.animationSpeed Speed of the animation, in milliseconds */
		animationSpeed: 1000,
		
		/** @config {string} config.opencloseOpenedTitle Title of a opened pane, displayed when hovering the handle */
		opencloseOpenedTitle: 'Paneel sluiten',
		
		/** @config {string} config.opencloseClosedTitle Title of a closed pane, displayed when hovering the handle */
		opencloseClosedTitle: 'Paneel openen',
		
		/** @config {int} config.blenderOpacity Opacity of the active blender */
		blenderOpacity: 0.8,
		
		
		/** @property (null|jQueryElm) activeItem Reference to the active pane. */
		activeItem: null,
		
		/** @property @private (null|jQueryElm) blender Reference to DIV#blender. */
		blender: null,
		
		
		/**
		 * Finds all panes, and appends handlers on them.
		 * Also adds DIV#blender.
		 */
		init: function() {
			if (!$('#panes')) return;
			window.panes = $('#panes').addClass('js');
			
			var count = 0;           
			panes.find('.content-pane').not('.no-toggle').each(function() {
				var pane = $(this);
				
				pane.attr('id', 'pane-' + (count++));
				pane.append('<div class="wide" />');
				
				var wide = pane.find('.wide');
				wide.append(pane.find('.block-intro')).append(pane.find('.block-large'));
				wide.before('<a href="#" class="openclose" title="' + DLAM.funds.opencloseClosedTitle + '">' + DLAM.funds.opencloseClosedTitle + '</a>');
				wide.before(pane.find('h2'));
			})
			
			panes.find('.content-pane:not(.no-toggle)').click(DLAM.funds.togglePane);
			
			// Appending blender DIV to #content-center
			panes.before('<div id="blender" />');
			DLAM.funds.blender = $('#blender').hide(0).fadeTo(0, 0);
		},
		
		
		/**
		 * Calls when the user clicks on a handler.
		 * It checks what pane is clicked and decides what panes to open and/or close.
		 *
		 * @param {object.event} e Standard JavaScript event object.
		 * @returns {boolean} False if a pane is opened (to prefent default of clicking a hyperlink), or True if the clicked element is a hyperlink that is not the handler.
		 */
		togglePane: function(e) {
			if (panes.hasClass('busy')) return false;
			
			panes.addClass('busy');
			
			clickedBox = $(e.target);
			
			// if the clicked element is a link: follow it (but not if it is the plus-button in the top right corner)
			if (clickedBox.is('a') && !clickedBox.hasClass('openclose'))
				return true;
			
			while (!clickedBox.hasClass('content-pane'))
				clickedBox = clickedBox.parent();
			
			// If the clicked pane is the active pane, close the pane and deactivate #blender. Else open the clicked pane.
			if (DLAM.funds.activeItem && clickedBox.attr('id') == DLAM.funds.activeItem.attr('id')) {
				DLAM.funds.closePane(clickedBox);
				DLAM.funds.blender.fadeTo(DLAM.funds.animationSpeed, 0, function(){DLAM.funds.blender.hide(0)});
				DLAM.funds.activeItem = null;
			}
			else {
				// If there is a pane opened (and #blender is active), close the pane. If not: #blender is not active, so show it.
				if (DLAM.funds.activeItem)
					DLAM.funds.closePane(DLAM.funds.activeItem);
				else {
					DLAM.funds.blender.show(0).fadeTo(DLAM.funds.animationSpeed, DLAM.funds.blenderOpacity);
					$('body').addClass('opened-panes');
				}
					
				DLAM.funds.openPane(clickedBox);
				DLAM.funds.activeItem = clickedBox;
				
			}
			
			return false;
		},
		
		
		/**
		 * Called by DLAM.funds.toggleExpandable.
		 * Closes the pane given by the parameter.
		 *
		 * @param {jQueryElm} elm The element to be closed.
		 */
		closePane: function(elm) {
			elm.children().fadeOut(DLAM.funds.animationSpeed / 4);
			elm.animate({
				width: DLAM.funds.closedPaneWidth
			}, DLAM.funds.animationSpeed, 'easeInOutQuad', function() {
				elm.find('.block-large').hide();
				elm.find('.openclose').attr('title', DLAM.funds.opencloseClosedTitle).html(DLAM.funds.opencloseClosedTitle).removeClass('opened');
				elm.children().fadeIn(DLAM.funds.animationSpeed / 4);
				panes.removeClass('busy');
				
				// If the pane is closed and no other pane is opened, remove body class.
				if (!DLAM.funds.activeItem)
					$('body').removeClass('opened-panes');
			});
		},
		
		
		/**
		 * Called by DLAM.funds.toggleExpandable.
		 * Opens the pane given by the parameter.
		 *
		 * @param {jQueryElm} elm The element to be opened.
		 */
		openPane: function(elm) {
			elm.children().fadeOut(DLAM.funds.animationSpeed / 4);
			
			elm.animate({
				width: DLAM.funds.openedPaneWidth
			}, DLAM.funds.animationSpeed, 'easeInOutQuad', function() {
				elm.find('.block-large').show();
				elm.children().fadeIn(DLAM.funds.animationSpeed / 4);
				panes.removeClass('busy');
			});
			
			elm.find('.openclose').attr('title', DLAM.funds.opencloseOpenedTitle).html(DLAM.funds.opencloseOpenedTitle).addClass('opened');
		}
	}
}

// Everything that has a beginning has an end
if ($) $(DLAM.init);