if(com==undefined) var com = {};
if(com.dc==undefined) com.dc = {};

com.dc.sitemap = new sitemap();

function sitemap(){
	this.lang = "";
	
	this.productXmlPath = "/johnsonelectric/common/inc/product-name.xml";
	this.industryXmlPath = "/johnsonelectric/common/inc/industries-name.xml";
	this.brandXmlPath = "/johnsonelectric/common/inc/brands-name.xml";

	this.productSitemap = null;
	this.industrySitemap = null;
	this.brandSitemap = null;
}

sitemap.prototype.setLang = function(lang){
	this.lang = lang;
}
sitemap.prototype.getLang = function(){
	return this.lang;
}
sitemap.prototype.updateSitemapCache = function(){
	if(this.productSitemap==null){
		if(com.dc.ajax==undefined) return;		
		var ret = com.dc.ajax.request(this.productXmlPath);
		if(ret!=null){
			this.productSitemap = ret;
		}
	}	
	if(this.industrySitemap==null){
		if(com.dc.ajax==undefined) return;		
		var ret = com.dc.ajax.request(this.industryXmlPath);
		if(ret!=null){
			this.industrySitemap = ret;
		}
	}	
	if(this.brandSitemap==null){
		if(com.dc.ajax==undefined) return;		
		var ret = com.dc.ajax.request(this.brandXmlPath);
		if(ret!=null){
			this.brandSitemap = ret;
		}
	}	
};
sitemap.prototype.getMatchedValueFromSitemapById = function(xmlDoc, nodeName, id, childNodeName){	
	if(xmlDoc==null) return;		
	if(typeof xmlDoc == "XMLDocument") return;
	if(!childNodeName||childNodeName==undefined||childNodeName==""||childNodeName.length==0){
		childNodeName = "en";
	}
	var items = xmlDoc.getElementsByTagName(nodeName)[0].getElementsByTagName("item");
	for (var i=0; i < items.length; i++){
		if(items[i].attributes.getNamedItem("id").value == id){
			return items[i].getElementsByTagName(childNodeName)[0].firstChild.nodeValue;
		}
	}
	return "";
};
sitemap.prototype.setSitemapNameById = function(id, value){
	var e = document.getElementById(id);
	if(e) e.innerHTML = value;
};
sitemap.prototype.setSitemapImageById = function(id, value){
	var element = document.getElementById(id);
	if(element){
		element.src = value;
		element.style.display = "";
	}
};
sitemap.prototype.setSitemapLinkById = function(id, value){
	var element = document.getElementById(id);
	if(element) element.href = value;
};
sitemap.prototype.trySplit = function(id, index){
	var ret = "";
	try{
		var ids = id.split(':');
		if(index <= ids.length){
			ret = ids[index];
		}
	}catch(e){
	}finally{
		return ret;
	}
};

/*------------------------------------------------------------------------------------------------
// product
//------------------------------------------------------------------------------------------------*/	
sitemap.prototype.setProductTypeNameById = function(htmlId, id, lang){
	this.setSitemapNameById(htmlId, this.getProductTypeNameById(id, lang));
};
sitemap.prototype.getProductTypeNameById = function(id, lang){
	this.updateSitemapCache();
	return this.getMatchedValueFromSitemapById(this.productSitemap, "productType", id, lang);
};
sitemap.prototype.setProductCategoryNameById = function(htmlId, id, lang){
	this.setSitemapNameById(htmlId, this.getProductCategoryNameById(id, lang));
};
sitemap.prototype.getProductCategoryNameById = function(id, lang){
	this.updateSitemapCache();
	id = this.trySplit(id, 1);
	return this.getMatchedValueFromSitemapById(this.productSitemap, "productCategory", id, lang);
};
sitemap.prototype.setProductCategoryImageById = function(htmlId, id){
	this.setSitemapImageById(htmlId, this.getProductCategoryImageById(id));
};
sitemap.prototype.getProductCategoryImageById = function(id){
	this.updateSitemapCache();
	id = this.trySplit(id, 1);
	return this.getMatchedValueFromSitemapById(this.productSitemap, "productCategory", id, "image");
};
sitemap.prototype.setProductPlatformNameById = function(htmlId, id, lang){
	this.setSitemapNameById(htmlId, this.getProductPlatformNameById(id, lang));
};
sitemap.prototype.getProductPlatformNameById = function(id, lang){
	this.updateSitemapCache();
	id = this.trySplit(id, 2);
	return this.getMatchedValueFromSitemapById(this.productSitemap, "productPlatform", id, lang);
};
sitemap.prototype.setProductPlatformImageById = function(htmlId, id){
	this.setSitemapImageById(htmlId, this.getProductPlatformImageById(id));
};
sitemap.prototype.getProductPlatformImageById = function(id){
	this.updateSitemapCache();
	id = this.trySplit(id, 2);
	return this.getMatchedValueFromSitemapById(this.productSitemap, "productPlatform", id, "image");
};
sitemap.prototype.setProductSeriesNameById = function(htmlId, id, lang){
	this.setSitemapNameById(htmlId, this.getProductSeriesNameById(id, lang));
};
sitemap.prototype.getProductSeriesNameById = function(id, lang){
	this.updateSitemapCache();
	id = this.trySplit(id, 3);
	return this.getMatchedValueFromSitemapById(this.productSitemap, "productSeries", id, lang);
};
sitemap.prototype.trySetProductCategoryOrPlatformNameById = function(htmlId, categoryId, platformId, lang){
	try{			
		if(platformId==""){
			this.setProductCategoryNameById(htmlId, categoryId, lang);
		}else{
			this.setProductPlatformNameById(htmlId, platformId, lang);
		}
	}catch(e){
	}
};
sitemap.prototype.trySetProductCategoryOrPlatformImageById = function(htmlId, categoryId, platformId){	
	try{			
		if(platformId==""){
			this.setProductCategoryImageById(htmlId, categoryId);
		}else{
			this.setProductPlatformImageById(htmlId, platformId);
		}
	}catch(e){
	}
};
sitemap.prototype.convertToName = function(id, lang){
	lang = lang || "en";
	var str = "";	
	var lastName = "";
	
	var ids = id.split(':');
	if(ids.length>0){
		str = this.getProductTypeNameById(ids[0], lang);
	}	
	lastName = this.getProductCategoryNameById(id, lang);
	if(str!=""&&lastName!=""){
		str += ":"+lastName;
		lastName = "";
	}
	lastName = this.getProductPlatformNameById(id, lang);
	if(str!=""&&lastName!=""){
		str += ":"+lastName;
		lastName = "";
	}
	lastName = this.getProductSeriesNameById(id, lang);
	if(str!=""&&lastName!=""){
		str += ":"+lastName;
		lastName = "";
	}
	return str;
}

