/* Author: 

*/


$(document).ready(function(){

    /*! Application
    ---------------------------------------------- */
    $.app.external();                   // External links
    $.app.nav();                        // Highlights the current in a menu
    $.app.fbShare();                    // Facebook share button
    $.app.parallax();                   // Parallax background

}); // document ready


(function ($) {
    $.app = {
        external: function (_el) {
            var _2d = {
                selector: "a"
            };
            if (typeof _el == "string") {
                _2d.selector = _el;
            }
            var _el = $.extend(_2d, _el);
            var _2e = window.location.hostname;
            _2e = _2e.replace("www.", "").toLowerCase();
            return $(_el.selector).each(function () {
                var _2f = $(this).attr("href").toLowerCase();
                if (_2f.indexOf("http://") != -1 && _2f.indexOf(_2e) == -1) {
                    $(this).attr("target", "_blank");
                    $(this).addClass("external");
                }
            });
        },
        nav: function (_el) {
            // Highlights the current in a menu by comparing the links to the current URL path
            var path = location.pathname.substring(1);
            if (path)
                $('nav a[href$="' + path + '"]').attr('class', 'active');
        },
        fbShare: function (_el) {
            // Facebook Like Box Plugin
            $('[role="fb-share"]').live('click', function (e) {
                e.preventDefault();
                $this = $(this);

                var $fbShare = {}
                $fbShare.method = $this.data('method'),
			        $fbShare.name = $this.data('name'),
			        $fbShare.link = $this.data('link'),
			        $fbShare.picture = $this.data('picture'),
			        $fbShare.caption = $this.data('caption'),
			        $fbShare.description = $this.data('description');
                FB.ui({
                    method: $fbShare.method,
                    name: $fbShare.name,
                    link: $fbShare.link,
                    picture: $fbShare.picture,
                    caption: $fbShare.caption,
                    description: $fbShare.description
                });
            });
        },
        parallax: function (_el) {
            /*! Parallax Scrolling
            ---------------------------------------------- */
            // Cache the Window object
            $window = $(window);

            // Cache the Y offset and the speed of each sprite
            $('[data-type]').each(function () {
                $(this).data('offsetY', parseInt($(this).attr('data-offsetY')));
                $(this).data('Xposition', $(this).attr('data-Xposition'));
                $(this).data('speed', $(this).attr('data-speed'));
            });

            // For each element that has a data-type attribute
            $('section[data-type="background"]').each(function () {


                // Store some variables based on where we are
                var $self = $(this),
			        offsetCoords = $self.offset(),
			        topOffset = offsetCoords.top;

                // When the window is scrolled...
                $(window).scroll(function () {

                    // If this section is in view
                    if (($window.scrollTop() + $window.height()) > (topOffset) &&
				         ((topOffset + $self.height()) > $window.scrollTop())) {

                        // Scroll the background at var speed
                        // the yPos is a negative value because we're scrolling it UP!								
                        var yPos = -($window.scrollTop() / $self.data('speed'));

                        // If this element has a Y offset then add it on
                        if ($self.data('offsetY')) {
                            yPos += $self.data('offsetY');
                        }

                        // Put together our final background position
                        var coords = '50% ' + yPos + 'px';

                        // Move the background
                        $self.css({ backgroundPosition: coords });

                        // Check for other sprites in this section	
                        $('[data-type="sprite"]', $self).each(function () {

                            // Cache the sprite
                            var $sprite = $(this);

                            // Use the same calculation to work out how far to scroll the sprite
                            var yPos = -($window.scrollTop() / $sprite.data('speed'));
                            var coords = $sprite.data('Xposition') + ' ' + (yPos + $sprite.data('offsetY')) + 'px';

                            $sprite.css({ backgroundPosition: coords });

                        }); // sprites

                        // Check for any Videos that need scrolling
                        $('[data-type="video"]', $self).each(function () {

                            // Cache the video
                            var $video = $(this);

                            // There's some repetition going on here, so 
                            // feel free to tidy this section up. 
                            var yPos = -($window.scrollTop() / $video.data('speed'));
                            var coords = (yPos + $video.data('offsetY')) + 'px';

                            $video.css({ top: coords });

                        }); // video	

                    }; // in view

                }); // window scroll

            }); // each data-type
        },
        resizeBackground: function (_el) {

        }
    };
})(jQuery);
