var PricingFeaturesInfo;
(function($){
    PricingFeaturesInfo = {
        elID: "feature-tooltip",
        linkEl: "",
        offset: {
            left: 50,
            top: 0
        },
        
        build: function(data){
            var self = this;
            
            this.el.hide().html(data).css({
                left: this.offset.left,
                top: $(this.linkEl).offset().top - $('#content').offset().top - this.el.outerHeight() + this.offset.top
            }).show();
            $('#feature-tooltip a.featureClose').bind('click', function(event){
                event.preventDefault();
                self.el.hide();
            });
            Cufon.refresh('#feature-tooltip .featureDetails h5.cufon_condensed');
        },
        
        show: function(url, callback){
            var self = this;
            
            $.get(url, function(data){
                callback();
                self.build(data);
            });
        },
        
        initialize: function(){
            var self = this;
            this.links = $('#features_list a.link-question');
            
            if(!$('#' + this.elID).length){
                var domElem = document.createElement('DIV');
                    domElem.id = this.elID;
                    domElem.style.display = "none";
                $('#content').append(domElem);
                
                this.el = $('#' + this.elID);
            }
            
            this.links.bind('click', function(event){
                event.preventDefault();
                
                var el = $(this);
                var img = el.find('img');
                var oldSrc = img.attr('src');
                
                img.attr('src', oldSrc.replace('icon_question.png', 'indicator_16x16.gif'));
                
                var callback = function(){
                    img.attr('src', oldSrc);
                };
                
                if(self.linkEl == this){
                    self.el.show();
                    callback();
                } else {
                    self.linkEl = this;
                    self.show(this.href, callback);
                }
            });
        }
    };

	$(document).ready(function(){
        PricingFeaturesInfo.initialize();
        
	    $('.btnDownload').each(function(ind){
			$(this).attr({
				href: '#become_a_member_modal',
				title: this.href
			}).bind('click', function(event){
				$('#input_up-to-date_destination').val(this.title);
				$('#stay_up_to_date_modal_form').attr({
					target: (this.title.indexOf('wordpress') != -1 ? '_blank' : "")
				});
			}).fancybox({
				padding: 0,
				hideOnOverlayClick: false,
				showCloseButton: false,
				titleShow: false,
				showNavArrows: false,
				autoScale: false
			});
		});
        
        $('#input_notifications_email').bind('focus', function(event){
            $(this).css({
                color: '#333'
            });
        }).bind('blur', function(event){
            if(this.value == this.defaultValue || this.value.replace(/\s/,"").length == 0){
                this.style.color = "";
            }
        });
        
        $('#become_a_member_modal .submit-row input[type="image"]').bind('click', function(event){
            this.blur();
        });
        
        $('#become_a_member_modal_form').bind('submit', function(event){
            event.preventDefault();
            $.ajax({
                type: 'post',
                url: '/email-update',
                data: 'email=' + $('#input_notifications_email').val(),
                success: function(data){
                    if(data == "true"){
    					$.fancybox.close();
                        document.location.href = $('#input_up-to-date_destination').val();
                    } else {
                        $('#become_a_member_modal .error.message').show();
                    }
                }
            });
        });
	});
})(jQuery);