/**
 * UI aplikace Priroda Karlovarska
 *
 * @copyright   Copyright (c) 2010 Roman Pistek
 * @package     PrirodaKarlovarska
 */

$(document).ready(function() {
    
    /* -- KALENDAR ----------------------------------------------------------*/
    $('#calendar td').each(function() {
        $(this).prepend('<span class="highlight">' + $(this).text() + '</span>');
    });

    $('#calendar td a').live('click', function() {
        var td = $(this).parent('td');
        var tdPos = td.position();
        $('#calendar .details.' + $(this).attr('class')).show();
        $('#calendar .dialog').fadeIn();
        $('#calendar .highlight').hide();
        $('.highlight', td).css({ top: tdPos.top, left: tdPos.left + 5 }).show().animate({
            top: '15',
            left: '15'
        }, {
            duration: 250, 
            specialEasing: {
                width: 'swing',
                height: 'easeOutBounce'
            }
        });
        return false;
    });

    $('#calendar .dialog .close a').live('click', function() {
        $('#calendar .highlight').remove();
        $('#calendar .dialog').fadeOut();
        $('#calendar .details').hide();

        // uf...
        $('#calendar td').each(function() {
            $(this).prepend('<span class="highlight">' + $(this).text() + '</span>');
        });

        return false;
    });
    
    
    
    $('.video-placeholder').live('click', function() {
        var container = $(this).parent();
        $(this).hide();
        $('iframe').show();
        return false;
    });
    
    
    
    $('#banners .item img')
        .mouseenter(function() {
            $(this).animate({ top: '-10px' }, 50);
        })
        .mouseleave(function() {
            $(this).animate({ top: '0' }, 50);
        });
    
    
    
    /* -- SUPERBOX --------------------------------------------------------- */
    $(function(){
        $.superbox.settings = {
            boxId: "superbox", // Id attribute of the "superbox" element
            boxClasses: "", // Class of the "superbox" element
            overlayOpacity: .5, // Background opaqueness
            boxWidth: "960", // Default width of the box
            boxHeight: "604", // Default height of the box
            loadTxt: "Načítám&hellip;", // Loading text
            closeTxt: "Zavřít", // "Close" button text
            prevTxt: "Předchozí", // "Previous" button text
            nextTxt: "Další" // "Next" button text
        };
        $.superbox();
    });


    
    /* -- CAROUSEL --------------------------------------------------------- */
    (function() {
        var carouselId    = 'carousel',
            itemCls       = 'carriage',
            navId         = 'carousel-nav',
            navWrapperCls = 'carousel-items'
            navItemCls    = 'item',
            leftBtnCls      = 'nav-left',
            rightBtnCls    = 'nav-right',

            navPos        = 0,
            carPos        = 0,
            timer         = null,
        
        _items = function() { return '#' + carouselId + ' .' + itemCls; },
        _navItems = function() { return '#' + navId + ' .' + navItemCls; },
        _navLeftBtn = function() { return '#' + navId + ' .' + leftBtnCls; },
        _navRightBtn = function() { return '#' + navId + ' .' + rightBtnCls; },
            
        indexNav = function() {
            $(_navItems() + ' img').each(function(i) {
                $(this).attr('class', i.toString());
            });
        },
        
        isInt = function(x) {
            var y = parseInt(x); 
            if (isNaN(y)) { return false; }
            return x == y && x.toString() == y.toString();
        },
        
        hideItem = function(item, callback) {
            var item = item;
            var callback = callback;
            $('.image', item).animate({ top: '-100px', opacity: 0 }, 400, function() { 
                $(this).hide();
            });
            $('.shadow img', item).animate({ /*width: '110%', marginLeft: '-20px',*/ opacity: 0 }, 400, function() {
                $(this).hide();
            });
            $('.itemtext', item).animate({ marginTop: '-500px' }, 400, function() {
                $(item).hide();
                callback();
            });
            
        },
        
        showItem = function(item) {
            if (!item) return;
            $('.image', item).animate({ top: '-100px', opacity: 0 }, 0).show();
            $('.shadow img', item).animate({ /*width: '110%', marginLeft: '-20px',*/ opacity: 0 }, 0).show();
            $('.itemtext', item).animate({ marginTop: '-500px' }, 0);
            $(item).show();
            $('.image', item).animate({ top: '-20px', opacity: 1 }, 400);
            $('.shadow img', item).animate({ /*width: '100%', marginLeft: 0,*/ opacity: 1 }, 400);
            $('.itemtext', item).animate({ marginTop: 0 }, 400);
        },
        
        /*highlightNavItem = function(newPos) {
            if (timer) {
                if (newPos == navPos + 4) { // 4 = pocet radku navigace (v layoutu)
                    slideNav();
                } else if (carPos > 0 && newPos == 0) {
                    slideNav(true, $(_navItems()).length - 4); // 4 = pocet radku navigace (v layoutu)
                }
            }
            if (newPos != null) {
                $($(_navItems()).get(newPos)).addClass('active');
                $($(_navItems()).get(carPos)).removeClass('active');
                return;
            }
            $($(_navItems()).get(carPos)).addClass('active');
        },*/

        moveCarousel = function(pos) {
            var items = $(_items());
            if (pos == undefined) { pos = carPos + 1; }
            if (pos == carPos || pos < 0) { return; }
            var pos = pos;

            // rewind
            if (pos > (items.length - 1)) {
                pos = 0;
            }
            
            hideItem(items[carPos], function() {
                showItem(items[pos]);
            });
            
            //highlightNavItem(pos);
            carPos = pos;
        },

        /*slideNav = function(up, steps) {
            var down = !up;
            var items = $(_navItems());
            var steps = (isInt(steps)) ? steps : 1;
            if ((up && navPos == 0) || (down && (navPos + 4) >= items.length)) { return; }
            navPos += (down) ? steps : -steps;
            scrollY(items, 73 * steps, up, 150);
        },*/
        
        _foo = 56;
        
        
        // startup
        if ($('#' + carouselId).length != 1) {
            return;
        }
        indexNav();
        //highlightNavItem();
        timer = setInterval(function() {
            moveCarousel();
        }, 8000);
        
        // events binding
        $(_navLeftBtn() + ', ' + _navRightBtn()).click(function() {
            clearInterval(timer);
            slideNav(($(this).hasClass(leftBtnCls)) ? true : false);
            return false;
        });
        
        $(_navItems()).click(function() {
            clearInterval(timer);
            pos = $('img', this).attr('class');
            moveCarousel(pos);
        });
        
    }());

});



$.admin = {
    init: function() {

        $('input.datetimepicker').datepicker({
            duration:       '',
            changeMonth:    true,
            //changeYear:     true,
            numberOfMonths: 1,
            yearRange:      '2010:2020',
            showTime:       true,
            time24h:        true,
            currentText:    'Dnes',
            closeText:      'Použít',
            //regional:       'cs'
        });
        
        $('#photogallery-browse .photo form textarea').live('keypress', function(event) {
            if (event.keyCode == '13') {
                event.preventDefault();
                $(this).parent('form').ajaxSubmit(this, event);
                return;
            }
        });

    },
    
    foo: ''
};

