/*
 * 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 = '&nbsp;<img src="images/pin.gif" class="off" border="0" alt="remember me">';
var img_on = '&nbsp;<img src="images/pin-h.gif" class="on" border="0" alt="remember me">';
	return this.each(function(){
		var id = $(this).attr('id');
		var input = $(this);
		$(this)
        .after('<a href="#" id="pinv'+id+'" class="pin" style="text-decoration:none;">'+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());
                //alert('cookie seted onchange');
            }
		})
		.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);
			}
		})
	});	
};