var maxSize;
var actualBigBoxClass;
var newActualBigBoxClass;
var itemWidth;
var numPos;
var show_init = false;
var smer;
var getBox = true;
var bNext;
var wrapWidth;
var BB_startLeft;
var BB_startWidth;
var BB_startHeight;
var BB_startTop;
var BB_endWidth;
var BB_endHeight;
var BB_endTop;
var BB_endLeft;
var init = false;
var goAction = true;
function getBigBoxClass() {
    posMyCarousel = $('#mycarousel').position();
    if($('#wrap div.panel:visible').width()) {
        a = $('#wrap div.panel:visible').attr("class").split(" ");
        key = a[1];
    }
    if (smer=='left') {
        actualBigBoxClass=aNextPanels[key];
    }else if (smer=='right'){
        actualBigBoxClass=aPrevPanels[key];
    }else if(actualBigBoxClass == undefined){
        actualBigBoxClass = $('#mycarousel li:visible:eq(2)').attr("id");
    }
}
function showBigBox() {
    if (goAction==false) return false;
    goAction = false;
    if (!init) {
        BB_startWidth = $('#mycarousel li div').width();
        BB_startHeight = $('#mycarousel li div').height();
        //Korekce startovni vysky (ciste subjektivni pohled na to z jak vysok se ma panel zacit rozbalovat)
        BB_startHeight = BB_startHeight + 72;
        BB_startTop = 0;
        BB_endWidth = $('#wrap .panel').width();
        BB_endHeight = $('#wrap .panel').height();
        BB_endTop = $('#wrap .box').height() - BB_endHeight;
        BB_endLeft = Math.round(($('#wrap').width() - BB_endWidth) / 2) + 1;
        BB_endTop = Math.round(BB_endTop / 2);
        //Korekce startovni vysky (ciste subjektivni pohled na to z jake hodnoty top se ma panel zacit rozbalovat)
        BB_startTop = BB_startTop - 72;
        //Korekce koncove vysky (ciste subjektivni pohled na to kde ma rozbaleny panel skoncit)
        BB_endTop = BB_endTop - 20;
        
        init = true;
    }
    if(getBox) {
        getBigBoxClass();
        if (smer=='left') {
            BB_startLeft = itemWidth * 3;
        }else if (smer=='right') {
            BB_startLeft = itemWidth;
        }
    }
    hideBigBox();
    if (BB_startLeft!='undefined') {
        $('.'+actualBigBoxClass).css("left", BB_startLeft+'px');
        $('.'+actualBigBoxClass).css("top", BB_startTop+'px');
        $('.'+actualBigBoxClass).width(BB_startWidth+'px');
        $('.'+actualBigBoxClass).height(BB_startHeight+'px');
    }
    $('.'+actualBigBoxClass)
        .animate({"opacity" : "show", "top" : BB_endTop+'px', "left" : BB_endLeft+'px', "width" : BB_endWidth+'px', "height" : BB_endHeight+'px'});
}
function hideBigBox() {
    wrapWidth = $('#wrap').width();
    BB_hideLeft = wrapWidth - BB_startLeft - itemWidth;
    $('#wrap div.panel:visible')
        .animate({"top" : BB_startTop, "left" : BB_hideLeft+'px', "width" : BB_startWidth+'px', "height" : BB_startHeight+'px', "opacity" : "hide"}, 500, function () {
            $('#wrap div.panel').width(BB_endWidth+'px');
            $('#wrap div.panel').height(BB_endHeight+'px');
        });
}
function afterReset() {
    
}
function mycarousel_initCallback(carousel) {
    itemWidth = $('#mycarousel li').outerWidth(true);
    $('#mycarousel li div a.top').bind('click', function(){
        goAction = true;
        getBox = false;
        posMyCarousel = $('#mycarousel').position();
        clickPosBox = $(this).parent().position();
        clickPosBox = clickPosBox.left + posMyCarousel.left;
        numPos = (clickPosBox / itemWidth);
        
        actualBigBoxClass = $(this).parents('li').attr("id");
        
        BB_startLeft = itemWidth * (numPos);
        if (numPos < 3) {
            carousel.options.scroll = 2 - numPos;
            carousel.prev();
        }else if (numPos >= 3) {
            carousel.options.scroll = numPos - 2;
            carousel.next();
        }
        carousel.options.scroll = 1;
        return false;
    });
    $('.jcarousel-next').click( function (){ goAction = true; getBox = true; smer = 'left';})
    $('.jcarousel-prev').click( function (){ goAction = true; getBox = true; smer = 'right';})
}
$(document).ready(function() {
    aNextPanels = new Array();
    aPrevPanels = new Array();
    $('#mycarousel li div').each(function() {
        key = $(this).parent().attr("id");
        nextVal = $(this).parent().next().attr("id");
        prevVal = $(this).parent().prev().attr("id");
        if (nextVal!=undefined) {
            aNextPanels[key] = nextVal;
        }else{
            lastKey = key;
        }
        if (prevVal!=undefined) {
            aPrevPanels[key] = prevVal;
        }else{
            firstKey = key;
        }
    });
    aNextPanels[lastKey] = firstKey;
    aPrevPanels[firstKey] = lastKey;
    $('#mycarousel').jcarousel({
        wrap: 'circular',
        scroll: 1,
        initCallback: mycarousel_initCallback,
        itemLoadCallback: {
            onBeforeAnimation: showBigBox,
            onAfterAnimation: afterReset
        }
    });
    var initSize = $('#mycarousel li').size();
    maxSize = initSize * 2;
});
