var currentPage = 1;
var handle = null;
$(document).ready(function(){
    $('#leftMenu').children('li').click(function(){
        var page = $(this).prevAll('li').length + 1;

        if(page != currentPage)
        {
            goToPage(page);
        }
    });
    $('#prevPage').click(function(){
        goToPage(currentPage-1);
    });
    $('#nextPage').click(function(){
        if($('#page_'+(currentPage+1)).length)
            goToPage(currentPage+1);
        else
            goToPage(1);
    });
//    if($('#rightPages').hasClass('isGallery'))
//        handle = setInterval(showNextGalleryImage,3000);
});
function goToPage(page)
{
    if($('#page_'+page).length && $('#page_'+currentPage).length)
    {
        //if we are moving to a different page then current
        if(page != currentPage)
        {
            var target = $('#page_'+page);
            var current = $('#page_'+currentPage);
            var _zIndex = 100;
            //hide video player
            current.find('#player').replaceWith('<p id="player" style="width:294px;margin:0 auto;"></p>');

            //stop gallery auto scroll
            if(handle != null)
                clearInterval(handle);

            //put target page to the left of current page
            target.css({
                opacity:1,
                zIndex:_zIndex
            }).show();
            current.css({
                opacity:1,
                zIndex:_zIndex+1
            }).show();

            //move current page to right
            current.animate({
                opacity:0
            },500,function(){
                current.hide();

                if(target.find('#player').length)
                    target.find('#player').show();
                else
                    insertPlayer();

                //target.removeAttr("filter");
                if($.browser.msie)
                    document.getElementById("page_"+page).style.removeAttribute("filter");
            });
                        
            currentPage = page;
            updateLeftNav();
            updatePageNav();
        }
    }
}
function updateLeftNav()
{
    $('#leftMenu').children('li').each(function(){
        $(this).removeClass('active');
        if($(this).prevAll('li').length+1 == currentPage)
            $(this).addClass('active');
    });

}
function updatePageNav()
{
    $('div.pageNav').children('a').removeClass('disabled');
    
    if($('#page_'+currentPage).prevAll('div.page').length == 0)
        $('#prevPage').addClass('disabled');
    else if($('#page_'+currentPage).nextAll('div.page').length == 0)
        $('#nextPage').addClass('disabled');
}
function showNextGalleryImage()
{
    if($('div.page:visible').nextAll('div.page').length)
    {
        $('#nextPage').click();
    }
    else
    {
        goToPage(1);
    }

}
