/**
 * User:    Shane Johnson
 * Blog:    http://blog.ultravisual.co.uk
 * Email:   shanej@ultravisual.co.uk
 * Date:    16/10/11
 * Time:    21:51
 */

var KINETIC = KINETIC || {};

KINETIC.addVideo = function (id) {

    var content = 'http://www.youtube.com/embed/' + id  + '?rel=0';

    Shadowbox.init({
        handleOversize:"drag",
        skipSetup:true,
        modal:true
    });

    Shadowbox.setup();

    Shadowbox.open({
        content:content,
        player:'iframe',
        width:640,
        height:385
    });

    return false;
}

KINETIC.navigation = function ($) {
    var currentNode;
    $('#top-navigation li').mouseenter(function () {
        if (currentNode != null)$(this).parent().find("ul.nav-sub").slideUp('fast');
        currentNode = $(this).parent().find("ul.nav-sub");
        $(this).find("ul.nav-sub").slideDown('fast').show();
        $(this).parent().hover(function () {
            $(this).parent().find("ul.nav-sub").slideUp('fast');
        });
        return false;
    })
};

KINETIC.tenets = function ($) {
    var currentImage;
    $('#tenets_container div .over').mouseenter(function () {
        if (currentImage != null)currentImage.fadeOut('slow');
        currentImage = $(this).parent().find('.under');
        $(this).parent().find('.under').fadeIn('slow', function () {
            $(this).mouseleave(function () {
                $(this).fadeOut('slow');
            })
        });
        return false;
    });
};

KINETIC.pushBottom = function ($) {
    var $mainHeight = $('#main').height() - 75;
    if ($mainHeight < 300) $mainHeight = 300;
    $('.push').css('height', $mainHeight + "px");
};

KINETIC.homePage = function ($) {
    var index = 0;
    var images = $('.gallery img');
    $(images[index]).fadeIn('slow');
    var play = setInterval(fadeNextImage, 6000);

    function fadeNextImage() {
        $(images[index]).fadeOut('slow', function () {
            index++;
            (index >= images.length) ? index = 0 : index = index;
            $(images[index]).fadeIn('slow');
        });

    }
};

KINETIC.imageGallery = function ($) {
    var amount = 0;
    var distance = 525;
    var inactive = false;
    var width = ($('.image-gallery-thumbs').width() + 20) * $('.thumbs img').size();
    var over = distance - (width % ($('.image-gallery-thumbs').width() + 20));
    $('.thumbs').css({width:width});
    var images = $('.image-gallery .img');
    var currentImage = $(images[0]);
    currentImage.fadeIn('slow');

    $('.thumbs img').click((function () {
        if (!inactive) {
            inactive = true;
            var id = 'l' + $(this).attr('id');
            currentImage.fadeOut('slow', function () {
                currentImage = $('#' + id);
                currentImage.fadeIn('slow', function () {
                    inactive = false;
                });
            })
        }
    }));

    $('.arrow-left').click(function () {
        slideThumbs('r');
    });

    $('.arrow-right').click(function () {
        slideThumbs('l');
    });

    var slideThumbs = function (dir) {
        if (dir == 'l') {
            if (inBounds(dir)) {
                amount -= distance;
            }
        }
        else {
            if (inBounds(dir)) {
                amount += distance;
            }
        }
        $('.thumbs').animate({left:amount}, 500);
    };

    var inBounds = function (dir) {
        if (dir == 'l') {
            if (amount <= 0 && amount > -(width - over))return true;
            else return false;
        }
        else {
            if (amount >= -(width) && amount < 0)return true;
            else return false;
        }
    }
};

KINETIC.getBaseURL = function () {
    return location.href;
};

KINETIC.detectMACWebkit = function ($) {
    if (isMacOrIOS()) {
        $('#home_anim_container').css('bottom', '135px');
    }
    function isMacOrIOS() {
        if (navigator.appVersion.indexOf('Mac') != -1 && navigator.userAgent.indexOf('WebKit') != -1) {
            return true;
        }
        if (navigator.userAgent.indexOf('iPad') != -1 || navigator.userAgent.indexOf('iPhone') != -1) {
            return true;
        }
        return false;
    }
};


(function ($) {
    KINETIC.homePage($);
    KINETIC.detectMACWebkit($);
    KINETIC.pushBottom($);
    KINETIC.navigation($);
    KINETIC.tenets($);
}(jQuery));

