$(document).ready(function() {
    $("div#imageflow p img[src=fileadmin/img/galerie_rechts.gif]").mousedown(function() {
        imageFlow.iVal = -1;
        imageFlow.startInterval();
    }).mouseup(function() {
        imageFlow.finishInterval();
    });
    
    $("div#imageflow p img[src=fileadmin/img/galerie_links.gif]").mousedown(function() {
        imageFlow.iVal = 1;
        imageFlow.startInterval();
    }).mouseup(function() {
        imageFlow.finishInterval();
    });
});


window.imageFlow = {
    oDiv      : null,
    oInterval : null,
    iVal      : -1,
    iDelay    : 1,
    iWidth    : 0,
    iMargin   : 0,
    bStopIt   : false,
    
    startInterval : function(iTempDelay) {
        this.stopInterval();
        this.bStopIt = false;
        
        this.iWidth = parseInt($("div#imageflow div").css("width").substr(0, ($("div#imageflow div").css("width").length - 2)));
        this.oInterval = window.setInterval("imageFlow.moveImageflow()", 25);
    },
    
    finishInterval : function() {
        //this.stopInterval();
        this.bStopIt = true;
    },
    
    stopInterval : function() {
        if (this.oInterval) {
            window.clearInterval(this.oInterval);
            this.oInterval = null;
        }
    },
    
    moveImageflow : function() {
        this.iMargin = this.iMargin + this.iVal;
        $("div#imageflow div").css("margin-left", this.iMargin + "px");
        
        if ((this.iVal < 0)) {
            if (this.bStopIt == true) this.iVal++;
            else if (this.iVal > -10) this.iVal--;
        }
        else if ((this.iVal > 0)) {
            if (this.bStopIt == true) this.iVal--;
            else if (this.iVal < 10) this.iVal++;
        }
            
        if ((this.iVal == -1) || (this.iVal == 1)) {
            this.stopInterval();
        }
        else if (this.iMargin > 0) {
            this.iMargin = 0;
            $("div#imageflow div").css("margin-left", "0px");
            this.stopInterval();
        }
        else if (this.iMargin < -1 * (this.iWidth - 551)) {
            this.iMargin = (-1 * (this.iWidth - 551));
            $("div#imageflow div").css("margin-left", this.iMargin + "px");
            this.stopInterval();
        }
    }
}


