var Profilemon = {
  birthday: {
    daySelect: {},
    monthSelect: {},
    yearSelect: {},
    init: function(){

      var me = this;

      me.daySelect = jQuery('#i_birthday_day');

      me.daySelect.attr('current', me.daySelect.attr('selectedIndex'));
      me.daySelect.change(function() {
        var t = jQuery(this);
        if (t.find('option:selected').eq(0).attr('disabled')) t.attr('selectedIndex', t.attr('current'));
        t.attr('current', t.attr('selectedIndex'));
      });

      me.monthSelect = jQuery('#i_birthday_month');
      me.yearSelect = jQuery('#i_birthday_year');

      if (me.daySelect.length <= 0 || me.monthSelect.length <= 0 || me.yearSelect.length <= 0) return;

      me.monthSelect.change(me.seNoOfDays);
      me.yearSelect.change(me.seNoOfDays);

      me.seNoOfDays();

    },

    seNoOfDays: function(){

      var me = Profilemon.birthday;

      var year = parseInt(me.yearSelect.val());
      var month = parseInt(me.monthSelect.val(),10);
      var day = parseInt(me.daySelect.val(),10);

      var daysInMonth = 32 - new Date(year, month - 1, 32).getDate();
      
      var opts = me.daySelect.find('option');
      opts.attr('disabled', 'disabled').css({ color: 'graytext' });
      me.daySelect.find('option').slice(0, parseInt(daysInMonth) + 1).removeAttr('disabled').css({ color: 'menutext' });
    }
  },

  switchSection: {
    root: {},
    init: function(){

      var me = this;

      me.root = jQuery('.switchSection');

      me.root.find('.tabs a').click( me.switchTab );

    },
    switchTab: function(e){

      if((jQuery(e.target).parent()).hasClass('active')) { return; }

      var me = Profilemon.switchSection;
      var clickedElem = jQuery(e.target);

      me.root.find('.active').removeClass('active');
      clickedElem.parent().addClass('active');
      me.root.find('#'+clickedElem.attr('rel')).addClass('active');
    }
  },

  photoSection: {
    root: {},
    init: function(){

      var me = this;

      me.root = jQuery('.photoSection');

      me.root.find('.zwin').click( me.closeForm );
      me.root.find('.iCancel').click( me.cancel );
      me.root.find('.rozwin').click( me.openForm );
      me.root.find('.edytuj').click( me.openForm );
      me.root.find('.serwisy img').click( me.changeSerwis );

    },

    cancel: function() {
      var me = Profilemon.photoSection;
      var uv = jQuery("#i_photoSerwis_login").get(0);
      if (uv.value == '') {
        me.closeForm();
      } else {
        uv.value = '';
      }
    },

    openForm: function(){

      var me = Profilemon.photoSection;

      me.root.find('.rozwin').addClass('hidden');
      me.root.find('.zwin').removeClass('hidden');
      me.root.find('.formularz').removeClass('hidden');
      me.root.find('.middle').addClass('hidden');
    },
    closeForm: function(){

      var me = Profilemon.photoSection;

      me.root.find('.zwin').addClass('hidden');
      me.root.find('.rozwin').removeClass('hidden');
      me.root.find('.formularz').addClass('hidden');
      me.root.find('.middle').removeClass('hidden');
    },
    changeSerwis: function(e){

      if((jQuery(e.target).parent()).hasClass('active')) { return; }

      var me = Profilemon.photoSection;
      var serwisName = jQuery(e.target).attr('alt');

      me.root.find('.serwisy li').removeClass('active');
      jQuery(e.target).parent().addClass('active');
      me.root.find("input[name=photoSerwis]").val(serwisName);
      me.root.find("label[for=i_photoSerwis_login] span").html(serwisName);
    }
  },

  blogSection: {
    root: {},
    init: function(){

      var me = this;

      me.root = jQuery('.blogSection');
      me.root.find('.zwin').click( me.closeForm );
      me.root.find('.iCancel').click( me.cancel );
      me.root.find('.rozwin').click( me.openForm );
      me.root.find('.edytuj').click( me.openForm );

    },

    cancel: function() {
                  var me = Profilemon.blogSection;
                  var bv = jQuery("#blogUrl").get(0);
                  if (bv.value == "") {
                                  me.closeForm();
                  } else {
                                  bv.value = "";
                  }
    },

    openForm: function(){

      var me = Profilemon.blogSection;

      me.root.find('.rozwin').addClass('hidden');
      me.root.find('.zwin').removeClass('hidden');
      me.root.find('.formularz').removeClass('hidden');
      me.root.find('.middle').addClass('hidden');
    },
    closeForm: function(){

      var me = Profilemon.blogSection;

      me.root.find('.zwin').addClass('hidden');
      me.root.find('.rozwin').removeClass('hidden');
      me.root.find('.formularz').addClass('hidden');
      me.root.find('.middle').removeClass('hidden');
    }
  },

  removeProfile: {
    $overlay: null,
    initialized: false,

    init: function() {
      jQuery('#profilEditPage #btnRemoveProfile').click(function() {

        var
          g = window.gazeta_pl || {},
          msg = g.removeProfileMessage || '<h1>Usuwanie profilu</h1><div class="body"><p>Czy wiesz, \u017ce usuni\u0119cie profilu spowoduje <b>skasowanie</b>:</p><ul><li>- wszystkich Twoich zdj\u0119\u0107</li><li>- wszystkich wiadomo\u015bci prywatnych w profilu</li><li>- wszystkich znajomych z Twojej listy znajomych</li></ul><p>Twoje konto Gazeta.pl, poczta, blogi, wpisy i komentarze oraz zdj\u0119cia na Fotoforum <b>nie zostan\u0105</b> usuni\u0119te.</p></div><div class="footer"><a href="javascript:void(0)" id="btnRemoveProfileCancel">Nie chc\u0119 kasowa\u0107 profilu</a><a href="%href%">Chc\u0119 skasowa\u0107 profil</a></div></div>';

        msg = msg.replace(/%href%/g, jQuery(this).attr('href'));
        
        jQuery('body').append(Profilemon.removeProfile.$overlay = jQuery('<div id="profileRemoveOverlay"><div class="bg"></div><div class="window">' + msg + '</div>'));
        jQuery('#btnRemoveProfileCancel').click(function() {
          Profilemon.removeProfile.$overlay.remove();
        });

        Profilemon.removeProfile.$overlay.find('.bg').css({ opacity: 0.5 });

        if (!Profilemon.removeProfile.initialized) {
          Profilemon.removeProfile.initialized = true;
          jQuery(window).resize(Profilemon.removeProfile.onResize);
        }

        jQuery(window).trigger('resize');

        return false;
      });
    },

    onResize: function() {
      var
        l = 0,
        t = 0,
        $p = null;

      if (!Profilemon.removeProfile.$overlay.length) return;

      l = Math.max(0, Math.floor((jQuery(window).width() - ($p = Profilemon.removeProfile.$overlay.find('.window')).outerWidth()) / 2));
      t = Math.max(0, Math.floor((jQuery(window).height() - $p.outerHeight()) / 2));

      Profilemon.removeProfile.$overlay.css({ left: l, top: t });
    }
  },

  init: function() {
    if (Profilemon.isInit) return;

    Profilemon.birthday.init();
    Profilemon.switchSection.init();
    Profilemon.photoSection.init();
    Profilemon.blogSection.init();
    Profilemon.removeProfile.init();
    Profilemon.isInit = true;
  },

  isInit: false
};

