// Copyright 2009 by Magnus Health Technology
// Author: x.lu@magnushealth.com

var Magnus = (function(){
	if ( !window.jQuery ) {
		window.alert("jQuery not set!");
	}
	
	var mjs = $("#magnusJS");
	
	var basepath = mjs.attr("webroot");
	if ( !basepath) {
		window.alert("Base not set!");
	}
	
	var ct = parseInt(mjs.attr("time"));
	return {
		app: {webroot: basepath, currentTime: ct, webrootUrl: window.location.protocol + '//' + window.location.host + basepath},
		ui: {
			refreshTop: function(arg) {
				if (typeof arg === "undefined") {
					top.location = (top.location.hash.length > 0) ? top.location.href.replace(top.location.hash,"") : top.location.href.replace("#","");	
				} else {
					top.location.href=top.location.href + "?" + arg;
				}
				return false;
			},
			resizeWizard: function() {
				if (top.Wizard) {
					Magnus.ui.resizeTB(top.Wizard.width, top.Wizard.height);
				}
			},
			resizeTB: function() {
				var width, height, callback;
				if (typeof arguments[arguments.length-1]=="function") {
					callback = arguments[arguments.length-1];
				} else {
					callback = function(){};
				}
				if (typeof arguments[0] === "object") {
					width = arguments[0].width;
					height = arguments[0].height;
				} else {
					width = arguments[0];
					height = arguments[1]; 
				}
				// get the outer window
				var win = (self.parent === window) ? window : self.parent ;
				if (typeof win.TB_WIDTH === "undefined" || typeof win.TB_HEIGHT === "undefined") { 
					return; // TB doesn't exist nothing to do
				}
				width+=30;
				height+=40;
				// don't resize if already this size
				if (win.TB_WIDTH == width && win.TB_HEIGHT == height) {
					return;
				}
				win.TB_WIDTH=width;
				win.TB_HEIGHT=height;
				win.tb_position();
				win.$("#TB_iframeContent").animate({'width': (width-30+29), 'height': (height-45+17)}, "normal", "linear", callback);
			},
			selectTab: function(tabIndex) {
				$("#tabnav li").removeClass("selected").removeClass("nrb");
				$("#tabnav li:eq("+ tabIndex + ")").addClass("selected").prev().addClass("nrb");
				$("#tabnav li:last").addClass("nrb");		
			},
			closeTB: function(home) {
				if (self.parent && self.parent != window) {
					self.parent.tb_remove();
					return false;
				}
				if (home && home.length > 0) {
					window.location.href= basepath + home;
				} else {
					window.history.go(-1);
				}
				return false;
			}
		}
	};
})();