// logging
var debugging = true; // or true

if(typeof console == "undefined")
{
  var console = { log: function(){} };
}
else if(!debugging || typeof console.log == "undefined")
{
  console.log = function(){};
}

$(document).ready(function(){
  
  // auto clear for sign in
  $('div.sidebox #username').focus(function(){
    $(this).val('');
  });
  
  $('div.sidebox #username').blur(function(){
    if($(this).val() == '')
    {
      $(this).val('USERNAME');
    }
  });
  
  $('div.sidebox #password').focus(function(){
    $(this).val('');
  });
  
  $('div.sidebox #password').blur(function(){
    if($(this).val() == '')
    {
      $(this).val('PASSWORD');
    }
  });
  
  // setu p discipline hover
  $('#disciplineSelector li').hover(
    function(){
      $(this).parent().addClass($(this).attr('id'));
    },
    function(){
      $(this).parent().removeClass($(this).attr('id'))
    }
  );
  
  // set one click buttons
  $('input.oneClick, button.oneClick').click(function(){
    $(this).attr('disabled', 'true');
    
    // SUBMIT PARENT FORM
    $(this).parents('form').submit();
  });
  
  /**
   * set up sliders
   */
  $('.sliderTrack').each(function(){
    
    if($(this).hasClass('numeric'))
    {
      var min = parseInt($($(this).find('div').get(0)).html());
      var max = parseInt($($(this).find('div').get(1)).html());
      
      // alert(min + '|' + max);
      
      // var min = 0;
      // var max = 5;
      
      $(this).slider({
        'step': 1,
        'min': min,
        'max': max,
        'value': $(this).find('input:first').val(),
        change: function(event, ui){
          $(this).find('input:first').val(ui.value)
        }
      });
    }
    else
    {
      $(this).slider({
        'step': 25,
        'min': 0,
        'max': 100,
        'value': $(this).find('input:first').val(),
        change: function(event, ui){
          $(this).find('input:first').val(ui.value)
        }
      });
    }
    
    if($(this).hasClass('locked'))
    {
      $(this).slider('disable');
    }
    
    // remove jq formatting
    $(this).removeClass('ui-widget-content');
    $(this).removeClass('ui-widget');
    
  });
  
  $(".lightbox").lightbox({
    fitToScreen: true
  });
  
  // accordions
  $('.accordionThis').accordion({
    collapsible: false,
    autoHeight: false
  });
  
  $('#membershipTypeSelect').accordion({
    collapsible: false,
    autoHeight: false,
    active: false
  });
  
  // assign click event to article edit address show/hide checkbox
  $('#articleUseAddress').click(function(){
    
    if($('#articleUseAddress').is(':checked'))
    {
      $('#articleAddress').show();
    }
    else
    {
      $('#articleAddress').hide();
    }
    
  });
  
  // setup date selector
  $('div.dateSelectorWrap select.dateSelector').change(function(){
    setDate(this);
  });
  
  // setup country selector
  $('.country_selector').change(function(){
    
    // get id
    var id = $(this).attr('id');
    
    getProvinces(id.substr(0, id.length - 11));
    
  });
  
  // get initial provinces
  $('.country_selector').each(function(){
    // get id
    var id = $(this).attr('id');
    
    getProvinces(id.substr(0, id.length - 11));
  });
  
  // alt billing address selector
  $('#alt_billing_address').click(function(){
    if(this.checked)
    {
      $('#billingAddressWrap').show();
    }
    else
    {
      $('#billingAddressWrap').hide();
    }
  })
  
  // clicks for homepage
  // set up click link for table rows
  $('ul.homepageListing.clickable > li').each(function(){
    
    var url = $(this).find('a').attr('href');
    
    $(this).click(function(){
      
      window.location = url;
      
    });
    
  });
  
  // set up bulletins
  $('ul#bulletinList li div.bulletinDetail').hide();
  
  $('ul#bulletinList li').click(function(){
    
    // collapse all bulletins
    $('ul#bulletinList li div.bulletinDetail').hide('fast');
    
    // expand this one
    $(this).find('div.bulletinDetail').show('fast');
    
  });
  
  
  
});

/*
  for date selector, updates hidden date field on update
*/
function setDate(select_box)
{
  // get name of input field to update
  var name = $(select_box).attr('id').split('-')[0];
  
  // alert(name);
  
  // get full date
  var date = $('#' + name + '-year').val() + '-' + $('#' + name + '-month').val() + '-' + $('#' + name + '-day').val();
  
  // alert(date);
  
  // update hidden field
  $('#' + name).val(date);
}

/*
  gets the list of provinces, or a text entry field
*/
function getProvinces(address_prefix)
{
  if($('#' + address_prefix + '_country_id').length == 1)
  {
    // disable submit
    $('input[type="submit"]').attr('disabled', 'disabled');
    
    // get country id
    var country_id = $('#' + address_prefix + '_country_id').val();

    // get address id
    var province_id = $('#' + address_prefix + '_hidden_province_id').val();
    
    // get other province
    var other_province = $('#' + address_prefix + '_hidden_other_province').val();

    $.get(
      '/profile/getProvinceSelect',
      {
        'id': country_id,
        'province_id': province_id,
        'other_province': other_province,
        'prefix': address_prefix
      },
      function(data){
        $('#' + address_prefix + 'ProvinceWrap').html(data);
        
        // enable submit
        $('input[type="submit"]').removeAttr('disabled');
      },
      'html'
    );
  }
}

/*
  saves the discipline cookie
*/
function saveDisciplineCookie(value)
{
  // save cookie
  $.cookie('primary_discipline', value, {path: '/'});
  
  // hide selector
  $('#disciplineSelectorWrap').hide(500);
  
  // show bypass button
  $('#landingBypassWrap').show(500);
}


/*
  survey
*/
function expandQuestion(index)
{
  $('#questionBrief_' + index).slideUp();
  $('#questionDetail_' + index).slideDown();
}

function hideQuestion(index)
{
  $('#questionBrief_' + index).slideDown();
  $('#questionDetail_' + index).slideUp();
}

