﻿/*/
/* A3 News Rotator 1.0 jQuery Plugin
/* Requires jQuery 1.3.2 
/* By Lars Michael Astrom (http://www.lars-astrom.com)
/* Copyright (c) 2010 A3 IT Solutions
/* Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
/* Tested in IE6, IE7, IE8, Firefox 3.5, and Chrome 4.0
/*/

// To make A3 News Rotator work add $(selector).A3NewsRotator() or 
// uncomment the below and change "selector" to containing element

$(document).ready(function(){
  $("#NewsRotator").A3NewsRotator({
    overlayPosition: "bottom", 
    overlayXOffset: 7, 
    overlayYOffset: 3, 
    overlayHeight: 20, 
    viewAllLink: "/news/", 
    viewAllText: "More news, team previews and race reports"
  });
});

(function($){  
  $.fn.extend({   
    A3NewsRotator: function(options){  
      //Set the default values  
      var defaults = {  
        interval: 5000, // rotation interval in milliseconds
        fadeIn: 500, // fade in speed
        fadeOut: 500, // fade out speed
        
        showNewsItems: true, // true/false
        newsItemLimit: 65, // character limit of news items to show
        viewAllLink: "", // if empty, link will not display
        viewAllText: "View All", // only displays if link is present
        
        showOverlay: true, // true/false
        overlayColor: "#ffffff", // hexidecimal color of overlay
        overlayPosition: "top", // top or bottom
        overlayYOffset: 0, // offset (from top/bottom depending on overlayPosition) in pixels
        overlayXOffset: 0, // offset (from the left) in pixels
        overlayHeight: "auto", // height in pixels or "auto"
        overlayPadding: 3, // overlay padding in pixels
        overlayOpacity: .7, // opacity of overlay
        
        showCaption: true, // true/false
        showControls: true // true/false
      }  
      
      // private variables (DO NOT EDIT)     
      var o =  $.extend(defaults, options);  
      var s = $(this).selector; // selector
      var h = s + " div.a3nr_newsimages a"; // holder
      var cur = 0; // current image
 
      return this.each(function() {
        // Initialize the script and bind events
        $.fn.A3NewsRotator.init = function(){
          // Hide all but the first image
          $(h).each(function(a){
            if (a > 0){$(this).hide();}
          });
        
          // Initial caption update
          $.fn.A3NewsRotator.updateCaption();
          
          // Initial active states
          $.fn.A3NewsRotator.setActive();
          
          // Start the initial interval
          var t = setInterval(function(){$.fn.A3NewsRotator.cycle();}, o.interval);

          // Bind mouse enter/leave events
          $(s).bind("mouseenter", function(){
            clearInterval(t); // stops cycling
          }).bind("mouseleave", function(){
            t = setInterval(function(){$.fn.A3NewsRotator.cycle();}, o.interval);
          });
        };

        // Handle hide/show cycle
        $.fn.A3NewsRotator.cycle = function(n){
          $.fn.A3NewsRotator.hide(); // hide image
          
          // check n to see if we're iterating or jumping
          if (typeof(n) != "undefined"){cur = n;}
          else{if (cur == $(h).size() - 1){cur = 0;} else {cur++;}}
          
          $.fn.A3NewsRotator.show(); // show image
          $.fn.A3NewsRotator.updateCaption(); // update caption
          $.fn.A3NewsRotator.setActive(); // set active states
        }
        
        // Fades out the current image
        $.fn.A3NewsRotator.hide = function(){$($.fn.A3NewsRotator.getObj()).fadeOut(o.fadeOut);}
        
        // Fades in the next image
        $.fn.A3NewsRotator.show = function(){$($.fn.A3NewsRotator.getObj()).fadeIn(o.fadeIn);}           
        
        // Get the current news object
        $.fn.A3NewsRotator.getObj = function(){ var obj = $(h).eq(cur); return obj;}
        
        // Update captions
        $.fn.A3NewsRotator.updateCaption = function(){
          $(s + " .a3nr_caption").html("<strong>" + $(h + " img").eq(cur).attr("alt") + "</strong><br />" + $(h).eq(cur).attr("title"));
        }
        
        // Set active states
        $.fn.A3NewsRotator.setActive = function(){
          // Update control active state
          $(s + " div.a3nr_ctrls a").each(function(k){
            if (k == cur){$(this).addClass("active");}else{$(this).removeClass("active");}
          });
          
          // Update news item active state
          $(s + " div.a3nr_newsitems a").each(function(k){
            if (k == cur){$(this).addClass("active");}else{$(this).removeClass("active");}
          });
        }
        
        // Build the initial components
        // Set selector needed
        $(s).css({position: "relative"});
        
        // Add the image container
        $(s).wrapInner("<div class=\"a3nr_newsimages\"></div>");
        
        var f; // generic variable for building structure
        
        // Build and append the news items
        if (o.showNewsItems){
          f = "<div class=\"a3nr_newsitems\">";
          $(h + " img").each(function(a){
            var i = $(this).attr("alt");
            if (i.length > o.newsItemLimit){i = i.substring(0, o.newsItemLimit) + "...";}
            f += "<a href=\"" + $(h).eq(a).attr("href") + "\">" + i + "</a>";
          });
          // Add view all link if applicable
          if (o.viewAllLink != ""){
            f += "<a href=\"" + o.viewAllLink + "\" class=\"a3nr_viewall\">" + o.viewAllText + "</a>";
          }         
          f += "</div>";
          // Append the elements
          $(s).append(f);
        }
        
        // No use in showing overlay with nothing in it
        if ((!o.showCaption) && (!o.showControls)){o.showOverlay = false}
        
        if (o.showOverlay){
          // Build overlay
          f = "<div class=\"a3nr_overlay\">";
          
          // Build caption
          if (o.showCaption){f += "<span class=\"a3nr_caption\"></span>";}
          
          // Build controls
          if (o.showControls){
            f += "<div class=\"a3nr_ctrls\">";
            $(h).each(function(a){f += "<a href=\"javascript:$.fn.A3NewsRotator.cycle(" + a + ")\">" + (a + 1) + "</a>";});
            f += "</div>";
          }
          
          f+= "</div>";
          
          // Append the elements
          $(s).append(f);
          
          // Style the overlay
          $(s + " .a3nr_overlay").css({
            "position": "absolute", 
            "left": o.overlayXOffset + "px",
            "background": o.overlayColor, 
            "width": ((jQuery.support.boxModel) ? ($(s + " img").width() - (o.overlayPadding * 2)) : $(s + " img").width()) + "px", 
            "height": (isNaN(o.overlayHeight) ? "auto" : (((jQuery.support.boxModel) ? (o.overlayHeight - (o.overlayPadding * 2)) : o.overlayHeight) + "px")),
            "padding": o.overlayPadding,
            opacity: o.overlayOpacity
          });
          
          // Position overlay either at top or bottom depending on options
          if (o.overlayPosition == "top"){$(s + " .a3nr_overlay").css({"top": o.overlayYOffset + "px"});}
          else {$(s + " .a3nr_overlay").css({"bottom": o.overlayYOffset + "px"});}
        }
        
        // Run the script init
        $(s).A3NewsRotator.init();
      });  
    }  
  });  
})(jQuery); 