/*
 * jQuery pin plugin
 * @requires jquery.cookie.js
 *
 * Copyright (c) 2007 Sadri Sahraoui (brainfault.com)
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Original version for prototype can be found here 
 * http://ajax.phpmagazine.net/2007/04/pin_plugin_for_prototype_remem.html
 *
 * Revision: $Id$
 * Version: 0.90
 */
jQuery.fn.pin = function() {

	var img_off = '<img src="/ncimg/disconnect16.png" class="off" border="0" title="設定内容を保存">';
	var img_on = '<img src="/ncimg/connect16.png" class="on" border="0" title="設定内容を破棄">';

	return this.each(function(){
		var id = $(this).attr('id');
		var input = $(this);

		$(this)
		.after('<a href="javascript:void(0)" id="pinv'+id+'" class="pin" style="text-decoration:none;padding-left:2px;">'+img_off+'</a>')
		.ready(function() {
			var val = $.cookie(id);
			if (val != null) {
				input.val($.cookie(id));
				input.next('a.pin').html(img_on);
			}
		})
		.change(function() {
			var img = input.next('a.pin').children('img');
			if (img.attr('class') == 'on') {
				$.cookie(id, input.val());
				$('.error').remove();
				$('#submitNotice').append('<span class="error">※保存情報を更新しました</span>');
			}
		})
		.next('a.pin')
		.click(function() {
			var img = $(this).children('img');
			if (img.attr('class') == 'off') {
				$(this).html(img_on);
				$.cookie(id, input.val());
			} else {
				$(this).html(img_off);
				$.cookie(id, null);
			}
		})
	});	
};
