(function($){

    $.fn.inlineFieldLabel = function(options) {
        var opts = $.extend({}, $.fn.inlineFieldLabel.defaults, options);
        // Only textarea ant input text
        this.each(function(){
          $this = $(this);
          var o = $.metadata ? $.extend({}, opts, $this.metadata({type:opts.metadataType, name:opts.metadataName})) : opts;
          innerFunction($this,  o.label);

        });
        // Chain:
        return this;

    };
    // plugin defaults
    $.fn.inlineFieldLabel.defaults = {
      label : 'some placeholder',
      metadataType: 'class',
      metadataName: 'metadata'
    };

    //private function
    function innerFunction(jqElement, fieldLabel) {
       var textInput = null;
        var clonedTextInput = null;
        var strBefore = "";
        var strAfter = "";
        var counter = 0;
        textInput = jqElement;

       /* add the field label css class to the form field and set the value */


       if (textInput.attr('type') == 'password') {
         clonedTextInput = textInput.clone();
         strBefore = clonedTextInput.append(textInput.clone()).html();
         strAfter = strBefore.replace('type="password"', 'type="text"');;
         strAfter.replace('type="password"', 'type="text"');
         textInput.after(strAfter);
         clonedTextInput = textInput.next();
         clonedTextInput.addClass("intra-field-label").val(fieldLabel);
         textInput.hide();
       } else {
         textInput.addClass("intra-field-label").val(fieldLabel);
       }

      if(clonedTextInput != null) {
          clonedTextInput.focus(function() {
          textInput.show();
          textInput.trigger('focus');
          clonedTextInput.hide();
        });
      }



       /* remove the placeholder string when field gets focus */
       textInput.focus(function()
        {

           if(this.value == fieldLabel )
           {

              textInput.removeClass("intra-field-label").val("");
           };


        });

       /* add the field label string when field looses focus */
       textInput.blur(function()
        {

           if(this.value == "")
           {
             if(clonedTextInput != null) {
               textInput.hide();
               clonedTextInput.show();
             }
             else {
               textInput.addClass("intra-field-label").val(fieldLabel );
             }

           };

        });
        /* if the field is set to the fieldLabel on submit, clear the field */
        textInput.parents('form:first').find('input[type="submit"]').click(function() {

            if (clonedTextInput != null) {
              textInput.show();
               clonedTextInput.remove();
            }
            if(textInput.val() == fieldLabel)
             {
                textInput.removeClass("intra-field-label").val("");
             };
         });

     }



})(jQuery);



$(document).ready(function(){
    // skrypt do rela
    $("a[rel='external']")
        .addClass("external")
        .click(function() {
            var href = $(this).attr('href');

            var prefix1 = "http://";
            var prefix2 = "https://";
            var new_href = href;
            if ((href.search(prefix1) == -1)&&(href.search(prefix2) == -1)) {
                new_href = prefix1 + href;
            }

            window.open(new_href);
            return false;
        })

    $('.newsletter_popup.deactive').live('click', function(){
        $('.newsletter_form').css('display', 'block');
        $(this).addClass('active');
        $(this).removeClass('deactive');
        $(this).blur();
        return false;
    });
    $('.newsletter_popup.active').live('click', function(){
        $('.newsletter_form').css('display', 'none');
        $(this).addClass('deactive');
        $(this).removeClass('active');
        $(this).blur();
        return false;
    });
    $('.newsletter').submit(function(){
        if($('#agreement:checked').length){
            return true;
        }
        else {
            $('.newsletter p').text('Musisz wyrazić zgodę na przetwarzanie Twoich danych.');
            return false;
        }
    });
});




