var g = {
	cd: null,
	jw: false,
	start: function(){
		g.cd = $("bkcd").innerHTML;
		if(cart) cart.start(g._updCart);
		$("inqa").className.match(/b_random([A-C])/);
		var c = RegExp.$1;
		clk('h0', c);
	},
	add: function(cd, cw){
		g.jw = false;
		$("inqa").className.match(/b_random([A-C])/);
		var c = RegExp.$1;
		if(cart.iscart(cd)){
			clk('h2', c);
			if(cw) return true;
			cart.del(cd);
		}
		else {
			clk('h1', c);
			if(!cart.add(cd)) alert("既に１０件登録済みです。\r\n(登録は最大１０件までです)");
			else g.jw = true;
		}
		return false;
	},
	_updCart: function(){
		if($("wslcnt")) $("wslcnt").innerHTML = cart.length();
		if(!cart.iscart(g.cd)){
			if($("sinq")) $("sinq").className = $("sinq").className.replace(/d_mitsumori_on/, "d_mitsumori_off");
			$("inlist").style.display = "none";
			if($("inqa")) $("inqa").className = $("inqa").className.replace(/ b_sumi/, "");
		}
		else {
			if($("sinq")) $("sinq").className = $("sinq").className.replace(/d_mitsumori_off/, "d_mitsumori_on");
			$("inlist").style.display = "inline";
			if($("inqa")) $("inqa").className = $("inqa").className + " b_sumi";
			if(g.jw) location.href = "/car/w.php";
		}
	}
}



var mimg = new function(){
	this.curImg = null;
	this.imgl = null;
	this.cmnt = null;
	this.init = function(){
		this.imgl = $("imgl");
		this.cmnt = $("cmnt");
	}
	this.chg = function(obj){
		if(!this.imgl) this.init();
		if(!obj.childNodes[0]) return;
		if(this.curImg) this.curImg.className = "";
		this.imgl.src = obj.childNodes[0].src;
		this.imgl.alt = obj.childNodes[0].alt;
		this.cmnt.style.display = "block";
		this.cmnt.innerHTML = this.imgl.alt;
		obj.className = "mj";
		this.curImg = obj;
	}
}
var wsl = new function(){
	this.cd = "";
	this.bklist = null;
	this.exp = 10*24*60*60*1000;
	this.timer = null;
	this.init = function(){
		this.cd = $("bkcd").innerHTML;
		$("wslcnt").innerHTML = "0";
		this.load();
		this.timer = setInterval(wsl._interval, 500);
	}
	this._interval = function(){
		wsl.load();
		wsl._marking();
	}
	this._marking = function(){
		if(!wsl.bklist) return;
		if(wsl.bklist.indexOf(wsl.cd) < 0){
//			$("d_HIKAKU").className = $("d_HIKAKU").className.replace("/\s+mj/", "");
			$("s_HIKAKU").className = $("s_HIKAKU").className.replace("/\s+mj/", "");
		}
		else {
//			$("d_HIKAKU").className += " mj";
			$("s_HIKAKU").className += " mj";
		}
	}
	this.add = function(cd){
		this._add(cd);
		this._upd();
		this.load();
	}
	this._add = function(cd){
		if(!wsl.bklist) wsl.bklist = new Array;
		if(wsl.bklist.indexOf(cd) < 0) wsl.bklist.push(cd);
	}
	this._upd = function(){
		var s = (wsl.bklist) ? wsl.bklist.join("\t") : "";
		setCookie("wsl", s, wsl.exp);
	}
	this.load = function(){
		if(wsl.bklist){
			delete wsl.bklist;
			wsl.bklist = null;
		}
		var c = getCookie("wsl");
		if(c == "") return;
		var l = c.split("\t");
		for(var i = 0 ; i < l.length ; i++){
			if(l[i] == "" || l[i].length != 12) continue;
			wsl._add(l[i]);
		}
		$("wslcnt").innerHTML = wsl.bklist.length;
	}
}
var wst = new function(){
	this._st = null;
	this.start = function(){
		var dt = new Date;
		this._st = dt.getTime();
	}
	this.end = function(){
		var dt = new Date;
		var w = (dt.getTime() - this._st).toString();
		var p = "?no=1&tm=" + w.toString() + "&rf=" + encodeURIComponent(document.referrer) + "&r" + Math.random();
		var ajax = new Ajax.Request("/car/sty.php" + p);
	}
	this._send = function(tm, rf){
		var p = "?no=1&tm=" + tm.toString() + "&rf=" + encodeURIComponent(rf) + "&r" + Math.random();
		var ajax = new Ajax.Request("/car/sty.php" + p, {method:'get'});
	}
}
window.onload = function(){
	wst.start();
//	wsl.init();
	g.start();
}
window.onunload = function(){
	wst.end();
}
function chgimg(obj){
	mimg.chg(obj);
	return false;
}
function hcmnt(obj){
	obj.style.display = "none";
	return false;
}



function onwsl(bkcd){
	if(wsl.bklist && wsl.bklist.length >= 5){
		alert("登録は最大５件までです。");
		return false;
	}
	wsl.add(bkcd);
	window.location.href = "/car/w.php";
	clk(2, bkcd);
	return false;
}
function chgimg2(obj){
	var src = obj.src;
	var alt = obj.alt;
	obj.src = $("pic1").src;
	obj.alt = $("pic1").alt;
	$("pic1").src = src;
	$("pic1").alt = alt;
}

function onsi(bkcd){
	clk(1, bkcd);
	return true;
}
function clk(type, cd){
	var p = "?t=" + type + "&c=" + cd + "&r=" + Math.random();
	var ajax = new Ajax.Request("/car/clk.php" + p, {method:'get'});
}
function pr(key){
	window.open("dp.php?k=" + key, "print", "scrollbars=yes,location=no,width=800,height=600,menubar=yes");
	return false;
}

function tweet() {
	var login  = 'cslab';								// 固定
	var apiKey = 'R_2a30a09b0e31b550131f96606ecca7d9';	// 固定
	bitly = 'http://api.bit.ly/shorten'
		+ '?version=2.0.1&format=json&callback=callback'
		+ '&login=' + login
		+ '&apiKey=' + apiKey + '&longUrl=';
	var script = document.createElement('script');
	script.type = 'text/javascript';
	var url = location.href + '\?vos=labtwitter001';
	script.src = bitly + encodeURIComponent(url);
	document.body.appendChild(script);
	return false;
}
function callback(bitlyResponse) {
	var tweetText = 'このクルマ、気になる！ ' + bitlyResponse.results[location.href+'\?vos=labtwitter001']['shortUrl'] + ' #carsensorlab';
	var url = 'http://twitter.com/home/?status=' + encodeURIComponent(tweetText);
//	location.href = url;
	window.open(url);
}

