(function($){
	dropDownNav = function() {
		this.settings = {
			top: '0',
			height: '200px',
			backgroundColor: '#000000',
			opacity: 0.7
		};
	};
	dropDownNav.prototype = {
		overbg : false,
		overmenu : false,
		menuHideDelay: 400, // in ms
		init : function (holder,options) {
			var t = this;
			t.settings = $.extend(this.settings,options);
			//console.log('setting:',t.settings);
			//console.log(t.ddd);
			t.holder = holder;
			//console.log('holder', t.holder);
			t.holder.append('<div></div>');
			t.ddd = t.holder.children().last(); //drop down div
			//console.log('ddd',t.ddd);
			t.ddd.css('position', 'absolute');
			t.ddd.css('width', t.holder.width()+"px");
			t.ddd.css('top', t.settings.top);
			t.ddd.css('height', t.settings.height);
			t.ddd.css('background-color', t.settings.backgroundColor);
			t.ddd.css('opacity', t.settings.opacity);
			t.ddd.css('z-index',10000);
			t.ddd.hide();

			//displaying div - over the dropdown => opacity 1
			t.holder.append('<div></div>');
			t.dd = t.holder.children().last(); //display div
			t.dd.css('position', 'absolute');
			t.dd.css('width', t.holder.width()+"px");
			t.dd.css('top', t.settings.top);
			t.dd.css('height', t.settings.height);
			t.dd.css('visibility', 'hidden');
			t.dd.css('z-index',10001);
			t.ajaxCache();

			$('.dropdown>a').bind('mouseover',function() {
				//console.log('dropdown mouse over',this);
				t.linkover(this);
			});
			$('.dropdown>a').bind('mouseout',function() {
				//console.log('dropdown mouse out');
				t.linkout();
			});
			this.dd.bind('mouseover',function() {
				//console.log('background mouse out');
				t.bgover();
			});
			this.dd.bind('mouseout',function() {
				//console.log('background mouse out');
				t.bgout();
			});
		},
		ajaxCache: function() {
			//console.log('ajaxCache');
			var t = this;
			$('.dropdown>a').each(function(){
				var link = this;
				//console.log('attr:href',$(link).attr('href'));
				//console.log('t.dd',t.dd);
				//$.ajax($(link).attr('href'));
				$.ajax({
					url: $(link).attr('href'),
					context: t.dd,
					success: function(data) {
						//console.log('this success',this);
						//console.log('data',data);
						this.empty();
						this.css('visibility','visible');
						$.data(link,"ajaxCache",data);
						this.append(data);
						$.data(link,'ajaxHeight',this.height());
						this.css('visibility','hidden');
						//console.log('this?',link);
						//console.log('ajaxHeight',$.data(link,'ajaxHeight'));
						$(link).attr('href','#');
					}
				});/**/
			});
		},
		linkover: function(link) {
			this.overmenu = true;
			this.stopHide();
			this.show(link);
		},
		linkout: function() {
			this.overmenu = false;
			this.hide();
		},
		bgover: function() {
			this.overbg = true;
			this.stopHide();
		},
		bgout: function () {
			this.overbg = false;
			this.hide();
		},
		show: function(link) {
			var t = this;
			//console.log('link',link);
			t.dd.empty();
			t.ddd.css('height',$.data(link,'ajaxHeight'));
			//t.ddd.animate({height: $.data(link,'ajaxHeight')},'fast',)
			t.ddd.stop(true, true).slideDown('slow',function() {
				data = $.data(link,"ajaxCache");
				//console.log('there is data');
				t.dd.empty();
				if (t.overgb || t.overmenu) { //still over
					t.dd.append(data);
					t.dd.css('visibility','visible');
				}
			});
		},
		hide: function() {
			var t = this;
			t.toHide = setTimeout(function() {
				if (!t.overbg && !t.overmenu){
					t.dd.empty();
					t.dd.css('visibility','hidden');
					t.ddd.stop(true, true).slideUp('slow');
				}
			},t.menuHideDelay);
		},
		stopHide: function() {
			clearTimeout(this.toHide);
		}
	}
	
	$.fn.dropDownNav = function(options) {
		return this.each(function() {
			ddn = new dropDownNav();
			ddn.init($(this),options);
		});
	} // plugin
})(jQuery);



$(function () {
	$('#container').dropDownNav({
		top: '156px',
		backgroundColor: '#ff9c00',
		height: '320px',
		opacity: 0.85
	}); /**/
	//$('#container').dropDownNav();
});
