// showroom tab event
function Showroom() {
	TARGET_CONTAINER_ID_LIST = [
		"list_showroom_staff",
		"list_usedcar_staff",
		"list_service_staff",
		"list_bodyshop_staff"
	];
	var len_target = TARGET_CONTAINER_ID_LIST.length;
	
	var param = location.search.match(/brand=(.*)/);
	if(param) var init_brand = param[1];
	
	for(var i = 0 ; i < len_target ; i++) {
		var stm = new ShowroomTabManager(init_brand);
		stm.setTabAction(TARGET_CONTAINER_ID_LIST[i]);
	}
}

function ShowroomTabManager(init_brand) {
	var FERRARI     = "ferrari";
	var MASERATI    = "maserati";
	var BENTLEY     = "bentley";
	var ROLLSROYCE = "rolls-royce";
	this.BRAND_LIST = [FERRARI,MASERATI,BENTLEY,ROLLSROYCE];
	this.BRANDS = {
		"ferrari"     : FERRARI,
		"maserati"    : MASERATI,
		"bentley"     : BENTLEY,
		"rolls-royce" : ROLLSROYCE
	}
	this.tabs         = null
	this.tab_contents = {}
	this.init_brand   = init_brand ? init_brand : "";
}
ShowroomTabManager.prototype = {
	setTabAction : function(target_id) {
		var self = this;
		if(this.tabActionMain(target_id) === false) return;
		this.hideAll();
		
		if(this.init_brand && this.tab_contents[this.init_brand]) {
			setStartupTabContents(this.tab_contents[this.init_brand]);
		} else {
			var loop_flag = true;
			var num;
			while(loop_flag) {
				num = Math.floor(Math.random() * 4);
				if(this.tab_contents[this.BRAND_LIST[num]]) {
					setStartupTabContents(this.tab_contents[this.BRAND_LIST[num]]);
					loop_flag = false;
				}
			}
		}
		
		function setStartupTabContents(_target) {
			_target.style.display = "block";
			_target.img.src = _target.img.src.replace("_f1","_f2");
			self.detachSari(_target.img);
		}
	},
	tabActionMain : function(target_id) {
		var list_container = document.getElementById(target_id);
		if(!list_container) return false;
		
		var list_contents = list_container.getElementsByTagName("div");
		if(!list_contents) return false;
		var len_contents = list_contents.length;
		var class_name = "";
		var HAS_CONTENTS = false;
		// create object that containes tab contents
		for(var i = 0 ; i < len_contents ; i++) {
			class_name = list_contents.item(i).className;
			if(class_name.indexOf("brand") == -1) continue;
			for(var name in this.BRANDS) {
				if(class_name.indexOf(this.BRANDS[name]) != -1) {
					this.tab_contents[this.BRANDS[name]] = list_contents.item(i);
					HAS_CONTENTS = true;
					break;
				}
			}
		}
		if(!HAS_CONTENTS) return false;
		
		this.tabs = list_container.getElementsByTagName("ul").item(0).getElementsByTagName("li");
		var len_tab = this.tabs.length;
		var tmp_tab = null;
		// relate tabs to contents
		for(var i = 0 ; i < len_tab ; i++) {
			tmp_tab = this.tabs.item(i);
			if(this.tab_contents[tmp_tab.className]) {
				this.tab_contents[tmp_tab.className].tab = tmp_tab;
				this.tab_contents[tmp_tab.className].img = tmp_tab.getElementsByTagName("img").item(0);
				this.doTabAction(this.tab_contents[tmp_tab.className]);
			}
		}
	},
	doTabAction : function(target) {
		var self = this;
		target.tab.onclick = function() {
			self.hideAll();
			target.style.display = "block";
			target.img.src = target.img.src.replace("_f1","_f2");
			self.detachSari(target.img);
		}
	},
	hideAll : function() {
		if(!this.tab_contents) return;
		for(var tmp in this.tab_contents) {
			this.tab_contents[tmp].style.display = "none";
			this.tab_contents[tmp].img.src = this.tab_contents[tmp].img.src.replace("_f2","_f1");
			this.attachSari(this.tab_contents[tmp].img);
		}
	},
	attachSari: function(obj) {
		obj.onmouseover = function(){this.src = this.src.replace("_f1","_f2");}
		obj.onmouseout  = function(){this.src = this.src.replace("_f2","_f1");}
	},
	detachSari: function(obj) {
		obj.onmouseover = obj.onmouseout = "";
	}
	
}

com._startup_items.push(Showroom);





