/**
 * .cacheInCookie - Storm form values in cookies
 *
 * Version: 1.0
 * Updated: 2009-01-20
 *
 * Used to store form values (values of input type text, select a.so) in cookie
 **/

/**
 * Requirements:
 * - jQuery (John Resig, http://www.jquery.com/)
 * - cookie (Klaus Hartl, http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/)
 * - cookieJar (James Dempster (letssurf@gmail.com, http://www.jdempster.com/category/jquery/cookieJar/))
 * - selectBox (http://www.texotela.co.uk/code/jquery/select/)
 **/
    (function($) {
        $.fn.cacheInCookie = function(settings) {
            var config = {};
            if (settings) $.extend(config, settings);
            this.each(function() {
                if (settings && settings['storage']) {
                    var cookie_name = settings['storage'];
                } else {
                    var cookie_name = $(this).attr('storage');
                }
                if (typeof(cookie_name) == "undefined") return true;
                self.mCJ = $.cookieJar(cookie_name,{cookiePrefix: 'rcom_'});
                $.fn.cacheInCookie.restore(this);
                var el = this;
                $(this).submit($.fn.cacheInCookie.store);
                $(this).find(".cookiestore").click(function(){
                    $.fn.cacheInCookie.store(el);
                });
            });
            return this;
        };
        
        $.fn.cacheInCookie.restore = function(el){
            $(el+":input[class!=nocache]").each(function(i,item){
                switch(item.type){
                    case 'select-one':
                        var v= self.mCJ.get(item.id);
			            $(item).selectOptions(v);
                        break;
                    case 'text':
                        var v= self.mCJ.get(item.id);
			            $(item).val(v);
                        break;                
                    case 'hidden':
                        var v= self.mCJ.get(item.id);
			            $(item).val(v);
                        break;        
                }
            });
        };
        
        $.fn.cacheInCookie.store = function(t){
            if (typeof(t) == "undefined")  t = this;
            $(t+":input[class!=nocache]").each(function(i,item){
                 self.mCJ.set(item.id, $(item).val());	
            });
		};
		
		$.fn.cacheInCookie.destroy = function() {
		    self.mCJ.destroy();
		};
		
     })(jQuery);