Site=function(){
	this.useModuleCache=true;
	this.tpl_cache=new Array();
	this.app_title=document.title;
	this.title='';
	this.subtitle='';
	this._attr=new Array();
}
Site.prototype.setTitle=function( title ){
	this.title=title;
	dojo.byId("module_title").innerHTML=this.title;
	document.title=this.title+' - '+this.app_title;
}
Site.prototype.setSubTitle=function( subtitle ){
	this.subtitle=' - '+subtitle;
	dojo.byId("module_title").innerHTML=this.title+this.subtitle;
}


////////////////////MODULE SETUP//////////////////
Site.prototype.selectModuleButton=function( mod_name ){
	if( !dojo.byId(mod_name+'Button') )return;
	dojo.query('.ModuleButtonSelected').removeClass('ModuleButtonSelected');
	dojo.addClass(mod_name+'Button','ModuleButtonSelected');
}
Site.prototype.buildModuleDOM=function ( module_data ){
	if( !module_data )return;
	this.module_data=module_data;
	this.renderTpl('module_menu',{menuitems:this.module_data});
	
	if( !location.hash ){
		location.href+='#'+this.module_data[0].name
	}
	this.hashChange();
}
Site.prototype.loadModule=function (mod_name){
	if( this.currentModule==mod_name )return;
	this.unloadModule( this.currentModule );
	this.currentModule=mod_name;
	this.selectModuleButton( this.currentModule );

	var container=dojo.byId('holder'+this.currentModule);
	if( !container ){
		var container=document.createElement('div');
		container.id='holder'+mod_name;
		dojo.byId('ModuleContainer').appendChild(container);
	}
	container.style.display='block';
	for( var i in this.module_data ){
		if(this.module_data[i].name==this.currentModule)this.setTitle(this.module_data[i].label);
	}
	if(container.innerHTML==''){
		var _this=this;
		Connector.addRequest( {mod:this.currentModule}, function(response){_this.setUpModule(mod_name,response);} );
	}
}
Site.prototype.unloadModule=function ( mod_name ){
	if( !mod_name )return;//firstTime
	var container=dojo.byId('holder'+mod_name);
	container.style.display='none';
}
Site.prototype.setUpModule=function( mod_name, html ){
	this.setInnerHTML('holder'+mod_name,html);
	if( window[mod_name+'Js'] && window[mod_name+'Js'].init )window[mod_name+'Js'].init();
}
////////////////////MODULE SETUP//////////////////






//////////////////////DOM FUNCTIONS//////////////////////////
Site.prototype.setInnerHTML = function(ele_id, content){
	if (!ele_id)return;
	var ele=dojo.byId(ele_id);
	ele.innerHTML = this.splitContent( content );
	dojo.require('dojo.parser');
	dojo.parser.parse(ele);
};
Site.prototype.splitContent=function ( raw_html ){
	if( !raw_html )return;
	var scriptExpr = "<script[^>]*>(.|\s|\n|\r)*?</script>";
	var matches = raw_html.match(new RegExp(scriptExpr, "img"));
	if (matches){
		for (var i = 0; i < matches.length; i++){
			eval(matches[i].replace(/<script[^>]*>[\s\r\n]*(<\!--)?|(-->)?[\s\r\n]*<\/script>/img, ""));
		}
	}
	return raw_html.replace(new RegExp(scriptExpr, "img"), "");
}
Site.prototype.renderTpl=function( node_id, data ){
	try{
		var tpl_node=document.getElementById(node_id);
		if( !tpl_node )return;
	}
	catch(e){return;}
	
	dojo.require("dojox.dtl");
	dojo.require("dojox.dtl.Context");
	
	if( !this.tpl_cache[node_id] ){
		this.tpl_cache[node_id]=unescape(tpl_node.innerHTML);
	}
	if( data ){
		var template = new dojox.dtl.Template(this.tpl_cache[node_id].replace(/<\!--|-->/img,""));
		tpl_node.innerHTML=template.render(new dojox.dtl.Context(data));
		dojo.removeClass(tpl_node,'hidden');
	}
	else{//Reset if no data
		tpl_node.innerHTML=this.tpl_cache[node_id];
	}
}
//////////////////////DOM FUNCTIONS//////////////////////////





