YUI().use('node', 'event', 'history', 'anim', function (Y) {

    function stopWheel(e) {
        if (!e) { e = window.event; } /* IE7, IE8, Chrome, Safari */
        if (e.preventDefault) { e.preventDefault(); } /* Chrome, Safari, Firefox */
        e.returnValue = false; /* IE7, IE8 */
    }
    var currentScroll = '';
    var history = new Y.HistoryHash();
    var doScroll = function (target) {
        if (window.scrollAnim)
            window.scrollAnim.stop();

        var scrollTarget = Y.one('#' + target.replace('sct=', ''));
        if (!scrollTarget || currentScroll == target) {
            return;
        }
        currentScroll = target;
        document.onmousewheel = function () { stopWheel(); } /* IE7, IE8 */
        if (document.addEventListener) { /* Chrome, Safari, Firefox */
            document.addEventListener('DOMMouseScroll', stopWheel, false);
        }

        window.scrollAnim = new Y.Anim({
            node: Y.one('window'),
            to: {
                scroll: function () {
                    return [0, scrollTarget.getY()];
                }
            },
            duration: 2,
            easing: Y.Easing.easeOutStrong
        });

        window.scrollAnim.on('end', function () {
            history.replaceValue('sct', target.replace('sct=', ''));
            document.onmousewheel = null;  /* IE7, IE8 */
            if (document.addEventListener) { /* Chrome, Safari, Firefox */
                document.removeEventListener('DOMMouseScroll', stopWheel, false);
            }

        });

        window.scrollAnim.run();

        Y.all('.highlighted-news').removeClass('highlighted-news');
        var sibItm = scrollTarget.siblings('.itm');
        sibItm.each(function (item) { item.addClass('highlighted-news'); });
    };

    Y.one('window').on('scroll', function () {
        if (!window.scrollAnim || !window.scrollAnim.get('running')) {
            currentScroll = '';
        }
    });

    var scrollPage = function (e) {

        var tg = e.currentTarget.get('hash').substr(1);

        e.preventDefault();
        if (Y.one('#' + tg)) {
            // remove the active class on each menu item
            Y.all('ul#nav-test > li').each(function (item) {
                item.removeClass('active');
            });

            // add the active class on the clicked menu element
            if (e.currentTarget.ancestor('ul#nav-test')) {
                e.currentTarget.ancestor('li').addClass('active');
            }
            doScroll(tg);
        } else {
            if (window.fadeAnim) window.fadeAnim.stop();

            window.fadeAnim = new Y.Anim({
                node: Y.one('#main'),
                to: {
                    opacity: 0
                },
                duration: 0.4
            });
            window.fadeAnim.on('end', function () {
                window.location.href = e.currentTarget.get('href');
            });

            window.fadeAnim.run();
        }
    };
    history.on('change', function (e) {
        if (e.src != 'hash')
            return;
        if (e.changed.sct) {
            doScroll(e.changed.sct.newVal);
        } else {
            if (window.scrollAnim) window.scrollAnim.stop();
            window.scrollAnim = new Y.Anim({
                node: Y.one('window'),
                to: {
                    scroll: function () {
                        return [0, 0];
                    }
                },
                duration: 2,
                easing: Y.Easing.easeOutStrong
            });
            window.scrollAnim.run();
        }
    });

    Y.on('click', scrollPage, 'a.scrollable');

    var fade = function (e) {
        e.preventDefault();
        if (window.fadeAnim) window.fadeAnim.stop();

        window.fadeAnim = new Y.Anim({
            node: Y.one('#main'),
            to: {
                opacity: 0
            },
            duration: 0.4
        });
        window.fadeAnim.on('end', function () {
            window.location.href = e.currentTarget.get('href');
        });

        window.fadeAnim.run();
    };

    Y.on('click', fade, 'a.fading');

    var rollIn = function(e) {
        var target = e.currentTarget;
        if (target) {
            var pop = target.one('div.popable');
            if (!pop) return;
            var popAnim = new Y.Anim({
                    node: pop,
                    to: {
                        bottom: 0
                    },
                    duration: 0.5,
                    easing: Y.Easing.easeOutStrong
                });

            popAnim.run();
        }
    };

    var rollOut = function (e) {
        var target = e.currentTarget;
        if (target) {
            var pop = target.one('div.popable');
            if (!pop) return;
            var delta = pop.getComputedStyle('height').replace('px', '') * -1;
            var unpopAnim = new Y.Anim({
                node: pop,
                to: {
                    bottom: delta
                },
                duration: 0.5,
                easing: Y.Easing.easeOutStrong
            });

            unpopAnim.run();
        }
    };

    Y.on('mouseenter', rollIn, '.rollover');
    Y.on('mouseleave', rollOut, '.rollover');

    Y.on('available', function () {

        if (window.fadeAnim) window.fadeAnim.stop();

        window.fadeAnim = new Y.Anim({
            node: Y.one('#main'),
            to: {
                opacity: 1
            },
            duration: 1
        });


        window.fadeAnim.run();
        window.fadeAnim.on('end', function () {
            if (window.location.hash != '') {
                doScroll(window.location.hash.replace('#sct=', '').replace('#', ''));
            }
        });
    }, 'body');

});

