
function disableProfileFeature(oElement,featureName) {
  if (oElement.checked == true) {
    $(featureName).addClassName('disabled');
    $(featureName).select('select')[0].disabled = true;
    $(featureName).select('select')[0].setStyle({fontStyle: 'italic'});
  } else if (oElement.checked == false) {
    $(featureName).removeClassName('disabled');
    $(featureName).select('select')[0].disabled = false;
    $(featureName).select('select')[0].setStyle({fontStyle: 'normal'});  
  }
}

function showAdditionalSport() {
  var hiddenSportsCount = $$('.additional').findAll(function(e){return e.visible() == false}).length;
  $$('.additional').find(function(e){return e.visible() == false}).show();  
  $$('.remove_sport').invoke('hide');
  $('remove_additional_sport_' + (6 - hiddenSportsCount)).show();
  if (hiddenSportsCount <= 1) $('add_sport').hide();
}

function hideAdditionalSport(index) {
  var hiddenSportsCount = $$('.additional').findAll(function(e){return e.visible() == false}).length;

  if($('additional_sport_field_' + index)) {
    $('additional_sport_field_' + index).hide();
    $('profile_additional_sport_' + index).value = '';
  }

  if (hiddenSportsCount < 5) {
    if (tmp = $('remove_additional_sport_' + (4 - hiddenSportsCount))) {
      tmp.show();
    }
  }
  $('add_sport').show();
}

function previewLogo(logoName) {
  $('logo').setStyle({
    backgroundImage: 'url(/images/logos/' + logoName + '.png)'
  });
}

function changeBackground() {
  return changeBackgroundSetting;
}

function changeTheme(themeName,changeBackground) {
  if ($('theme'))
    $('theme').value = themeName;
  var bgcolor = MIP_THEMES[themeName].backgroundColor;
  $('page_body').setStyle({
    backgroundImage: (changeBackground == true) ? 'url(/images/themes/' + themeName + '/background.jpg)' : '',
    backgroundRepeat: (changeBackground == true) ? 'repeat-x' : 'repeat',
    backgroundColor: '#' + bgcolor
  });
  $('header').setStyle({
    backgroundImage: 'url(/images/themes/' + themeName + '/banner.jpg)',
    backgroundColor: '#' + MIP_THEMES[themeName].primaryColor
  });
  $('tabs').setStyle({
    backgroundColor: '#' + MIP_THEMES[themeName].secondaryColor
  });
  $$('ul#tabs li').each(function(e) { e.setStyle({ 
      backgroundColor: '#' + MIP_THEMES[themeName].primaryColor,
      color: '#' + MIP_THEMES[themeName].primaryText
      });
    });
  $$('ul#tabs li.active').each(function(e) { e.setStyle({ backgroundColor: 'transparent' }); });
  $('panel').setStyle({
    borderColor: '#' + MIP_THEMES[themeName].secondaryColor
  });
  $$('#breadcrumb a').each(function(e) { e.setStyle({ color: '#' + MIP_THEMES[themeName].primaryColor }); });  
}

function previewTheme(themeName,changeBackground) {
  $$('#themes .wrapper').each(function(e) { e.removeClassName('selected'); });
  $(themeName + '_thumb_wrapper').addClassName('selected');
  changeTheme(themeName,changeBackground);
}

function restoreTabs(oTheme) {
  changeTheme(oTheme,changeBackground());
  $$('.tab').each(function(e) { e.setStyle({ backgroundColor: ''}); });
}

function useThemeBackground(themeName) {
  var bgcolor = MIP_THEMES[themeName].backgroundColor;
  $('upload').hide();
  $('sample').hide();
  $('custom_change').hide();
  changeBackgroundSetting = true;
  $('page_body').setStyle({
    backgroundImage: 'url(/images/themes/' + themeName + '/background.jpg)',
    backgroundRepeat: 'repeat-x',
    backgroundColor: '#' + bgcolor
  });

}

function setCustomBackground(customImageURL) {
  $('page_body').setStyle({
    backgroundRepeat: 'repeat', 
    backgroundImage: 'url(' + customImageURL + ')'
  });
}

function useCustomBackground(customImageEmpty,customImageURL) {
  changeBackgroundSetting = false;
  if (customImageEmpty == false) {
    $('custom_change').show();
    $('sample').show();
    setCustomBackground(customImageURL)
  } else {
    $('upload').show();
  }
}

function onEndCrop(coords, dimensions) {
  $('x1').value = coords.x1;
  $('y1').value = coords.y1;
  $('x2').value = coords.x2;
  $('y2').value = coords.y2;
  $('width').value = dimensions.width;
  $('height').value = dimensions.height;
}

function initEditThumbnail() {
  var thumbnailEdit = function(){
    new Cropper.ImgWithPreview('portraitImage', {
      previewWrap: 'portraitThumbnail',
      minWidth: 73,
      minHeight: 73,
      ratioDim: { x: 73, y: 73 },
      onEndCrop: onEndCrop
    });
  }
  thumbnailEdit();
}

function switchTabHiLite(oTab) {
  if ($(oTab).id != 'appearance' && $('original_theme') != null) {
    restoreTabs($F('original_theme'));
  }
  $$('.tab').invoke('removeClassName','active');
  $(oTab).addClassName('active');
}

$(tabID).addClassName('active'); 
$$('.tab').each( function(oTab) { 
  $(oTab).observe('click', function() { 
    switchTabHiLite($(this));
  }); 
}); 


