jCustom(function() {
  jCustom.fn.actsAsDefault = function() {
    jCustom(this).each(function() {
      var defaultValue = jCustom(this).val();
      jCustom(this).focus(function() {
        if (jCustom(this).val() === defaultValue) {
          jCustom(this).removeClass('default').val('');
        }
      }).blur(function() {
        if (jCustom.trim(jCustom(this).val()) === '') {
          jCustom(this).addClass('default').val(defaultValue);
        }
      });
    });
  };
  
  cci_global.homeSetup();
  cci_global.globalSetup();
  jCustom('input.text', '#home-subscribe').actsAsDefault();
});

var cci_global = {
  ie6: jCustom.browser.msie && jCustom.browser.version < 7,
  // Math.max applied to arrays (picks out max value)
  arrayMax: function(array) {
    return Math.max.apply(Math, array);
  },
  // Math.min applied to arrays (picks out min value)
  arrayMin: function() {
    return Math.min.apply(Math, array);
  },
  globalSetup: function() {
    jCustom('.RadMenu_nav-primary', '#header').find('ul.rmVertical a.rmSelected').eq(0).each(function() {
      jCustom(this).parents('div.rmSlide').prev().addClass('rmSelected');
    });
    jCustom('#nav-sidebar ul.rsmList').eq(0).find('a').each(function() {
      var wloc = window.location.pathname;
      var cloc = jCustom(this)[0].href;
      if (cloc.indexOf(wloc) > -1 && cloc.length === cloc.indexOf(wloc) + wloc.length) {
        jCustom(this).addClass('selected').parent('li').parents('li').find('>a').addClass('selected');
        jCustom(this).parent('li').find('>ul').show();
        jCustom(this).parents('ul').show()
      }
    });
  },
  homeSetup: function() {
    if (!jCustom('#home-intro')[0]) {
      return false;
    }
        
    //Fix addthis width
    jCustom('#footer-addthis').width('auto');
    
    //Rotating image on the homepage
    var imgs = jCustom('#home-intro .sfimagesList li img');
    var imgs_sel = imgs.eq(Math.min(Math.floor(Math.random() * imgs.length), imgs.length -1));
    jCustom('#home-intro').css({
      'background-image': 'url(' + imgs_sel.attr('src') + ')',
      'background-color': '#' + imgs_sel.attr('alt'),
      'background-position': '50% 0',
      'background-repeat': 'no-repeat'
    });
    
    //Home features
    jCustom('#home-features').each(function() {
      jCustom('#home-features-left, #home-features-right').addClass('disabled');
      jCustom.ajax({
        url: '/Feeds/featured',
        dataType: 'text',
        type: 'GET',
        success: function(xml) {
          jCustom('#home-features-loading').remove();
          var xml = jCustom.parseXML(xml);
          xml = jCustom(xml);
          var ref = jCustom('#home-features .inner').append('<ul></ul>').find('ul:first');
          xml.find('channel>item').each(function() {
            var temp_html = jCustom(jCustom(this).find('description').text());
            var a_href = temp_html.find('a').attr('href');
            var ref_li = ref.append('<li></li>').find('li:last');
            ref_li.append('<a href="' + a_href + '" class="img-a"></a>');
            ref_li.find('a.img-a').append(temp_html.find('img'));
            var ref_desc = ref_li.append('<div class="desc"></div>').find('div.desc');
            ref_desc.append('<h3><a href="' + a_href + '">' + jCustom.trim(jCustom(this).find('title').text()) + '</a></h3>');
            ref_desc.append(temp_html.filter('p:first'));
            ref_desc.append('<p class="buy"><a href="' + a_href + '">Buy Now</a></p>');
          });
          var item_count = ref.find('li').length;
          if (item_count > 1) {
            jCustom('#home-features-left, #home-features-right').removeClass('disabled');
            
            ref.width((389 * ref.find('li').length) + 'px');
            
            jCustom('#home-features-left').click(function() {
              var margin_left = parseInt(ref.css('margin-left'), 10);
              if (margin_left >= 0) {
                ref.animate({'margin-left': (item_count - 1) * -389}, 400);
              } else {
                ref.animate({'margin-left': margin_left + 389}, 200);
              }
              
              return false;
            });
            jCustom('#home-features-right').click(function() {         
              var margin_left = parseInt(ref.css('margin-left'), 10);
              if ((item_count - 1) * 389 <= -1 * margin_left) {
                ref.animate({'margin-left': 0}, 400);
              } else {
                ref.animate({'margin-left': margin_left - 389}, 200);
              }
              
              return false;
            });
          }
        }
      })
    });

    // Fix heights of home featured nav boxes
    var hfn = jCustom('#home-featured-nav a');
    var hfn_heights = [];
    for (var i = 2; i >= 0; i--) {
      hfn_heights[i] = hfn.eq(i).height(); 
    }
    hfn.height(cci_global.arrayMax(hfn_heights) + 'px');
  }
};
