/*
* CertificaTag for Clasificados.pr
* Copyright 2010 - Clasificados.pr
* $id: CertificaTag-0.1.js 2010-03-25 
*/

//DEBUG 
//alert("Tag inserted Here");
	var i=0;
	var strDomains = new Array();
	strDomains[i++] = "www.clasificados.pr";
	strDomains[i++] = "www.subastalo.com";
	strDomains[i++] = "subastalo.com";
	strDomains[i++] = "localhost";

	var strUrl = window.location.toString().toLowerCase();
	var certificaPath = getCertificaPath(strUrl);
	//var certificaPathLevelCnt = getLevelCount(certificaPath);

//document.write("Url: " + strUrl + "<br/>");
//document.write("CertificaPath: " + certificaPath + "<br/>");
//document.write("LevelCount: " + certificaPathLevelCnt + "<br/>");

	// Certifica Libs
	tagCertifica( 106027, certificaPath);


	function getCertificaPath(url){
		var strUrl = url;
		for(i=0; i<strDomains.length; i++){
			strUrl = strUrl.replace("http://"+strDomains[i], "");
		}
		strUrl = getExceptions(strUrl);

//document.write("strUrl: " + strUrl + "<br/>");
		strUrl = strUrl.replace("//", "/");
		strUrl = strUrl.replace("default.html", "home");
		strUrl = strUrl.replace(".html", "");
		strUrl = strUrl.replace(".aspx", "");
		if(strUrl.search("\\?") >= 0){
			strUrl = strUrl.split("?")[0];
		}
		
		// Trailing Slash
		strUrl = trailingSlash(strUrl);

		// Defaults
		switch(getLevelCount(strUrl)){
			case 0: strUrl = "/home/default"; break;
			case 1: strUrl = strUrl + "/default"; break;
			default: break;
		}
		
		
//document.write("strUrl: " + strUrl + "<br/>");
		return strUrl;
	}

	function getLevelCount(certPath){
		var levelCount = 0;
		var arrLevels = certPath.split("/");
		for(i=0; i<arrLevels.length; i++){
			//document.write("arrLevel: "+i+ " - " +arrLevels[i]+"<br/>");
			levelCount += (arrLevels[i]!="")?1:0;
		}
		//document.write("Real Level Count : "+levelCount + "<br/>");
		return levelCount;
		
	}
	
	function getExceptions(path){
		returnPath = path;
//document.write("returnPath: "+returnPath+"<br/>");
		//Exception #1
		if(strUrl.search("subastalo.com")>=0){
			returnPath = "/subastalo/"+returnPath;
		}
		//Exception #2 DEBUG
		if(strUrl.search("localhost")>=0){
			returnPath = "/localhost/"+returnPath;
		}
		//Exception #3
		//Exception #4
		
		
//document.write("returnPath: "+returnPath+"<br/>");
		return returnPath;
	}
	
	function trailingSlash(path){
		var returnPath = path;
		var pathLen = path.length;
		if(returnPath.length>0){
			returnPath = (returnPath[pathLen-1] == "/")?returnPath.substring(0,pathLen-1):returnPath;
		}
		return returnPath;
	}
