define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/sniff", "chy/_base/_RequestMixin","chy/_base/_ScrollMixin","dojo/request/notify","dojox/html/_base","dijit/registry","dojo/_base/array","dojo/dom","dojo/parser","dojo/dom-construct"], 
	function(dojo, lang, has, req_mixin,scroll_mixin, notify, html,reg,array,dom,parser,domConstruct){
	var ctrl = {
			load : function(url, id, addHistory, callback /* optional; called after content is loaded */) {
				console.info("Controllor.load called, url:", url, "id",id);
				
				var _this=this;
				
				this.getContent(url, id,function(response){
					_this.cleanup(id);
					
					html.set(id,response.data,{
						parseContent: false,
						executeScripts:true,
						scriptHasHooks: false,
						renderStyles:false	
					});	

					var domNode = dom.byId(id);
					if (callback && typeof(callback) === 'function') {
						callback(url,domNode);
					}
					
					//_this.scrollIntoView(id);
				},function(response){
					console.error("request error!!!", response );
					
					KPWindowShowOverlayHtml('<div style="background-color: silver; width: 80%; top: 10%; left: 10%; position: absolute; overflow-y: scroll; z-index: 98; opacity: 0.8;"><br/><br/><br/><font color="red" size="+2pt" style="word-wrap: break-word;"><pre>'+response.response.data+'</pre></font></div>');
		
				});

				console.info("Controllor.load done, url:", url, "id",id);
				
			},
			post : function(url, id,callback) {
				console.info("Controllor.post called, url:", url, "id",id);

				var _this=this;
				
				this.postContent(url, id,function(response){
					_this.cleanup(id);
					
					html.set(id,response.data,{
						parseContent: false,
						executeScripts:true,
						scriptHasHooks: false,
						renderStyles:false	
					});	
					var domNode = dom.byId(id);
					if (callback && typeof(callback) === 'function') {
						callback(url,domNode);
					}
				},function(response){
					console.error("request error!!!", response );
					
					KPWindowShowOverlayHtml('<div style="background-color: silver; width: 80%; top: 10%; left: 10%; position: absolute; overflow-y: scroll; z-index: 98; opacity: 0.8;"><br/><br/><br/><font color="red" size="+2pt" style="word-wrap: break-word;"><pre>'+response.response.data+'</pre></font></div>');
		
				});
				console.info("Controllor.post done, url:", url, "id",id);

			},
			cleanup: function(id){
				console.info("Controllor.cleanup called, id",id);
			    var idObj=dom.byId(id);
			    console.info("cleanup window",idObj);
			    if (idObj!=null){
					var windowWidgets = reg.findWidgets(dom.byId(id));
				    array.forEach(windowWidgets,function(item){
				    	console.debug("remove object", item);
				    	item.destroyRecursive();
				    });			    	
			    }
				console.info("Controllor.cleanup done, id",id);
				
			},
			isIe: false,
			isSafari: false,
			isChrome: false,
			isFirefox: false,
		}
	//sniff browser type	
	if (has("ie")) ctrl.isIe=true;
	if (has("safari")) ctrl.isSafari=true;	
	if (has("chrome")) ctrl.isChrome=true;
	if (has("ff")) ctrl.isFirefox=true;	
	
	//set loading actions
	notify("start", function(){
		console.log("Start loading");
		document.body.style.cursor='progress';
		//_this.resetLoginTimer();
		//ONSITE_this.loaderTimeout=setTimeout("gzm.displayLoad()",6000);
        //_this.displayLoad();
    });
	notify("stop", function(){
		document.body.style.cursor='default';
		console.log("End loading");
        //ONSITE_this.hideLoad();
    });
	
	//mixin features
	lang.mixin(ctrl,new req_mixin);
	lang.mixin(ctrl,new scroll_mixin);
	
	//start kpwindow
	//resetMenuBox();KPWindowInit();KPWindowComponentForward(document.getElementById('kpwindowa1'));KPWindowRunComponentJS();
	window.chyCtrl=ctrl;
	return ctrl;
});