var Nav = {
    isLoading:false,
    current_section: -1,
    cache: new Array(),
    to: function(section)
    {
        if(section == this.current_section || this.isLoading)
            return;
        this.isLoading = true;
        this.current_section = section;
        var Section = (Nav.sections[section])
            ? Nav.sections[section] : Nav.sections.MAIN;
        this.getXML(Section);
    },
    main: function()
    {
      $("#section_holder").hide();
      this.to(0);
    },
    getXML: function(Section)
    {
        if(this.cache[Section.file]) {
            this.loadSection(this.cache[Section.file]);
        }else {
            $.ajax({
                url: 'sections/' + Section.file,
                dataType: "html",
                success:function(data)
                {
                    var callback = null;
                    if( Section == Nav.sections.PHOTOS ) {
                        callback = function() //pointer to stackframe
                        {
                            Galleria.loadTheme('js/galleria/themes/classic/galleria.classic.js');
                            $('#gallery').galleria();
                            $('#gallery').bind("dblclick", function() {
                                var gallery = Galleria.get(0);
                                var fullScreenCallback = function() {
                                    
                                    gallery.refreshImage();
                                    gallery.play();
                                };
                                if( gallery.isFullscreen() )
                                    gallery.exitFullscreen(fullScreenCallback);
                                else
                                    gallery.enterFullscreen(fullScreenCallback);
                            });
                            //Wait to fully load, usually takes about 3 seconds
                            setTimeout("Galleria.get(0).play()", 3000);
                        };
                    }
                    Section.data = data;
                    if(Section.cache == true)
                        Nav.cache[Section.file] = Section;
                    Nav.loadSection(Section, callback);
                }
            });
        }
    },
    loadSection: function(Section, callback)
    {
        $("#section_holder").slideUp(800, function() {
            $("#section_holder").html(Section.data);

            if(Section.hasOwnProperty("adjustmentProp"))
                $( Section.adjustmentProp ).height( Section.height );

            $(".section_data").height( Section.height );
            
            if(callback != null)//if closure
                callback();//Callback before section visibility
            $("#section_holder").slideDown(800, function() {
                Nav.isLoading = false;
            });

        });
    }
};
Nav.sections = {
  MAIN:{file:"main.xml", height:"249px", adjustmentProp:".jMyCarousel", cache:true},
  AUTHOR:{file:"author.xml", height:"400px", cache:true},
  PHOTOS:{file:"photos.php", height:"249px", cache:false},
  FILM:{file:"film.xml", height:"249px", cache:true},
  FILM_A:{file:"films/filmA.xml", height:"249px", cache:true},
  FILM_B:{file:"films/filmB.xml", height:"249px", cache:true},
  FILM_C:{file:"films/filmC.xml", height:"249px", cache:true},
  FILM_D:{file:"films/filmD.xml", height:"249px", cache:true},
  ARTICLES:{file:"articles.php", height:"249px", cache:true},
  REFERENCE:{file:"reference.php", height:"249px", cache:true},
  BOOK:{file:"book.xml", height:"600px", cache:true},
  IRANS_NUCLEAR_STORY:{file:"irans_nuclear_story.xml", height:"249px", cache:true}
};
