/*
 * dialogs.js
 * 
 * 使用時は YUI Compressorで圧縮済みの dialogs.compressed.js を使用してください
 * java -jar yuicompressor-2.4.2.jar --charset UTF-8 -o dialogs.compressed.js dialogs.js
 *
 * @category   Car ITPack
 * @package    JavaScript
 * @author     $Author: d-watanabe $
 * @since      $Date: 2010-04-14 14:22:23 +0900 (水, 14  4月 2010) $
 * @version    $Rev: 1690 $
 * @copyright  Copyright (c) 2002-2008 AIC Corporation All Rights Reserved.
 *             http://www.aic-co.jp
 * @see        YUI Compressor http://yuilibrary.com/ 
 * 
 * jQuery 及び、jQuery UI が必須です
 */

;( function($)
{
	/**
	 * cit.dialog
	 *
	 * @param  string title
	 * @return jQuery
	 */
	$.fn.cit.dialog = function( title)
	{
		if( 1 > arguments.length){ title = this.attr('title');}
	
		var div  = $('<div id="dialog"></div>')
			.dialog({
				autoOpen : false,
				title    : title,
				bgiframe : true,
				modal    : true,
				resizable: false,
				close    : function(){$(this).parent('.ui-dialog').empty();}
			});
		;
		return $.extend( this, this.dialog, { citDialog: div});
	};
	
	/**
	 * Alert dialog.
	 *
	 */
	$.fn.cit.dialog.alert = function( message)
	{
		var div = this.citDialog;
		var btn = {
			'了解' : function(){ $(div).dialog('close');}
		};
		var msg = $.isArray(message) ? '<p>' + message.join('</p><p>') + '</p>' : message;
		
		$(div)
			.append('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + msg + '</p>')
			.dialog('option', 'title'  , 'Alert')
			.dialog('option', 'buttons', btn)
			.dialog('open')
		;
		return this;
	};
	
	/**
	 * Remove confirm dialog.
	 *
	 */
	$.fn.cit.dialog.remove = function( message)
	{
		if( 1 > arguments.length){ message = '削除します。よろしいですか？';}
		
		var div = this.citDialog;
		var msg = $.isArray(message) ? '<p>' + message.join('</p><p>') + '</p>' : message;
		var cb;
		if('undefined' != typeof $(this).attr('href'))
		{
			// for anchor link
			var href = $(this).attr('href');
			cb = function(){ location.href = href;};
		}
		else if('undefined' != typeof $(this).attr('action'))
		{
			// for form
			var form = $(this);
			cb = function(){ $(form).unbind('submit'); $(form).submit();};
		}
		var btn  = {
			'キャンセル': function(){ $(div).dialog('close');},
			'削除する'  : function(){ $(div).dialog('close'); cb();}
		};
		
		
		$(div)
			.append('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + msg + '</p>')
			.dialog('option', 'buttons', btn)
			.dialog('open')
		;
		return this;
	};
	
	/**
	 * Image dialog.
	 *
	 */
	$.fn.cit.dialog.image = function( url)
	{
		if( 'undefined' == typeof url)
		{
			url = this.attr('src');
		}
		
		var img  = new Image();
		img.src = url;
		
		var width = Math.ceil($(window).width() * 0.8);
		if( width > img.width + 30)
		{
			width = img.width + 30;	// widthはダイアログ幅なので、img.width+余白(26px)分の幅が必要
		}
		
		var div  = this.citDialog;
		var btn  = {
			'閉じる': function(){ $(div).dialog('close');}
		};
		
		$(div)
			.append('<img src="' + img.src + '" />')
			.dialog('option', 'width'    , width)
			.dialog('option', 'resizable', false)
			.dialog('option', 'buttons'  , btn)
			.dialog('open')
		;
		return this;
	};
	
	/**
	 * Contents dialog.
	 *
	 */
	$.fn.cit.dialog.contents = function( url)
	{
		if( 'undefined' == typeof url)
		{
			url = this.attr('href');
		}
		
		var div  = this.citDialog;
		var btn  = {
			'閉じる': function(){ $(div).dialog('close');}
		};
		
		$(div)
			.load  ( url + ' #for-dialog')
			.dialog('option', 'width'    , Math.ceil($(window).width() * 0.8))
			.dialog('option', 'resizable', true)
			.dialog('option', 'buttons'  , btn)
			.dialog('open')
		;
		return this;
	};
	
	/**
	 * Form dialog.
	 *
	 */
	$.fn.cit.dialog.form = function( url)
	{
		if( 1 > arguments.length){ url = this.attr('href');}
		
		var div  = this.citDialog;
		var btn  = {
			'閉じる': function(){ $(div).dialog('close');}
		};
		var form;
		
		$(div)
			.load  ( url + ' #for-dialog', {}, function(){ form = $('form',$(this));})
			.dialog('option', 'width'    , Math.ceil($(window).width() * 0.8))
			.dialog('option', 'resizable', true)
			.dialog('option', 'buttons'  , btn)
			.dialog('open')
		;
		return form;
	};
	
	/**
	 * iFrame dialog.
	 *
	 */
	 $.fn.cit.dialog.iframe = function( url)
	 {
		if( 1 > arguments.length){ url = this.attr('href');}
		
		var div  = this.citDialog;
		var btn  = {
			'閉じる': function(){ $(div).dialog('close');}
		};
		var iframe = $('<iframe frameborder="0" width="100%" height="99%"></iframe>').attr('src', url);
		
		$(div)
			.append( iframe)
			.dialog('option', 'title'  , url)
			.dialog('option', 'width'  , Math.ceil($(window).width()  * 0.8))
			.dialog('option', 'height' , Math.ceil($(window).height() * 0.8))
			.dialog('option', 'resizable', true)
			.dialog('option', 'buttons', btn)
			.dialog('open')
		;
		return this;
	 };

	/**
	 * iFrame222 dialog.
	 *
	 */
	 $.fn.cit.dialog.iframe222 = function( url)
	 {
		if( 1 > arguments.length){ url = this.attr('href');}
		
		var div  = this.citDialog;
		var btn  = {
			'閉じる': function(){ $(div).dialog('close');}
		};
		var iframe = $('<iframe frameborder="0" width="100%" height="99%"></iframe>').attr('src', url);
		
		$(div)
			.append( iframe)
			.dialog('option', 'title'  , url)
			.dialog('option', 'width'  , Math.ceil($(window).width()  * 0.9))
			.dialog('option', 'height' , Math.ceil($(window).height() * 0.9))
			.dialog('option', 'resizable', true)
			.dialog('option', 'buttons', btn)
			.dialog('open')
		;
		return this;
	 };
// end of closure, bind to jQuery Object
})(jQuery);

