/* IE Cleartype fix*/
jQuery.fn.fadeIn = function(speed, callback) {
    return this.animate({opacity: 'show'}, speed, function() {
        if (jQuery.browser.msie) 
            this.style.removeAttribute('filter'); 
        if (jQuery.isFunction(callback))
            callback(); 
    });
};

jQuery.fn.fadeOut = function(speed, callback) {
    return this.animate({opacity: 'hide'}, speed, function() {
        if (jQuery.browser.msie) 
            this.style.removeAttribute('filter'); 
        if (jQuery.isFunction(callback))
            callback(); 
    });
};

jQuery.fn.fadeTo = function(speed,to,callback) {
    return this.animate({opacity: to}, speed, function() {
        if (to == 1 && jQuery.browser.msie) 
            this.style.removeAttribute('filter'); 
        if (jQuery.isFunction(callback))
            callback(); 
    });
};
(function($) {
  // plugin definition
  $.fn.stripe = function(options) {
    var defaults = {'dark': '', 'light': '', 'head': ''};
    var opts = $.extend(defaults, options)
    return this.each(function () {
      var stripe = [opts.dark, opts.light];
      var stripeClass = '';
      $(this).children('tbody').length ? $tbody = $(this).children('tbody').eq(0) : $tbody = $(this);
      $tbody.children('tr').each(function(){
        if ($(this).children('th').length || $(this).children('td.thead').length) {
          $(this).addClass(opts.head);
        }
        else {
          stripeClass == stripe[0] ? stripeClass = stripe[1] : stripeClass = stripe[0];
          $(this).addClass(stripeClass);
        }
      });
    });
  };
})(jQuery);



//Set active Nav Links
function setActiveLinks() {
  var path = window.location.pathname;
  var pathparts = trimPath(path.split('/'));
  $('#nav>ul>li').each(function() {
     if ($(this).children('a').eq(0).length) {
       var href = trimPath($(this).children('a').eq(0).attr('href').split('/'));
       //we're only looking at the first match for the top nav
       href[0] == pathparts[0] ? $(this).addClass('active') : false;
     }
   });
}

function trimPath(pathArray) {
  var filename = pathArray[pathArray.length - 1];
  pathArray.shift(); //get rid of empty first element
  while (filename == 'index.html' || filename == '') {
    pathArray.pop();
    filename = pathArray[pathArray.length - 1];
  }
  return pathArray;
}


$(document).ready(function() {
  //if it's the homepage, fade in a random header
  if($('#page.home').length) {
    $headers = $('#header img').css('display', 'none');
    $headers.eq(Math.floor(Math.random()*$headers.length)).fadeIn(500);
  }
  setActiveLinks();
  $('#tools>ul>li, #nav>ul>li').hover(
    function () {
      $(this).addClass('hover');
    },
    function () {
      $(this).removeClass('hover');
    }
  );
  //set equal blocks height
  $('.equal-blocks').each(function(){
    var maxheight = 0;
    $(this).find('div.block-content, div.block-banner').each(function(){
      if(maxheight < $(this).height()) {
        maxheight = $(this).height();
      }
    }).height(maxheight+'px');
  });
  
  //ie6 substitution for tools imgs
  if (jQuery.browser.msie && (jQuery.browser.version < 7)) {
    $('#tools>ul>li>a>img').attr('src', '/images/customer-tools.gif');
    $("#logo>img").attr('src', '/images/logo.gif');
    $('body').addClass('ie6');
    $('#plans img').each(function() {
       if($(this).attr('src') == '/images/checkmark.png')
          $(this).attr('src', '/images/checkmark.gif');
    });
  }
  
  //stripe tables 
  $('table.striped').stripe({dark: 'dark', light: 'light'}).children('tbody').each(function() {
       $(this).children('tr:last').children('td').css('border-bottom', '1px solid #cccccc');
  });
});

function insertTestimonial(tdata) {
  $('#sidebar').append(themeTestimonial(tdata));
  $('#sidebar .block').filter('.hidden').fadeIn(200);
}
function themeTestimonial(tdata) {
  var output =  '<div class="block">';
      output += '<div class="block-title"><h3>Customer Quotes</h3></div>'
      output += '<div class="block-inner">';
      output += '<div class="bt"><div></div></div>';
      output += '<div class="block-content">';
      output += '<blockquote><p>' + $(tdata).children('quote').eq(0).text() + '</p></blockquote>';
      output += '<p class="citequote">';
      output += '<strong>' + $(tdata).children('name').eq(0).text() + '</strong>';
      output += '<br />' + $(tdata).children('title').eq(0).text() + '</strong>';
      output += '<br />' + $(tdata).children('company').eq(0).text();
      output += '</p>';
      output +='</div>';
      output += '<div class="bb"><div></div></div>';
      output += '</div></div>';
        
  return output;
}
//Customer testimonials
$(document).ready(function(){
  if ($('#sidebar').length) {
    $.ajax({
      type: "GET",
      url: "/xml/testimonials.xml",
      dataType: "xml",
      success: function(xml) {
        var $testimonials = $('testimonial', xml);
        insertTestimonial($testimonials.eq(Math.floor(Math.random()*$testimonials.length)));
      }
    });
  }
  $('head').append('<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>')
  $('head').append('<script type="text/javascript" src="/js/tracker.js"></script>');
});
