/**
 * Changing the default BlueStep Connect format.
 * Dan Rasband (danrasband@gmail.com)
 * 2/26/2011
 */

/**
 * Set global variable to show that this page uses this javascript file.
 */
var javascript_hacked_format = true;


/**
 * On ready, perform hacks.
 */
$(function() {
  custom_hack_format();
  custom_append_slider_script();
  add_desturl();
  move_footer();
  add_sidebar_animations();
  replace_copyright_year();
});

/**
 * Remove empty <p>s
 */
function custom_hack_format() {
  $('p').each(function() { if ($(this).html() == '') { $(this).remove(); } });
  $('table').attr('cellpadding', 0);
  var tables = $.makeArray($('body').children('table'));
  $(tables[0]).find('td').attr('style', 'margin:0;padding:0;');
  var html = $(tables[0]).html();
  html = html.replace(/<br>/i, '');
  $(tables[0]).html(html);
  $('div#footer').hide();
  var subTables = $.makeArray($('#contentArea').children('table'));
  $(subTables[0]).attr('style', 'position: absolute;top:40px;right:40px;');
}


/**
 * Append slider script.
 */
function custom_append_slider_script() {
  var script = $('<script />');
  script.attr({
    src: '/download/123321_U137143__740555/slider.js',
    type: 'text/javascript'
  });
  $('body').append(script);
}

/**
 * Add desturl to login link.
 */
function add_desturl() {
  if ($('.login-link').size() > 0) {
    href = $('.login-link').attr('href');
    href += '&desturl=' + escape(document.location.pathname + get_params_string());
    $('.login-link').attr('href', href);
  }
}

/**
 * Get all params.
 */
function get_params_string() {
  var regexS = "(\\?.*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if (results && results.length > 1) {
    return results[1];
  }
  else {
    return '';
  }
}

/**
 * Move footer to bottom.
 */
function move_footer() {
  var cloned = $('#custom-page-footer').clone();
  $('#custom-page-footer').remove();
  $('#contentArea').after(cloned);
  $('#contentArea').append('<div class="clearfix"></div>');
}

/**
 * Add sidebar animations.
 */
function add_sidebar_animations() {
  $('.sidebar-menu li.menu-item-motion span')
    .mouseover(function() {
      $(this).stop().animate({
        paddingLeft: 25
      }, 300, 'swing', function() {
        $(this).css('background', 'transparent url(/download/123321_U137143__740545/circled-arrow-right-darker.png) no-repeat 0 0');
      });
    })
    .mouseout(function() {
      $(this)
        .stop()
        .css('background', 'none')
        .animate({
          paddingLeft: 0
        }, 300, 'swing');
      /*
      .css({
        background: 'none',
        padding: 0
      });
      */
    });
}

/**
 * Replace copyright year (span class="copyright-year-replace")
 */
function replace_copyright_year() {
  var d = new Date();
  $('.copyright-year-replace').html(d.getFullYear());
}
