dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.StackContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.dijit");
// optimize: load dijit layer
dojo.require("dojox.image.SlideShow");
dojo.require("dojo.data.ItemFileReadStore");
dojo.require("dijit.Tooltip");
dojo.require("dijit.Dialog");
dojo.require("dojox.layout.ContentPane");
dojo.require("dojo.parser");


//global variables
// catalogs
var nlsCatGeneral = new Object(eval((new XMLHttpLoader()).getSync("./data/content/common_stuff/general_nls_content.json")));
var titleCycleDelay = 5000;


//end global variables


dojo.addOnLoad(function() {
dojo.parser.parse();




    var buildUI = function() {



        var filestore = new dojo.data.ItemFileReadStore({
            url: "../../../data/context/depaoli.json"
        });



        var slideshow = new dojox.image.SlideShow({
            slideShowInterval: 10,
            loop: true,
            autoLoad: true,
            id: 'defaultslideshow',
            autoStart: false,
            noLink: true,
            dynamicScale: true
        }).placeAt(dojo.body());


        dojo.style(dijit.byId("defaultslideshow").domNode, {
            "position": "absolute",
            "left": "0px",
            "top": "0px",
            "width": "100%",
            "height": "100%",
            "text-align": "left",
            "margin": "0px",
            "padding": "0px",
            "z-index": "0",
            "border": "none"
        });



        slideshow.setDataStore(
        filestore,
        {
            query: {},
            count: 5
        },
        {
            imageThumbAttr: "thumb",
            imageLargeAttr: "large"
        }
        );









        var statuscheck = function() {

 
            var titlescript = function() {
				console.log("titlescript running");
                //detect layouting fail and correct the problem
                if (dijit.byId("outer")._contentBox.h === 0) {
                    location.reload();
                }
                hidden_content = dojo.byId("work_title_hidden");

                dojo.subscribe(
                dijit.byId("defaultslideshow").getShowTopicName(),
                function(packet) {
                    var title = dojo.byId('image_title');
                    var hidetitle = dojo.animateProperty({
                        node: title,
                        duration: 500,
                        properties: {
                            opacity: {
                                end: 0
                            }
                        },
                        onEnd: function() {
                            title.innerHTML = packet.title;
                        }
                    });
                    var showtitle = dojo.animateProperty({
                        node: title,
                        duration: 500,
                        properties: {
                            opacity: {
                                end: 1
                            }
                        }
                    });

                    var titlereplacer = dojo.fx.chain([hidetitle, showtitle]);


                    titlereplacer.play();

                });

            };

            titlescript();


            var footerscript = function() {
                //detect layouting fail and correct the problem
                if (dijit.byId("outer")._contentBox.h === 0) {
                    location.reload();
                }

                //internationalize footer content
                nls.regCatId("i18n_slideshow_playpayse_tt", nlsCatGeneral.i18n_slideshow_playpayse_tt);
                nls.regCatId("i18n_slideshow_prev_tt", nlsCatGeneral.i18n_slideshow_prev_tt);
                nls.regCatId("i18n_slideshow_next_tt", nlsCatGeneral.i18n_slideshow_next_tt);


                //variables needed by the content toggling function
                setupSlideshowNaviDisplacerVars();

                slideshowNaviDisplacer();


                //this is the new slideshow navigation menu, takes context from the first slideshow widget instantiated
                dojo.connect(dojo.byId("slideshow_playpause"), "onclick", dijit.byId("defaultslideshow"), 'toggleSlideShow');
                dojo.connect(dojo.byId("slideshow_playpause"), "onclick",
                function() {
                    dojo.toggleClass(dojo.byId("slideshow_playpause"), "slideShowActive");
                })
                dojo.connect(dojo.byId("slideshow_next"), "onclick", dijit.byId("defaultslideshow"), '_next');
                dojo.connect(dojo.byId("slideshow_prev"), "onclick", dijit.byId("defaultslideshow"), '_prev');


                dojo.subscribe(
                dijit.byId("defaultslideshow").getShowTopicName(),
                function(packet) {

                    dojo[ + (dijit.byId("defaultslideshow").imageIndex) < 1 ? "addClass": "removeClass"](dojo.byId("slPrev"), "slideShowCtrlHide");
                    dojo[( + (dijit.byId("defaultslideshow").imageIndex) + 1) >= dijit.byId("defaultslideshow").maxPhotos ? "addClass": "removeClass"](dojo.byId("slNext"), "slideShowCtrlHide");
                    dojo[( + (dijit.byId("defaultslideshow").imageIndex) === 0) && (dijit.byId("defaultslideshow").maxPhotos == 1) ? "addClass": "removeClass"](dojo.byId("slPlay"), "slideShowCtrlHide");
                });

                dojo.connect(dojo.byId("i18n_gallerytext"), "onclick", togglecontent);
                dojo.connect(dojo.query(".blackLink")[0], "onclick", togglecontent);
            };


            var footerscript_trigger = function() {
                return (dijit.byId("bottom_region").isLoaded === true) ? footerscript: dojo.connect(dijit.byId("bottom_region"), "onLoad", footerscript);
            };

            footerscript_trigger();


            var contentscript = function() {

                //detect layouting fail and correct the problem
                if (dijit.byId("outer")._contentBox.h === 0) {
                    location.reload();
                }

                dojo.connect(dojo.byId("viewallgallery"), "onclick", togglecontent);
              

            };



            contentscript();



            var headerscript = function() {

                //detect layouting fail and correct the problem
                if (dijit.byId("outer")._contentBox.h === 0) {
                    location.reload();
                }


                //internationalize stuff
                // animations
                //static texts
                //tooltips
            };


       headerscript();



        };



        // do all the layout etc
        statuscheck();

		dojo.style(dojo.query(".soria")[0], "opacity", 0);
		dojo.style(dojo.query(".soria")[0], "visibility", "visible");
		dojo.animateProperty({node: dojo.query(".soria")[0], duration: 500, properties: {opacity: { end: 1 }} }).play();


    };

    dojo.addOnLoad(buildUI);



});