$(document).ready(function () {
    var slides = $('div.slide-proof').hide();
    var fadeValue = 1500;
    var nextSlideBtn = $('#next-slide').hide();
    var prevSlideBtn = $('#prev-slide').hide();
    var busy = false;

    $('#hm-teaser').mouseover(function (e) {
        if (busy) return;
        prevSlideBtn.show();
        nextSlideBtn.show();
    });
    $('#hm-teaser').mouseout(function (e) {
        prevSlideBtn.hide();
        nextSlideBtn.hide();
    });
    var index = 0;
    for (var i = 0; i < slides.length; i++) {
        if (slides.eq(i).hasClass('current-slide')) {
            index = i;
            break;
        }
    }

    slides.eq(index).show();
    $('div#hm-teaser').css({visibility: 'visible'});
    var slideHide = function () {

        slides.eq(index).fadeOut(fadeValue, function () {
            if (index >= slides.length - 1) index = 0;
            slides.eq(++index).fadeIn(fadeValue, function () { slides.eq(index - 1).hide(); });
        });
        
    };

    var tick = setInterval(slideHide, 5000);

    prevSlideBtn.click(function (e) {
        if (busy) return;
        busy = true;
        nextSlideBtn.trigger('mouseout');
        prevSlideBtn.trigger('mouseout');
        e.preventDefault();
        clearInterval(tick);

        slides.eq(index).fadeOut(fadeValue);
        if (index <= 0) index = slides.length - 1;
        slides.eq(--index).fadeIn(fadeValue, function () {
            slides.eq(index + 1).hide();
            busy = false;
            nextSlideBtn.trigger('mouseover');
            prevSlideBtn.trigger('mouseover');
        });

        tick = setInterval(slideHide, 15000);
    });
    nextSlideBtn.click(function (e) {
        if (busy) return;
        busy = true;
        nextSlideBtn.trigger('mouseout');
        prevSlideBtn.trigger('mouseout');
        e.preventDefault();
        clearInterval(tick);

        slides.eq(index).fadeOut(fadeValue);
        if (index >= slides.length - 1) index = 0;
        slides.eq(++index).fadeIn(fadeValue, function () {
            slides.eq(index - 1).hide();
            busy = false;
            nextSlideBtn.trigger('mouseover');
            prevSlideBtn.trigger('mouseover');
        });

        tick = setInterval(slideHide, 15000);
    });
	



	var shareUrl = "http://www.essencedesign.com" //location.href,
		shareTitle = document.title,
		shareFalse = false;
		
	$(".facebook-btn").click(function (e) {
		e.preventDefault();
		shareFalse ? window.open("http://www.facebook.com/sharer.php?u=" + shareUrl + "&t=" + encodeURIComponent(shareTitle), "sharer", "toolbar=0,status=0,width=626,height=436") : window.open("http://www.facebook.com/sharer.php?u=" + encodeURIComponent(shareUrl) + "&t=" + encodeURIComponent(shareTitle), "sharer", "toolbar=0,status=0,width=626,height=436");
		return false
	});
	$(".twitter-btn").click(function (e) {
		e.preventDefault();
		shareFalse ? window.open("http://twitter.com/share?text=" + encodeURIComponent(shareTitle) + "&url=" + shareUrl + "&data-counturl=" + encodeURIComponent(shareUrl), "sharer", "toolbar=0,status=0,width=775,height=436") : window.open("http://twitter.com/share?text=" + encodeURIComponent(shareTitle) + "&url=" + encodeURIComponent(shareUrl), "sharer", "toolbar=0,status=0,width=775,height=436");
		return false
	});
	$(".linkedin-btn").click(function (e) {
		e.preventDefault();
		shareFalse ? window.open("http://www.linkedin.com/shareArticle?mini=true&url=" + shareUrl + "&title=" + encodeURIComponent(shareTitle), "sharer", "toolbar=0,status=0,width=626,height=436") : window.open("http://www.linkedin.com/shareArticle?mini=true&url=" + encodeURIComponent(shareUrl) + "&title=" + encodeURIComponent(shareTitle), "sharer", "toolbar=0,status=0,width=626,height=436");
		return false
	});
	
	$(".googleplus-btn").click(function (e) {
		e.preventDefault();
		shareFalse ? window.open("http://plusone.google.com/_/+1/confirm?url=${" + shareUrl + "}", "sharer", "toolbar=0,status=0,width=626,height=436") : window.open("http://plusone.google.com/_/+1/confirm?url=${" + encodeURIComponent(shareUrl) + "}", "sharer", "toolbar=0,status=0,width=626,height=436");
		return false
	});

});


