var __rotation;
var __click = false;
var __duration = 10000;

$( document ).ready( function() {
    constructor.start();
    __rotation = setInterval( "rotation()", __duration );
    $( ".fancybox" ).click( function() {
        $.fancybox({
            'opacity': false,
            'overlayShow': true,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'padding': 0,
            'centerOnScroll': true,
            'autoScale': false,
            'width': 640,
            'height': 385,
            'href': this.href.replace( new RegExp( "watch\\?v=", "i" ), 'v/' ),
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
            }
        });
        return false;
    });
    $( ".pacific-54" ).click( function() {
        $.fancybox({
            'opacity': false,
            'centerOnScroll': true,
            'overlayShow': true,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'padding': 0,
            'autoScale': false,
            'width': 640,
            'height': 385,
            'href': this.href,
            'type': 'swf',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen': 'true'
            }
        });
        return false;
    });
});

var index = 0;

function rotation()
{
    if ( __click == false )
    {
        index += 1;
        if ( index == 5 ) index = 0;
        $( "#navigation a[rel=" + index + "]" ).click();
    }
}

var __clicktimeout;
function clicktimeout()
{
    __click = false;
}

var constructor = {
    start: function() {
        $.getScript( "http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js", function() {
            constructor.carousel();
        });
        constructor.fonts();
        constructor.corrections();
        constructor.other();
    },
    carousel: function() {
        $( "#navigation" ).data( { stop: "false" } );
        $( "#carousel" ).css( { height: $( "#carousel" ).parent().height(), width: $( "#carousel li" ).size() * $( "#carousel" ).parent().width() } );
        $( "#navigation a:first" ).addClass( "selected" );
        $( "#navigation a" ).click( function( e ) {
            clearTimeout( __clicktimeout );
            __clicktimeout = setTimeout( "clicktimeout()", __duration );
            __click = true;
            index = parseInt( $( this ).attr( "rel" ) );
            e.preventDefault();
            if ( $( "#navigation" ).data( "stop" ) == "false" )
            {
                $( "#navigation" ).data( { stop: "true" } );
                $( ".overlay" ).each( function() {
                    if ( $( this ).css( "top" ) == "0px" )
                    {
                        var overlay = $( this );
                        overlay.animate( { top: "-320px", opacity: "0" }, { duration: 500, easing: "easeOutExpo", queue: true,
                            complete: function() {
                                overlay.css( { top: "320px" } );
                            }
                        });
                    }
                });
                $( "#navigation a" ).each( function() {
                    $( this ).removeClass( "selected" );
                });
                $( this ).addClass( "selected" );
                var rel = $( this ).attr( "rel" );
                $( ".overlay[rel='" + rel + "']" ).animate( { top: "0px", opacity: "1" }, { duration: 1000, easing: "easeOutExpo", queue: true } );
                $( "#carousel" ).animate( { left: -rel * $( "#carousel" ).parent().width() }, { duration: 1000, easing: "easeOutExpo", queue: true,
                    complete: function() {
                        $( "#navigation" ).data( { stop: "false" } );
                    }
                });
            }
        });
        $( ".overlay[rel='0']" ).animate( { top: "0px", opacity: "1" }, { duration: 1000, easing: "easeOutExpo", queue: true } );
    },
    fonts: function() {
        Cufon.replace( "#page h1.avant", { fontFamily: "AvantGarde Bk BT" } );
        Cufon.replace( ".avant, .inside-title", { fontFamily: "AvantGarde Bk BT" } );
        Cufon.replace( ".futura", { fontFamily: "Futura Lt BT" } );
    },
    corrections: function() {
        if ( $.browser.mozilla )
        {
            $( "#menu ul > li:last-child a" ).css( { padding: "17px 24px 15px 25px" } );
        }
        if ( $.browser.webkit )
        {
            $( "#menu ul" ).css( { width: "1100px" } );
            $( "#menu ul li a" ).css( { padding: "17px 24px 15px" } );
            $( "#menu ul > li:last-child a" ).css( { padding: "17px 25px 15px 26px" } );
            $( "#menu ul li a:first, #menu ul li a:last" ).css( { padding: "17px 27px 15px" } );
        }
    },
    other: function() {
        $( "#menu .container > ul > li" ).each( function() {
            if ( $( this ).find( "ul" ).size() != 0 )
            {
                $( this ).find( "ul" );
                var max = 0;
                $( this ).find( "ul li" ).each( function() {
                    if( $( this ).width() > max )
                    {
                        max = $( this ).width();
                    }
                });
                $( this ).find( "ul" ).css( { width: max, display: "none" } );
                $( this ).find( "ul li" ).each( function() {
                    $( "a", this ).css( { width: max - parseInt( $( ".sub-menu li > a" ).css( "padding-left" ) ) * 2 } );
                });
                if ( $( this ).attr( "rel" ) == "right" )
                {
                    $( "ul", this ).css( { right: "0px" } )
                }
            }
        });
        $( "#footer .links a" ).css( { opacity: 0.65 } ).hover( function() {
            $( this ).css( { opacity: 1 } );
        }, function() {
            $( this ).css( { opacity: 0.65 } );
        });
        $( "#menu ul li" ).hover( function() {
            $( "> ul", this ).css( { display: "block" } );
        }, function() {
            $( "> ul", this ).css( { display: "none" } );
        });
    }
}
