﻿/// <reference path="jquery-1.5.1.min.js" />
/// <reference path="SnT.Utils.js" />

SnTUtils.RegisterNameSpace("ITU.HorizontalMenu");

ITU.HorizontalMenu = function (options) {

	var _me = this;
	this.Config = $.extend({}, {
		sensitivity: 2,     // number = sensitivity threshold (must be 1 or higher)    
		interval: 100,      // number = milliseconds for onMouseOver polling interval    
		timeout: 500,       // number = milliseconds delay before onMouseOut
		root: 'ul#topnav',
		ulLi: 'li',
		ulSub: 'ul#topnav li .sub',
		subPadding: 22,
		subPaddingAdd: 22,
		QuickSearch: {},
		Selected: null
	}, options);

	if (options && options.QuickSearch) {

		this.Config.QuickSearch = $.extend({}, {
			Selector: ".quickSearch",
			SearchPage: '/Search.htm',
			QueryString: 'q',
			DefaultTerm: '',
			ButtonText: 'Traži',
			ButtonType: 'a',
			InputType: 'input'
		}, options.QuickSearch);
	}
	var Init = function () {

		_me.Config = $.extend({}, _me.Config, {
			over: megaHoverOver,    // function = onMouseOver callback (REQUIRED)    
			out: megaHoverOut       // function = onMouseOut callback (REQUIRED)    
		});
		_me.Config.root = $(_me.Config.root);
		_me.Config.ulLi = _me.Config.root.find("li");

		$(_me.Config.ulSub).css({ 'opacity': '0' });
		_me.Config.ulLi.hoverIntent(_me.Config);

		$(_me.Config.QuickSearch.Selector).QuickSearch(_me.Config.QuickSearch);

		if (_me.Config.Selected) {

			var li = _me.Config.root.children("li");
			li.find("a").removeClass("current");
			li.filter("." + _me.Config.Selected).find("a").addClass('current');
		}
	}
	var megaHoverOver = function () {

		$(this).find(".sub").stop().fadeTo('fast', 1).show();

		//Calculate width of all ul's
		(function ($) {
			jQuery.fn.calcSubWidth = function () {
				rowWidth = 0;
				//Calculate row
				var uls = $(this).find("ul");
				uls.each(function () {

					var ul = $(this);
					var ulWidth = ul.data("ulWidth");
					if (!ulWidth) {

						ulWidth = 0;
						ul.find("li a:first-child").each(function () {

							var a = $(this);
							var html = a.html();
							if (uls.length > 1) {
								html = html.replace(/\s/g, $.browser.msie ? '|' : '');
							}
							var span = $('<span style="display:none;"/>').html(html).appendTo("body");
							var width = span.width() + _me.Config.subPaddingAdd;

							if ($.browser.msie) {
								width += 5;
							}
							span.remove();

							if (width > ulWidth) {
								ulWidth = width;
							}
						});
						ul.data("ulWidth", ulWidth);
					}
					rowWidth += ulWidth;
					ul.width(ulWidth);
				});
			};
		})(jQuery);

		if ($(this).find(".row").length > 0) { //If row exists...

			var biggestRow = 0;
			//Calculate each row
			$(this).find(".row").each(function () {
				$(this).calcSubWidth();
				//Find biggest row
				if (rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({ 'width': biggestRow });
			$(this).find(".row:last").css({ 'margin': '0' });

		} else { //If row does not exist...

			var me = $(this);
			var sub = me.find(".sub");
			rowWidth = me.data("rowWidth");

			if (!rowWidth) {
				me.calcSubWidth();
				//Set Width
				var meWidth = me.width() - _me.Config.subPadding;
				if (rowWidth < meWidth) {
					rowWidth = meWidth;
				}
				me.data("rowWidth", rowWidth);
			}
			sub.css({ 'width': rowWidth });
		}
	};

	var megaHoverOut = function () {

		$(this).find(".sub").stop().fadeTo('fast', 0, function () {
			$(this).hide();
		});
	};

	Init();
};