if (typeof(jQuery) == "function") {
    jQuery(document).ready( Profilemon.init );
};

function submitPaging(formName, currentPage, pageStep) {
  if (document.forms[formName].page != null) {
    document.forms[formName].page.value = currentPage;
  };
  if (pageStep != null)
    document.forms[formName].pageStep.value = pageStep;
  else
    document.forms[formName].pageStep.value = 0;
  document.forms[formName].submit();
};

function manageFriends(msg, nick, formName) {
  if (confirm(msg.replace(/%nick%/g, nick))) {
    var f = jQuery('form[name='+formName+']');
    f.find('[name=nick]').val(nick);
    f.submit();
  };
};

function acceptFriend(nick) { 
    var f = jQuery('form[name=acceptFriendF]');
    f.find('[name=nick]').val(nick);
    f.submit();
}
function declineFriend(nick) { manageFriends('Odrzucić zaproszenie od %nick%?', nick, 'declineFriendF'); };
function removeFriend(nick) { manageFriends('Jesteś pewny, że chcesz usunąć %nick% ze znajomych?', nick, 'removeFriendF'); };
function removeInvitation(nick) { manageFriends('Cofnąć zaporszenie dla %nick%?', nick, 'removeInvitationF'); };

function textCounter(field, maxlimit) {
if (field.value.length > maxlimit){
  field.value = field.value.substring(0, maxlimit);
  alert("Uwaga, maksymalna długość ("+maxlimit+" znaków) treści została przekroczona!");
}
}

