/**
 * application core javascript for this application.  
 * recommand all javascript relating to the application should extends this file
 * e.g: common/loading/loading.js
 */
var application = new function application()
{
	var instance = this;
	application.getInstance = function()
	{
		return instance;
	};

	//remove the error element if js is enabled.
	this.run = function(){
		this.removeRequireJS();
		this.removeErrorDiv();
	};

	// remove error div if there are no more errors
	this.removeErrorDiv = function(){
		errorsDiv = dojo.query(".page_errors");
		dojo.forEach(errorsDiv,
			    function(errDiv, index, array) {
			        li =dojo.query('ul > li',errDiv);
			        // if no li, we'll remove the Div.
			        if(li.length == 0){
			        	errDiv.parentNode.removeChild(errDiv);
			        }
			    }
			);
	};

	// remove the require javascript message
	this.removeRequireJS = function(){
		requireJSmsg = dojo.byId("requireJS");
		if (requireJSmsg) {
			//get the parent node which is the li
			li = requireJSmsg.parentNode;
			//remove the li.
			li.parentNode.removeChild(li);
		}
	};
	
	// help to add a css or javascript to the head tag.
	// url  = some/path/to/file.js or file.css;
	// type = js or css default to css;
	this.addToHeadBlock = function(url,type){
		if(!url || !type){
			return;
		}
		
		var headNode = document.getElementsByTagName("head")[0]; 
		
		// if url already exists we don't want to add it.
		url = url.replace(/(\n|\r|\s)+$/g, '');
		if(headNode.innerHTML.search(url) != -1)
			return;
		
		// adding the new node
		if(type == 'js'){
			this.createJS(url);
		}else if(type == 'css'){
			this.createCSS(url);
		}
	};
	
	this.createCSS = function(url){
		var headNode = document.getElementsByTagName("head")[0]; 
		var node = document.createElement('link');
		node.type = 'text/css';
		node.rel = 'stylesheet';
		node.href = url;
		headNode.appendChild(node);
	};
	this.createJS =function(url){
		var headNode = document.getElementsByTagName("head")[0]; 
		var node = document.createElement('script');
		node.type = 'text/javascript';
		node.src = url;
		headNode.appendChild(node);
	};
	return application;
};

// help to fine and return the url of a tag string. e.g: <link href='test/test.css' />
application.getUrl = function(str,type){
	if(!str || !type)
		return;
	
	if(type == 'js')
		type = 'src';
	else if(type == 'css')
		type = 'href';
	
	var pos = str.indexOf(type + '=');
	str = str.substring(pos);
	pos = str.indexOf('"');
	str = str.substring(pos+1);
	pos = str.indexOf('"');
	return str.substring(0,pos);
};

// help to add all js & css to head blog.
// str = a big big string that potentially have many css and javascript tags.
application.addJsCss = function(str){
	var start = 0;
	var app = application.getInstance();
	//search for all css
	do{
		var cssURL = undefined;
		var start = str.indexOf('<link',start);
		if(start == -1)
			break;
		var end = str.indexOf('>',start);
		var linkStr = str.substring(start,end+1);
		app.addToHeadBlock(application.getUrl(linkStr,'css'),'css');
		start = end;
	}while(str.indexOf('<link',start) != -1);
	
	//search for all js
	start = 0;
	do{
		var jsURL = undefined;
		var start = str.indexOf('<script',start);
		if(start == -1)
			break;
		var end = str.indexOf('>',start);
		var scriptStr = str.substring(start,end+1);
		app.addToHeadBlock(application.getUrl(scriptStr,'js'),'js');
		start = end;
	}while(str.indexOf('<script',start) != -1);
};

/*
 * XHR Calls
 * url = URL to make the call
 * data = params: e.g p=page,m=method,etc...
 * func = call back function when done.
 */
application.makeXHR = function(type,url,data,handle,func,loadOnBackGround){
	
	if (application._xhr) {
		application._xhr.cancel();
    }
	
	var args = {
            url: url,
            content: data,
            handleAs: handle ? handle : 'text',
            
            timeout: 5000,
            preventCache: true,

            load: function(xhrObject,ioArgs){
		
				if(!loadOnBackGround)
					application.loading.hide();
				
				if(func)
					func(xhrObject,ioArgs);
			},

            error: function (xhrObject) {
				application.loading.hide();
				if (xhrObject.dojoType=='cancel') { return; }
				alert(xhrObject);
            }
		};
	
	// show the waitDlg first;
	if(!loadOnBackGround)
		application.loading.show();
	
	application._xhr = (type == 'post') ? dojo.xhrPost(args) : dojo.xhrGet(args);
};