/* ----------------------------------------------------
VitalSource Technologies, Inc.
http://vitalsource.com
---------------------------------------------------- */

jQuery.fn.supersleight = function(settings) {
	settings = jQuery.extend({
		imgs: true,
		backgrounds: true,
		shim: 'x.gif',
		apply_positioning: true
	}, settings);
	
	return this.each(function(){
		if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
			jQuery(this).find('*').andSelf().each(function(i,obj) {
				var self = jQuery(obj);
				// background pngs
				if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
					var bg = self.css('background-image');
					var src = bg.substring(5,bg.length-2);
					var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
					var styles = {
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
						'background-image': 'url('+settings.shim+')'
					};
					self.css(styles);
				};
				// image elements
				if (settings.imgs && self.is('img[src$=png]')){
					var styles = {
						'width': self.width() + 'px',
						'height': self.height() + 'px',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
					};
					self.css(styles).attr('src', settings.shim);
				};
				// apply position to 'active' elements
				if (settings.apply_positioning && self.is('a, input') && (self.css('position') === '' || self.css('position') == 'static')){
					self.css('position', 'relative');
				};
			});
		};
	});
};

(function () {
  $.fn.simpleCarousel = function (){
    return this.each(function (){
      var selectBubble = function(which){
        bubbles.removeClass('current');
        $(bubbles[which-1]).addClass('current');
      };
      
      var animateToPage = function(page){
        currentPage = page;
        // we need to add the marginLeft of 
        var newLeftOffset = (page - 1) * singleWidth * -1;
        $itemsWrapper.stop().animate({left: newLeftOffset + 'px'}, 500, 'swing');
        selectBubble(currentPage);
      };
      
      var $wrapper      = $(this).css('overflow', 'hidden'),
          $itemsWrapper  = $('> ul', this),
          $items        = $('> li', $itemsWrapper),
          $single       = $items.filter(':first'),

          singleWidth   = $single.outerWidth(),
          visible       = Math.ceil($wrapper.innerWidth() / singleWidth),
          currentPage   = 1,
          totalPages    = Math.ceil($items.length / visible);
      
      // insert the page navigation
      var carouselNavigation  = '<ul class="carousel_navigation">' +
                                  '<li class="prev"><a href="#prev">Previous</a></li>' +
                                  '<li class="next"><a href="#next">Next</a></li>' +
                                '</ul>';
      
      var carouselBubbleNavigation =  '<ul class="carousel_bubble_navigation clear">';
      for (var i = 0; i <= totalPages - 1; i++){
        carouselBubbleNavigation += '<li class="bubble_nav';
        if(i==0){ carouselBubbleNavigation += ' first'; }
        if(i== (totalPages - 1)){ carouselBubbleNavigation += ' last'; }
        carouselBubbleNavigation += '"><a href="#' + (i+1) + '">' + (i+1) + '</a></li>';
      };
      carouselBubbleNavigation += '</ul>';
      
      if(totalPages > 1){
        $wrapper.after(carouselNavigation);
        $wrapper.after(carouselBubbleNavigation);
      }else{ return; }
      
      var bubblesWrapper  = $('ul.carousel_bubble_navigation', $wrapper.parent()),
          bubbles         = $('> li', bubblesWrapper);
    
      bubblesWrapper.css({marginLeft: ((bubblesWrapper.outerWidth() / 2) * -1) + 'px'});
    
      // here, we need to set the proper height of the wrapper
      var maxHeight = 0;
      $items.each(function(i){
        var item = $(this);
        if(item.height() > maxHeight){ maxHeight = item.height(); }
      });
    
      // first things first, select the proper carousel bubble
      selectBubble(currentPage);
    
      bubbleNavHeight = bubblesWrapper.height();
      // $wrapper.css('height', (maxHeight + bubbleNavHeight) + 'px');
      $wrapper.css('height', maxHeight + 'px');
    
      // prev observer
      $('.carousel_navigation .prev a').click(function(e){
        e.preventDefault();
        var page;
        if(currentPage != 1){ page = currentPage - 1; }
        else{ page = totalPages; }
        animateToPage(page);
      });
    
      // next observer
      $('.carousel_navigation .next a').click(function(e){
        e.preventDefault();
        var page;
        if(currentPage != totalPages){ page =  currentPage + 1; }
        else{ page = 1; }
        animateToPage(page);
      });
    
      $('.carousel_bubble_navigation a').click(function(e){
        e.preventDefault();
        var page = parseInt($(this).text());
        animateToPage(page);
      });
    });
  };
})(jQuery);

/* redesign JS */
$(document).ready(function(){
  // setup the carousel
  $('.carousel').simpleCarousel();  
  $('.ltIE7 img, .ltIE7 .carousel_wrapper').supersleight({shim:'/images/new/blank.gif'});
});

