﻿var t;
var i = 0;

$(function() {
    if ($('.workinfoRight').length > 0) {
		workImgSlider();
		t = setTimeout("cycleImg()", 6000);
}
	homerotator();	
	t = setTimeout("homecycle(0)", 3600);
});



this.homerotator = function () {
    $('.navigation div').click(function () {
        var pos = $('.navigation div').index(this);

        var $active = $('.innercase div.currentcase');
        var $next = $('#workCarousel .case_' + pos);
        var $nav = $('.navigation div.current');

        $nav.removeClass('current');

        $active.fadeOut(300, function () {
            $next.fadeIn(300, function () {
                $active.removeClass('currentcase');
            });
        });
        $next.addClass('currentcase');

        $(this).addClass('current');
        clearTimeout(t);
        
        t = setTimeout("homecycle(" + pos + ")", 3600);
    });
}

this.homecycle = function (casenum) {
    var casecount = $('.innercase > div').size() - 1;
    var activecase = casenum;

    //console.log($('.innercase div.currentcase').next().length);

    if (casenum == casecount) activecase = 0; else activecase++;

    var $active = $('.innercase div.currentcase');
    var $nav = $('.navigation div.current');

    if ($active.next().length > 0) {
        $active.fadeOut(300, function () {
            $active.next().fadeIn(300, function () {
                $active.removeClass('currentcase').next().addClass('currentcase');
            });
        });
        $nav.removeClass('current').next().addClass('current');
    }
    else {
        $active.fadeOut(300, function () {
            $('.innercase div.case_0').fadeIn(300);
        });
        $active.removeClass('currentcase');
        $('.innercase div.case_0').addClass('currentcase');
        $nav.removeClass('current');
        $('.navigation div.navdot0').addClass('current');

    }

    t = setTimeout("homecycle(" + activecase + ")", 3600);
}

this.workImgSlider = function () {
    $('.imgSwithcer li a').click(function () {
        

        $('.imgSwithcer li a').removeClass('current');
        $(this).addClass('current');

        $('.imgHolder li.imgactive').fadeOut(600);
        $('.imgHolder li.imgactive').removeClass('imgactive');

        var img = $(this).attr('href');
        $('li' + img).addClass('imgactive').fadeIn(600);

        $(this).addClass('current');
        
        clearTimeout(t);
        t = setTimeout("cycleImg()", 6400);
        return false;
    });
}

this.cycleImg = function () {

    var $active = $('.imgHolder li.imgactive');
    var $nav = $('.imgSwithcer li a.current');

    if ($active.next().length > 0) {
        $active.fadeOut(600, function () {
            $active.next().fadeIn(600, function () {
                $active.removeClass('imgactive').next().addClass('imgactive');
            });
        });
        $nav.removeClass('current').parent().next().contents().addClass('current');
        clearTimeout(t);
        t = setTimeout("cycleImg()", 6400);
    }
    else {
        $active.fadeOut(600, function () {
            $('.imgHolder li:first').addClass('imgactive').fadeIn(600);
        });
        $nav.removeClass('current');
        $('.imgSwithcer li a:first').addClass('current');
        clearTimeout(t);
        t = setTimeout("cycleImg()", 6400);
    }

}

this.divJump = function (name) {
    var div = $("div#" + name);
    $.scrollTo(div, 400, { axis:'y' });
}

$(document).ready(function () {

    $(window).keydown(function (event) {

        switch (event.keyCode) {

            case 40: //downArrow
                i++; //initialize
                if ($("div#section" + i).length == 0) {
                    i = i - 1; //dont add do i when it isnt there.
                }
                else if ($("div#section" + i).length > 0) {
                    divJump('section' + i); // only jump if its there
                    event.preventDefault();
                }
                break;
            case 38: //upArrow
                if (i > 1) {
                    i = i - 1;
                    divJump("section" + i);
                }
                else {
                    i = 0;
                    $.scrollTo(0, 400, { axis: 'y' });
                }
                break;
            case 37: //leftArrow
            case 39: //rightArrow            
                var wp = buildPageArray();
                var sPath = window.location.pathname;
                var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
                sPage = sPage.toLowerCase();
                var idx = wp.indexOf(sPage);

                switch (event.keyCode) {
                    case 37:
                        if (idx > 0) {
                            window.location = "/work/" + wp[idx - 1];
                        }
                        else {
                            window.location = "/work/" + wp[14];
                        }
                        break;
                    case 39:
                        if (idx < 17) {
                            window.location = "/work/" + wp[idx + 1];
                        }
                        else {
                            window.location = "/work/" + wp[0];
                        }
                        break;
                }
        }

    });
});

function buildPageArray() {
    //build Array of pages (in correct order)


    var workPages = new Array();
    workPages[0] = "centralmarketcase.aspx";
    workPages[1] = "livestrongredesign.aspx";
    workPages[2] = "lsiphone.aspx";
    workPages[3] = "lsmobile.aspx";
    workPages[4] = "lg.aspx";
    workPages[5] = "delllounge.aspx";
    workPages[6] = "utvac.aspx";
    workPages[7] = "dell.aspx";
    workPages[8] = "unicast.aspx";
    workPages[9] = "hrhthejoint.aspx";
    workPages[10] = "nascar.aspx";
    workPages[11] = "centralmarket.aspx";
    workPages[12] = "aeg.aspx";
    workPages[13] = "simplerwebb.aspx";
    workPages[14] = "hoovers.aspx";
    workPages[15] = "netfits.aspx";
    workPages[16] = "scion.aspx";
    workPages[17] = "lollapalooza.aspx";

    return workPages;
}    

