/***********************************************
* idea from
* Cross browser Marquee II- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
Forward/reverse with speed adjustments, jquerry implementation 
and any thing else remotely cool and/or sexy
© by Jakob Marasch 2011 for
DMT Workholding, 
210 Slinger Rd, 
Slinger, Wi 53086
Code may be freely used and distributed provided 
this notice MUST stay intact for legal use
code may not be sold for monitary or personal gains,
if you paid for this script you got ripped off call the
proper authorites, not me.

*****************************************************************
OPTIONS
These are default options if you want to have different settings
Call setOptions() use nulls to keep default options
****************************************************************/
var divName = "scrlObj"; //name of div objects + a number below maxObjs

//negative numbers here cause the scroller to scroll up
//positive numbers will cause the scroller to scroll down
var marqueespeed    =  -2; //Specify idle scroll speed (larger is faster 1-10)
var mouseWheelSpeed = -10; //Specify active scroll speed (larger is faster 1-10)

//speed multipliers for edge scrolling, use negative or positive to adjust edge scroll direction
var fasterScrollMult  = 2; //first stage of faster edge scroll
var fastestScrollMult = 3; //second stage of faster edge scroll

var delayb4scroll = 000; //Specify initial delay before marquee starts to scroll on page (2000=2 seconds)
var pauseit       =   1;         //Pause marquee onMousever (0=no. 1=yes)?
var maxObjs       = 100;       //max div objects to search for
/***************************************************/

////start code////
////No changes should be required below this line////

function setOptions(DivName,
                    Marqueespeed,
                    MouseWheelSpeed,
                    FasterScrollMult,
                    FastestScrollMult,
                    Delayb4scroll,
                    Pauseit,
                    MaxObjs) {
    DivName != null ? divName = DivName : null;
    Marqueespeed != null ? marqueespeed = Marqueespeed : null;
    MouseWheelSpeed != null ? mouseWheelSpeed = MouseWheelSpeed : null;
    FasterScrollMult != null ? fasterScrollMult = FasterScrollMult : null;
    FastestScrollMult != null ? fastestScrollMult = FastestScrollMult : null;
    Delayb4scroll != null ? delayb4scroll = Delayb4scroll : null;
    Pauseit != null ? pauseit = Pauseit : null;
    MaxObjs != null ? maxObjs = MaxObjs : null;

    copyspeed = marqueespeed;
    pausespeed = (pauseit == 0) ? copyspeed : 0;

    findObjs();
}

var copyspeed = marqueespeed;
var pausespeed = (pauseit == 0) ? copyspeed : 0;
var actualheight = '';
var mainDiv = null;
var divs = new Array();
var divCount = 0;

function scrollmarquee() {
    for (var i = 0; i < divCount; i++) {
        //move all divs up one step
        divs[i].style.top = parseInt(divs[i].style.top) + copyspeed + "px";
    }
    checkLocations(copyspeed);
}

function checkLocations(move) {
    var tmpobj;
    //scroll direction
    if (move < 0) {
        //is top div still visable
        if (Math.abs(parseInt(divs[0].style.top)) >= parseInt(divs[0].style.height)) {
            divs[0].style.top = parseInt(divs[divCount - 1].style.top) + parseInt(divs[divCount - 1].style.height) + "px";
            tmpobj = divs[0];
            for (var j = 1; j < divCount; j++) {
                divs[j - 1] = divs[j];
            }
            divs[divCount - 1] = tmpobj;
        }
    } else if (move > 0) {
        //do we need to bring up one from the bottom
        if (parseInt(divs[0].style.top) >= 0) {
            divs[divCount - 1].style.top = parseInt(divs[0].style.top) - parseInt(divs[divCount - 1].style.height) + "px";
            tmpobj = divs[divCount - 1];
            for (var k = divCount - 2; k >= 0; k--) {
                divs[k + 1] = divs[k];
            }
            divs[0] = tmpobj;
        }
    }
    divs[0].style.visibility = "visible";
}

function findPos(obj) {
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
        return [curleft, curtop];
    }
    return [0.0];
}

function findObjs() {
    divs = new Array();
    mainDiv = document.getElementById(divName);
    //find all scroller objects upto the defined max
    if (mainDiv != null || mainDiv != undefined) {
        for (var i = 0; i < maxObjs; i++) {
            var name = divName + i;
            var divobj;
            divobj = document.getElementById(name);
            if (divobj == null || divobj == undefined) i = maxObjs; //exit for loop
            else {
                divs[i] = divobj; 
                if (i == 0) {
                    divs[i].style.top = 0 + "px";
                    divs[i].style.height = divs[i].offsetHeight + "px";
                }
                else {
                    divs[i].style.top = parseInt(divs[i - 1].style.top) + divs[i - 1].offsetHeight + 1 + "px";
                    divs[i].style.height = divs[i].offsetHeight + "px";
                }
                divCount += 1;
            }
        }
        if (window.opera || navigator.userAgent.indexOf("Netscape/7") != -1) { //if Opera or Netscape 7x, add scrollbars to scroll and exit
            mainDiv.style.height = marqueeheight + "px";
            mainDiv.style.overflow = "scroll";
            return;
        }
    }
}

$(document).ready(function () {
    findObjs();
    setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll);

    $("#" + divName).mousemove(function () {
        if (mainDiv == null || mainDiv == undefined) return;

        var mainDivHight = parseInt(mainDiv.style.height);
        var offsetY = findPos(mainDiv)[1]; //parseInt(mainDiv.offsetTop);
        var prPx = mainDivHight / 100;
        var mousePos = Math.round((window.event.clientY - offsetY) / prPx);

        if (mousePos >= 0 && mousePos <= 5) copyspeed = marqueespeed * -fastestScrollMult;
        if (mousePos > 5 && mousePos <= 25) copyspeed = marqueespeed * -fasterScrollMult;
        if (mousePos > 25 && mousePos <= 75) copyspeed = 0;
        if (mousePos > 75 && mousePos <= 95) copyspeed = marqueespeed * fasterScrollMult;
        if (mousePos > 95 && mousePos <= 100) copyspeed = marqueespeed * fastestScrollMult;

    });
    $("#" + divName).mouseout(function () { copyspeed = marqueespeed; });
    $("#" + divName).mousewheel(function (event, delta) {
        for (var j = 0; j < divCount; j++) {
            divs[j].style.top = parseInt(divs[j].style.top) + (delta * mouseWheelSpeed) + "px";
        }
        checkLocations(-delta);
    });

});
