﻿//Some magic part.
if (location.href != top.location.href) {
	top.location.href = location.href;
}

;(function ($) {

//----------------------------College submit button functionality---------------------------------//
var submitBtnUniqueIdCounter = 1;
var linkIdPart = "submitLinkId";

var namespacedClickEvent = "click.CollegeSubmitButton";

function submitLinkHandler() {
	var form = $(this).closest("form"), fdom = form.get(0), onsubmit = fdom.onsubmit;

	if ($.isFunction(onsubmit)) {
		onsubmit() && fdom.submit();
	} else fdom.submit();

	return false;
}

function falseFn() { return false; }

function makeSubmitButtonFunctional() {
	$("form a[rel=submit]").each(function () {
		var link = $(this), linkId = link.attr("id");
		var newLinkId = linkId || (linkIdPart + submitBtnUniqueIdCounter++), inputId = newLinkId + "_inputSubmit";

		$('<input type="image" src="/Content/img/void_pix.gif" style="position: absolute; width: 0; height: 0; outline: none;" hidefocus="true"/>')
			.attr("id", inputId)
			.appendTo(link.closest("form"));
			
		link.bind(namespacedClickEvent, submitLinkHandler).attr({href: "#", id: newLinkId});
	});
}

/**
* Disables college submit button link.
*/
$.fn.disableCollegeSubmit = function () {
	return this.each(function () {
		var me = $(this).addClass("disabled").unbind(namespacedClickEvent).bind(namespacedClickEvent, falseFn);
		$('#' + me.attr("id") + "_inputSubmit").attr("disabled", "disabled");
	});
};

/**
* Enables college submit button link.
*/
$.fn.enableCollegeSubmit = function () {
	return this.each(function () {
		var me = $(this).removeClass("disabled").unbind(namespacedClickEvent).bind(namespacedClickEvent, submitLinkHandler);
		$('#' + me.attr("id") + "_inputSubmit").removeAttr("disabled");
	});
};

$(document).ready(makeSubmitButtonFunctional);

//-----------------------College template engine------------------------------//
function tpl(html) {
	
	var me = this;

	function fn(m, name){                        
			name = "values['" + name + "']";
			return "',(" + name + " == undefined ? '' : " + name + "),'";
	}
				
	eval("this.compiled = function(values){ return ['" +
			html.replace(/\\/g, '\\\\').replace(/(\r\n|\n)/g, '\\n').replace(/'/g, "\\'").replace(this.re, fn) +
			"'].join('');};");
};

//The regular expression used to match template variables
tpl.prototype.re = /\{([\w-]+)\}/g;

//Applies values to this template and returns a result
tpl.prototype.apply = function(values) {
	return this.compiled(values);
};

//Attach as jquery extension
$.template = function(html) {
	html = $.isArray(html) ? html.join('') : $.makeArray(arguments).join('');
	return new tpl(html);
}

//---------------------Simple image preloader---------------------------------//
$.preLoadImages = function(imageList, callback) {
	var images = $.isArray(imageList) ? imageList : [imageList], 
		total = images.length, loaded = 0, pic = [];        
		
	var fn = function() {
		if (++loaded < total) return;
		$.isFunction(callback) && callback();
	};

	for (var i = 0; i < total; i++) {
		var img = new Image();
		img.src = images[i];
			
		if (img.complete || img.readyState === 4) {
			fn();
		} else {
			img.onload = fn;
			pic[pic.length] = img;
		}	
	}
	pic = null;
};

//-------------------------------College menu-------------------------------//

if ($.fn.treeview) {    //silly check for plugin availability

$(document).ready(function () {

    $("#menutree").treeview({
		unique: true,
		collapsed: true
	});

	var menucnt = $(".menucontainer");

	$("li", menucnt).css('background-image', 'none');
	$(".treeview .hitarea", menucnt).css('background-image', 'none');
	$(".smallfont", menucnt).css('background-color', '#F8FBFC');

	var menuElement;

	var pageTitle = $.trim($("#page_menutitle").html());
    if (pageTitle == "" || $("a:contains('" + pageTitle + "')", menucnt).length != 1) {
        menuElement = $("a[href='" + window.location.pathname + "']", menucnt);        
    } else {
        menuElement = $("a:contains('" + pageTitle + "')", menucnt);
    }
	//Seek a's that contain current menu title and set it bold.
	menuElement.css('font-weight', 'bold');
	//Expand menu tree.
    menuElement.parents("li.expandable").children(".hitarea").click();
});

}

})(jQuery);

