/* Copyright 2011 Unternehmen Online GmbH & Co. KG */
/* Author: Christian Drengenberg */

(function($) {
        
    $.fn.uoImageRotation = function(options) {
        
        var defaultOptions = { rotationSpeed : 10000, effectSpeed : 500,  progressClass : 'uo-rotation-progress',
            itemsContainerClass : 'uo-items', itemsClass: 'uo-rotation-item' };
        
        options = $.extend(defaultOptions, options);
        
        var $parent = $(this);
        var $progress;
        var progressLength = 0;
        
        if($parent.length > 1) {
            $parent.each(function() { $(this).uoImageRotation(options); })
        }
        else {
            $progress = $('.' + options.progressClass, $parent);
            progressLength = $progress.length;
            
            var $itemsContainer = $('.' + options.itemsContainerClass, $parent);
            var $items = $('.' + options.itemsClass, $parent);
            var interval;
            
            interval = setInterval(__rotate, options.rotationSpeed);
            
            $itemsContainer.append($($items.get(0)));
            $items = $('.' + options.itemsClass, $parent);
              
            $items.mouseenter(function() { clearInterval(interval); });
            $items.mouseleave(function() { interval = setInterval(__rotate, options.rotationSpeed / 2); });
        }
        
        function __rotate() {
            var $cur = $($items.get(0));
            var $next = $cur.next();
                        
            $cur.hide(function() {
                var $curProg = $($progress.get(progressLength-1));
                
                $progress.parent().prepend($curProg);
                $progress = $('.' + options.progressClass, $parent);
                
                $itemsContainer.append($cur);
                $items = $('.' + options.itemsClass, $parent);
                $items.fadeIn(options.effectSpeed);
            });
        }
    }
    
})(jQuery);

(function($) {
        
    $.fn.uoFoldTeaser = function(options) {
        
        var defaultOptions = { effectSpeed : 250,  collapsedItemClass : 'uo-fold-teaser-collapsed', visibleItemClass: 'uo-fold-teaser-visible' };
        
        options = $.extend(defaultOptions, options);
        
        var $parent = $(this);
        
        if($parent.length > 1) {
            $parent.each(function() { $(this).uoFoldTeaser(options); })
        }
        else {
           var $collapsedItem = $('.' + options.collapsedItemClass, $parent);
           var $visibleItem = $('.' + options.visibleItemClass, $parent);
           
           $visibleItem.hide();
           
           $collapsedItem.click(function() {
                $collapsedItem.hide();
                $visibleItem.slideDown(options.effectSpeed);
                $visibleItem.click(function() { $(this).slideUp(options.effectSpeed); $collapsedItem.show(options.effectSpeed); });
           });
        }
    }
    
})(jQuery);

(function($) {
        
    $.fn.uoFlyoutNavigation = function(options) {
        
        var defaultOptions = { effectSpeed : 250,  level1Class : 'uo-navigation-01', level2Class: 'uo-navigation-02', level3Class: 'uo-navigation-03',
            itemOnClass : 'on' };
        
        options = $.extend(defaultOptions, options);
        
        var $parent = $(this);
        var hideTimer;
        
        if($parent.length > 1) {
            $parent.each(function() { $(this).uoFoldTeaser(options); })
        }
        else {
           $level1Items = $('.' + options.level1Class, $parent).children();
           $level2Items = $('.' + options.level2Class, $parent);
           $level2Items.hide();
           
           var level1ItemsLength = $level1Items.length;
           var $firstItem = $level1Items.eq(0);
           
           var firstPosition = $firstItem.position();
           var firstSize = { width : $firstItem.width(), height : $firstItem.height() };
           var level2Pos = { left : firstPosition.left, top : firstSize.height };
           
           $level1Items.mouseenter(function() {
                $(this).addClass(options.itemOnClass);
                
                $level2Items.hide();
                $level2ItemsLocal = $(this).find('.' + options.level2Class);
                $level2ItemsLocal.css({ position : 'absolute', left : level2Pos.left, top : level2Pos.top + $(this).position().top, 'z-index' : 100000 });
                $level2ItemsLocal.show();
           });
           
           $level1Items.mouseleave(function() {
                $(this).removeClass(options.itemOnClass);
                $level2Items.hide();
           });
        }
    }
    
})(jQuery);

(function($) {
    
    $.fn.uoAnyBox = function(options) {
        
        var defaultOptions = { fadeBackground : true };
        
        options = $.extend(defaultOptions, options);
        
        var $parent = $(this);
        
        if($parent.length > 1) {
            $parent.each(function() { $(this).uoAnyBox(options); });
        }
        else {
            var relItemClass = $parent.attr('rel');
            var $relItem = $('.' + relItemClass).hide();
            var $bg = $('<div/>').hide();
            
            $relItem.click(function() { $relItem.slideUp(500, function() { $bg.fadeOut(500); }); });
            $parent.click(function() { return __show($relItem); });
            $(window).resize(function() { if($relItem.is(':visible')) __show($relItem); }).scroll(function() { if($relItem.is(':visible')) __show($relItem); });
        }
        
        function __show($item) {
            var itemSize = { width : $item.width(), height : $item.height() };
            var position = { left : ($(window).width() - itemSize.width) / 2, top : (($(window).height() - itemSize.height) / 2) + $(window).scrollTop() };
            var css = { position : 'absolute', left : position.left, top : position.top, 'z-index' : 10000 };
            
            $bg.css({ position: 'absolute', left : 0, top : 0, background : '#000', width : '100%', height : $(document).height(), filter : 'alpha(opacity=85)',
                    '-moz-opacity' : 0.85, opacity: 0.85 });
                    
            $item.css(css);           
            
            if(!$item.is(':visible'))
            {
                $bg.appendTo('body').fadeIn(500, function() {
                    $item.slideDown(500);
                });
            }
            
            return false;
        }
    }
    
})(jQuery);

