//  ========================================================
//  tlab-recommend.js ---- recommend view class
//  Copyright 2008 TEAM-LAB
//  ========================================================

/*************************************************************
//sample
<html>
<head>
<script type="text/javascript" src="http://honda.teamlab-recommend.jp/js/tlab-recommend.js"></script>
</head>
<body>
	<div id="hogehoge"></div>
	<script>(new TLAB.JSImport("honda.teamlab-recommend.jp", "hogehoge")).loadScript();</script>
</body>
</html>
*************************************************************/

if ( typeof(TLAB) == 'undefined' ) TLAB = function() {};

TLAB.JSImport = function (host, elem) {
	var ts = (new Date()).getTime();

	if(window.location.hostname != 'www.honda.co.jp'){
		var colledurl = escape('http://www.honda.co.jp' + window.location.pathname);
		this.jsurl = "http://" + host + "/recommend.js?staging=true&elem=" + elem + "&colledurl=" + colledurl + "&ts=" + ts;
	}else{
		var colledurl = escape(window.location.protocol + '//' + window.location.host + window.location.pathname);
		this.jsurl = window.location.protocol + "//" + host + "/recommend.js?elem=" + elem + "&colledurl=" + colledurl + "&ts=" + ts;
	}
	this.elem = elem;
	return this;
}

TLAB.JSImport.prototype.loadScript = function () {
	var jsElement=document.createElement("script");
	jsElement.type="text/javascript";
	jsElement.src=this.jsurl;
	jsElement.characterSet="Shift_JIS";
	document.getElementById(this.elem).appendChild(jsElement);
}

TLAB.displayCheck = function (argDirs) {
	this.hideDirs_ary = argDirs;
	return this;
}
TLAB.displayCheck.prototype.difDir = function () {
	function fixPathAry(path_ary) {
		if(path_ary[0] == ''){
			path_ary.shift();
		}
		
		if(
			path_ary[path_ary.length - 1].match(/^index\..+?$/i) ||
			path_ary[path_ary.length - 1] == ''
		){
			path_ary.pop();
		}
		return path_ary;
	}
	
	var dirCheckFlag = true;
	
	var currentDir_ary = fixPathAry(window.location.pathname.split('/'));

	for(var i = 0; i < this.hideDirs_ary.length; i++){
		var tempDir_ary = fixPathAry(this.hideDirs_ary[i].split('/'));
		
		var tmpCheckFlag = true;
		
		if(tempDir_ary.length == currentDir_ary.length){
			for(var j = 0; j < tempDir_ary.length; j++){
				
				if(tempDir_ary[j] != currentDir_ary[j]){
					tmpCheckFlag = false;
				}
			}
		}else{
			tmpCheckFlag = false;
		}
		
		if(tmpCheckFlag){
			dirCheckFlag = false;
			break;
		}
	}
	return dirCheckFlag;
}

if(
	(
		new TLAB.displayCheck(
			Array()
		)
	).difDir()
){
	document.write('<div id="tlab-recommendation"></div>');
	(new TLAB.JSImport("honda.teamlab-recommend.jp", "tlab-recommendation")).loadScript();
}
