﻿///<reference path="/Scripts/jquery-1.4.1-vsdoc.js" />

var pos1;
var pos2;
var pos3;
var pos4;

var currentpos = 5;

var animating = false;

var page = 0;

var paused = false;

var timecounter = 1;

var spacing = 0;

var lastspacing = 0;

$(function () {
    UpdatePositions();
    var forceright = -3605 * 2;
    var start = forceright + pos1;
    $("#homepagescroller_forground").css("background-position", start + "px 0px");
    setTimeout("Start()", 1000);
});

function Start() {
    UpdatePositions();    
    HotspotPositions();
    spacing = getSpacing();
    var forceright = -3605 * 2;
    var start = forceright + pos1;
    $("#homepagescroller_forground").pan({ fps: 30, speed: 1.6, dir: 'left' });
    ///$("#homepagescroller_background").pan({ fps: 30, speed: 1, dir: 'left' });
    $("#homepagescroller_forground").spStop();
    $("#homepagescroller_forground").css("background-position", start + "px 0px");
    $("#homepagescroller_forground").spStart();
    $("#homepagescroller_forground").spSetBg(start);
    $(".hotspot").mouseover(function () {
        $(this).children("div").css("display", "block");
        timecounter = 1;
        paused = false;
    }).mouseleave(function () {
        $(this).children("div").css("display", "none");
        paused = true;
    });
    setInterval("position_check()", 100);
}

function SetBodyHeight() {
    var windowHeight = $(window).height();
    $("body").css("height", windowHeight + "px");
}

function homepage_left() {
    switch (currentpos) {
        case 1:
            page--;
            PositionShift(4);
            break;
        case 2:
            PositionShift(1);
            break;
        case 3:
            PositionShift(2);
            break;
        case 4:
            PositionShift(3);
            break;
    }
    $("#homepagescroller_forground").spStop();
    timecounter = 1;
    paused = true;
}

function homepage_right() {
    switch (currentpos) {
        case 1:
            PositionShift(2);
            break;
        case 2:
            PositionShift(3);
            break;
        case 3:
            PositionShift(4);
            break;
        case 4:
            page++;
            PositionShift(1);
            break;
    }
    $("#homepagescroller_forground").spStop();
    timecounter = 1;
    paused = true;
}

function PositionShift(position) {
    animating = true;
    $("#homepagescroller_forground").spStop();
    ///$("#homepagescroller_background").spStop();
    currentpos = position;
    var adder = page * -3605;
    switch (position) {
        case 1:
            TurnOffHotspots();
            $("#homepagescroller_forground").animate({ backgroundPosition: (pos1 + adder) + "px 0px" }, 400, "swing", function () {
                animating = false;
                UpdateHotspots();
            });
            $("#homepagescroller_forground").spSetBg(pos1 + adder);
            break;
        case 2:
            TurnOffHotspots();
            $("#homepagescroller_forground").animate({ backgroundPosition: (pos2 + adder) + "px 0px" }, 400, "swing", function () {
                animating = false;
                UpdateHotspots();
            });
            $("#homepagescroller_forground").spSetBg(pos2 + adder);
            break;
        case 3:
            TurnOffHotspots();
            $("#homepagescroller_forground").animate({ backgroundPosition: (pos3 + adder) + "px 0px" }, 400, "swing", function () {
                animating = false;
                UpdateHotspots();
            });
            $("#homepagescroller_forground").spSetBg(pos3 + adder);
            break;
        case 4:
            TurnOffHotspots();
            $("#homepagescroller_forground").animate({ backgroundPosition: (pos4 + adder) + "px 0px" }, 400, "swing", function () {
                animating = false;
                UpdateHotspots();
            });
            $("#homepagescroller_forground").spSetBg(pos4 + adder);
            break;
    }
}

function position_check() {

    if (paused == true) {
        timecounter++;
        if (timecounter == 26) {
            TurnOffHotspots();
            $("#homepagescroller_forground").spStart();
            //$("#homepagescroller_background").spStart();
            timecounter = 1;
            paused = false;
        }
    }
    UpdatePositions();
    HotspotPositions();
    var bgpos = $("#homepagescroller_forground").css("backgroundPosition");
    bgpos = bgpos.replace("px 0px", "").replace("-", "");
    var devid = roundNumber((bgpos / 3605), 2);

    var arr = devid.toString().split(".");
    if (arr.length > 1) {
        var sub1 = "." + arr[1];
        var sub2 = sub1 * -3605 - 50;
        var subpos = Math.floor(sub2);
        if (!animating) {
            currentpos = 4;
            if (subpos > pos4 || currentpos == 3) {
                currentpos = 3;
            }
            if (subpos > pos3 || currentpos == 2) {
                currentpos = 2;
            }
            if (subpos > pos2 || subpos < (-3605 + getSpacing()) || currentpos == 1) {
                currentpos = 1;
            }
        }

        if (subpos < (-3605 + getSpacing())) {
            arr[0]++;
        }

        page = arr[0];

        SetSectionName();
        //Debug Line
        //Uncomment for debug info
        //$("#status").html("Position 1: " + pos1 + "<br />" + "Position 2: " + pos2 + "<br />" + "Position 3: " + pos3 + "<br />" + "Position 4: " + pos4 + "<br /><br />" + "Full Position: -" + bgpos + "<br />" + "Sub Position: " + subpos + "<br />" + "Current Position: " + currentpos + "<br />" + "Current Page: " + page + "<br /><br />" + "Time Counter: " + timecounter + "<br />" + "Paused : " + paused + "<br />" + "Spacing: " + getSpacing() + "<br />");
    }
}

function roundNumber(num, dec) {
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    return result;
}

function getSpacing() {
    var bwidth = $("body").width();
    var cwidth = $("#bodycontent").width();
    var difwidth = bwidth - cwidth;
    var fromleft = difwidth / 2;
    return parseInt(fromleft);
}

function UpdatePositions() {
    var spacing = getSpacing();
    pos1 = 0 + spacing;
    pos2 = -761 + spacing;
    pos3 = -1610 + spacing;
    pos4 = -2500 + spacing;
}
function SetSectionName() {
    switch (currentpos) {
        case 1:
            $("#section_name").text("Farm");
            break;
        case 2:
            $("#section_name").text("Orchard");
            break;
        case 3:
            $("#section_name").text("Warehouse");
            break;
        case 4:
            $("#section_name").text("Grocery");
            break;
    }
}

function TurnOffHotspots() {
    $(".hotspot").each(function () {
        $(this).fadeOut("fast");
    });
}

function UpdateHotspots() {
    switch (currentpos) {
        case 1:
            $("#hotspot_two").fadeIn("fast");
            $("#hotspot_three").fadeIn("fast");
            break;
        case 2:
            $("#hotspot_four").fadeIn("fast");
            break;
        case 3:
            $("#hotspot_one").fadeIn("fast");
            $("#hotspot_five").fadeIn("fast");
            break;
        case 4:
            $("#hotspot_six").fadeIn("fast");
            $("#hotspot_seven").fadeIn("fast");
            break;
    }
}

function HotspotPositions() {
    var space = getSpacing();
    if (space != spacing) {
        $(".hotspot").each(function () {
            var currentspace = $(this).css("left");
            currentspace = parseInt(currentspace.replace("px", "")) - lastspacing;
            var fullspace = currentspace + space;
            $(this).css("left", fullspace + "px");
        });
        lastspacing = space;
    }
}
