/*
 * jQuery UI Watermark @VERSION
 *
 * Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://docs.jquery.com/UI/Watermark
 *
 * Depends:
 *	ui.core.js
 */
(function($){

    $.widget("ui.watermark", {
    
        _init: function(){

            var o = this.options, self = this;
            var input = this.element;
			var _this = this;

			if($(input).css('display')=='none'){
				setTimeout(waitStyle(input), 100);
			}else{
				create();
			}

			function waitStyle(input){
				if (parseInt($(input).css('paddingTop')) != 0) {
					create();
				}else{
					setTimeout(function(){waitStyle(input)}, 500);
				}
			}            
			
			function create(){
				
				var placeholder = $.isFunction(o.placeholder) ? o.placeholder.apply(_this.element[0]) : o.placeholder;
				_this.element.wrap("<span/>").parent().addClass("ui-watermark-container ui-watermark-" + input[0].tagName.toLowerCase());
				var label = (_this.label = $('<label for="' + input.attr("id") + '">' + placeholder + '</label>').insertBefore(input));
				label.addClass("ui-watermark-label");
				
				
				label.css({
					left: parseInt(input.css("borderLeftWidth")) + parseInt(input.css("paddingLeft")),/* + parseInt(input.css("marginLeft"))*/
					top: parseInt(input.css("borderTopWidth")) + parseInt(input.css("paddingTop"))/* + parseInt(input.css("marginTop"))*/
				//TODO в ие не для всех инпутов работает css(margin)
				});
				
				
				if (input.val()) {
					label.hide();
				}
				
				var observer = $.isFunction(o.observer) ? o.observer.apply(_this.element[0]) : input.hasClass(o.observer) ? true : false;
				
				input.bind("focus." + _this.widgetName, function(){
					if (observer) {
						var other_input = input.parents('form').find('input[type="password"]');
						this.otimer = setInterval(function(){
							other_input.each(function(){
								if ($(this).val() != '') 
									$(this).trigger("forcibly");
							})
						}, o.timer);
					}
					if (!o.disabled && !_this.value) {
						o.animate ? label.fadeOut("fast") : label.hide();
					}
				}).bind("blur." + _this.widgetName, function(e){
					clearInterval(this.otimer);
					if (!o.disabled && (!this.value)) {
						o.animate ? label.fadeIn("fast") : label.show();
					}
				}).bind("forcibly." + _this.widgetName, function(e){
					if (!o.disabled) 
						label.hide();
				})
				
			}
            
        },
        
        destroy: function(){
            if (this.element.data("placeholder")) {
                this.element.attr("placeholder", this.element.data("placeholder"));
            }
            // TODO remove this once replaceWith handles a disconnected element (see core #3940)
            if (this.element.parent().parent().length) 
                this.element.parent().replaceWith(this.element);
            this.label.remove();
            $.widget.prototype.destroy.apply(this, arguments);
        }
    });
    
    
    $.extend($.ui.watermark, {
        version: "@VERSION",
        defaults: {
            placeholder: function(){
                var result = $(this).attr("title");
                $(this).data("placeholder", result);
                $(this).removeAttr("placeholder");
                return result;
            },
            observerClass: '',
            animate: true,
			timer: 100
        }
    });
    
})(jQuery);







