/* vim: set tabstop=2 shiftwidth=2 foldmethod=marker: */
/**
 * 
 * VirtuaWave Common JavaScript
 *
 * @author      Shogo Kawase <shogo@virtuawave.jp>
 * @copyright   2005-2006 VirtuaWave Inc.
 * @version     CVS: $Id: vw.js,v 1.14.2.2 2007/05/31 07:11:21 shogo Exp $
 *
 */

// 初期化
var vw = {
	_debug: false,
	body: function()
	{
		if (vw.userAgent.msie) {
			return document[(document.compatMode == 'CSS1Compat') ? 'documentElement' : 'body'];
		} else if (vw.userAgent.gecko || vw.userAgent.khtml) {
			return document.documentElement;
		}
	}
};

/**
 *
 * ブラウザ判別オブジェクト
 *
 */
vw.userAgent = {
	/** プロパティ */
	msie:      false,
	opera:     false,
	safari:    false,
	konqueror: false,
	khtml:     false,
	gecko:     false,
	firefox:   false,
	mozilla:   false,
	
	/** ユーザーエージェント判別 */
	detect: function()
	{
		// USER_AGENT取得
		var ua = navigator.userAgent.toLowerCase();
		
		// USER_AGENT判別
		if (window.opera) {
			this.opera = true;
		} else if (ua.indexOf('msie') != -1) {
			this.msie = true;
		} else if (ua.indexOf('applewebkit/') != -1) {
			this.khtml = this.safari = true;
		} else if (ua.indexOf('konqueror/') != -1) {
			this.khtml = this.konqueror = true;
		} else if (ua.indexOf('khtml/') != -1) {
			this.khtml = true;
		} else if (ua.indexOf('firefox/') != -1) {
			this.gecko = this.mozilla = this.firefox = true;
		} else if (ua.indexOf('gecko/') != -1) {
			this.gecko = this.mozilla = true;
		} else if (ua.indexOf('mozilla/') != -1) {
			this.mozilla = true;
		}
	},
	toString: function(){ return navigator.userAgent; }
};
vw.userAgent.detect();

/**
 *
 * Cookie操作オブジェクト
 *
 */
vw.cookie = {
	/** Cookieの読み込み */
	get: function(name)
	{
		var tmp1 = ' ' + document.cookie + ';';
		var tmp2 = '';
		var len  = tmp1.length;
		var x, y, z;
		x = y = z = 0;
		
		while (x < len) {
			y = tmp1.indexOf(';', x);
			tmp2 = tmp1.substring(x + 1, y);
			z = tmp2.indexOf('=');
			if (decodeURIComponent(tmp2.substring(0, z)) == name) {
				return decodeURIComponent(tmp2.substring(z + 1, y - x - 1));
			}
			x = y + 1;
		}
		return null;
	},
	/** Cookieの書き込み */
	set: function(name, data, expires, domain, path, secure)
	{
		var tmp = [encodeURIComponent(name) + '=' + encodeURIComponent(data)];
		if (typeof(expires) != 'undefined') {
			var now = new Date();
			expires = new Date(now.getTime() + (expires - 0));
			tmp.push('expires=' + expires.toGMTString());
		}
		if (typeof(domain) != 'undefined') {
			tmp.push('domain=' + domain);
		}
		if (typeof(path) != 'undefined') {
			tmp.push('path=' + path);
		}
		if (secure) {
			tmp.push('secure');
		}
		document.cookie = tmp.join(';') + ';';
		return null;
	},
	/** Cookieの削除 */
	remove: function(name, domain, path, secure)
	{
		var tmp = [encodeURIComponent(name) + '='];
		if (typeof(expires) != 'undefined') {
			var time    = new Date();
			var expires = new Date(time.getTime() - 8640000);
			tmp.push('expires=' + expires.toGMTString());
		}
		if (typeof(domain) != 'undefined') {
			tmp.push('domain=' + domain);
		}
		if (typeof(path) != 'undefined') {
			tmp.push('path=' + path);
		}
		if (secure) {
			tmp.push('secure');
		}
		document.cookie = tmp.join(';') + ';';
		return null;
	},
	/** Cookieのチェック */
	checkEnabled: function(secure)
	{
		if (typeof(navigator.cookieEnabled) != 'undefined') {
			return navigator.cookieEnabled;
		}
		var dummy = undefined;
		this.set('VW_COOKIE_CHECK_ENABLED', 'check', 86400, dummy, dummy, secure);
		if (this.get('VW_COOKIE_CHECK_ENABLED') != 'check') {
			return false;
		}
		this.remove('VW_COOKIE_CHECK_ENABLED');
		return true;
	}
};

/** FlashのIE対策用関数 */
vw.displayActiveFlash = function(id)
{
	if (vw.userAgent.msie && !!document.getElementById) {
		var swf = document.getElementById(id);
		if (swf != null) {
			var parent = swf.parentNode;
			var html   = '<object';
			var attr   = {
				data:     null,
				width:    null,
				height:   null,
				type:     'application/x-shockwave-flash',
				classid:  'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000',
				codebase: 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0'
			};
			for (var key in attr) {
				var tmp = swf.getAttribute(key) || attr[key];
				if (tmp != null) html += ' ' + key + '="' + tmp + '"';
			}
			html += '>'
			var tmp = swf.childNodes;
			for (var i = 0; i < tmp.length; ++i) {
				if (tmp[i].tagName.toLowerCase() != 'param') continue;
				html += '<param name="' + tmp[i].name + '" value="' + tmp[i].value + '" />';
			}
			html += '</object>';
			parent.removeChild(swf);
			document.write(html);
		}
	}
}

/** フォーム関連処理オブジェクト */
vw.form = {
	/** カーソルをエレメントの末尾に **/
	cursorMoveToEnd: function(obj)
	{
		if (obj.tagName == 'textarea' || obj.type == 'text') {
			if (obj.createTextRange) {
				var range = obj.createTextRange();
				range.move('character', obj.value.length);
				range.select();
			} else if (obj.setSelectionRange) {
				obj.setSelectionRange(obj.value.length, obj.value.length);
			}
		}
		obj.focus();
	},
	/** チェックボックスの一斉ON/OFF */
	checkAll: function(checked, form, name)
	{
		var parent = form ? document.getElementById(form) || document.body : document.body;
		var inputs = parent.getElementsByTagName('input');
		if (name) {
			for (var i = inputs.length - 1; i >= 0; --i) {
				if ((inputs[i].type != 'checkbox') || (!inputs[i].name.match(name))) continue;
				inputs[i].checked = checked ? true : false;
			}
		} else {
			for (var i = inputs.length - 1; i >= 0; --i) {
				if (inputs[i].type != 'checkbox') continue;
				inputs[i].checked = checked ? true : false;
			}
		}
	}
};

/** デバッグ情報出力関数 */
vw.log = function(msg)
{
	if (!vw._debug) return;
	if (vw.userAgent.opera) {
		opera.postError(msg);
	} else if (window.console && (vw.userAgent.safari || vw.userAgent.firefox)) {
		window.console.log(msg);
	} else {
		alert(msg);
	}
}
