var IE = jQuery.browser.msie;

var profil_birthday = {
 daySelect: jQuery,
 monthSelect: jQuery,
 yearSelect: jQuery,
 init: function(){
  
  var me = this;
 
  me.daySelect = jQuery('#i_birthday_day');
  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 );
  
 },
 seNoOfDays: function(){

  var me = profil_birthday;

  var year = parseInt(me.yearSelect.val());
  var month = parseInt(me.monthSelect.val());
  var day = parseInt(me.daySelect.val());
  
  
  switch(month){
   case 2:
    if(year%4) {
     me.daySelect.find("option[value='29'], option[value='30'], option[value='31']").attr('disabled','disabled');
     me.daySelect.find("option[value='29'], option[value='30'], option[value='31']").attr('selected','');
     if(IE){
      me.daySelect.find('option').remove();
      for(var i = 1; i < 29; i++){
       me.daySelect.append('<option value="'+i+'">'+i+'</option>');
       if(day < 29) me.daySelect.find("option[value='"+day+"']").attr('selected','selected');       
      }
     }
    } else {
     me.daySelect.find("option[value='29']").attr('disabled','');
     me.daySelect.find("option[value='30'], option[value='31']").attr('disabled','disabled');
     me.daySelect.find("option[value='30'], option[value='31']").attr('selected','');
     if(IE){
      me.daySelect.find('option').remove();
      for(var i = 1; i < 30; i++){
       me.daySelect.append('<option value="'+i+'">'+i+'</option>');
       if(day < 30) me.daySelect.find("option[value='"+day+"']").attr('selected','selected');      
      }
     }
    }
    break;
   case 4:
   case 6:
   case 9:
   case 11:
    me.daySelect.find("option[value='29'], option[value='30']").attr('disabled','');
    me.daySelect.find("option[value='31']").attr('disabled','disabled');
    me.daySelect.find("option[value='31']").attr('selected','');
    if(IE){
     me.daySelect.find('option').remove();
     for(var i = 1; i < 31; i++){
      me.daySelect.append('<option value="'+i+'">'+i+'</option>');
      if(day < 31) me.daySelect.find("option[value='"+day+"']").attr('selected','selected');      
     }
    }
    break;
   default:
    me.daySelect.find("option[value='29'], option[value='30'], option[value='31']").attr('disabled','');
    if(IE){
     me.daySelect.find('option').remove();
     for(var i = 1; i < 32; i++){
      me.daySelect.append('<option value="'+i+'">'+i+'</option>');
      if(day < 32) me.daySelect.find("option[value='"+day+"']").attr('selected','selected');      
     }
    }
    break;
  }
 }
}

var profil_switchSection = {
 root: jQuery,
 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 = profil_switchSection;
  var clickedElem = jQuery(e.target);
  
  me.root.find('.active').removeClass('active');
  clickedElem.parent().addClass('active');
  me.root.find('#'+clickedElem.attr('rel')).addClass('active'); 
 }
}

var profil_photoSection = {
 root: jQuery,
 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 = profil_photoSection;
	var uv = $("#i_photoSerwis_login").get(0);
	if (uv.value == "") {
		me.closeForm();
	} else {
		uv.value = "";
	}
 },

 openForm: function(){
 
  var me = profil_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 = profil_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 = profil_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);
 }
}

var profil_blogSection = {
 root: jQuery,
 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 = profil_blogSection;
        var bv = $("#blogUrl").get(0);
        if (bv.value == "") {
                me.closeForm();
        } else {
                bv.value = "";
        }
 },

 openForm: function(){
 
  var me = profil_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 = profil_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');
 }
}

jQuery(document).ready(function() {

 try { profil_birthday.init(); } catch(e) { alert(e); }
 try { profil_switchSection.init(); } catch(e) { alert(e); }
 try { profil_photoSection.init(); } catch(e) { alert(e); }
 try { profil_blogSection.init(); } catch(e) { alert(e); }

});

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 acceptFriend(nick){
	var f = document.forms['acceptFriendF'];
	if( confirm('Dodać "'+nick+'" do znajomych ?')){
		f.nick.value=nick;
		f.submit();
	}
}

function declineFriend(nick){
	var f = document.forms['declineFriendF'];
	if( confirm('Odrzucić zaproszenie od "'+nick+'" ?')){
		f.nick.value=nick;
		f.submit();
	}
}

function removeFriend(nick){
	var f = document.forms['removeFriendF'];
	if( confirm('Jeste¶ pewny, że chcesz usun±ć "'+nick+'" ze znajomych ?')){
		f.nick.value=nick;
		f.submit();
	}
}

function removeInvitation(nick){
	var f = document.forms['removeInvitationF'];
	if( confirm('Cofn±ć zaproszenie dla "'+nick+'" ?')){
		f.nick.value=nick;
		f.submit();
	}
}


