/*
 *	@author: Zsotyooo
 *	@description:
 *
 */

(function($){
	$.fn.dropDown=function(options)
	{
		$(this).each(
			function(elementIndex)
			{
//				return;
				new $dropDown(elementIndex,this,options);
			}
		);
	}

	//konstruktor
	$.dropDown=function(elementIndex,element,options)
	{
		this.instanceID=$dropDown.instanceCnt;
		$dropDown.instances.push(this);
		$dropDown.instanceCnt++;

		this.element=element;

		if(options == undefined)options={};
		this.options={};
		$.extend(
			this.options,
			$dropDown.defaultoptions,
			options
		);

//		this.hoverOnly();
		var This=this;

		if($('ul',this.element).length>0)
		{
			$('> span > a',This.element).bind(
				'mouseover',
				function(){
					$(This.element).addClass(This.options.hoverClass);
				}
			)
			.bind(
				'mouseout',
				function(){
					$(This.element).removeClass(This.options.hoverClass);
				}
			);

			this.disableHide=false;
			this.hideTimer=null;
			this.mode='drop';

			this.createElements();

			this.addEvents();
		}
		else
		{
//			this.hoverOnly();
			this.mode='simple';
		}
	}

	//shortcutok
	$dropDown=$.dropDown;
	$dropDown.fn=$dropDown.prototype={};
	//egyszerűség kedvéért a jQuery extend fgv-ét használjuk a statikus és példány szintű elemeknél is
	$dropDown.fn.extend = $dropDown.extend = $.extend;

	//statikus változók, függvények:
	$dropDown.extend(
	{
		defaultoptions: {
			timeoutTime: 1000,
			animationDuration: 'fast',
			dropClass: 'BreadCrumbItem_active',
			hoverClass: 'BreadCrumbItem_hover',
			hideAll: true,
			mode: 'over'
		},
		instances:[],
		instanceCnt: 0,
		/*
		 * Itt adhatóak meg a konstansok
		 */
		constants: {

		}
	});

	//Instance függvényei
	$dropDown.fn.extend({
		hoverOnly: function()
		{
			
		},
		createElements: function()
		{
			var This=this;
			This.dataContainer=$('ul',This.element)
		},
		/*
		 * Ez rakja rá az eseményeket
		 */
		addEvents: function()
		{
			var This=this;

			var activatemethod=This.options.mode=='over'?'mouseover':'click';
			$(this.element).bind(
				activatemethod, function(){
					This.showData();
				}
			)
			.bind('mouseout', function(){
					This.disableHide=false;
					This.hideTimer=setTimeout(
						function()
						{
							This.hideData();
						},
						This.options.timeoutTime
					);
				}
			);

			This.dataContainer.bind(
				{
					'mouseover': function(){
						This.disableHide=true;
						This.dataContainer.stop(true, true).show();
						clearTimeout(This.hideTimer);
					},
					'mouseout': function(){
						This.disableHide=false;
						This.hideTimer=setTimeout(
							function()
							{
								This.hideData();
							},
							This.options.timeoutTime
						);
					}
				}
			);
		},
		showData: function()
		{
			var This=this;
			if(This.options.hideAll)
			{
				$.each($dropDown.instances,function(i)
				{
					if(This.instanceID!=i && $dropDown.instances[i].mode=='drop')
					{
						$dropDown.instances[i].disableHide=false;
						$dropDown.instances[i].hideData();
					}
				});
			}

			$(This.element).addClass(This.options.dropClass);
			if(!$.browser.msie)
			{
				This.dataContainer.slideDown(This.options.animationDuration,function(){
//					window.BannerHandler.hideAll();
				})
			}
			else
			{
				This.dataContainer.show();
//				window.BannerHandler.hideAll();
			}
			

			if(this.hideTimer)clearTimeout(this.hideTimer);
		},
		hideData: function()
		{
			var This=this;
			if(!This.disableHide) {
				if(!$.browser.msie)	{
					This.dataContainer.stop(true, true).slideUp(This.options.animationDuration,function(){
						$(This.element).removeClass(This.options.dropClass);
//						window.BannerHandler.showAll();
					});
				} else {
					This.dataContainer.hide();
					$(This.element).removeClass(This.options.dropClass);
//					window.BannerHandler.showAll();
				}
			}
		}
	});
})(jQuery)