/*------------------------------------------------------------------------------------------------
// industry
//------------------------------------------------------------------------------------------------*/	
sitemap.prototype.setIndustryServedNameById = function(htmlId, id, lang){
	this.setSitemapNameById(htmlId, this.getIndustryServedNameById(id, lang));
};
sitemap.prototype.getIndustryServedNameById = function(id, lang){
	this.updateSitemapCache();
	return this.getMatchedValueFromSitemapById(this.industrySitemap, "industry", id, lang);
};
sitemap.prototype.setIndustrySegmentNameById = function(htmlId, id, lang){
	this.setSitemapNameById(htmlId, this.getIndustrySegmentNameById(id, lang));
};
sitemap.prototype.getIndustrySegmentNameById = function(id, lang){
	this.updateSitemapCache();
	id = this.trySplit(id, 1);
	return this.getMatchedValueFromSitemapById(this.industrySitemap, "industrySegment", id, lang);
};
sitemap.prototype.setIndustryApplicationNameById = function(htmlId, id, lang){
	this.setSitemapNameById(htmlId, this.getIndustryApplicationNameById(id, lang));
};
sitemap.prototype.getIndustryApplicationNameById = function(id, lang){
	this.updateSitemapCache();
	id = this.trySplit(id, 2);
	return this.getMatchedValueFromSitemapById(this.industrySitemap, "industryApplication", id, lang);
};
sitemap.prototype.setApplicationExample = function(htmlId, id, lang){
	var temp = id.split(':');
	if(temp.length==1){
		this.setIndustryServedNameById(htmlId, id, lang);
	}else if(temp.length==2){
		this.setIndustrySegmentNameById(htmlId, id, lang);
	}else if(temp.length==3){
		this.setIndustryApplicationNameById(htmlId, id, lang);
	}
};

/*------------------------------------------------------------------------------------------------
// brand
//------------------------------------------------------------------------------------------------*/	
sitemap.prototype.setBrandById = function(htmlId, id, lang){
	this.setBrandImageById(htmlId, id);
	this.getBrandLinkById(htmlId, id, lang);
}; 
sitemap.prototype.setBrandImageById = function(htmlId, id){
	this.setSitemapImageById(htmlId, this.getBrandImageById(id));
};
sitemap.prototype.getBrandImageById = function(id){
	this.updateSitemapCache();
	return this.getMatchedValueFromSitemapById(this.brandSitemap, "brand", id, "img");
};	
sitemap.prototype.setBrandLinkById = function(htmlId, id, lang){
	this.setSitemapLinkById(htmlId, this.getBrandLinkById(id, lang));
};
sitemap.prototype.getBrandLinkById = function(id, lang){
	this.updateSitemapCache();
	if(lang){
		return this.getMatchedValueFromSitemapById(this.brandSitemap, "brand", id, "link_"+lang);
	}else {
		return this.getMatchedValueFromSitemapById(this.brandSitemap, "brand", id, "link");
	}
};
