/* Pixel Fusion namespace */
pf = {}

/* Image slider Before / After - Function */
pf.slideSwitch = function(switchSpeed, slider) {
  var $active = $('.active', slider);

  if ( $active.length == 0 ) $active = $('.panel:last', slider);

  var $next =  $active.next('.panel').length ? $active.next('.panel')
     : $('.panel:first', slider);
  
  $active.animate({opacity: 0.0}, switchSpeed);
  $next.css({display: 'block'});
  
  $active.addClass('last-active');


  $next.css({opacity: 0.0})
      .addClass('active')
      .animate({opacity: 1.0}, switchSpeed, function() {
          $active.removeClass('active last-active');
      });
}

pf.runScripts = function() {
  
  if ($.browser.msie && $.browser.version.substring(0,1) == '9')
  {
    Cufon.set('engine', 'canvas');  // fixes cufon display issues in ie9
  }

  /* Cufon - Call */
  Cufon.replace('h1', {fontFamily: 'HelveNeue', wordSpacing: '-10px'});
  Cufon.replace('h1 strong', {fontFamily: 'HelveNeue', fontWeight: 'bold', wordSpacing: '-10px'});
  Cufon.replace('h1.big-title', {fontFamily: 'HelveNeue', wordSpacing: '-20px'});
  Cufon.replace('h1.big-title strong', {fontFamily: 'HelveNeue', fontWeight: 'bold', wordSpacing: '-20px'});
  Cufon.replace('h2', {fontFamily: 'HelveNeue', wordSpacing: '-10px'});
  Cufon.replace('h2 strong', {fontFamily: 'HelveNeue', fontWeight: 'bold', wordSpacing: '-10px'});
  Cufon.replace('h3', {fontFamily: 'HelveNeue', wordSpacing: '-5px'});
  Cufon.replace('h3 strong', {fontFamily: 'HelveNeue', fontWeight: 'bold', wordSpacing: '-5px'});
  Cufon.replace('h4', {fontFamily: 'HelveMenu', wordSpacing: '-5px', hover: true});
  Cufon.replace('h4 strong', {fontFamily: 'HelveMenu', fontWeight: 'bold', wordSpacing: '-5px'});

  Cufon.replace('#mainNav li ul li a', {fontFamily: 'HelveMenu', wordSpacing: '-5px', hover: true});
  Cufon.replace('.article dt', {fontFamily: 'HelveMenu', wordSpacing: '-5px', hover: true});
  Cufon.replace('.article .list-1 dt', {fontFamily: 'HelveNeue', wordSpacing: '-5px', hover: true});
  Cufon.replace('.article .list-3 dt', {fontFamily: 'HelveMenu', wordSpacing: '-5px', hover: true});
  Cufon.replace('#donation-meter', {fontFamily: 'HelveMenu', wordSpacing: '-5px', hover: true});
  Cufon.replace('#shaveometer', {fontFamily: 'HelveMenu', wordSpacing: '-5px', hover: true});
  Cufon.replace('#donation-target', {fontFamily: 'HelveNeue', wordSpacing: '-7px'});
  Cufon.replace('form legend', {fontFamily: 'HelveNeue', wordSpacing: '-10px'});
  Cufon.replace('p.btn', {fontFamily: 'HelveMenu', wordSpacing: '-10px'});

  /* Image slider Before / After - Call */
  $('.photo-slider').each(function(i) {
    $(this).attr('id', 'photoSlider'+i);
    setInterval("pf.slideSwitch(1000, '#"+$(this).attr('id')+"')", 4000 );
  });

  /* counter -Call */
  $(".max1000").counter({
        count: 'down',
        goal: 1000
  });
  if (!$.browser.opera) {
    $("input[type=file]:not(.gallery_file_upload)").filestyle({
      imageheight : 23,
      imagewidth : 95,
      width: 255
    });
  }
  $('form').jqTransform();
  $('.jqTransformRadio').each(function(){
    $('input[name="'+$(this).attr('rel')+'"]').each(function() {
      var id  = $(this).attr('id');
      //$('label[for="'+id+'"]').not('.style-2 label').css({'padding-top': '4px'});      
      $('.radio-inline label[for="'+id+'"]').parents('ul').find('li:first-child').not('.radio-inline li').css({'padding-top': '8px'});

    });
  });

  /* Transform email address in mailto */
  $('.email').each(function(){
    var email = ($(this).html().replace(/\[AT\]/gi, "@").replace(/\[DOT\]/gi, "."));
    $(this).html("<a href='mailto:" + email + "'>" + email + "</a>");
  });

  /* Table scrollable */
  if ( ($.browser.mozilla) && ($.browser.version.substr(0,1) < 2) ) {
    $('.article table tbody').not($('.shavees tbody')).not($('.classic tbody')).each(function() {
      var $height = 183;

      if($(this).height() > $height) {
          $(this).css({'overflow-y': 'auto', 'overflow-x': 'hidden', 'height': $height+'px'});
      }
    });
  } else {
    $('.article table').not($('.shavees')).not($('.classic')).each(function() {
      var $height = 207 ;
      if($(this).height() > $height) {
          $(this).wrap($('<div />').css({'overflow-y': 'auto', 'overflow-x': 'hidden', 'height': $height+'px', 'margin-right': '-15px'}));
      }
    });
  }
  $('div.comments').each(function() {
    var $height = 530;
    if($(this).height() > $height) {
      if ( ($.browser.msie) && ($.browser.version.substr(0,1) < 7) ) {
        $(this).css({'height': $height+'px'});
      } else {
          $(this).css({'max-height': $height+'px'});
      }
    }
  });

  if($('#formRegister').size()) {
	  $('.progress-bar').hide();
	  if ($('input:radio:checked').attr('id') == 'sf_guard_user_participation_type_1') {
		  $('.steps-4').show();
	  }
	  if ($('input:radio:checked').attr('id') == 'sf_guard_user_participation_type_3') {
		  $('.steps-3').show();
	  }

	  $('#sf_guard_user_participation_type_1').change(function(){
		  if($(this).attr('checked')) {
        $('#part_type_steps').html('<strong>Step 1 of 5:</strong> Personal details');
        Cufon.refresh();
			  $('.steps-4').show();
		  } else {
        $('#part_type_steps').html('<strong>Step 1 of 3:</strong> Personal details');
        Cufon.refresh();
			  $('.steps-4').hide();
		  }
	  });
	  $('#sf_guard_user_participation_type_3').change(function(){
		  if($(this).attr('checked')) {
			  $('.steps-3').show();
		  } else {
			  $('.steps-3').hide();
		  }
	  });
  }
  
  if ($.browser.msie) {
    $('#principal .article:last-child').css({'border-bottom':0});
    $('#principal .column-448-230 .article:last-child').css({'border-bottom':0});
    $('#principal .wide-column  .article:last-child').css({'border-bottom':0});
    $('#principal .narrow-column .article:last-child').css({'border-bottom':0});
    $('#principal .wrapper .wide-column .article:last-child').css({'border-bottom':0});
    $('#principal .wrapper .narrow-column .article:last-child').css({'border-bottom':0});
    $('#sidebar .article:last-child').css({'border-bottom':0});
  }
  if ($.browser.msie && parseInt($.browser.version) == 6) {
	$('form ul.checkbox li:first-child').css({'width': '230px'});
	$('form ul li:first-child').css({'padding-right':'15px','text-align':'right','width':'200px'});
	$('form input[type=password]').css({'width': '422px', 'border': '1px solid #DDE1E3', 'padding': '2px 5px', 'font-family': 'Arial, Verdana, sans-serif', 'font-size': '11px'});
	$('form input[type=text]').not('.comments-form dl input[type=text]').css({'width': '422px', 'border': '1px solid #DDE1E3', 'padding': '2px 5px', 'font-family': 'Arial, Verdana, sans-serif', 'font-size': '11px'});
  $('form li.profile-photo input[type=text]').css({'width': '255px'});
  $('form input#event_start_date').css({'width': '403px'});
  $('form input#event_end_date').css({'width': '403px'});
	$('form ul.style-1 input[type=password]').css({'width': '317px'});
	$('form ul.style-1 input[type=text]').css({'width': '317px'});
	$('#search input[type=text]').css({'width': '190px', 'float': 'left'});
	$('#search ul li:first-child').css({'width':'280px'});
	$('div#drop-us-a-line form input[type="text"]').css({'border':'1px solid #DDE1E3','font-family':'Arial,Verdana,sans-serif','font-size':'11px','padding':'2px 5px','width':'395px'});
	$('.commentlist ul li:first-child').css({'margin-left':'0'});
  }

  $('#fundraising_group_type_of_group').change(function() {
    var type_box = $(this);

    if (type_box.val() == 'Other')
    {
      $('#fundraising_group_type_other').show();
    } else {
      $('#fundraising_group_type_other').hide();
    }

    if (type_box.val() == 'Group of friends' || type_box.val() == 'Other')
    {
      $('.more.form_fields').closest('ul').hide();
      $('.less.form_fields').closest('ul').show();
    } else {
      $('.less.form_fields').closest('ul').hide();
      $('.more.form_fields').closest('ul').show();
    }
  }).change();
}

pf.preventMultiSubmit = function() {
  /* Disable the form submit button once the form is submitted
   * to prevent multiple form submissions.
   *
   * Note: here we are removing the disabled attribute because some forms are
   * ajax so they won't get refreshed and will remain disabled unless we remove
   * the disabled attribute.
   */
  $('input[type=submit]').removeAttr('disabled');
  $('button[type=submit]').removeAttr('disabled');

  $('form').submit(function () {
    $('input[type=submit]').attr('disabled', 'disabled');
    $('button[type=submit]').attr('disabled', 'disabled');
  });
}

$(function() {
  pf.runScripts();
  pf.preventMultiSubmit();
});

