(function() {

    var scrollDuration = 750;
    var catchUpDuration = 350;
    var relWidth;
    var diff;
    var cloudsDiff;
    var scrollPositions = {};
    var codaStopped = false;
    var windowLeft;
    var stopScroll = false;

    $(document).ready(function() {

      $(".fancyFrame").click(function() {
        $("#coda-nav-1").find(".current").click();
      });

        if($.browser.msie){
            $("#tour2Cow > .beam").hide();
            $("#landscape2").hide();
            $("#clouds").css('position','fixed');
        }

        $('#coda-slider-1').codaSlider({
            dynamicArrows: false,
            dynamicTabs: false
        });


        if ($(window).width() > $("#clouds").width()) {
            $("#clouds").css({width: $(window).width()}); // TODO: figure out this ratio
        }
        if ($(window).width() > $("#landscape2").width()) {
            $("#landscape2").css({width: $(window).width()}); // TODO: figure out this ratio
        }
        relWidth = $(document).width() - $(window).width();
        diff = $("#landscape1").outerWidth() - $("#landscape2").outerWidth();
        cloudsDiff = $("#landscape1").outerWidth() - $("#clouds").outerWidth();

        $("a.scroll").each(function(idx, el) {
            var elId = $(this).attr('href');
            if (elId != 'javascript:') {
                scrollPositions[$(this).attr('href')] = $(elId).position().left;
            }
        });

        $('a.scroll').click(function(event) {
            event.stopPropagation();
            stopScroll = true;

            var footTop = $("#foot").height() - $(document).height();

            $("#head").addClass('fixed').css('left', 0).css('top', -$(document).scrollTop());
            $("#foot").addClass('fixed').css('left', 0).css('top', -$(document).scrollTop() + 820);


            if (!codaStopped) {
                $("#coda-nav-1").find(".tab1 a").click();
                codaStopped = true;
            }

            var elId = $(this).attr('href');
            var relScroll = scrollPositions[elId] / relWidth;
            if (relScroll > 1) relScroll = 1;

            $('html,body').animate({scrollLeft: scrollPositions[elId]}, scrollDuration, 'linear');
            if(!$.browser.msie){
                $("#landscape2").animate({left: (diff * relScroll)}, scrollDuration, 'linear');
                $("#clouds").animate({left: (cloudsDiff * relScroll)}, scrollDuration, 'linear');
            }
            windowLeft = $('html,body').scrollLeft();
            return false;
        });


        $("#tour2Cow > .cow").click(function () {
            var craftIntro = {
                height:'85px',
                width:'170px',
                bottom:'376px',
                left:'-36px'
            };
            if (!$.browser.webkit && !$.browser.safari && !$.browser.opera) {
                $("#tour2Cow .craft").css({
                    height:'85px',
                    width:'170px',
                    bottom:'1000px'
                });

                craftIntro = {
                    height:'85px',
                    width:'170px',
                    bottom:'383px',
                    left:'-43px'
                };
            }

            $("#tour2Cow .craft").animate(craftIntro, 2000, 'easeOutQuad', function() {
                $("#tour2Cow > .cow").css({'background-position': '0px -114px'});
                if($.browser.msie) { $("#tour2Cow > .beam").show(); }
                $("#tour2Cow > .beam").animate({opacity: 1}, 1000, 'linear', function() {
                    $("#tour2Cow > .cow").animate({opacity: 0, top: '-260px'}, 1000, 'easeInQuad', function() {
                        $("#tour2Cow > .beam").animate({opacity: 0}, 1000, 'linear', function() {
                            $("#tour2Cow .craft").animate({bottom: '1000px'}, 1000, 'easeInQuad');
                        });
                    });
                });


            });
        });

        $("#solutionslist li").click(function(event) {

            if (!$(this).hasClass("on")) {

                var clickedClass = $(this).attr("class");

                $("#solutionsFor .on").removeClass("on");
                $("#dynamiccontent .on").removeClass("on");
                $("#moreinfo .on").removeClass("on");

                $("#solutionsFor").find("." + clickedClass).addClass("on");
                $("#dynamiccontent").find("." + clickedClass).addClass("on");
                $("#moreinfo").find("." + clickedClass).addClass("on");
            }
        });


    });

    //    jQuery(window).bind('scrollstart', function() {
    //    });

    jQuery(window).bind('scrollstop', function(e) {
        var scrollLeft = $(document).scrollLeft();
            $("#head").removeClass('fixed').css('left', scrollLeft + 'px').css('top', '0px');
            $("#foot").removeClass('fixed').css('left', scrollLeft + 'px').css('top', '820px');
        if (!stopScroll && !$.browser.webkit && !$.browser.safari && !$.browser.opera) {
            $("#head").animate({left: $(document).scrollLeft()}, catchUpDuration);
            $("#foot").animate({left: $(document).scrollLeft()}, catchUpDuration);
        }
        stopScroll = false;        
    });


})(window);
