﻿$(document).ready(function () {
    $('.contentHolderOuter').each(function () {
        var $outer = $(this);

        $.ajax({
            url: '/' + $('.urlHid', $outer).val(),
            context: document.body,
            success: function (data) {
                $(data).find('#pullThroughArea:first').appendTo($('.contentHolder', $outer))
                pullThroughLoadComplete($('.contentHolder', $outer), $outer);
                _gaq.push(['_trackPageview', '/' + $('.urlHid', $outer).val()]);
            },
            async: false
        });

        //        $('.contentHolder', $outer).load('/' + $('.urlHid', $outer).val() + ' #pullThroughArea:first', function () {


        //            pullThroughLoadComplete($(this));
        //            //$('.main .main', $outer).parents('.main').removeClass('main');
        //        });
    });

    function pullThroughLoadComplete($contentHolder, $outer) {
        $('.projectLinksDiv').hide();
        $('.projectHeader').children('.desc').hide();

        $('.main').each(function () {
            var $main = $(this);
            $('.projectLinksDiv', $main).hide();
            $('.projectHeader', $main).children('.desc').hide();

            $('.projectHeader:not(:first)', $main).hoverIntent(function (e) {
                if (!$(this).closest("div.section").find(".projectLinksDiv").is(":visible")) {
                    $(this).find('.desc').stop(true, true).show("slide", { direction: "left" }, 500);
                } else {
                    $(this).find('.desc').hide();
                }
            }, function (e) {
                if (!$(this).closest("div.section").find(".projectLinksDiv").is(":visible") && $(this).find('.desc').is(":visible")) {
                    $(this).find('.desc').stop(true, true).hide("slide", { direction: "left" }, 500);
                } else {
                    $(this).find('.desc').hide();
                }
            });
        });

        $('.backToTop', $outer).live("click", function (e) {
            e.preventDefault();
            $('html,body').animate({
                scrollTop: 0
            }, 500, 'easeOutCirc', function () {

            });
        });

        $('.closeProject', $outer).live("click", function (e) {
            e.preventDefault();
            $this = $(this);
            $('html,body').animate({
                scrollTop: $(this).closest('div.section').find('.pullThroughLinkImg').offset().top - 30
            }, 500, 'easeOutCirc', function () {
                $('.projectLinksDiv', $this.closest('div.section')).hide();
                $('.pullThroughHolderDiv', $this.closest('div.section')).slideUp('slow', function () {

                });
            });

        });

        $('.closeAll', $outer).live("click", function (e) {
            e.preventDefault();
            var $this = $(this);
            var pullThroughCount = $('.pullThroughHolderDiv').length;
            var pullThroughCounter = 0;
            $('.pullThroughHolderDiv').slideUp('slow', function () {
                $('.projectLinksDiv').hide();
                pullThroughCounter++;
                if (pullThroughCounter == pullThroughCount) {
                    $('html,body').animate({
                        scrollTop: $this.closest('div.section').find('.pullThroughLinkImg').offset().top - 30
                    }, 500, 'easeOutCirc');
                }
            });
        });

        $('.nextProject', $outer).live("click", function (e) {
            e.preventDefault();
            if (!$(this).hasClass('disabled')) {
                var thisIndex = $('.pullThroughLinkImg').index($(this).closest('div.section').find('.pullThroughLinkImg'));
                selectProject($('.pullThroughLinkImg').eq(thisIndex + 1), true);
            }
        });

        $('.previousProject', $outer).live("click", function (e) {
            e.preventDefault();
            if (!$(this).hasClass('disabled')) {
                var thisIndex = $('.pullThroughLinkImg').index($(this).closest('div.section').find('.pullThroughLinkImg'));
                selectProject($('.pullThroughLinkImg').eq(thisIndex - 1), true);
            }
        });

        $('.slideshow', $contentHolder).after('<div class="pager"></div>').cycle({
            prev: $contentHolder.find('.prev'),
            next: $contentHolder.find('.next'),
            pager: $contentHolder.find('.pager'),
            timeout: 0,
            speed: 600,
            easing: 'easeInOutCubic',
            fx: 'scrollHorz' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        });

        // load production here
        $('.autoLoadProject', $outer).each(function () {
            var url = '/' + $(this).closest('div.section').children('#autoProjectUrlHid').val();
            $(this).load(url + ' .project:first', function () {
                $('.slideshow', $(this)).after('<div class="prev">Prev</div>', '<div class="next">Next</div>').cycle({
                    prev: $(this).find('.prev'),
                    next: $(this).find('.next'),
                    pager: $(this).find('.pager'),
                    timeout: 0,
                    speed: 600,
                    easing: 'easeInOutCubic',
                    fx: 'scrollHorz' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                });
                _gaq.push(['_trackPageview', url]);
            });
        });

        $('.nextProject', $outer).show();
        $('.main', $outer).each(function () {
            $('.nextProject:last', $(this)).hide();
        });
        $('.previousProject', $outer).show();
        $('.main', $outer).each(function () {
            $('.previousProject:first', $(this)).hide();
        });

        $('.pullThroughLinkImg', $outer).live("click", function () {
            selectProject($(this));
        });

        $('.desc', $outer).live('click', function () {
            selectProject($(this).closest('.section').find('.pullThroughLinkImg'));
        });
    }

    //    $('.projectHeader').live("click", function () {
    //        $(this).find('.pullThroughLinkImg:first').click();
    //    });



    function selectProject($pullThroughLinkImg, keepOpen) {
        var loadComplete = 0;
        var animateComplete = 0;
        var $projectLinksDiv = $('.projectLinksDiv', $pullThroughLinkImg.closest('div.section'));
        var $pullThroughHolderDiv = $('.pullThroughHolderDiv', $pullThroughLinkImg.closest('div.section'));
        $('html,body').animate({
            scrollTop: $pullThroughLinkImg.offset().top - 30
        }, 500, 'easeOutCirc', function () {
            animateComplete = 1;
            openProjectIfReady($pullThroughLinkImg, $pullThroughHolderDiv, loadComplete, animateComplete);
        });

        if (!$projectLinksDiv.is(":visible")) {
            var $urlHid = $('.urlHid', $pullThroughLinkImg.closest('div.section'));
            $pullThroughHolderDiv.hide();
            $pullThroughHolderDiv.load('/' + $urlHid.val() + ' .project:first', function () {
                $('.slideshow', $pullThroughHolderDiv).after('<div class="prev">Prev</div>', '<div class="next">Next</div>').cycle({
                    prev: $pullThroughHolderDiv.find('.prev'),
                    next: $pullThroughHolderDiv.find('.next'),
                    pager: $pullThroughHolderDiv.find('.pager'),
                    timeout: 0,
                    speed: 600,
                    easing: 'easeInOutCubic',
                    fx: 'scrollHorz' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
                });
                loadComplete = 1;
                openProjectIfReady($pullThroughLinkImg, $pullThroughHolderDiv, loadComplete, animateComplete);
                _gaq.push(['_trackPageview', '/' + $urlHid.val()]);
            });

        } else {
            if (!keepOpen) {
                $('.projectLinksDiv', $pullThroughLinkImg.closest('div.section')).fadeOut(400);
                $pullThroughHolderDiv.slideUp(500, function () {
                    if (navigator.userAgent.match(/like Mac OS X/i)) {

                        document.getElementById('fixed').style.top =
     			(window.pageYOffset + window.innerHeight - 45) + 'px';


                    }
                });

            }
        }

    }

    function openProjectIfReady($pullThroughLinkImg, $pullThroughHolderDiv, loadComplete, animateComplete) {
        if (loadComplete === 1 && animateComplete === 1) {
            if ($pullThroughLinkImg.closest('div.section').find('.desc').is(':visible')) {
                $pullThroughLinkImg.closest('div.section').find('.desc').stop(true, true).hide("slide", { direction: "left" }, 500);
            }
            $pullThroughHolderDiv.hide();
            $pullThroughHolderDiv.slideToggle(500, function () {
                $('.projectLinksDiv', $pullThroughLinkImg.closest('div.section')).fadeIn(500);
                if (navigator.userAgent.match(/like Mac OS X/i)) {

                    document.getElementById('fixed').style.top =
     			(window.pageYOffset + window.innerHeight - 45) + 'px';


                }
            });

        }
    }
});
























