        var fragment = document.createDocumentFragment();



        var panelCurrent;

        var panelNext;



        var panelCaption;

        var panelNavigation;

        var panelShowcase;

        var panelPlaylist;

        

        var linkThumbnail;



        var isVisiblePanelPlaylist;

        var bannerImagesLoaded = false;




        $(document).ready(function() {


            /* disable right click */
            $(document).bind("contextmenu",function(e){
                return false;
            });


             panelLoader = $("#loader");

             panelCaption = $("#caption");

             panelNavigation = $("#navigation-showcase");

             panelShowcase = $("#showcase");

             panelPlaylist = $("#playlist");



             isVisiblePanelPlaylist = true;



             fitGallery();



             $("#container.testimonals").css("height", "auto");



             if (  $("#container.homepage img").length > 0 ){

                 isVisiblePanelPlaylist = false;

                 fitGallery();

             }



             $( "a", panelPlaylist ).click( function(){



                panelNext = jQuery( this ).parent("li");



                isVisiblePanelPlaylist = false;



                loadImage ();



                // reassign values

                panelCurrent = panelNext;

                return false;



             })



             $( "#link-previous" ).click ( function() {



                panelNext = jQuery( panelCurrent ).prev();



                if ( panelNext.length == 0 ) {

                    panelNext = jQuery("li:last", panelPlaylist );

                }



                loadImage ();



                panelCurrent = panelNext;



                return false;



             })







             $( "#link-next, #showcase" ).click( function(){



                var isHomepage = jQuery( ".homepage" );

                if ( isHomepage.length > 0  ){

                     return false;

                }



                panelNext = jQuery( panelCurrent ).next();



                if (panelNext.length == 0 ) {

                    panelNext = jQuery("li:first", panelPlaylist );

                }



                loadImage ();



                // reassign values

                panelCurrent = panelNext;



                return false;



            })





            $( "#link-thumbnails" ).click( function() {

                

                isVisiblePanelPlaylist = true;



                $( panelCaption ).hide( function(){

                    $( panelNavigation ).hide( function(){



                        $( "#showcase" ).fadeOut( function() {

                            $( "#playlist" ).fadeIn();

                            $( "#showcase").attr( "src", "" );

                            fitGallery()

                        });



                    });

                });



                return false;



            })

            /*** start menu items ***/


            $("li#navigation-item-photographs").hover (
                function () {
                    // nothing happens
                } ,

                function () {
                    $("#menu-photographs").slideUp();
                }
            )


            $("#link-photographs").hover(

                function () {
                    $("#menu-photographs").show();
                },

                function (){
                     // nothing happens
                }

            )

            $("li#navigation-item-projects").hover (
                function () {
                    // nothing happens
                } ,

                function () {
                    $("#menu-projects").slideUp();
                }
            )



            $("#link-projects").hover(

                function () {
                    $("#menu-projects").show();
                },

                function (){
                    // nothing happens
                }

            )

            /*** end menu items ***/


            /*** start banner roation on homepage ***/
            
            jQuery("ul#banner li").hide();

            jQuery("ul#banner").everyTime(3000, function (i) {
              
              

                    if ( bannerImagesLoaded ){

                        if (  jQuery("ul#banner li:visible").length > 1 ) {
    
                            jQuery("ul#banner li:visible:last").fadeOut();
                        } else {
                             jQuery("ul#banner li:last").fadeIn( function(){
                                 jQuery("ul#banner li").show();
                             });

                        }
                    }

    
            });

            // check to see if the images have been loaded yet.
            jQuery("ul#banner li:last img").load(function() {
                bannerImagesLoaded = true;
                jQuery("ul#banner li").show();
            });

            /*** end banner roation on homepage ***/








        })






        $(window).resize( function() {

             

             if ( $("#container.testimonals").length == 0 ){

                fitGallery();

             }

        })





        function loadImage (){



            jQuery( panelShowcase ).fadeOut( function(){



                jQuery( panelShowcase ).attr( "src", "" );

                var panelNextHref = jQuery( "a",  panelNext ).attr("href");

                var isPreloaded = false;



                for( i =0; i < fragment.childNodes.length; i++){

                    var fragmentSrc =  jQuery( fragment.childNodes[i] ).attr( "src" );

                    if ( panelNextHref == fragmentSrc ) {

                        isPreloaded = true;

                    }

                }



                jQuery( panelCaption ).hide();

                jQuery( panelCaption ).empty();

                jQuery( panelCaption ).append( jQuery( "a", panelNext ).html() );





                if ( !isPreloaded ) {



                    var cacheImage = document.createElement("img");

                    cacheImage.src = panelNextHref;

                    jQuery( cacheImage ).attr("class", jQuery( panelNext ).attr( "class" ) );

                    fragment.appendChild( cacheImage );





                    jQuery( cacheImage ).load(

                        function() {

                            jQuery( panelShowcase ).attr( "src", panelNextHref );

                            fitGallery();

                            ShowcaseFadeIn ( panelShowcase, panelCaption, panelNavigation, panelPlaylist );



                        }

                    )



                } else {

                    jQuery( panelShowcase ).attr( "src", panelNextHref );

                    fitGallery();

                    ShowcaseFadeIn ( panelShowcase, panelCaption, panelNavigation, panelPlaylist );



                }

            });
            







        }





        function ShowcaseFadeIn () {



            if ( $( panelPlaylist ).is(':visible') ) {

                $( panelPlaylist).fadeOut( function(){



                    jQuery( panelShowcase ).fadeIn();

                    jQuery( panelCaption ).fadeIn();

                    jQuery( panelNavigation ).fadeIn();

                })

            } else {      

                jQuery( panelShowcase ).fadeIn();

                jQuery( panelCaption ).fadeIn();

                jQuery( panelNavigation ).fadeIn();

            }

        }







        function fitGallery(){

        



            var windowHeight =  jQuery(window).height()-(55+30);

            var windowWidth =  jQuery(window).width();





            var windowProportion = windowWidth/windowHeight;

            var proportion = 3 / 2;



            if ( windowHeight > (800 + (55+30)) ) { windowHeight =  800; }

            if ( windowWidth > 1200 ) { windowWidth =  1200; }





            if ( isVisiblePanelPlaylist ) {



                jQuery("#container").css( "width", 775 + 'px');

                jQuery("#container").css( "height", 540 + (55+30) + 'px');



                return;



            }





            if ( windowProportion > proportion) {

                jQuery( panelShowcase ).css( "height", windowHeight + 'px');

                jQuery( panelShowcase ).css( "width", (windowHeight * (3/2)) + 'px' );



                jQuery("#container").css( "width", windowHeight * (3/2) + 'px');

                jQuery("#container").css( "height", windowHeight + (55+30) + 'px');


                /* for the case of the front page banner */
                jQuery( "#banner img:visible" ).css( "height", windowHeight + 'px');
                jQuery( "#banner img:visible" ).css( "width", (windowHeight * (3/2)) + 'px' );





            } else {



                jQuery( panelShowcase ).css( "height", (windowWidth * (2/3)) + 'px');
                jQuery( panelShowcase ).css( "width", windowWidth + 'px' );

                jQuery("#container").css( "width", windowWidth + 'px' );
                jQuery("#container").css( "height", (windowWidth * (2/3) + (55+30) ) + 'px' );
                

                /* for the case of the front page banner */
                jQuery( "#banner img:visible" ).css( "height", (windowWidth * (2/3)) + 'px');
                jQuery( "#banner img:visible" ).css( "width", windowWidth + 'px' );

            }

        }
