/*
 * waitForImages 1.2.2
 * -----------------
 * Provides a callback when all images have loaded in your given selector.
 * http://www.alexanderdickson.com/
 *
 *
 * Copyright (c) 2011 Alex Dickson
 * Licensed under the MIT licenses.
 * See website for more info.
 *
 */

;(function($) {
    $.fn.waitForImages = function(finishedCallback, eachCallback, waitForAll) {

        // Handle options object.
        if (typeof finishedCallback === 'object') {
            eachCallback = finishedCallback.each;
            waitForAll = finishedCallback.waitForAll;
            finishedCallback = finishedCallback.finished;
        }

        // Handle missing callbacks.
        finishedCallback = finishedCallback || function() {};
        eachCallback = eachCallback || function() {};

        // Convert waitForAll to Boolean
        waitForAll = !! waitForAll;

        // Ensure callbacks are functions.
        if (!$.isFunction(finishedCallback) || !$.isFunction(eachCallback)) {
            throw new TypeError('An invalid callback was supplied.');
        };

        return this.each(function() {
            // Build a list of all imgs, dependent on what images will be considered.
            var obj = $(this),
                allImgs = [];

            if (waitForAll) {
                // CSS properties which may contain an image.
                var hasImgProperties = $.fn.waitForImages.hasImgProperties || [
                    'backgroundImage',
                    'listStyleImage',
                    'borderImage',
                    'borderCornerImage'
                    ];
                   
                var matchUrl = /url\(['"]?(.*?)\1\)/g;

                // Get all elements, as any one of them could have a background image.
                obj.find('*').filter(function() {
                    var element = $(this);

                    // If an `img` element, add it. But keep iterating in case it has a background image too.
                    if (element.is('img')) {
                        allImgs.push({
                            src: element.attr('src'),
                            element: element[0]
                        });
                    }

                    $.each(hasImgProperties, function(i, property) {
                        var propertyValue = element.css(property);
                        // If it doesn't contain this property, skip.
                        if ( ! propertyValue) {
                            return true;
                        }

                        // Get all url() of this element.
                        var match;
                        while (match = matchUrl.exec(propertyValue)) {
                            allImgs.push({
                                src: match[1],
                                element: element[0]
                            });
                        };
                    });
                });
            } else {
                // For images only, the task is simpler.
                obj.find('img').each(function() {
                    allImgs.push({
                        src: this.src,
                        element: this
                    });
                });
            };

            var allImgsLength = allImgs.length,
                allImgsLoaded = 0;

            // If no images found, don't bother.
            if (allImgsLength == 0) {finishedCallback.call(obj[0]);};

            $.each(allImgs, function(i, img) {

                var image = new Image;

                image.onload = function() {
                    allImgsLoaded++;
                    eachCallback.call(img.element, allImgsLoaded, allImgsLength);
                    if (allImgsLoaded == allImgsLength) {
                        finishedCallback.call(obj[0]);
                        return false;
                    };
                };

                image.src = img.src;
            });
        });
    };
})(jQuery);


var bannerCount = 5;
var currentBannerNumber = 0;
       
function switch_banner() {
							   
	 //this function gets all divs in a page and checks their classes
	 //if the div has a banner class it is added to a bannerDivs array
	 //a function is then called that displays a random banner
	 
	  var divs = document.getElementsByTagName("div");
		var bannerDivs = new Array();
		var classes;

		for ( var i=0; i<divs.length; i++ ) {
				//there may be multiple class assignments
				//separates the div class into an array
				classes = divs[i].className.split(" ");
				
				//checks if div has a class of banner
				//if so, add it to bannerDivs array
				if ( inArray("banner", classes) ) {bannerDivs.push(divs[i]);}
		}
		display_banner();
}


function display_banner() {

	if (document.getElementById("banner_" + currentBannerNumber)) {
		document.getElementById("banner_" + currentBannerNumber).style.display = "none";
	}
	
	if (currentBannerNumber < bannerCount) {
		currentBannerNumber = currentBannerNumber + 1;
	} else 	{
		currentBannerNumber = 1;	
	}
	
	if (document.getElementById("banner_" + currentBannerNumber)) {

		document.getElementById("banner_" + currentBannerNumber).style.display = "block";
		}
		
	setTimeout("display_banner()", 5000);	
}



$(document).ready(function(){

//wait for all images to load then call banner switcher
$.fn.waitForImages.hasImgProperties = ['backgroundImage'];
$('#banners').waitForImages(function() {switch_banner();},function(){},true);});

