/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var menuWhileAnimation = false;
$(function() {
    $('*').click(hideCat);
    $('#categories .main > li > a').unbind('click', hideCat).click(showSubmenu);
});

function hideCat() {
    if(menuWhileAnimation == false) {
        var up = $(this).hasClass('up');
        $( '#categories .main .active .up' ).remove();
        $('ul', $( '#categories .main .active' ).removeClass('active').parent().css('z-index', '1')).slideUp( 'fast' );
        if(up)
            return false;
    }
    return true;
    
}
function showSubmenu() {
    var link = $(this);
    var up = link.parent();
    var sub = $('ul', up);
    if( sub.get().length < 1 ) {
        return true;
    } else if(menuWhileAnimation) {
        return false;
    } else if( link.hasClass('active')) {
        //hideCat();
        return true;
    }
    hideCat();
    up.css('z-index', '20');
    menuWhileAnimation = true;
    link.addClass('active');
    link.append('<span class="up"></span>');
    $('.up', link).click(hideCat);
    sub.slideDown( 'normal', function() { menuWhileAnimation = false } );
    return false;
}

$( function() {
    $( '#special a' ).attr('target', 'bla');
}) ;

var lastShow;
var toShow;
var toShowObject;

$(bindInfo);
 function  bindInfo() {
    $('#products .title').hover(function() {
        clearTimeout(toShow);
    });
    $('#products .info').hover(function() {
        if(lastShow != null)
            clearTimeout(lastShow);
        if(toShow != null)
            clearTimeout(toShow);
    }, function() {
        lastShow = setTimeout(function() {
            $('#products .info').fadeOut('fast', function() {
                $( '#products' ).css( 'z-index', '3' );
            });
            
        }, 1000);
    });
    $('.info').append('<span class="pic"></span>');
    $('#products .info .close').click(closeInfo);
    $('#products li .min').hover(function() {
        if(lastShow != null)
            clearTimeout(lastShow);
        if(toShow != null)
            clearTimeout(toShow);
        $('#products .info').hide();
        toShowObject = $('.info', $(this).parent())
        toShow = setTimeout(function() {
            $( '#products' ).css( 'z-index', '88' );
            $( '#products li' ).css('z-index', '3');
            toShowObject.parent().css( 'z-index', '6' );
            var windowHeight = $(window).height();
            var scroll = window.scrollY;
            if(navigator.appName == "Microsoft Internet Explorer")
                scroll = document.body.parentNode.scrollTop;
            var offsetTop = $(toShowObject.parent()).offset();
            var objectHeight = $(toShowObject).height();
            offsetTop = offsetTop.top;
            var pos = offsetTop - scroll - 57;
            //alert(offsetTop + '-' + scroll);
            if(windowHeight - pos - 70 < objectHeight) {
                //alert(pos);
                toShowObject.css('top', -(objectHeight - (windowHeight - pos - 70)) + 'px');
                toShowObject.find('.pic').css('top', ((objectHeight - (windowHeight - pos - 70))+7)+'px');
            } else {
                toShowObject.css('top', '0px');
                toShowObject.find('.pic').css('top', '15px');
            }

            toShowObject.fadeIn('fast');
            toShowObject.next().fadeIn('fast');
        }, 500);
    }, function() {
        lastShow = setTimeout(function() {
            $('#products .info').fadeOut('fast');
            $( '#products' ).css( 'z-index', '3' );
        }, 500);
    });
}
function closeInfo() {
    $(this).parent().fadeOut();
    return false;
}
var nextProductsUrl = '/home/getPromoted';
var nextProductsTimeout;
var nextProductsInterval = 10000;

$( function() {
    $('#products .more').remove()
    nextProductsTimeout = setInterval( nextProducts, nextProductsInterval );
    $('#products h2').append('<a href="#" id="backProducts">&laquo;</a><a href="#" id="nextProducts"><span>więcej</span>&raquo;</a>');
    $('#backProducts').click(backProducts);
    $('#nextProducts').click( function() { nextProducts(); return false; } );
    $('#products ul, #products h2').hover( function() {
        clearInterval( nextProductsTimeout );
        return false;
    }, function() {
        clearInterval( nextProductsTimeout );
        
        nextProductsTimeout = setInterval( nextProducts, nextProductsInterval );
        return false;
    } );
} );

function nextProducts() {
    var oldPage = $('#products .show');
    
    
    if(oldPage.next('.productsPage').get().length == 0) {
        $('#productsPages').height( $('#productsPages .show ul').height() + 10);
        $(oldPage).hide();
        $('#productsPages').load( nextProductsUrl, {}, function() {
            bindInfo();
            
            $( $('#productsPages .productsPage').get(0) ).fadeIn('normal', function() { $('#productsPages').css('height', 'auto'); }).addClass('show');
            $('#products ul').hover( function() {
                clearInterval( nextProductsTimeout );
                return false;
            }, function() {
                clearInterval( nextProductsTimeout );

                nextProductsTimeout = setInterval( nextProducts, nextProductsInterval );
                return false;
            } );
        } );
        
    } else {
        oldPage.removeClass('show').fadeOut('normal', function() {
            $(this).hide().next().hide().addClass('show').fadeIn('normal');
        });
    }
}

function backProducts() {
    var oldPage = $('#products .show');
    var newPage = oldPage.prev('.productsPage');
    if(newPage.get().length == 1) {
        oldPage.removeClass('show').fadeOut('normal', function() {
            $(this).hide().prev().hide().addClass('show').fadeIn('normal');
        });
    }
    return false;
}