﻿var scrollerInterval;
var bounceInterval;
var scrollerpos = 0;
var scrollerstep = 20;
var slowdownat = 34;
var slowdowndiff = 70;

var offsetX = 25;
var setoffset = false;
var offFrom0 = 0;
var maxleft = 0 + offFrom0;
var scrolling = false;
var maxbounce = 2;
var maxbouncetimes = 3;
var PE = 0;
var bouncing = false;
var bouncedir = 1;
var bounceEnabled = false;
var bouncetimes = 2;
var IsPlaying = false;
var scrollAtATime = 1;
var iconsvisible = 5;
var increment = parseInt(((scrollAtATime * 103) + (scrollAtATime / iconsvisible)));
var minicon = 0;
var totalicons = 9;
var maxicon = parseInt((totalicons - iconsvisible) / scrollAtATime);
//alert(maxicon);
//maxicon += (((totalicons - iconsvisible) % iconsvisible) > 0 ? 1 : 0);
//alert(maxicon);
var curicon = 0;


function bounce() {
    var scroller = $get("scrollermain");
    var position = Sys.UI.DomElement.getBounds(scroller);
    var x = position.x;
    var y = position.y;

    if (PE > 0) {
        if (bouncedir == 1) { scrollerpos = maxleft - PE; } else { scrollerpos = maxleft + PE; }
        bouncedir = -bouncedir;
        bouncetimes--;

        if (bouncetimes == 0) { PE--; bouncetimes = maxbouncetimes; }
    }
    if (PE == 0) { resetScroller(); window.clearInterval(bounceInterval); Sys.UI.DomElement.setLocation(scroller, maxleft, 0); } else { Sys.UI.DomElement.setLocation(scroller, scrollerpos, 0); }
}

function resetScroller() {
    PE = 0;
    scrolling = false;
    bouncing = false;
    scrollerstep = 11;
}

function movescroller(forward) {
    var scroller = $get("scrollermain");
    var position = Sys.UI.DomElement.getBounds(scroller);
    var x = position.x;
    var y = position.y;


    if (forward) {
        if (scrollerpos < slowdownat && scrollerstep > 1) { scrollerstep -= 1 }
        if ((scrollerpos - scrollerstep) < maxleft) {
            scrollerpos = maxleft; clearInterval(scrollerInterval);
            if (!bounceEnabled) {
                resetScroller();
            }
            else {
                PE = maxbounce;
                bounceInterval = window.setInterval("bounce()", 50);
            }
        }
        else {
            scrollerpos -= scrollerstep;
        }
    }
    else {
        if (scrollerpos > slowdownat) {
            if (scrollerstep > 1)
                scrollerstep -= 1;
        }
        if ((scrollerpos + scrollerstep) > maxleft) {
            scrollerpos = maxleft;
            clearInterval(scrollerInterval);
            if (!bounceEnabled) {
                resetScroller();
            }
            else {
                PE = maxbounce;
                bounceInterval = window.setInterval("bounce()", 50);
            }
        }
        else {
            scrollerpos += scrollerstep;
        }
    }

    Sys.UI.DomElement.setLocation(scroller, scrollerpos, 0);
}

function scroll(forward) {
    if (scrolling || bouncing) {
        return;
    }

    scrolling = true;

    var scroller = $get("scrollermain");
    var position = Sys.UI.DomElement.getBounds(scroller);

    var x = position.x;
    var y = position.y;
    var w = position.width;
    var h = position.height;

    if (!setoffset) {
        offsetX = x;
        setoffset = true;
    }

    if (forward) {
        if ((curicon + 1) > maxicon) {
            if (bouncing)
                return;
            bouncing = true;
            PE = maxbounce;
            bounceInterval = window.setInterval("bounce()", 50);
            return;
        }
        curicon++;
        scrolling = true;

        // forward nav is backward scroll
        maxleft -= increment;
        slowdownat = maxleft + slowdowndiff;
        scrollerInterval = setInterval("movescroller(true)", 10);
    }
    else {
        if ((curicon - 1) < minicon) {
            if (bouncing)
                return;
            bouncing = true;
            PE = maxbounce;
            bounceInterval = window.setInterval("bounce()", 50);
            return;
        }
        curicon--;

        scrolling = true;
        scrollerstep = 11;
        maxleft += increment;
        slowdownat = maxleft - slowdowndiff;
        scrollerInterval = setInterval("movescroller(false)", 10);
    }
}
resetScroller();
var icondata =
		{ "icons": [
			{ "iconurl": "about/commercials/", "newwindow": false },
			{ "iconurl": "about/news-releases/", "newwindow": false },
			{ "iconurl": "mailto:report.fraud@safeauto.com?subject=On-Line%20Fraud%20Report", "newwindow": false },
			{ "iconurl": "about/faqs/", "newwindow": false },
			{ "iconurl": "about/events/", "newwindow": false },
			{ "iconurl": "auto-insurance-claims/", "newwindow": false },
			{ "iconurl": "state-insurance/", "newwindow": false },
			{ "iconurl": "about/safeauto-motorsports/wyler-racing/", "newwindow": true },
			{ "iconurl": "http://www.dothejingle.com/", "newwindow": true },
			]
		}

function processGlassClick(e) {
    var iconClicked = Math.floor(e.offsetX / 103) + (((e.offsetX % 103) > 0) ? 1 : 0) + (curicon - 1);
    var linkElement = $get('S' + iconClicked.toString());
    e.preventDefault();
    if (icondata.icons[iconClicked].newwindow == true)
        window.open(icondata.icons[iconClicked].iconurl, "_blank");
    else
        location.href = icondata.icons[iconClicked].iconurl;

    //linkElement.click();			
}