
jQuery(document).ready(function() {

    var sActivePage = jQuery('#sMnuID').val();
    var sCurrentPage = sActivePage;
    var bDoSlide = true;
    var bDoIt = true;
    var bDoFirstSlide = true;
    var aCursorPos = new Array();
    var oTimer = iElementY = iElementX = '';

    document.body.onmousemove = getCursor;

    // ---

    if( bDoFirstSlide == true && bDoSlide == true )
    {
        bDoSlide = false;

        jQuery( '#' + sActivePage + 'Page' ).css({visibility: 'visible'}).animate(
            { width: '656px' }, 
            { queue: false, duration: 300, 
                complete: function() {
                    jQuery( '#' + sActivePage + 'Arrow' ).removeClass( 'NotActive' ).addClass( 'Active' );

                    bDoSlide = true; 
                    bDoFirstSlide = false;
                }
            }
        );
    }

    // ---

    var doReset = function()
    {
        oEleSS = document.getElementById('slideshow');
        if( !iElementY ) iElementY = getY( oEleSS );
        if( !iElementX ) iElementX = getX( oEleSS );

        if( aCursorPos[0] ) iCursorPosX = aCursorPos[0];
        else iCursorPosX = 0;

        if( aCursorPos[1] ) iCursorPosY = aCursorPos[1];
        else iCursorPosY = 0; 

        // ---

        bDoIt = false;

        if(( ( iCursorPosX > iElementX ) && ( iCursorPosX < (iElementX + 1000) ) ))
        {
            if(( ( iCursorPosY > iElementY ) && ( iCursorPosY < (iElementY + 400) ) ))
            {
                bDoIt = true;
            } 
        }

        if( !bDoIt ) doSlide( sCurrentPage );
    }

    // ---

    function getCursor(e)
    {
        if (typeof e == 'undefined') evt = window.event;
        else evt = e;

        if( evt.pageX ) aCursorPos[0] = evt.pageX;
        else aCursorPos[0] = (evt.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - (document.documentElement.clientLeft || 0));

        if( evt.pageY ) aCursorPos[1] = evt.pageY;
        else aCursorPos[1] = (evt.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - (document.documentElement.clientTop || 0));

        return aCursorPos;
    }

    function getY( oEle ) 
    {
      var y = parseInt( oEle.offsetTop );
      if (!oEle.offsetParent) return y;
      else return y + getY(oEle.offsetParent);
    }

    function getX( oEle ) 
    {
        var x = parseInt( oEle.offsetLeft );
        if (!oEle.offsetParent) return x;
        else return x + getX(oEle.offsetParent);
    }

    // ---

    jQuery( '#ss-btnNeues, #ss-btnKonzeption, #ss-btnDesign, #ss-btnOnline' ).mouseover(function() {
        doSlide( this.id );
    });

    // ---

    function doSlide( oEle )
    {
        if( ( sActivePage != oEle ) && ( bDoSlide == true ) ) 
        {
            bDoSlide = false;
            window.clearInterval(oTimer);

            jQuery( '#' + sActivePage + 'Page' ).css({visibility: 'hidden'}).animate(
                { width: '0px' }, 
                { queue: false, duration: 150, 
                    complete: function() {
                        jQuery( '#' + sActivePage + 'Arrow' ).removeClass( 'Active' ).addClass( 'NotActive' );
                    }
                }
            );

            jQuery( '#' + oEle + 'Page' ).css({visibility: 'visible'}).animate(
                { width: '656px' }, 
                { queue: false, duration: 150, 
                    complete: function() {
                        jQuery( '#' + oEle + 'Arrow' ).removeClass( 'NotActive' ).addClass( 'Active' );

                        bDoSlide = true; 
                        sActivePage = oEle; 
                        oTimer = window.setTimeout(doReset, 2000);
                    }
                }
            );
        }
    }

    // ------------------------------------------------------------------------------

    jQuery('.viewport').mouseenter(function() {
        jQuery('a img', this).stop(false,true).animate({ height: '215', left: '0', top: '0', width: '214' }, 100 );
        jQuery('a span', this).stop(false,true).show();
    }).mouseleave(function() {
        jQuery('a img', this).stop(false,true).animate({ height: '250', left: '-20', top: '-20', width: '250' }, 100 );
        jQuery('a span', this).stop(false,true).hide();
    });

/*
    jQuery('.viewport').mouseenter(function() {
        jQuery('a img', this).stop(false,true).animate({ height: '125', left: '0', top: '0', width: '123' }, 100 );
        jQuery('a span', this).stop(false,true).show();
    }).mouseleave(function() {
        jQuery('a img', this).stop(false,true).animate({ height: '150', left: '-10', top: '-10', width: '150' }, 100 );
        jQuery('a span', this).stop(false,true).hide();
    });
*/
    // ------------------------------------------------------------------------------

    var iOldElement     = 0;
    var iCurrentElement = 0;
    var iDiff           = 0;
    var iCountPreview   = 0;
    var sPreviewOP      = '-';
    var bPreview        = true;

    // ---

    function doContentSlide( oEle )
    {
        switch( oEle.id ) {
          case "css-leftarrow":
            if( iOldElement == 0 ) iCurrentElement = 6;
            else iCurrentElement = ( iOldElement - 1 );
          break;
          case "css-rightarrow":
            if( iOldElement == 6 ) iCurrentElement = 0;
            else iCurrentElement = ( iOldElement + 1 );
          break;
        }

        if( iOldElement != iCurrentElement )
        {
            bPreview = false;
            sPreviewOP = ( iOldElement < iCurrentElement ? '-' : '+' );

            iDiff = ( iCurrentElement - iOldElement );
            iDiff = Math.abs(iDiff);

            iCountPreview = 276 * iDiff;

            // ---

            jQuery( '#css-slider-list' ).animate({
                left: sPreviewOP + '=' + iCountPreview
                }, 'normal', function() {
                    bPreview = true;
            });

            // ---

            iOldElement = iCurrentElement;
        }
    }

    // ---

    jQuery( 'a#css-leftarrow, a#css-rightarrow' ).click(function()
    {
        if( bPreview ) doContentSlide( this );
    });

    // ------------------------------------------------------------------------------

    var iRefID = jQuery('#iRefID').val();
    var doSlideDown = true;
    var doDetailLoad = true;
    var doFirstLoad = true;
    var bHeight = false;
    var iNewPos = 0;
    var oDiv;

    // ---

    if( doFirstLoad == true && iRefID.length >= 2 )
    {
        showReference( iRefID );
    }

    // ---

    jQuery( 'a.css-projekt' ).click(function()
    {
        showReference( this );
    });

    // ---

    function showReference( opEle )
    {
        if (typeof opEle == 'string') oEle = opEle;
        else oEle = opEle.id;

        if( doSlideDown )
        {
            doSlideDown = false;
            jQuery( '#ref-torso' ).slideDown("slow");
        }

        // ---

        if( doDetailLoad )
        {
            doDetailLoad = false;

            jQuery( '#ref-torso' ).load( '/index.php?id=136&bokP=' + oEle , function() 
            {
                if( !oDiv ) oDiv = document.getElementById('ref-torso');
                if( bHeight ) oDiv.style.height = 'auto';

                iNewPos = ( oDiv.offsetTop ? oDiv.offsetTop : 1350 );

                jQuery('html, body').animate({ scrollTop: iNewPos }, 'slow' );

                bHeight = true;
                doDetailLoad = true; 
                doFirstLoad = false;
            });
        }
    }

    // ---

    var UserAgent = navigator.userAgent.toLowerCase();

    if( UserAgent.search(/(iphone|ipod|ipad|opera mini|mobile safari|fennec|palm|blackberry|android|symbian|series60)/) == -1 && window.innerWidth > 1160 ) 
    {
        jQuery().toTop();
    }

});

