﻿$(function () {
	// Top nav
	function showNav() {
		var _this = $(this),
			dropDown = _this.find("div.drop-down");

		// inject some extra elements for styling
		if (!_this.data("initialized")) {
			_this.children("a").append("<span class=\"right-corner\" />");
			_this.data("initialized", true);

			dropDown.append("<div class=\"drop-down-bottom\" />");
		}

		// place the dropdown right aligned with the nav item
		if (dropDown.find("a").length > 0) {
			_this.children("a").addClass("hover");

			dropDown.show();
		}
	}

	function hideNav() {
		$(this).children("a").removeClass("hover").end().find("div.drop-down").hide();
	}

	var config = {
		over: showNav, // function = onMouseOver callback (REQUIRED)    
		timeout: 150, // number = milliseconds delay before onMouseOut    
		out: hideNav // function = onMouseOut callback (REQUIRED)    
	};

	$("#top-nav > ul > li").hoverIntent(config);

	// Sub nav
	var nav = $("#sub-nav .box-body > ul, #top-nav ul");
	nav.children(".expanded").prev().addClass("prev-expanded");
	nav.children("li").hover(function () {
		var _this = $(this);
		if (!_this.hasClass("expanded")) {
			$(this).prev().toggleClass("prev-active");
		}
	});

	// Setup hover list - lists that have the dotted divider 
	$("ul.hover-list li").hover(function (e) {
		if (e.type === "mouseenter") {
			$(this).addClass("hover").prev().addClass("prev-hover");
		} else {
			$(this).removeClass("hover").prev().removeClass("prev-hover");
		}
	}).click(function () {
		window.location = $(this).children("a").attr("href");
	});

	// Ajax defaults
	jQuery.ajaxSetup({
		type: 'POST',
		contentType: 'application/json; charset=utf-8',
		dataType: 'json',
		data: "{}",
		dataFilter: function (data) {
			var msg;
			if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') {
				msg = JSON.parse(data);
			} else {
				msg = eval('(' + data + ')');
			}
			if (msg.hasOwnProperty('d')) {
				return msg.d;
			} else {
				return msg;
			}
		}
	});
});

// align sub-menu with main content
$(window).load(function () {
	var subNav = $("#sub-nav div.box-body"),
		sh = subNav.innerHeight(),
		mh = $("#main-content div.box-body").innerHeight();

	if (mh > 0 && mh > sh) {
		subNav.css("height", (mh + 2) + "px");
	}
});

// Proffice util methods
var Proffice = {};

Proffice.Utils = {
	stringConcat: function () {
		return Array.prototype.slice.call(arguments).join("");
	},
	findInArray: function (array, key, value) {
		for (var i = 0, l = array.length; i < l; i++) {
			var o = array[i];
			if (o.hasOwnProperty(key) && o[key] === value) {
				return o;
			}
		}

		return null;
	},
	getHashTagValues: function () {
		var values = {};
		if (window.location.hash !== "") {
			var hashTagValues = window.location.hash;
			if (hashTagValues.length > 0) {
				var kvArr = hashTagValues.split("&");
				for (var i = 0, l = kvArr.length; i < l; i++) {
					var kv = kvArr[i].split("=");
					values[kv[0]] = unescape(kv[1]);
				}
			}
		}

		return values;
	},
	setHashTagValues: function (values) {
		var hashTag = "";

		for (var i in values) {
			var v = values[i];
			if (v !== null && v !== "") {
				if (hashTag !== "") {
					hashTag += "&";
				}
				hashTag += i + "=" + escape(values[i]);
			}
		}

		window.location.hash = hashTag;
	},
	clearHashTag: function (e) {
		window.location.hash = "";
	},
	shortenString: function (text, length, prefix) {
		if (text.length > length) {
			var ssl = prefix ? length - prefix.length : length;
			text = text.substring(0, ssl);
			if (prefix) {
				text += prefix;
			}
		}
		return text;
	}
};

// Ajax loader - used to display loading messages when calling ajax functionallity
var AjaxLoader = function () {
	var _loader;

	var _init = function () {
		_loader = $("<div class=\"ajax-loader\"><div class=\"logo\"></div><span></span></div>");
		$("body").append(_loader);
	}

	return {
		show: function (msg) {
			if (!_loader) {
				_init();
			}

			if (msg) {
				_loader.find("span").html(msg);
			}

			_loader.show();
		},
		hide: function () {
			_loader.hide();
		}
	}
} ();

// Display lightbox when click login link
function loginLightBoxHookup() {
	$("div.login-lightbox").find("button.login-button").click(function () {
		window.location = $.colorbox.element().attr("href");
		e.preventDefault();
	});
}

// Display lightbox when click register link
function registerLightBoxHookup() {
	//	$("div.login-lightbox").find("button.login-button").click(function () {
	//		window.location = $.colorbox.element().attr("href");
	//		e.preventDefault();
	//	});
}

function openregisterwindow(_var) {
	if (_var == null)
		return;

	if (window.showModalDialog) {
		var returnValue = showModalDialog(_var, 'Register', 'status=no;help=no;dialogheight=750px;dialogwidth=1000px;resizable=no');

		if (returnValue != null && returnValue != "")
			location.href = returnValue;
	}
	else {
		windowname = "Register";
		settings = "left=20,top=20,toolbar=no,directories=no,location=no,status=no,menubar=no,resizable=no,scrollbars=yes,copyhistory=no,width=1000,height=750";
		newWin = window.open(_var, windowname, settings);
		newWin.focus();
	}
}


function openapplywindow(_var) {
	if (_var == null)
		return;

	if (window.showModalDialog) {
		var returnValue = showModalDialog(_var, 'Apply', 'status=no;help=no;dialogheight=820px;dialogwidth=610px;resizable=yes');

		if (returnValue != null && returnValue != "")
			location.href = returnValue;
	}
	else {
		windowname = "Register";
		settings = "left=20,top=20,toolbar=no,directories=no,location=no,status=no,menubar=no,resizable=yes,scrollbars=yes,copyhistory=no,height=820,width=610";
		newWin = window.open(_var, windowname, settings);
		newWin.focus();
	}
}
