/**
*  jquery.popupt
*  (c) 2008 Semooh (http://semooh.jp/)
*
*  Dual licensed under the MIT (MIT-LICENSE.txt)
*  and GPL (GPL-LICENSE.txt) licenses.
*
**/
// this code is in googlecode.com, url is:
// http://code.google.com/p/jquery-form-observe/
(
	function($)
	{
		$.fn.extend
		(
			{
				FormObserve: function(opt)
				{
					opt = $.extend
					(
						{
							changeClass: "inputContentChanged",
							filterExp: "",
							msg: "Unsaved changes will be lost.\nReally continue?"
						}
						, opt || {}
					);

					var fs = $(this);
					fs.each
					(
						function()
						{
							this.reset();
							var f = $(this);
							var is = f.find(':input');
							f.FormObserve_save();
							setInterval
							(
								function()
								{
									is.each
									(
										function()
										{
											var node = $(this);
											var def = $.data(node.get(0), 'FormObserve_Def');
											if(node.FormObserve_ifVal() == def)
											{
												if(opt.changeClass) node.removeClass(opt.changeClass);
											}
											else
											{
												if(opt.changeClass) node.addClass(opt.changeClass);
											}
										}
									);
								}
								,
								1000
							);
						}
					);

					function beforeunload(e)
					{
						var changed = false;
						fs.each
						(
							function()
							{
								if($(this).find(':input').FormObserve_isChanged())
								{
									changed = true;
									return false;
								}
							}
						);
						
						if(changed)
						{ 
							//~ try
							//~ {//colorbox prompt for use to chose to stay on page or not 
									//NOTE: sorry but I(zf) failed to implement this method
									//please refer to following link, seems it's impossible not working with the default dialog. 
									//http://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own
								//~ var hintinfo = opt.msg; 
								//~ htmlinfo = '<div><a href="#" class="close" style="top:1px;">X</a></div> '+
												//~ "<div>&nbsp;</div>"+
												//~ '<div class="shorthintinfo" style="padding-right:23px;">'+
													//~ hintinfo +
												//~ '</div> '; 
								//~ $.fn.colorbox({ 
									//~ html: htmlinfo ,  
									//~ maxWidth:"400",
									//~ maxheight:"400",
									//~ height:400,
									//~ transition:"none",
									//~ opacity:0.17,
									//~ scrolling:false, 
									//~ onCleanup:function()
									//~ {   
										//~ try
										//~ {	
											 
										//~ }
										//~ catch(e)
										//~ {
											 //~ alert(e);
										//~ }
									//~ } 
								//~ })
							//~ }
							//~ catch(e)
							//~ { 
								//~ alert(e);
							//~ }
							//~ return false;
							try
							{//js embedd prompt for use to chose to stay on page or not
								// add a judgement to judge whether this is a submit action of a form, it so, we should NOT give prompt.								
								if(formsubmitting==0)
								{//if not form submitting, we show prompt
									e = e || window.event;
									e.returnValue = opt.msg;//this one works fine in ie6 and ff3.6, but not working in chrome 
									return opt.msg ; //while this one works fine in chrome
								}
								else
								{//else if form submitting, actually we can just do nothing.
									formsubmitting==0;
									//~ return 
								}
							}
							catch(e)
							{ 
								//~ alert(e);
							}
							finally
							{
								//~ return opt.msg ; //make sure the msg show!
							}
							
							//~ try
							//~ {//js embedd prompt for use to chose to stay on page or not
								//~ e = e || window.event;
								//~ e.returnValue = false//this one works fine in ie6 and ff3.6, but not working in chrome 
								//~ return false; //while this one works fine in chrome
							//~ }
							//~ catch(e)
							//~ { 
								//~ alert(e);
							//~ }
							
						}
					}
					$(window).bind('beforeunload', beforeunload );//jquery way
					//~ if(window.attachEvent)
					//~ {
						//~ window.attachEvent('onbeforeunload', beforeunload);
					//~ }
					//~ else if(window.addEventListener)
					//~ {
						//~ window.addEventListener('beforeunload', beforeunload, true);
					//~ }
				},
				FormObserve_save: function()
				{
					var node = $(this);
					if(node.is('form'))
					{
						node.find(':input').each(function(){
							$(this).FormObserve_save();
						});
					}
					else if(node.is(':input'))
					{
						$.data(node.get(0), 'FormObserve_Def', node.FormObserve_ifVal());
					}	
				},
				FormObserve_isChanged: function()
				{
					var changed = false;
					this.each(function() 
					{
						var node = $(this);
						if(node.eq(':input'))
						{
							var def = $.data(node.get(0), 'FormObserve_Def');
							if(typeof def != 'undefined' && def != node.FormObserve_ifVal())
							{
								changed = true;
								return false;
							}
						}
					});
					
					return changed;
				},
				FormObserve_ifVal: function()
				{
					var node = $(this.get(0));
					if(node.is(':radio,:checkbox'))
					{
						var r = node.attr('checked');
					}
					else if(node.is(':input'))
					{
						var r = node.val();
					}
					return r;
				}
			}
		);
	}
)(jQuery);

