/// <reference path="FunctionsCommon.js" />
/// <reference path="jquery-1.3.2.min.js" />

// ********************* ********************* ** HANDLE MOSS MISBEHAVIOURS ************** ********************* //
function RemoveLinkToLists() { removeLinkToLists(document); }
function RemoveLinkToLists(node) {
	if (isMarkedNode(this) == true) { return; }
	markNode(this, true);
	// 
	$('*[href*="/Lists"]', node).each(function (i) {
		var style = $(this).attr('style');
		var classes = $(this).attr('class');
		var html = $(this).html();
		$(this).replaceWith($('<div' + (classes ? ' class="' + classes + '"' : '') + (style ? ' style="' + style + '"' : '') + '>' + html + '</div>'));
	});
	$('.ms-unselectedtitle', node).attr({ onmouseover: "", onfocus: "" });
	$('.ms-vb-title', node).each(function (i) {
		$(this).mouseover(function () {
			removeLinkToLists();
		});
	});
	$('.ms-menutoolbar', node).remove();
}
function markNode(node, markQ) { node.markedQ = markQ; }
function isMarkedNode(node) { return (node.markedQ == true); }
// ********************* ********************* ** GOOGLE ANALYTICS * ********************* ********************* //
function ConfigAnalyticsForExitLinks(onExitHandle) {
	// ADD onclick action: <a href="outbound link" onClick="recordOutboundLink(this, 'Outbound Links', 'example.com');return false;">

	$('a').each(function () {
		if (this.href && isLinkExternal(this.href) == true) {
			$(this).click(function (e) {
				onExitHandle(this.href);  // recordOutboundLink(this.href, 'Outbound Links', this.href);
				//return true;
			});
		}
	});

	$('.linksCombo option').each(function () {
		if (this.value && isLinkExternal(this.value) == true) {
			$(this).click(function (e) {
				onExitHandle(this.value);  // recordOutboundLink(this.value, 'Outbound Links', this.value);
				//return true;
			});
		}
	});
}
// ********************* ********************* ********************* ********************* ********************* //
function ConfigModalPopups() {
	$('body').append('<div id="maskJQModalPopup" style="display:none;WIDTH:100%;HEIGHT:100%;position:fixed;top:0;left:0;_position:absolute;background:#000;z-index:10;"></div>');
	$('head').append('<style type="text/css">.windowJQModalPopup{position:absolute;top:50%;left:50%;z-index:50;padding:0;margin:0;}</style>');

	//select all the a tags with the 'modalPopup' attribute
	$('*[modalPopup]').each(function (index) {
		var id = $(this).attr('modalPopup'); // gets the modalPopup selector ID (i.e. $(id) is the object)
		$(id).hide(); // force starting hidden

		$(this).click(function (e) {
			// cancel the link behavior
			e.preventDefault();

			var modalPopup = $(this).attr('modalPopup'); // gets the modalPopup selector ID (i.e. $(modalPopup) is the object)
			$(modalPopup).addClass('windowJQModalPopup');

			// get the window height and width
			var winH = $(window).height();
			var winW = $(window).width();

			// set the popup window to center
			$(modalPopup).css('top', winH / 2 - $(modalPopup).height() / 2);
			$(modalPopup).css('left', winW / 2 - $(modalPopup).width() / 2);

			// transition effect
			$("#maskJQModalPopup").css({ "opacity": "0.7" });
			$('#maskJQModalPopup').fadeIn(1000);

			$(modalPopup).each(function (modalWindowIndex) {
				// transition effect
				$(this).fadeIn(750);
			});
		});
	});

	//if close button is clicked
	$('.closeModalPopup, .closeJQModalPopup').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();

		var modalPopup = $(this).attr('modalPopup'); // gets the modalPopup selector ID (i.e. $(modalPopup) is the object)
		if (!modalPopup) { modalPopup = 'document'; } // if no modalPopup defined, apply to all
		// 
		$(modalPopup).each(function (modalWindowIndex) {
			$(this, '.windowJQModalPopup').hide();
		});
		$('#maskJQModalPopup').hide();
	});

	//if mask is clicked
	$('#maskJQModalPopup').click(function () {
		$(this).hide();
		$('.windowJQModalPopup').hide(); // apply to all
	});
}
// ********************* ********************* ********************* ********************* ********************* //
function ConfigModalWidth() {
	resizeModalWidthElements();
	$(window).resize(function () {
		resizeModalWidthElements();
	});
}
function resizeModalWidthElements() {
	//select all the a tags with the 'modalWidth' attribute
	$('*[modalWidth]').each(function (index) {
		var id = $(this).attr('modalWidth'); // gets the modalWidth selector ID (i.e. $(id) is the object)
		$(this).width($(id).width());
	});
}
function ConfigModalHeight() {
	resizeModalHeightElements();
	$(window).resize(function () {
		resizeModalHeightElements();
	});
}
function resizeModalHeightElements() {
	//select all the a tags with the 'modalHeight' attribute
	$('*[modalHeight]').each(function (index) {
		var id = $(this).attr('modalHeight'); // gets the modalHeight selector ID (i.e. $(id) is the object)
		$(this).height($(id).height());
	});
}
// ********************* ********************* ********************* ********************* ********************* //
// template engine <# Yeeha #>
$.fn.execTmpl = function (data) {
	var str = (this).html();
	var _tmplCache = {}
	var err = "";
	var func = _tmplCache[str];
	if (!func) {
		var strFunc =
			"var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push('" +
			str.replace(/[\r\t\n]/g, " ")
			   .replace(/'(?=[^#]*#>)/g, "\t")
			   .split("'").join("\\'")
			   .split("\t").join("'")
			   .replace(/<#=(.+?)#>/g, "',$1,'")
			   .split("<#").join("');")
			   .split("#>").join("p.push('") +
			"');}return p.join('');";
		//alert(strFunc);
		func = new Function("obj", strFunc);
		_tmplCache[str] = func;
	}
	return func(data);
}
// ********************* ********************* ********************* ********************* ********************* //
// on load...
$(document).ready(function () {
	ConfigModalPopups();
	ConfigModalWidth();
	ConfigModalHeight();
});

