/* 
  headerFader jQuery Plugin 
	
  Copyright (c) 2011 Vic Jerczynski
	
  Dual licensed under the MIT and GPL licenses:
  http://www.opensource.org/licenses/mit-license.php
  http://www.gnu.org/licenses/gpl
	
*/
(function($) {
    $.fn.headerFader = function(options) {
        var defaults = {
            delay: 10000,
            navElem: null,
            random:false,
            sidenote:null
        },
        settings = $.extend({},
        defaults, options);
        this.each(function(i, v) {
            var $this = $(this);
            var images = $this.children("img");
            var index = 0;
            if(settings.random){
                index = Math.floor(Math.random()*images.length);
            }
            images.eq(index).show();
            
            if(settings.sidenote != null){
                displaySidenote(index);
            }
            
            if (settings.navElem != null) {
                switchNavigation(3);
            }
            setInterval(function() {
                faderSwitchImage(images,index)
            },
            settings.delay);
        });
        
        function displaySidenote(index){
            var sidenote = settings.sidenote;
           $("#sidenote").html('<p class="side-title">'+sidenote[index].title+'</p><p class="side-sub">'+sidenote[index].subtitle+'</p>');
        }
        
        function faderSwitchImage(img,index) {
            img.each(function(i, v) {
                if ($(this).is(":visible")) {
                    index = i;
                }
            });
            if (index != img.length - 1) {
                img.eq(index).css({
                    position: "absolute",
                    zIndex: 2,
                    top: 0,
                    left: 0
                });
                img.eq(index + 1).css({
                    position: "absolute",
                    zIndex: 1,
                    top: 0,
                    left: 0,
                    display: "inline"
                });
                if(settings.sidenote != null){
                displaySidenote(index+1);
            }
            } else {
                img.eq(index).css({
                    position: "absolute",
                    zIndex: 2,
                    top: 0,
                    left: 0
                });
                img.eq(0).css({
                    position: "absolute",
                    zIndex: 1,
                    top: 0,
                    left: 0,
                    display: "inline"
                });
                if(settings.sidenote != null){
                displaySidenote(0);
            }
            }
            
            img.eq(index).fadeOut("slow",
            function() {
                if (settings.navElem != null) {
                    switchNavigation(index);
                }
            });
            
        }
        function switchNavigation(index) {
            var navElement = $(settings.navElem);
            var currElem = navElement.eq(index);
            if (index != navElement.length - 1) {
                currElem.removeClass("activeFade");
                navElement.eq(index + 1).addClass("activeFade");
            } else {
                currElem.removeClass("activeFade");
                navElement.eq(0).addClass("activeFade");
            }
        }
    }
})(jQuery);
