/*
var textboxes = document.getElementsBySelector("input");
for (var i=0; i<textboxes.length; i++) {
	target = textboxes[i];

	target.addEventListener("focus", function () {
		if (this.className != "disabled")  this.className = "focused";
	}, false);

	target.addEventListener("blur", function () {
		if (this.className != "disabled")  this.className = "";
	}, false);
}
*/

function closePopup(event) {
	event.target.parentNode.parentNode.parentNode.parentNode.style.display = 'none'
}


window.visibilities = { };
var classNames = $("visibilities").className.split(" ");
for (var i=0; i<classNames.length; i++) {
	window.visibilities[classNames[i].slice(5, classNames[i].length)] = true;
}

function toggleAlert() {
	window.visibilities.alert = !window.visibilities.alert;
	updateVisibilities();

	if (!window.visibilities.alert) {
		$("alertbtn").className = "blue";
		$("alertbox-container").className = "blue";
	}
	
	if (readAlerts && getAlertNewcount() > 0) readAlerts();
}

function toggleOriginal() {
	window.visibilities.original = !window.visibilities.original;
	updateVisibilities();
}

function toggleLogin() {
	window.visibilities.login = !window.visibilities.login;
	updateVisibilities();
}

function getKeys(obj) {
	var res = [];
	for (var key in obj) {
		res.push(key);
	}
	return res;
}

function updateVisibilities() {
	var classes = "";
	for (var key in window.visibilities) {
		if (window.visibilities[key])
			classes += "show-"+key+" ";
	}
	$("visibilities").className = classes;
}

var commentTouched = false;
function onTextboxFocused(event) {
	if (!event.target) event.target = this;
	event.target.className = "focused";
}

function onCommentTextFocused(event) {
	if (!commentTouched) {
		//if (browser != "ie") {  
			updateCommentText("touched");
			updateAroundCommentText();
		//}

		commentTouched = true;
	}

	//if (!is_chrome) {
		window.visibilities.login = true;
		updateVisibilities();
	//}
}

function onTextboxBlured(event) {
	if (!event.target) event.target = this;
	event.target.className = "touched";
}

var comments = {
	"first"		: initial_comment,
	"touched" 	: touched_comment
}
function updateCommentText(id) {
	if ($("comment_txt")) {
		$("comment_txt").value = comments[id];
		$("comment_txt").selectionStart = 0;
		$("comment_txt").selectionEnd = 0;
		updateAroundCommentText();
	}
}

function updateAroundCommentText() {
	var restChars = 140 - $("comment_txt").value.length;	
	$("restchars").firstChild.nodeValue = restChars;

	if (restChars >= 0) {
		$("restchars").className = "ok";
	}
	else {
		$("restchars").className = "ng";
	}

	$("comment_loading_txt").value = $("comment_txt").value;
}

function onCommentTextChange(event) {
	updateAroundCommentText();

	//if (is_chrome) {
		window.visibilities.login = true;
		updateVisibilities();
	//}
}

function onCommentTextKeyUp(event) {
	updateAroundCommentText();
}


// form remember 対策
updateCommentText("first");


function alertTouch() {
	
}

function onContentsLoading() {
	$("bar").className = "loading";
	//$("comment_text").disabled = true;
}


if ($("loginbox")) {
	var ww = window.innerWidth;
	var wl = $("controller").offsetLeft;
	var rw = $("readybtn").offsetWidth;
	$("loginbox").style.right = (ww - wl - rw / 2) + "px";
}
