/**
 * Gallery
 * --------
 * 
 * author: Andrei Dinca & Alexandra Ipate
 * 		                    _		   _   _____  _                 
 *		    /\             | |        (_) |  __ \(_)                
 *		   /  \   _ __   __| |_ __ ___ _  | |  | |_ _ __   ___ __ _ 
 *		  / /\ \ | '_ \ / _` | '__/ _ \ | | |  | | | '_ \ / __/ _` |
 *		 / ____ \| | | | (_| | | |  __/ | | |__| | | | | | (_| (_| |
 *		/_/    \_\_| |_|\__,_|_|  \___|_| |_____/|_|_| |_|\___\__,_|
 *		
 * email: andrei.webdeveloper@gmail.com
 *
 * version 1.0 release date: 2.12.2010
 *
**/
(function($){
	$.fn.extend({
		//pass the options variable to the function
		jDropDown: function(options) {
			//Set the default values
			var defaults = {
				'demo'			: false, // for demo only. Turn this to false
				'effect'		: '',
				'notMenu'		: '.home', // exclude menu (.home in examples)
				'duration'		: {
					'fadeIn'	: 300,
					'fadeOut'	: 60,
					
					'slideIn'	: 300,
					'slideOut'	: 60	
				}
			};
			
			// extends options object
			options =  $.extend(defaults, options);
			
			// self define parent class
			var self = this;
			
			// prevent collisions
			var running = false;

			return this.each(function() {
				var opts = options;
				
				// cache $(this) object
				var $this = $(this);
				// cache $(this).first("li") object (level 0 li from class)
				var $thisFirstLi = $this.find("> li").not(opts.notMenu);

				// observe hover
				$thisFirstLi.hover(function(){
					// cache $(this) object
					var $currThis = $(this);
					
					// use for reinit height
					var initHeight = 0;
					
					// find on class and remove. (deselect last selected elements)
					$this.find('.on').removeClass('on');
					
					// select current cursor pointer is
					$currThis.find('a').addClass('on');	
					
					// demo only
					if(opts.demo){
						opts.effect = $("#effect").val();
					}
						
					// call effect in open mode
					effect($currThis, "open");
					
				}, function(){
					// cache $(this) object
					var $currThis = $(this);
					
					// remove last selected elements
					$currThis.find('a').removeClass("on");
					
					// call effect in close mode
					effect($currThis, "close");
				});
				
				
				/**
				 *	effect functions
				 *	----------------
				 *
				 *	@params $currThis object
				 *	@params mode string (open / close)
				 *
				**/
				function effect($currThis, mode){
					// open
					if(mode == 'open'){
						if(opts.effect == "fade"){
							$currThis
								.find('div:first')
									.css('opacity', 0)
									.css('visibility', 'visible')
									.animate({
										opacity: 1,
										MarginTop: 3
									}, opts.duration.fadeIn);
									
						}else if(opts.effect == "slide"){
							initHeight = $currThis.find('div:first').height();
							$currThis
								.find('div:first')
									.css('height', 0)
									.css('visibility', 'visible')
									.stop(false, true)
									.animate({
										height: initHeight
									}, opts.duration.slideIn);
									
						}else{
							// default - visibility hidden
							$currThis.find('div:first').css('visibility', 'visible');
						}
						
						return true;
					}
					
					// close
					if(mode == 'close'){
						if(opts.effect == "fade"){
							$currThis
								.find('div:first')
									.animate({
										opacity: 0
									}, opts.duration.fadeOut, function(){
										// callback. Animation end
										$(this).css('visibility', 'hidden');
									});
									
						}else if(opts.effect == "slide"){
							initHeight = $currThis.find('div:first').height();
							$currThis
								.find('div:first')
									.css('overflow', 'hidden')
									.stop(false, true)
									.animate({
										height: 0
									}, opts.duration.slideOut, function(){
										// callback. Animation end
										$(this)
										.css('visibility', 'hidden')
										.css('height', initHeight);
									});
									
						}else{
							// default - visibility hidden
							$currThis.find('div:first').css('visibility', 'hidden');
							
							unblockScript();
						}
						
						return true;
					}
					
					alert("Uncatch exception. Invalid effect mode!");
					return false;
				}
				
				// unbloc script. Call on call back function
				function unblockScript(){
					running = false;
				}
			});
		}
	});
})(jQuery);
