var aamfg_custom_scroll;
var carousel;

function ScrollPage(divName, divNextName, divPreviousName)
{
    var div=$("#"+divName);
    var divNext=$("#"+divNextName);
    var divPrevious=$("#"+divPreviousName);
    var offsets={};
    var max_list_page=4;
    var current_page=1;
    var max_pages=1;
    var offset_page=0;
    var oldPage=0;
    this.gotoPage = gotoPage;
    this.gotoSection = gotoSection;
    this.init = init;
    this.nextPage = nextPage;
    this.previousPage = previousPage;
    this.refineUp = refineUp;
    this.refineDown = refineDown;
    this.calculatePages = calculatePages;
    this.checkUrl = checkUrl;

    function init() {
        div.scrollTop(0);
        jQuery.each(jQuery('.field-item').children(), function (index, value) {
            offsets[index]=jQuery(this).position();
        });
        max_pages=this.calculatePages();
        display_pages();
        display_commands();
        var page=location.hash.substring(1);
        if (!isNaN(parseInt(page))) {
            oldPage=page;
            gotoSection(page);
        } else {
          gotoPage(1);
        }
        var self=this;
        setInterval(function () {self.checkUrl();} ,500);
    }

    function checkUrl() {
        var page=location.hash.substring(1);
        if (page!=null && page!=oldPage) {
            oldPage=page;
            gotoSection(page);
        }
    }
    function paginate(start, end) {
        var list="";
        var cls="";
        for (var i=start;i<end;i++) {
            if (i==(current_page-1)) {
                cls=" class=\"selected\" ";
                list+="<li"+cls+">"+(i+1)+"</li>";
            }
            else {
                list+="<li><a href=\"Javascript:aamfg_custom_scroll.gotoPage("+(i+1)+");\">"+(i+1)+"</a></li>";
            }
        }
        return list;
    }

    function display_pages() {
       var list="<ul class=\"pages\">";
       if (max_pages>max_list_page) {
            if (current_page<=1) {
                list+=paginate(0,max_list_page);
            } else {
                if (current_page>=max_pages) {
                   list+=paginate(max_pages-max_list_page,max_pages);
                } else {
                  var st=Math.floor(max_list_page/2);
                  var start=current_page-st;
                  var end=current_page+(max_list_page-st);
                  if (end>max_pages) {
                      start=max_pages-max_list_page;
                      end=max_pages;
                  }
                  list+=paginate(start,end);
              }
            }
       }
       else {
           list+=paginate(0,max_pages);
       }
       list+="</ul>";
       jQuery('#pages').html(list);
    }

    function gotoSection(which) {
        var page=Math.floor(offsets[which].top/div.height())+1;
        offset_page=offsets[which].top-div.height()*(page-1);
        current_page=page;
        scroll();
    }

    function gotoPage(which) {
        current_page=which;
        offset_page=0;
        scroll();
    }

    function scroll() {
        pix=div.height()*(current_page-1)+offset_page;
        // div.scrollTop(pix);
        div.animate( { scrollTop: pix }, 1000);
        display_pages();
        display_commands();
    }

    function nextPage()
    {
        if (current_page>=max_pages) return;
        current_page++;
        scroll();
    }

    function previousPage()
    {
        if (current_page<=1) return;
        current_page--;
        scroll();
    }

    function display_commands()
    {
        divPrevious.removeClass('disabled');
        divNext.removeClass('disabled');
        if (current_page<=1)
            divPrevious.addClass('disabled');

        if (current_page>=max_pages)
            divNext.addClass('disabled');
    }

    function refineUp()
    {
        pix=20;
        // div.animate( { scrollTop: div.scrollTop()+pix }, 1000);
        div.scrollTop(div.scrollTop()+pix);
    }

    function refineDown()
    {
        pix=-20;
        div.scrollTop(div.scrollTop()+pix);
    }

    function calculatePages()
    {
        var pos=jQuery('#end_item').position();
        if (pos)
            return Math.floor((pos.top/div.height())+1);
        else
            return 0;
    }
}

function ShowDiv(divClass) {

    var divs={};
    this.init = init;
    this.show = show;

    function init() {
        jQuery.each(jQuery(divClass).children(), function (index, value) {
            divs[index]=jQuery(this);
            if (index!=0)
                divs[index].css('display', 'none');
        });
    }

    function show(div) {
        jQuery.each(divs,function(index, value){
            divs[index].css('display', 'none');
        });
        divs[div].css('display', 'block')
    }

}

jQuery(window).load(function(){
    aamfg_custom_scroll=new ScrollPage('field_visible','next','prev');
    aamfg_custom_scroll.init();

    carousel=new ShowDiv('.charts');
    carousel.init();


jQuery("#browser").treeview({
    animated:"normal",
    persist: "cookie",
    toggle: function() {
            
    }
    });

});

function aamfg_gotoUrl(url, element) {
    window.location=url+element.value;
}