/*
 *
 * Copyright (c) 2009 Doubleclique (dev [at] doubleclique [dot] com)
 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license
 *
 */

/**
 *
 * jquery.dql.filtermenu
 *
 * @name     jquery.dql.filtermenu
 * @version  0.1
 * @author   Kyle Beattie (kyle [at] doubleclique [dot] com)
 * @requires jQuery
 *
 */
/*global jQuery */
"use strict";
(function ($) {

	$.fn.filtermenu = function (options) {

		var $this = $(this), priv = {}, publ = {};

		options = $.extend(
			{
				toggle : '.toggle',
				textElement : 'strong',
				menu : 'span',
				classForMenu : '',
				speed : 200
			},
			options
		);

		priv.init = function () {

			priv.menuOpen = false;
			
			priv.$toggles = $this.find(options.toggle);

			priv.$menus = priv.$toggles.find(options.menu);

			priv.$clone = $('<div></div>').attr({
				'id' : 'FilterMenu',
				'class' : options.classForMenu
			}).hide();

			$('body').append(priv.$clone);

			priv.contents = [];

			priv.$menus.each(function (i) {
				
				priv.addContentsToClone(i);
				
			});
			
			priv.addEventHandlers();

			var temp = $this.clone();

			temp.find(options.toggle + ' span').each(function () {
				$(this).remove();
			});

			temp.find('.resetLink').remove();

			var text = $.trim(temp.text())

			if ( text.length > 109) {

				$('body').addClass('filterExtended');

			} else {

				$this.find('.breaker').remove();

			}

		};

		priv.addEventHandlers = function () {

			priv.$toggles.click(function (event) {

				event.preventDefault();

				if (!$(event.target).is('a')) {

					var index = $(this).index(options.toggle);

					priv.openMenu(index);
					
				}

			});

			priv.$menus.find('a').click(function (event) {
				
				event.stopPropagation();
				
			});

			$(document).click(function () {

				if (priv.menuOpen) {

					priv.closeMenu(priv.currentIndex);

				}

			});
		};

		priv.addContentsToClone = function (index) {

			var $menu = priv.$menus.eq(index);

			var toggleWidth = priv.$toggles.eq(index).outerWidth();

			var menuWidth = $menu.outerWidth();

			var leftPos;

			if (menuWidth > toggleWidth) {

				leftPos = -(menuWidth-toggleWidth)/2;

			} else {

				leftPos = (toggleWidth-menuWidth)/2;

			}

			$menu.css({
				opacity : 0,
				display : 'block'
			});

			var info = {
				contents : $menu.contents(),
				width : menuWidth,
				left : $menu.offset().left+leftPos,
				top : $menu.offset().top
			};

			priv.contents[index] = info;

			$menu.hide();

		};

		priv.openMenu = function (index) {

			if (priv.menuOpen) {

				var openAgain;

				if (index != priv.currentIndex) {

					openAgain = true;

				} else {

					openAgain = false;
				}

				priv.closeMenu(index, openAgain);

			} else {
				
				priv.$clone.html(priv.contents[index].contents);

				priv.$clone.css({
					width : priv.contents[index].width,
					left : priv.contents[index].left,
					top : priv.contents[index].top
				}).slideDown(options.speed, function () {

					priv.currentIndex = index;

					priv.menuOpen = true;

				});

			}
			
		};

		priv.closeMenu = function (index, openAgain) {

			priv.$clone.slideUp(options.speed, function () {

				priv.$clone.hide();
				
				priv.currentIndex = index;

				if (openAgain) {

					priv.openMenu(index);

				}
				
				priv.menuOpen = false;

			});

		};

		priv.getFilteredContent = function (link) {

			// get filtered results/content with ajax

		};

		priv.init();

		return publ;

	}
	
}(jQuery));
