/*
 * MyClient
 */
var MyClient = function() {

};
MyClient.prototype = {
	submit : function(id, method, para)
	{
		//var log = id + " : " + method;

		var vals   = {
			formid : id
		};


		for (var i in para) {
			  vals[i] = para[i];
		}


		$(id + " .input").map(function(){
  			vals[$(this).attr("name")] = $(this).val();

  			if($(this).attr("type") == "checkbox" && $(this).attr("checked") == 0) {
  				vals[$(this).attr("name")] = 0;
  			}
 		});

		var options = {
			method : method,
			datatype : "script",
			input  : vals
		};


		this.send(options);
	},
	send : function(options)
	{
		$.ajax({
			type     : "POST",
			async    : false,
			dataType : options.datatype, // text、xml html、script、json、jsonp
			url      : options.method,
			data     : options.input,
			timeout  : 5000,
			complete : function (XMLHttpRequest, status) {
				switch (status) {
					case "success":
						//alert("成功");
						break;
					case "timeout":
						alert("タイムアウト");
						break;
					case "error":
						alert("リクエスト失敗");
						break;
					case "notmodified":
						alert("更新されていない");
						break;
					case "parsererror":
						alert("データパースエラー");
						break;
					default:
						break;
				}
			}
		});

	}
};



/*
 * MoriEffect
 */
var MoriEffect = function()
{
	this.openid   = "";
	this.closeid  = "";

};
MoriEffect.prototype = {
	toggle : function(openid, closeid, flag)
	{
		this.open(openid, flag);
		this.close(closeid, flag);
	},
	toggle_fadein : function(openid, closeid, flag)
	{
		this.fadeIn(openid, flag);
		this.close(closeid, flag);
	},
	sw : function(id)
	{
		if ($(id).css('display') != "block") {
			this.open(id);
		} else {
			this.close(id);
		}
	},
	fadeIn : function(id, flag)
	{
		if (flag == 1) this.openid = id;
		$(id).fadeIn("slow");
	},
	fadeOut : function(id, flag)
	{
		if (flag == 1) this.openid = id;
		$(id).fadeOut("slow");
	},
	open : function(id, flag)
	{
		if (flag == 1) this.openid = id;
		$(id).css('display', "block");
	},
	close : function(id, flag)
	{
		if (flag == 1) this.closeid = id;
		$(id).css('display', "none");
	},
	copy : function(from, to)
	{
		$(to).html($(from).html());
	},
	del : function(id)
	{
		$(id).html("");
	},
	dropdown_jump : function(target) {
		location.href=$(target).val();
	},
	c_toggle : function(trigger, target, str) {
		if($(target).css("display") == "none"){
			$(trigger).html("[-] "+ str).css("color", "#aaa");
			$(target).css("margin-bottom", "8px");
		}
		else {
			$(trigger).html("[+] "+ str).css("color", "#070");
			$(target).css("margin-bottom", "0px");
		}

		$(target).toggle();
	}
};



/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        return 1;
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie !== '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};


var client = new MyClient();
var effect = new MoriEffect();


var userno = $.cookie('userno');
var gid    = $.cookie('gid');
var gpriv  = $.cookie('gpriv');
var wpriv  = $.cookie('wpriv');


if (userno !== null) {
	//var username  = $.cookie('username');
	//$("#login b.username").text(username);
	$("#logout, div.no-users").css("display", "none");
	$("#login, div.users, div.userno" + userno).css("display", "block");
	$("span.users, span.userno" + userno).css("display", "inline");
}
if (gid > 0 && gpriv > 0) {
	$("div.gid" + gid).css("display", "block");
	$("span.gid" + gid).css("display", "inline");
	$("div.no-gid" + gid + ", span.no-gid" + gid).css("display", "none");
}
