﻿// JavaScript Document
// Grants Office Library
//------------------------------------------------------------
/* Grants Office's Javascript library is a bare javascript library 
optimized to work on any website, the engine lacks the use of javascript
libraries in an effort to work on all websites without disrupting other
items. The library will be updated over time but is primarily ment for use
on grantsoffice.com short term. Bits and parts will be pulled out as
grants office developes a long term javascript solution that can 
be used cross browser.
*/
// Bare Javascript Version
//Parent Grants Office Object
var goffice= new Object();
//Load Object
goffice.load= {
	javascript : function(filename){
		var file=document.createElement('script');
		file.setAttribute("type","text/javascript");
		file.setAttribute("src", filename);
		document.getElementsByTagName("head")[0].appendChild(file);
	},
	css : function(filename,mediaType){
		var type="screen";
		if(mediaType != undefined ) {
			type=mediaType;	
		}
		var loaded=false;
		for(var i in this.loaded.css){
			if(this.loaded.css[i]==filename){
				loaded=true;
			}
		}
		if(loaded!=true){
			var file=document.createElement("link");
			file.setAttribute("rel", "stylesheet");
			file.setAttribute("media", type);
			file.setAttribute("type", "text/css");
			file.setAttribute("href", filename);
			this.loaded.css.push(filename);
			document.getElementsByTagName("head")[0].appendChild(file);
		}
	},
	loaded : {
		javascript : new Array(),
		css : new Array()
	}
}
//Effects Object
goffice.effects={
	opacity : function(id,amt){
		if(document.getElementById(id)!=null){
			var theItem=document.getElementById(id).style;
			theItem.opacity=(amt/100);
			theItem.filter="alpha(opacity="+amt+")";
		}
	},
	fadeIn : function(id,dur){
		if ( dur === undefined ) {
			dur = '10';
		}
		goffice.effects.opacity(id,0);
		var tick=0;
		for(i=0; i<=100; i++){
			var ticker=tick*dur;
			var timer=setTimeout("goffice.effects.opacity('"+id.toString()+"','"+i.toString()+"')",ticker);
			if(document.getElementById(id) == null ){
				clearTimeout(timer);
			}
			if(i>=100){
				clearTimeout(timer);
			}
			tick++;
		}
	}
}
goffice.browser={
	version:function(){
		var version=999;
		if (navigator.appVersion.indexOf("MSIE") != -1)
		  version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		return version;
	}
}
goffice.ajax={
	updating:true,
	request:function(method,url,callback){
		this.updating=true;
		this.callbackMethod= callback;
		this.request=(window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP");
		this.request.onreadystatechange=function(){goffice.ajax.checkReadyState();};
		this.request.open(method,url,true);
		this.request.send(url);
	},
	rebuild:function(method,url,callback){
		this.updating=true;
		this.callbackMethod= callback;
		this.request=(window.XMLHttpRequest)? new XMLHttpRequest(): new ActiveXObject("MSXML2.XMLHTTP");
		this.request.onreadystatechange=function(){goffice.ajax.checkReadyState();};
		this.request.open(method,url,true);
		this.request.send(url);
	},
	checkReadyState:function(_id){
		//
		switch(this.request.readyState){
			case 1:break;
			case 2:break;
			case 3:break;
			case 4:
				this.updating=false;
				this.callbackMethod(this.request.responseXML.documentElement);
		}
	}
}
//UI Object
goffice.ui= new Object();
//[Message Box]
goffice.ui.messagebox={
	title:"Grants Office",
	content:"Content is Unavailible",
	options:{
		print:true
	},
	style:"http://static.grantsoffice.com/ui/messagebox/style_min.css",
	printStyle:"http://static.grantsoffice.com/ui/messagebox/print_min.css",
	opened:false,
	theme:"#006600",
	create : function(){
		goffice.load.css(this.style);
		goffice.load.css(this.printStyle,"print");
		//Message Box
		var MessageBox = document.createElement('div');
		MessageBox.setAttribute('id','goffice_ui_mb');
		if (goffice.browser.version() <= 7) {
			MessageBox.style.left="0px";
		}
		//style.background = "red";
		MessageBox.style.zIndex="999";
		document.body.insertBefore(MessageBox,document.body.childNodes[0]);
		//Message Box Container
		var MB_Container = document.createElement('div');
		MB_Container.setAttribute('id','goffice_ui_mb_container');
		MessageBox.appendChild(MB_Container);
		//Message Box Header
		var MB_Header = document.createElement('div');
		MB_Header.setAttribute('id','goffice_ui_mb_header');
		MB_Header.setAttribute('style','background-color:'+this.theme+';');
		MB_Container.appendChild(MB_Header);
			//Message Box Header Top
			var MB_Header_Top = document.createElement('div');
			MB_Header_Top.setAttribute('id','goffice_ui_mb_header_top');
			MB_Header.appendChild(MB_Header_Top);
			//Message Box Header Link
			var MB_Header_CloseLink = document.createElement('a');
			MB_Header_CloseLink.innerHTML="(close)";
			if (goffice.browser.version() <= 7) {
				MB_Header_CloseLink.setAttribute("onclick", function(){goffice.ui.messagebox.close()});
			}else{
				MB_Header_CloseLink.setAttribute("onclick","goffice.ui.messagebox.close()");
			}
			MB_Header_CloseLink.setAttribute('href','#');
			MB_Header_Top.appendChild(MB_Header_CloseLink);
			//Message Box Header Bottom
			var MB_Header_Bottom = document.createElement('div');
			MB_Header_Bottom.setAttribute('id','goffice_ui_mb_header_bottom');
			MB_Header.appendChild(MB_Header_Bottom);
			//Message Box Header Title
			var MB_Header_Title = document.createElement('h1');
			MB_Header_Title.setAttribute('id','goffice_ui_mb_header_title');
			MB_Header_Title.innerHTML=this.title;
			MB_Header_Bottom.appendChild(MB_Header_Title);
		//Message Box Content
		var MB_Content = document.createElement('div');
		MB_Content.setAttribute('id','goffice_ui_mb_content');
		MB_Container.appendChild(MB_Content);
		//function containing nav builder
			//Message Box Options
			var MB_Options = document.createElement('div');
			MB_Options.setAttribute('id','goffice_ui_mb_nav');
			MB_Content.appendChild(MB_Options);
			if(this.options.print==true){
				MB_Options.innerHTML+="<a class='goffice_ui_mb_print_link' href='#' onclick='print();'>Print</a>";
			}
		MB_Content.innerHTML+=this.content;
		//Message Box Footer
		var MB_Footer = document.createElement('div');
		MB_Footer.setAttribute('id','goffice_ui_mb_footer');
		MB_Footer.setAttribute('style','background-color:'+this.theme+';');
		if (goffice.browser.version() <= 7) {
			MB_Header.style.backgroundColor=this.theme;
			MB_Footer.style.backgroundColor=this.theme;
		}
		MB_Footer.innerHTML='<span id="goffice_ui_mb_footer_content">Call us for more information: <strong>585.473.1430 ext 105.</strong> Or Visit our website at <strong><a href="http://www.grantsoffice.com" target="_blank">www.grantsoffice.com</a></strong></span>';
		MB_Container.appendChild(MB_Footer);
		goffice.effects.fadeIn("goffice_ui_mb",5);
	},
	remove : function(){
		var MessageBox = document.getElementById('goffice_ui_mb');
		document.body.removeChild(MessageBox);
	},
	close : function(){
		if(this.opened==true){
			this.remove();
			this.opened=false;
		}
	},
	open : function(aTitle,aContent){
		if(this.opened==false){
			if(aTitle!=undefined){
				this.title=aTitle;
			}
			if(aContent!=undefined){
				this.content=aContent;
			}
			this.create();
			this.opened=true;
		}
	}
};