// Install pngFix
$( document ).ready( function() {
    $( '.pngfix' ).pngFix();
} );

/*
 * A basic image ticker.
 * @example  $("#client-list").imageTicker(); or $("#client-list").imageTicker(5000);
 */
(function( $ ) {
    $.fn.imageTicker = $.fn.imageTicker = function( interval ) {
        interval = interval || 4000;

        initTicker = function( el ) {
            stopTicker( el );
            el.images = $( "img", el );
            // current image
            el.currentimage = Math.floor( Math.random() * el.images.size() );
            // start with current image displayed and all the rest hidden.
            $( el.images[el.currentimage] ).show();
            el.images.not( ":eq(" + el.currentimage + ")" ).hide().end();
            startTicker( el );
        };
        startTicker = function( el ) {
            el.tickfn = setInterval( function() {
                doTick( el )
            }, interval )
        };
        stopTicker = function( el ) {
            clearInterval( el.tickfn );
        };
        doTick = function( el ) {
            // hide current image
            $( el.images[el.currentimage] )
                    .fadeOut( "slow",
                              function() {
                                  $( this ).hide();
                                  // move to next image and show
                                  el.currentimage = ++el.currentimage % (el.images.size());
                                  $( el.images[el.currentimage] ).fadeIn( "slow" );
                              }
                    );
        };
        this.each(
                function() {
                    if( this.nodeName.toLowerCase() == "div" ) {
                        initTicker( this );
                    }
                }
                );
        return this;
    };

})( jQuery );

/*
 * Basic horizontal menu - display remove sub-navs.
 */

(function( $ ) {
    $.fn.qubitMenu = $.fn.qubitMenu = function() {
        // Slide duration.
        animateDuration = {over: 10, out: 10};

        // Grab the top-level UL
        var $mainMenu = $( ">ul", this );

        // Grab all the top-level li elements which contain sub-menus.
        var $menuItems = $mainMenu.find( "ul" ).parent();

        // Iterate over each - we need to instrument these.
        $menuItems.each( function() {

            // Grab current LI
            var $currentUL = $( this );

            // Grab the submenu (first element ignores multiples).
            var $subUL = $currentUL.find( 'ul:eq(0)' );

            // Need to determine the height of the LI - this is used later for the position of the sub-menu.
            this._dimensions = {h:$currentUL.outerHeight( true )};
            this.isTopUL = $currentUL.parents( "ul" ).length == 1;
            if( !this.isTopUL )
                $subUL.css( {left:0, top:this._dimensions.h} );

            // Grab inner links.
            $currentUL.hover(
                    function( e ) {
                        // Grab target UL
                        var $targetUL = $( this ).children( "ul:eq(0)" );

                        // Check that this UL is a top-level menu.
                        if( this.isTopUL ) {
                            var left = $mainMenu.offset().left;
                            var top = $mainMenu.offset().top;
                            $targetUL.css( {left: left, top: top + this._dimensions.h} );
                        }
                        if( document.all && !window.XMLHttpRequest ) //detect IE6 or less, fix issue with overflow
                            $mainMenu.find( 'ul' ).css( {overflow: (this.isTopUL) ? 'hidden' : 'visible'} );
                        $targetUL.slideDown( animateDuration.over )
                    },
                    function( e ) {
                        var $targetul = $( this ).children( "ul:eq(0)" );
                        $targetul.slideUp( animateDuration.out )
                    }
                    ); //end hover
        } ); //end $headers.each()
        $mainMenu.find( "ul" ).css( {display:'none', visibility:'visible', width:$mainMenu.width()} )
    };
})( jQuery );


