// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	}
});

(function($) {

    /* slideshow plugin */
    var Slideshow = function(el, opts) {
        //Defaults are below
        var settings = $.extend({}, $.fn.slideshow.defaults, opts),
        $el = $(el), $slides = $el.find('.slide-wrap'), $slide = null, $next = $el.find('.next'), $prev = $el.find('.prev'),
        cur = 0, tot = 0, interval = {}, on = 'on';

        // private methods
        function init() {
            tot = $slides.find('.slide').length;
            $slide = $slides.find('.slide:first');
            $next.click(onNextClick);
            $prev.click(onPrev);
            showSlide();
        }
        function showSlide() {
            if (cur < 0) { cur = tot - 1; }
            else if (cur > tot - 1) { cur = 0; }
            $slide.stop().fadeTo(400, 0);
            $slide = $slides.find('.slide:nth-child(' + (cur + 1) + ')');
            $slide.stop().fadeTo(400, 1);
        }
        function onNext(e) {
            cur++;
            showSlide();
        }
        function onNextClick(e) {
            e.preventDefault();
            clearRequestInterval(interval);
            onNext();
        }
        function onPrev(e) {
            e.preventDefault();
            clearRequestInterval(interval);
            cur--;
            showSlide();
        }
        // publics
        this.play = function() {
            interval = requestInterval(onNext, settings.timer);
        }
        this.stop = function() {
            clearRequestInterval(interval);
        }
        init();
    };
    $.fn.slideshow = function(options) {
        return this.each(function(idx, el) {
            var $el = $(this), key = 'slideshow';
            if ($el.data(key)) { return; }
            var slideshow = new Slideshow(this, options);
            $el.data(key, slideshow);
        });
    };
    // default settings
    $.fn.slideshow.defaults = { timer:4000 };

    var Gallery = function(el, opts) {
        //Defaults are below
        var settings = $.extend({}, $.fn.gallery.defaults, opts),
        $el = $(el), $nav = $el.find('nav'), $t = $nav.find('.title'), $slides = $el.find('.slide-wrap'), $slide = null,
        cur = 0, tot = 0, interval = {}, on = 'on';

        // private methods
        function init() {
            tot = $slides.find('.slide').length;
            $slide = $slides.find('.slide:first');
            showSlide();
            $nav.find('a').click(onById).each(function(idx, el){
                $(el).data('idx', idx);
            });
        }
        function showSlide() {
            if (cur < 0) { cur = tot - 1; }
            else if (cur > tot - 1) { cur = 0; }
            $slide.stop().fadeTo(250, 0);
            $slide = $slides.find('.slide:nth-child(' + (cur + 1) + ')');
            $slide.stop().fadeTo(250, 1);
            $nav.find('.' + on).removeClass(on);
            $el = $nav.find('a:nth-child(' + (cur + 1) + ')');
            $el.addClass(on);
            $t.text($el.attr('title'));
        }
        function onById(e) {
            e.preventDefault();
            clearRequestInterval(interval);
            cur = $(this).data('idx');
            showSlide();
        }
        function onNext(e) {
            cur++;
            showSlide();
        }
        // publics
        this.play = function() {
            interval = requestInterval(onNext, settings.timer);
        }
        this.stop = function() {
            clearRequestInterval(interval);
        }
        init();
    };
    $.fn.gallery = function(options) {
        return this.each(function(idx, el) {
            var $el = $(this), key = 'gallery';
            if ($el.data(key)) { return; }
            var gallery = new Gallery(this, options);
            $el.data(key, gallery);
        });
    };
    // default settings
    $.fn.gallery.defaults = { timer:4000 };
})(jQuery);

var HOME = (function($) {
    var app = {}, $el, $g, $sp, $l = $('#layout'), pnt, sec, cur = '', diff = 'difference', testi = 'testimonials', demo = 'demo';
    // Public functions
    app.init = function() {
        if ($('#intro.home').length < 1) { return; }

        $('#slide-player').slideshow();
        $('#gallery').gallery();
        $sp = $('#slide-player').data('slideshow');
        $g = $('#gallery').data('gallery');
        $('a.nav').click(onNavClick);
        $(window).scroll(update);
        update();
    };
    function onNavClick(e) {
        e.preventDefault();
        var id = $(this).attr('href').split('#').join('');
        $el = $('#' + id);
        var t = $el.find('.tab'), tarY = (id === demo) ? 50 : 150;
        if (t.length > 0 && id !== cur) {
            if (id !== demo) {
                t.css({ marginTop:-36 }).delay(500).animate({ marginTop:0 });
            } else {
                t.css({ bottom:0, height:0 }).delay(500).animate({ height:36, bottom:-36 });
            }
        }
        cur = id;
        $("html,body").animate({ scrollTop:$el.offset().top - tarY }, 800, 'easeOutQuad');
    }
    function update() {
        pnt = document.elementFromPoint($l.position().left, 300);
        pnt = $(pnt);
        var id = pnt.attr('id');
        if (id === cur) { return; }
        $g.stop();
        $sp.stop();
        if (id === diff || $(window).scrollTop() == 0) {
            $sp.play();
        }
        if (id === testi) {
            $g.play();
        }
        cur = id;
    }

    $(app.init);
    return app;
} (jQuery));

