﻿/*
    Project:       Carousel
    Author:        Simon Wilder
    Date:          28/04/10
*/

(function($) {

    $.fn.ln_carousel = function(settings) {
        var config = {
            container: null,
            speed: 3000,
            transition_speed: 500,
            continuous: false,
            thumnails: false,
            thumbs_position: "below",
            show_details: true
        };
        var vars = {
            full_width: 0,
            item_width: 0,
            left_value: 0,
            thumbs_full_width: 0,
            thumbs_item_width: 0,
            thumbs_left_value: 0,
            index: 0,
            length: 0
        };

        if (settings) $.extend(config, settings);
        // create shorthand for configs
        $c = config;
        Initialize(this);

        function Initialize(hero) {
            vars.length = $(hero).find('.mask ul li').length;
            if (vars.length <= 3) {
                config.continuous = false;
                config.thumnails = false;
            };

            if (vars.length > 1) {
                var run = setInterval(function() { Next(hero); }, config.speed);

                vars.item_width = $(hero).find('.mask ul li').outerWidth();
                vars.full_width = vars.item_width * vars.length;

                if (config.thumnails) {
                    var Thumbs = $(hero).find('.mask').clone().attr('class', 'thumbs');
                    if ($c.thumbs_position == "below") {
                        $(Thumbs).appendTo($(hero));
                    };
                    if (config.thumbs_position == "above") {
                        $(Thumbs).prependTo($(hero));
                    };
                    $(hero).find('.thumbs .details').remove();
                    vars.thumbs_item_width = $(hero).find('.thumbs ul li').outerWidth() + 76;
                    vars.thumbs_full_width = vars.thumbs_item_width * vars.length;
                    if (config.continuous) {
                        $(hero).find('.thumbs li:first').before($(hero).find('.thumbs li:last'));
                        $(hero).find('.thumbs li:first').before($(hero).find('.thumbs li:last'));
                    };

                    $(hero).find('.thumbs ul').css({
                        'width': vars.thumbs_full_width,
                        'left': vars.thumbs_left_value
                    });

                    $(hero).find('.thumbs img').click(function() {
                        DirectMove(hero, this);
                    });
                };

                if (!config.show_details) {
                    $(hero).find('.mask .details').remove();
                };

                $buttons_cont = $(document.createElement('div'))
		            .attr('class', 'controls')
		            .appendTo($(hero).find('.mask'));
                $buttons_prev = $(document.createElement('a'))
		            .attr('class', 'prev')
		            .attr('href', '#')
		            .appendTo($buttons_cont);
                $buttons_next = $(document.createElement('a'))
		            .attr('class', 'next')
		            .attr('href', '#')
		            .appendTo($buttons_cont);

                $buttons_next.click(function() { Next(hero); });
                $buttons_prev.click(function() { Prev(hero); });


                if (config.continuous) {
                    $(hero).find('.mask li:first').before($(hero).find('.mask li:last'));
                    vars.left_value = vars.item_width * -1;
                };

                $(hero).find('.mask ul').css({
                    'width': vars.full_width,
                    'left': vars.left_value
                });

                $(hero).hover(
                    function() { clearInterval(run); $(hero).find('.slideContent').stop(true, true).show(300); },
                    function() { run = setInterval(function() { Next(hero); }, config.speed); $(hero).find('.slideContent').stop(true, true).hide(300); }
                );
                $(hero).find('.mask').hover(
                    function() { ShowHideNav('show'); },
                    function() { ShowHideNav('hide'); }
                );
                
                $(hero).find('.mask li').click(function(){
                    var click_href = $('.mask li:eq(' + vars.index + ') .details a').attr('href');
                    window.location = click_href;
                });

                var thumb_index = 0;

                $(hero).find('.thumbs ul li').hover(
                    function() {
                        thumb_index = $(this).index();
                        if (thumb_index != vars.index) {
                            $(this).addClass('current').fadeTo(0, 1);
                        };
                    },
                    function() {
                        thumb_index = $(this).index();
                        if (thumb_index != vars.index) {
                            $(this).removeClass('current').fadeTo(0, 0.6);
                        };
                    }
                );

                if ($c.thumnails) {
                    ManageThumbs(hero);
                };
            };
        };
        function ShowHideNav(o) {
            o == 'show' ? $buttons_cont.show(500) : $buttons_cont.hide(500);
        };
        function DirectMove(hero, element) {
            var thumb_index = $(hero).find('.thumbs li').index($(element).parent());
            if (!config.continuous && (vars.index != thumb_index) && thumb_index != -1) {
                vars.left_value = (vars.item_width * thumb_index) * -1;
                vars.index = thumb_index;
                if (thumb_index > vars.index) {
                    Animate(hero, "next");
                } else {
                    Animate(hero, "prev");
                };
            };
        };
        function Prev(hero) {
            vars.index--;
            if (!config.continuous && vars.index < 0) {
                vars.index = vars.length - 1;
                vars.left_value = (vars.full_width - vars.item_width) * -1;
            } else {
                vars.left_value = vars.left_value + vars.item_width;
            };

            if (config.continuous && config.thumnails) {
                vars.thumbs_left_value = 0 - vars.thumbs_item_width;
            } else {
                var page_no = Math.floor(vars.length / 4);
                if (vars.index % 4 == 3) {
                     if (vars.index == (vars.length - 1))
                     {
                        vars.thumbs_left_value = (((vars.thumbs_item_width * 4) * page_no) * -1) + (vars.thumbs_item_width * 4);
                     }else{
                        vars.thumbs_left_value = vars.thumbs_left_value + (vars.thumbs_item_width * 4);
                    }
                } else {
                    if (vars.index == (vars.length - 1)) {
                        vars.thumbs_left_value = ((vars.thumbs_item_width * 4) * page_no) * -1;
                    };
                };
            };
            Animate(hero, "prev");
            return false;
        };
        function Next(hero) {
            vars.index++;
            if (!config.continuous && vars.index >= vars.length) {
                vars.index = 0;
                vars.left_value = 0;
            } else {
                vars.left_value = vars.left_value - vars.item_width;
            };
            if (config.continuous && config.thumnails) {
                vars.thumbs_left_value = 0 - vars.thumbs_item_width;
            } else {
                if (vars.index % 4 == 0 || vars.index == 0) {
                    if (vars.index == 0) {
                        vars.thumbs_left_value = 0;
                    } else {
                        vars.thumbs_left_value = vars.thumbs_left_value - (vars.thumbs_item_width * 4);
                    };
                };
            };
            Animate(hero, "next");
            return false;
        };
        function Animate(hero, d) {
            $(hero).find('.mask ul').stop(true, true).animate({ 'left': vars.left_value }, config.transition_speed, function() {
                if (config.continuous) {
                    var new_left = 0;
                    if (d == "next") {
                        $(hero).find('.mask li:last').after($(hero).find('.mask li:first'));
                        if (vars.index >= vars.length) vars.index = 0;
                    } else {
                        if (vars.index < 0) vars.index = vars.length - 1;
                        $(hero).find('.mask li:first').before($(hero).find('.mask li:last'));
                    };
                    new_left = vars.item_width * -1;
                    vars.left_value = new_left;
                    $(hero).find('.mask ul').css({ 'left': new_left });
                };

            });
            if (config.thumnails) {
                if (config.continuous) {
                    $(hero).find('.thumbs ul').stop(true, true).animate({ 'left': vars.thumbs_left_value }, config.transition_speed, function() {
                        if (d == "next") {
                            $(hero).find('.thumbs li:last').after($(hero).find('.thumbs li:first'));
                        } else {
                            $(hero).find('.thumbs li:first').before($(hero).find('.thumbs li:last'));
                        };
                        $(hero).find('.thumbs ul').css({ 'left': 0 });
                    });
                } else {
                    $(hero).find('.thumbs ul').stop(true, true).animate({ 'left': vars.thumbs_left_value }, config.transition_speed);
                };

                ManageThumbs(hero);
            };
        };
        function ManageThumbs(hero) {
            $(hero).find('.thumbs li').removeClass('current').fadeTo(0, 0.6);
            if (config.continuous) {
                $(hero).find('.thumbs li:eq(2)').addClass('current').fadeTo(0, 1);
            } else {
                $(hero).find('.thumbs li:eq(' + vars.index + ')').addClass('current').fadeTo(0, 1);
            };
        };

        return this;
    };
})(jQuery);
