﻿/*/
/* A3 Banner 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 Banner Rotator work add $(selector).A3BannerRotator() or 
// uncomment the below and change "selector" to containing element

$(document).ready(function(){
  $("#Home .top-right").A3BannerRotator({
    showControls: false,
    fadeIn: 1000,
    fadeOut: 1000,
    Interval: 5000
  });
});

(function($){  
  $.fn.extend({   
    A3BannerRotator: function(options){  
      //Set the default values  
      var defaults = {  
        interval: 3000, // rotation interval in milliseconds
        fadeIn: 500, // fade in speed
        fadeOut: 500, // fade out speed
        showControls: true, // true/false
        controlOpacity: .9, // opacity of controls
        title: false // use title for the controls, true/false
      }  
      
      // private variables (DO NOT EDIT)     
      var o =  $.extend(defaults, options);  
      var s = $(this).selector; // selector
      var h = s + " div.a3br_images"; // images
      var c = s + " div.a3br_ctrls"; // controls
      var i = new Array(); // # of images and stores alts
      var cur = 0;
 
      return this.each(function() {
        // Build the initial components
        // Add selector styles
        $(s).css({position: "relative"});
        // Add the image container
        $(s).wrapInner("<div class=\"a3br_images\"></div>");
        $(s + " img").css({position: "absolute", top: "0px", left: "0px"});
        // Append the controls
        if (o.showControls){
          $(s).append(
            "<div class=\"a3br_ctrls\">" +
            "<a href=\"javascript:void(0);\" class=\"prev\">prev</a>" + 
            "<a href=\"javascript:void(0);\" class=\"next\">next</a>" +
            "</div>");
          $(s + " .a3br_ctrls").css({opacity: o.controlOpacity});
        }
                    
        // Initialize the script and bind events
        $.fn.A3BannerRotator.init = function(){
          // Build array of titles (used for previous/next) and hide inactive images
          $(h + " img").each(function(a){
            i[a] = this.title;
            if (a > 0){$(this).hide();}
          });
        
          // Initial control update
          if (o.title){ $.fn.A3BannerRotator.updateControls();};
          
          // Start the initial interval
          var t = setInterval(function(){$.fn.A3BannerRotator.cycle();}, o.interval);

          // Bind mouse events
          $(s).bind("mouseenter", function(){
            clearInterval(t);
          }).bind("mouseleave", function(){
            t = setInterval(function(){$.fn.A3BannerRotator.cycle();}, o.interval);
          });
          
          // Bind click events
          $(c + " .next").bind("click", function (){
            $.fn.A3BannerRotator.cycle();
          });
          
          $(c + " .prev").bind("click", function (){
            $.fn.A3BannerRotator.hide(); // hide image
            if (cur == 0){cur = i.length - 1} else {cur--;}
            $.fn.A3BannerRotator.show(); // show image
          });
        };

        // Handle hide/show cycle
        $.fn.A3BannerRotator.cycle = function(){
          $.fn.A3BannerRotator.hide(); // hide image
          if (cur == i.length - 1){cur = 0;} else {cur++;}
          $.fn.A3BannerRotator.show(); // show image
        }
        
        // Fades out the current image
        $.fn.A3BannerRotator.hide = function(){$($.fn.A3BannerRotator.getObj()).fadeOut(o.fadeOut);}
        
        // Fades in the next image
        $.fn.A3BannerRotator.show = function(){$($.fn.A3BannerRotator.getObj()).fadeIn(o.fadeIn); if (o.title){ $.fn.A3BannerRotator.updateControls();};}           
        
        // Get the current image object
        $.fn.A3BannerRotator.getObj = function(){ var obj = $(h + " img").eq(cur); return obj;}
        
        // Update controls with text from alt tags
        $.fn.A3BannerRotator.updateControls = function(){
          // Update next button
          if (cur + 1 == i.length){var m = 0;} else {var m = 1;}
          $(c + " a.next").html(i[(cur + 1) * m]);
          
          // Update previous button
          if (cur - 1 < 0){
            $(c + " a.prev").html(i[i.length - 1]);
          }else{
            $(c + " a.prev").html(i[cur - 1]);
          }
        } 
        
        $(s).A3BannerRotator.init();
      });  
    }  
  });  
})(jQuery); 
