var SitemapLoader = function(param_path){
	
	this._objectStructure;
	this._xmlStructure;
	var ref = this;

	$.ajax({
		  url: param_path,
		  success: function(param_data) {
				//alert('loadcomplete');
				ref._xmlStructure = jQuery.createXMLDocument(param_data);
				ref._objectStructure = new XML2Object(ref._xmlStructure).finalObject;
				ref.checkStructure();
			},
		  dataType: 'html'
		});
};

SitemapLoader.prototype = {
		
	checkStructure:function() {
		var tabLength = 0;
		
		if (this._objectStructure.hasOwnProperty('assets')) {
			tabLength =  this._objectStructure.assets.length;
			for (var i  = 0; i < tabLength ; ++i) {
				this._objectStructure.assets[i] = this.checkAssetTag(this._objectStructure.assets[i]);
				//_objectStructure.assets[i].path = _application.pathFinder.resolve(_objectStructure.assets[i].path);
			}
		}
		else
			this._objectStructure.assets = [];
		
		tabLength = this._objectStructure.sections.length;
		for (i = 0; i < tabLength; ++i) {				
			if (this._objectStructure.sections[i]['default'] == "true"){
				this._objectStructure.defaultSectionId = i;
			}
			
			this.parseSection(this._objectStructure.sections[i] , [i] , []);
		}
		
		if(this._objectStructure.defaultSectionId == undefined)
			this._objectStructure.defaultSectionId = 0;
		
		$(document).trigger('sitemap_loaded' , {xml:this._xmlStructure , object:this._objectStructure})
	},


	parseSection:function(param_section , param_map , param_chunks) {
		this.checkSectionTag(param_section);
		
		param_section.map = param_map.concat();
		param_section.chunks = param_chunks.concat();
		param_section.chunks.push(param_section.url);
					
		var tabLength = param_section.assets.length;
		for (var o  = 0; o < tabLength ; ++o) {	
			param_section.assets[o] = this.checkAssetTag(param_section.assets[o]);
			//param_section.assets[o].path = _application.pathFinder.resolve(param_section.assets[o].path);
		}
		
		tabLength = param_section.sections.length;
		var childMap;
		for (var i = 0; i < tabLength  ;++i) {
			childMap = param_section.map.concat();
			//childMap..push(i);
			this.parseSection(param_section.sections[i] , childMap , param_section.chunks);
		}
	},
	

	checkAssetTag:function(param_object){
		//alert('checkAssetTag');
		var finalObject = { };
		var reservedKeyWords = ['id' , 'path', 'group' , 'options'];
		
		if (!param_object.hasOwnProperty('id')){
			console.error('SitemapLoader.checkAssetTag : missing id property');
			return null;
		}
		if (param_object.id == ''){
			console.error('SitemapLoader.checkAssetTag : empty id property');
			return null;
		}
		if (!param_object.hasOwnProperty('path')){
			console.error('SitemapLoader.checkAssetTag : missing path property');
			return null;
		}
		if (param_object.path == ''){
			console.error('SitemapLoader.checkAssetTag : empty path property');
			return null;
		}
		
		if (!param_object.hasOwnProperty('group'))
			finalObject.group = '';
		else
			finalObject.group = param_object.group;
			
		finalObject.id = param_object.id;
		finalObject.path = param_object.path;
		finalObject.options = { };
					
		for (var i in param_object) {
			if (reservedKeyWords.indexOf(i) == -1){
				finalObject.options[i] =  param_object[i];
			}
		}

		return finalObject;
	},
	
	

	checkSectionTag:function(param_object) {
		//alert('checksectiontag');
		
		if (!param_object.hasOwnProperty('url')) {
			console.error('SitemapLoader.checkSectionTag : missing url property');
			return;
		}
		if (param_object.url == '') {
			console.error('SitemapLoader.checkSectionTag : empty url property');
			return;
		}
		
		if (param_object.hasOwnProperty('command')) {
			if(!testDefinition(param_object.command))
				console.error('SitemapLoader.checkSectionTag : command' + param_object.command + 'not found');
		}
		
		if (param_object.hasOwnProperty('preloader')) {
			if(!testDefinition(param_object.preloader))
				console.error('SitemapLoader.checkSectionTag : preloader' + param_object.preloader + 'not found');
		}
		
		if (!param_object.hasOwnProperty('sections'))
			param_object.sections = [];
		if (!param_object.hasOwnProperty('assets'))
			param_object.assets = [];
		if (!param_object.hasOwnProperty('external'))
			param_object.external = false;
		if (!param_object.hasOwnProperty('disposable'))
			param_object.disposable = false;
		if (!param_object.hasOwnProperty('default'))
			param_object['default'] = false;
		if (!param_object.hasOwnProperty('showchildren'))
			param_object.showchildren = true;
		if (!param_object.hasOwnProperty('remote'))
			param_object.remote = false;
	}
	
};
