/**
 *	Dan's jCordion© jquery plugin
 *	v1.0 - 23 March 2009
 *	By Daniel Steinhauser 8-)
 *
 *	Note: Requires hoverIntent plugin: '/js/jquery.hoverIntent.js'
 **/

(function($){
	$.fn.jcordion = function(options) {
		
		/*
		 *	Note: Restrict elements to a specific container by including a parent class/id. I.e: $("#your_container_id").jcordion() or $(".your_container_class").jcordion()
		 *  	  This is also how multiple jcordion sets can be defined.
		 */
		var defaults = {
			classContainer: "",			// Root container of this jcordion. Will map to 'body' if not defined. RECOMMENDED.
			speed: "fast",				// Options are "fast", "slow", "normal"; or a value between 0 (default speed), 1 (very fast), and infinite (anything over 2000 is very slow though!).
			hideOnLoad: true,			// hide all elements on page load: "true", "false"
			oneOpen: false,				// Set to true to force one open element at a time
			oneAlwaysOpen: false,		// Set to true to force one open element at all times
			firstOpen: false,			// Set to true to force the first element open
			useHover: false,			// Set to true to open elements on hover instead of click
			siblingElement: true		// * Set to false if .element more than one div/block away from .toggler
		};

		var options = $.extend(defaults, options);
		

		return this.each(function() {
			obj = $(this);
			var root = $('body');

			if (options.classContainer != '') { root = $('body').find(options.classContainer); }
			if (options.hideOnLoad == true){ obj.find('.element').hide(); }
			if (options.firstOpen == true){ obj.find('.element:first').slideDown(1); }

			if (options.useHover == true){	// Use hover

				obj.find('.toggler').hoverIntent(function() {	// Note user of hoverIntent

					if (options.siblingElement != true) {
						nextElement = $(this).parents().next('.element');
					} else {
						nextElement = $(this).siblings('.element');
					}
					
					if (options.oneAlwaysOpen == true){ $(this).parent().find('.element').slideUp(options.speed); }

					if (options.oneOpen == false){
						nextElement.slideToggle(options.speed);
					} else if (options.oneOpen == true){
						root.find('.element').slideUp(options.speed);	// Hide any open
						var visible = nextElement.css('display');
						if (visible == 'none') {
							nextElement.slideDown(options.speed);
						} else {
							nextElement.slideUp(options.speed);
						}
					}
					
					return false;
				},
				function() {
					
					if (options.siblingElement != true) {
						nextElement = $(this).parents().next('.element');
					} else {
						nextElement = $(this).siblings('.element');
					}
					
					if (options.oneAlwaysOpen != true){
						obj.find('.element').hide(options.speed);
					} else if (options.oneAlwaysOpen == true){
						//root.find('.element').hide(options.speed);
						//nextElement.slideDown(options.speed);
					}
					
				});

			} else {	// Use click

				obj.find('.toggler').click(function() {

					if (options.siblingElement != true) {
						nextElement = $(this).parents().next('.element');
					} else {
						nextElement = $(this).siblings('.element');
					}


					if (options.oneOpen == false){
						nextElement.slideToggle(options.speed);
					} else if (options.oneOpen == true){
						root.find('.element').slideUp(options.speed);	// Hide any open
						var visible = nextElement.css('display');
						if (visible == 'none') {
							nextElement.slideDown(options.speed);
						} else {
							nextElement.slideUp(options.speed);
						}
					}
					return false;
				});

			}
		});
	};
})(jQuery);
