$(function() {
    $('.thumb').live('mouseover', function() {
        id = $(this).attr('id')+'-b';

        imgh = $('#'+id).outerHeight();
        thumbh = $(this).outerHeight();

        // zisti ci sa neschova horna cast
        availableTop = $(this).offset().top - $(window).scrollTop();
        neededTop = (imgh-thumbh)/2;

        // zisti ci sa neschova dolna cast
        middle = ((imgh - thumbh)/2) + thumbh;
        neededBottom = $(this).offset().top + middle;
        availableBottom = $(window).height() + $(window).scrollTop();

        bottom = availableBottom >= (neededBottom + marginWindow);
        top = availableTop >= (neededTop + marginWindow);
        
        if(bottom && top) { // middle
            $('#'+id).css('top', -(imgh-thumbh)/2);
        } else {
            if(bottom || top) {
                if(top) { // zarovname na bottom
                    diff = neededBottom - availableBottom;
                    $('#'+id).css('top', -(((imgh-thumbh)/2) + diff + marginWindow));
                } else { // zarovname na top
                    diff = neededTop - availableTop;
                    $('#'+id).css('top', -(((imgh-thumbh)/2) - diff - marginWindow));
                }
            } else { // ake sa nezmesti nikde -> middle
                $('#'+id).css('top', -(imgh-thumbh)/2);
            }
        }
        
        if($('#'+id).css('display') == 'block') {
            clearTimeout(timer);
        }
        $('.popupp:not(#'+id+')').fadeOut(200);
        timer = setTimeout(function() {
            $('#'+id).fadeIn(200);
        }, 500);
    });

    $('.thumb').live('mouseout', function() {
        id = $(this).attr('id')+'-b';
        // ak este nie je zobrazeny popup, a odideme z nahladu, ano sa nezobrazi
        if($('#'+id).css('display') == 'none') {
            clearTimeout(timer);
        // ak je popup zobrazeny, a odideme z nahladu, schova sa
        } else {
            timer = setTimeout(function() {
                $('#'+id).fadeOut(200);
            }, 500);
        }
    });

    $('.popupp').live('mouseover', function() {
        clearTimeout(timer);
    });

    $('.popupp').live('mouseout', function() {
        id = $(this).attr('id');
        timer = setTimeout(function() {
            $('#'+id).fadeOut(200);
        }, 500);
    });

    $('.change').live('click', function() {
        name = $(this).attr('iname');
        id = $(this).attr('iid');
        img = $(this).parent().parent().prev().find('img');
        salt = $(img).attr('salt');
        $(img).attr('src',path+id+'/'+thumb+name);
        $('.change[salt='+salt+']').removeClass('selected');
        $('.change[iname='+name+'].change[salt='+salt+']').addClass('selected');
    });
});

$(function() {
    var catTimer;

    $('#cat-wrapper .link').live('mouseover', function() {
        
        catTimer = setTimeout(function() {
            loadCategories();
            $('#cat-popup').fadeIn(200);
        }, 500);
    });

    $('#cat-wrapper .link').live('mouseout', function() {
        // ak este nie je zobrazeny popup, a odideme z nahladu, ano sa nezobrazi
        if($('#cat-popup').css('display') == 'none') {
            clearTimeout(catTimer);
        // ak je popup zobrazeny, a odideme z nahladu, schova sa
        } else {
            catTimer = setTimeout(function() {
                $('#cat-popup').fadeOut(200);
            }, 500);
        }
    });

    $('#cat-popup').live('mouseover', function() {
        clearTimeout(catTimer);
    });

    $('#cat-popup').live('mouseout', function() {
        catTimer = setTimeout(function() {
            $('#cat-popup').fadeOut(200);
        }, 500);
    });
});
