(function($){
	$.fn.zzz = function(method){
		/* 
		 * public methods
		 */
		var methods = {
			/**
			 * コンストラクタ
			 * @param options
			 * @returns
			 */
			init: function(options){
				// オプションのマージ
				this.zzz.settings = $.extend({}, this.zzz.defaults, options);
				// ZZZ起動！
				if(!methods.get_cookie('zzz_enable')){
					methods.dropin();
				}else{
					$(this.zzz.settings.element_id).remove();
				}
			},

			/**
			 * ZZZ起動
			 */
			dropin: function(){
				var zzz_width, zzz_height, zzz_init_x, zzz_init_y, zzz_dest_x, zzz_dest_y;
				var settings = $(this).zzz.settings;
				var element_id = settings.element_id;

				zzz_width = $(element_id).width();
				zzz_height = $(element_id).height();
				zzz_init_x = Math.floor(($(document).width() - zzz_width) * 0.5);
				zzz_init_y = -zzz_height;
				zzz_dest_x = zzz_init_x;
				zzz_dest_y = settings.offset;

				// 初期ポジション
				$(element_id).css('position', 'absolute')
					.css('top', zzz_init_y+'px')
					.css('left', zzz_init_x+'px');

				// ##### インタフェースパーツ設定 #####
				$(settings.close_btn_id).click(function(e){
					if($(settings.disable_id).attr('checked')){
						methods.set_cookie('zzz_enable');
					}
					$(element_id).remove();
					return false;
				});
				$(window).resize(function(){
					$(element_id).css('left', Math.floor(($(document).width() - zzz_width) * 0.5));
				});
				// ##### /インタフェースパーツ設定 #####

				// ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ..................
				$(element_id).animate({top:zzz_dest_y+'px'}, settings.speed);
			},

			/**
			 * Cookieセット
			 */
			set_cookie: function(name){
				document.cookie = name + '=close;path=/;expires=Thu, 1-Jan-2030 00:00:00 GMT';
			},

			/**
			 * Cookie取得
			 */
			get_cookie: function(name){
				name += '=';
				cookiestr = document.cookie + ';';
				checkstart = cookiestr.indexOf(name);
				if(document.cookie.length>0){
					if(checkstart != -1){
						checkend = cookiestr.indexOf(";",checkstart);
						return unescape(cookiestr.substring(checkstart + name.length , checkend));
					}
				}
				return false;
			}
		};

		// if a method as the given argument exists
		if(methods[method]){
			// call the respective method
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		// if an object is given as method OR nothing is given as argument
		}else if(typeof method === 'object' || !method){
			// call the initialization method
			return methods.init.apply(this, arguments);
		// otherwise
		}else{
			// trigger an error
			$.error( 'Method "' +  method + '" does not exist in this plugin!');
		}

	};

	// デフォルトのオプション
	$.fn.zzz.defaults = {
		element_id: '#zzz',
		close_btn_id: '#zzz_close',
		disable_id: '#zzz_disable',
		speed: 1000,
		offset: 0,
		direction: 'top'
	};

	// プラグイン内で使用するオプション
	$.fn.zzz.settings = {};

})(jQuery);
