var pagePos = 0;


function scroller(mouseY){
    if($('#item_menu').length>0){
        size = $('#item_menu ul').height() - $('#item_menu').height()
        if(size > 0){
            // scrolling menu
            
            // list item count
            liCount = $('#item_menu ul li').length;
            //window item count - how many items can be displayed in the window
            windowLiCount = parseInt($('#item_menu').height() / $('#item_menu ul li').height());
            // scroll area count - 3 is added to make mouse movement smoother;
            //scrollArea = parseInt($('#item_menu').height() / liCount) - 3;
            // percentage ratio of each list item
            percent = ($('#item_menu ul').height() - $('#item_menu').height()) / (liCount - scrollArea);
            // position in the scrolling window
            position = mouseY - $('#item_menu').offset().top;
            // scrolled item 
            scrolledItem = parseInt(position/percent);
            // unsorted top position
            position = 0 - $('#item_menu ul li').height() * scrolledItem;
            // animate list move 
            $('#item_menu ul').animate({top: position}, {queue: false, duration:200});
            
        }else{
            // fixed
            showScrollerArrows(-1);
        }                    
    }else{
        showScrollerArrows(-1);
    }
}
function showScrollerArrows(position){
    var up = $('#item_menu_up img');
    var dwn = $('#item_menu_down img');
    if(position == -1){
        up.css({'visibility': 'visible'});
        dwn.css({'visibility': 'visible'});
    }    
}
function setScroller(pos){
    //local variables
    var up = $('#item_menu_up img');
    var dwn = $('#item_menu_down img');

    // list item count
    liCount = $('#item_menu ul li').length;
    // window item count - how many items can be displayed in the window
    windowLiCount = parseInt($('#item_menu').height() / $('#item_menu ul li').height());
    if(liCount > windowLiCount){
        switch(true){
            case (pos > (liCount - windowLiCount) ):
                position = 0 - $('#item_menu ul li').height() * (liCount - windowLiCount);
                break;
        }
        if(pos == -1)
            // animate list move
            $('#item_menu ul').animate({top: position}, {queue: false, duration:200});

    }else{
        up.css({'visibility': 'visible'});
        dwn.css({'visibility': 'visible'});            
    }
}

var closeText = true;
$(document).ready(function() {      
    
    $('#close').click(function(){
        //$(this).parent().hide();
        if(closeText)
        {
            $(this).parent().fadeTo(1000, .1);
            closeText = false;
        }
        else
        {
            $(this).parent().fadeTo(1000, .8);
            closeText = true;
        }
            //.animate({right: -1000, marginTop: 2+'em'},2000, 'easeOutBack',function(){i--;$(this).css('z-index', -1)})
            //.animate({right: 0, marginTop: 0+'em'},1000, 'easeOutBack');            
    });
    
    $(".ajax").colorbox({close: false});
    
    //scroller control event
    /*
    $('#item_menu').bind('mousemove.scroller', 
        function(e){            
            scroller(e.pageY);
        }
    );    
    $('#item_menu').bind('mouseout.scroller', 
        setScroller($('#item_menu .select').parent().index())
    );    
        
    // align scroller menu with selected page
    setScroller();
     */
    $('#mainslide').cycle({
        speed:    2000,
        timeout:  500, 
        /* easing:  'easeInBack', */
        fx: 'scrollLeft,scrollDown,scrollRight,scrollUp', 
        randomizeEffects: false,
        before:  function(c,n,o) {$(o.caption).html(o.currFx);},
        caption: '#caption2',
        timeoutFn: calculateTimeout 
    });
    $('#menu_home_info').toggle(function(){
        $('#homepagetext').show();
        return false;
    }, function(){
        $('#homepagetext').hide();
        return false;        
    });

    // Load the classic theme
    Galleria.loadTheme('/resources/js/galleria/themes/classic/galleria.classic.js');

    // Initialize Galleria
     $("#gallery").galleria({
        width: 600,
        height: 480,
        autoplay: 4000,
        transitionSpeed: 3000,
        transition: 'fadeslide',
        easing: 'linear',
        swipe: true,
        debug: false,
        dataConfig: function(img) {
            return {description: $(img).next('span').html()}
        }
    });        
    
    $('#gallery').show();
    
});


var start;
function calculateTimeout(currElement, nextElement, opts, isForward) {
    if(!start){
        timeout = 500;
        start = true;
    }else
        timeout = 3500;
   return timeout;
}





      
function scrollerworks(mouseY){
    if($('#item_menu').length>0){
        size = $('#item_menu ul').height() - $('#item_menu').height()
        if(size > 0){
            // scrolling menu
            
            // list item count
            liCount = $('#item_menu ul li').length;
            // scroll area count - 3 is added to make mouse movement smoother;
            scrollArea = parseInt($('#item_menu').height() / liCount) - 3;
            // percentage ratio of each list item
            percent = ($('#item_menu ul').height() - $('#item_menu').height()) / (liCount - scrollArea);
            // position in the scrolling window
            position = mouseY - $('#item_menu').offset().top;
            // scrolled item 
            scrolledItem = parseInt(position/percent);
            // unsorted top position
            position = 0 - $('#item_menu ul li').height() * scrolledItem;
            // animate list move 
            $('#item_menu ul').animate({top: position}, {queue: false, duration:200});
            
        }else{
            // fixed
            showScrollerArrows(-1);
        }                    
    }else{
        showScrollerArrows(-1);
    }
}
