/*
	Image Slider Plugin
	Green-Solutions UG 2011
*/


(function ($) {

    // Constructor
    $.fn.gsSlider = function (options) {

        var main_opts = $.extend({}, $.fn.gsSlider.defaults, options);

        return this.each(function () {

            var $this = $(this);
            this.options = main_opts;
            this.currentItemIndex = -1;
            this.sliderItems = new Array();
            this.navigationItems = new Array();

            initSlider(this);

        });

    };

    //Defaultwerte (vor der Benutzung dieses Plugins k?nnen diese Defaults ?berschrieben werden)
    $.fn.gsSlider.defaults = {
        itemSelected: 0,
        cycleInterval: 3000,
        activateCycle: true,
        navigation: true
    };

    // Initialisiere den Slider
    function initSlider($object) {

        $($object).addClass('gsSlider');

        // Sammle alle elemente für den Slider
        $('#' + $object.id).children('div')
			.each(function (i, item) {

			    $(this).addClass('slide');

			    if ($(this).attr('id') != "") {
			        $object.sliderItems[i] = $(this);
			        $(this).hide();
			    }
			});

        // Erzeuge Navigation
        if ($object.options.navigation) {
            initNavigation($object);
        }

        // Zeige das erste Item
        showItem($object, $object.options.itemSelected);

        // Starte timer für animation
        if($object.options.activateCycle) {
            $object.cycleInterval = window.setInterval(function () { nextItem($object) }, $object.options.cycleInterval);
        }
        
    }

    // Erzeuge Navigation
    function initNavigation($object) {
        var navigation = '';
        var navigationElement = $($object).children('ul');
        if (navigationElement == null) {
            $object.options.navigation = false;
            return;
        }
            

        navigationElement.addClass('gsSliderNav');

        // Sammle alle elemente für die navigation
        navigationElement.find('li')
			.each(function (i, item) {
			    $object.navigationItems[i] = $(this);

			    var navigationItemId = "navigationitem_" + $object.id + "_" + i;

			    $object.navigationItems[i].attr('id', navigationItemId);

			    $(this).bind("click", function () {
			        window.clearInterval($object.cycleInterval);
			        var parts = this.id.split('_');
			        showItem($object, parts[parts.length - 1], false);
			    });

			    $(this).bind("mouseover", function () {
			        window.clearInterval($object.cycleInterval);
			        var parts = this.id.split('_');
			        showItem($object, parts[parts.length - 1], false);
			    });


			});

    }

    // Zeige das Item
    function showItem($object, nIndex, fade) {

        if ($object.currentItemIndex == nIndex)
            return;

        var oldIndex = $object.currentItemIndex;
        if (oldIndex < 0) {
            oldIndex = 0;
        }

        $object.currentItemIndex = nIndex;



        // Show new item with fade
        if (fade) {
            $object.sliderItems[oldIndex].addClass('last-active');
            $object.sliderItems[$object.currentItemIndex].css({ opacity: 0.0 })
				.show()
				.addClass('active')
				.animate({ opacity: 1.0 }, 1000, function () {

				    // Hide old navigation item
				    if ($object.options.navigation) {
				        if ($object.navigationItems[oldIndex] != null)
				            $object.navigationItems[oldIndex].removeClass('active');
				    }

				    // Hide old slider
				    $object.sliderItems[oldIndex].hide()
						.removeClass('active last-active');

				    // Show new navigation item
				    if ($object.options.navigation) {
				        if ($object.navigationItems[$object.currentItemIndex] != null)
				            $object.navigationItems[$object.currentItemIndex].addClass('active');
				    }


				});
        }
        else {
            // hide old slider
            $object.sliderItems[oldIndex].hide()
						.removeClass('active last-active');

            // Hide old navigation item
            if ($object.options.navigation) {
                if ($object.navigationItems[oldIndex] != null)
                    $object.navigationItems[oldIndex].removeClass('active');
            }

            // Show new item without fade
            $object.sliderItems[$object.currentItemIndex]
				.css({ opacity: 1.0 })
				.show();

            // Show new navigation item
            if ($object.options.navigation) {
                $object.navigationItems[$object.currentItemIndex].addClass('active');
            }

        }

    };

    // Zeige nächstes Image
    function nextItem($object) {

        if ($object.currentItemIndex >= $object.sliderItems.length - 1) {
            showItem($object, 0, true);
        }
        else {
            showItem($object, $object.currentItemIndex + 1, true);
        }

    };

})(jQuery);