//////////////////////UTILS///////////////////
Site.prototype.showPopup=function( fvalue, callback, getvars ){
	if( !fvalue )return;
	var href='./popup.php?'+Connector.serialize(getvars);
	if( window.popup_dialog ){
		popup_dialog.close();
	}
	window.popupfvalue=fvalue;
	window.popupcallback=callback;
	window.popup_dialog=window.open(href,'popup_dialog','width=400,height=300,left=400,top=300');
	popup_dialog.fvalue=fvalue;
	popup_dialog.callback=callback;
}
Site.prototype.printOut=function( request ){
	var href='./?'+Connector.serialize(request);
	if( window.printer_window && !window.printer_window.closed ){
		window.printer_window.location.href=href;
	}
	else{
		window.printer_window=window.open(href,'_new');
	}
	window.printer_window.onlyPrint=true;
}
//Site.prototype.print=function( template, data, in_popup ){
//	if( !window.printer_queue )window.printer_queue=new Array();
//	window.printer_queue.push({template:template,data:data});
//	
//	if( window.printer_window && !window.printer_window.closed && window.printer_window.PrintNext ){
//		window.printer_window.PrintNext();
//	}
//	else{
//		if( in_popup || dojo.isIE || dojo.isOpera ){
//			printer_window=window.open(dojo.moduleUrl('baycik.printer','printer.html'),'printer_window','width=600,height=600,left=300,top=300');
//		}
//		else{
//			var iframe_node=dojo.byId('printer_window');
//			if( !iframe_node ){
//				iframe_node=document.createElement('iframe');
//				iframe_node.id='printer_window';
//				iframe_node.name='printer_window';
//				iframe_node.style.width="0px";
//				iframe_node.style.height="0px";
//				iframe_node.style.border="none";
//				iframe_node.src=dojo.moduleUrl('baycik.printer','printer.html');
//				document.body.appendChild(iframe_node);
//			}
//		}
//	}
//}
Site.prototype.initModTabs=function( _this, id ){
	var _class=this;
	_this.selectTab=function( tab ){
		if( !tab.tab_name )
		tab.tab_name=tab.attr('content');
		_this.active_script=_this[tab.tab_name];
		if( !_this.active_script )return;
		
		if( tab.html_loaded ){
			if( _this.active_script.focus )
				_this.active_script.focus();
			return;
		}
		tab.attr('content','Loading...');
		
		var tpl_path=_this.active_script.tpl;
		var handler=function( content ){
			tab.attr('content',_class.splitContent( content ));
			if(_this.active_script.init)_this.active_script.init();
		}
		Connector.addRequest({tpl:tpl_path},handler);
		tab.html_loaded=true;
	}
	_this.tabs=dijit.byId(id);
	_this.selectTab(_this.tabs.getChildren()[0]);
	dojo.connect( _this.tabs, 'selectChild', _this.selectTab );
	dojo.connect( _this.tabs, 'click', _this.selectTab );
}
Site.prototype.attr=function(index,value){
	if( value==undefined ){
		return this._attr[index];
	}
	else{
		this._attr[index]=value;
	}
}
//////////////////////UTILS///////////////////


///////////////////HASH CONTROL//////////////////////
Site.prototype.hashChange=function(){//#Site:t32
	var hash=this.hash();
	this.loadModule(hash.mod);
	this.onStateChange(hash.state);
}
Site.prototype.hash=function( new_hash ){
	var matches=location.href.match(/#(\w+)[:]?(\w*)/);
	if( !matches ){
		matches=new Array('','','');
	}
	if( new_hash ){
		location.hash='#'+(new_hash.mod || matches[1])+':'+(new_hash.state || matches[2]);
	}
	else
		return {mod:matches[1],state:matches[2]};
}
Site.prototype.onStateChange=function(){};
///////////////////HASH CONTROL//////////////////////
