$(function(){
    // --------------------------------------------------------------------------------
    // Content
    // --------------------------------------------------------------------------------
    initNavigationLinks();
    initContentLinks();
    initCommandLinks();
    // --------------------------------------------------------------------------------
    // Design
    // --------------------------------------------------------------------------------
    initShowHideBlock();
    // --------------------------------------------------------------------------------
    // Effects
    // --------------------------------------------------------------------------------
    var s = document.location.toString().replace( /.*\/([^\/]*)$/, "$1" );
    if ( s == $( "#header a" ).attr( "href" ) || s == "" )
        showStartAnimation();
    initPriceEffects();
    //$.ajaxHistory.initialize( loadHomePage );
});
// --------------------------------------------------------------------------------
// Content
// --------------------------------------------------------------------------------
function initNavigationLinks() {
    $( "#navigation a" )
        .remote()
        .not( "a[class=active]" )
        .mouseover( function(){ animateNavLinkOver( this ) } )
        .mouseout( function(){ animateNavLinkOut( this ) } );
    $( "#contactMap" ).remote();
}
/*
function loadHomePage() {
     $.get( 'default.aspx',
        function( xml ){
            changeContent( xml );
        }
    );
}*/
function calculateNavigationLinks( xml ) {
    var activeLinkHref = $( "#navigation a.active", xml ).attr( "href" );
    if ( activeLinkHref )
        animateNavLinkClick(
            $( "#navigation a[href$=section-" + activeLinkHref.replace( /.*sid=(\d+).*/, "$1" ) + "]" )
        );
}
function initContentLinks() {
    $( "#content a" ).not( "a[onclick]" ).not( "a[target=_blank]" ).remote();
}
function initCommandLinks() {
    $( "a.commandLink" ).mouseover( function() { animateCommandLink( this ) } );
}
function changeContent( xml ) {
    $( "#content" ).fadeOut( "slow",
        function() {
            $( this )
                .empty()
                .removeClass( "scrollContainer" )
                .addClass( "scrolledBlock" )
                .append( $( "#content>*", xml ) )
                .css( { display: "block", visibility: "hidden" } );

                Scroller.Init();
                initContentLinks();
                initPriceEffects();
                initCommandLinks();
                document.title = xml.match( /<[tT][iI][tT][lL][eE]>(.*)<\/[tT][iI][tT][lL][eE]>/ )[1];

            $( this )
                .css( { display: "none", visibility: "visible" } )
                .fadeIn( "slow" );
        }
    )
}
// --------------------------------------------------------------------------------
// Design
// --------------------------------------------------------------------------------
function initShowHideBlock() {
    $( "div.showHideBlock" ).slideUp( 500 );
    $( "li.activeTab" ).animate( {backgroundPosition: "0 0"}, 500, function(){ $( this ).removeClass( "activeTab" ) } );
}
// --------------------------------------------------------------------------------
// Effects
// --------------------------------------------------------------------------------
function showStartAnimation() {
    $( "#content" ).hide();
    $( "#left" ).hide();
    $( "#right" ).hide();

    $( window ).load(
        function(){
            $( "#content" ).fadeIn();
            $( "#left" ).fadeIn();
            $( "#right" ).fadeIn();
        }
    );
}
function initPriceEffects() {
    $( ".priceList tr" ).not( ":has(th)" ).mouseover( function(){ animatePriceRowOver( this ) } );
    $( ".priceList tr" ).not( ":has(th)" ).mouseout( function(){ animatePriceRowOut( this ) } );
}
function animateNavLinkClick ( link ) {
    if ( !$( link ).hasClass( "active" ) )
    {
        animateNavLinkOut(
            $( "a.active" )
                .removeClass( "active" )
                .mouseover( function(){ animateNavLinkOver( this ) } )
                .mouseout( function(){ animateNavLinkOut( this ) } )
        );
        $( link ).addClass( "active" ).removeAttr( "style" ).unbind( "mouseout" ).unbind( "mouseover" );
    }
}
function animateNavLinkOver( link ) {
    $( link )
        .animate(
            {   backgroundPosition: '5px center',
                paddingLeft: 20
            },
            {   queue: false, duration: 200 }
        );
}
function animateNavLinkOut( link ) {
    $( link )
        .animate(
            {
                backgroundPosition: '-5px center',
                paddingLeft: 10
            },
            { queue: false, duration: 200 }
        );
}
function animateCommandLink( link ) {
    $( link ).animate( { backgroundPosition: "0px 3px" }, { queue: false, duration: 100,
        complete: function(){
            $( link ).animate( { backgroundPosition: "0px 6px" }, { queue: false, duration: 100 } )
        } }
    );
}
function animatePriceRowOver( row ) {
    $( row ).css( { backgroundColor: "#ADA388" } ).animate( { backgroundColor: "#C3BCA5" }, { queue: false, duration: 200 } );
}
function animatePriceRowOut( row ) {
    $( row ).animate(
        { backgroundColor: "#E5DFC9" },
        { complete: function(){ $( row ).css( { backgroundColor: "transparent" } ) } ,queue: false, duration: 200 }
    );
}

