//give global scope to this variable
var hidden_content = "imglobal";
//global variable to track the state of the visibility
var content_fadestate = "0";
//other global variables
var navigation_node = "imglobal";
var core_contentpane = "imglobal";

var setupSlideshowNaviDisplacerVars = function(){
	//variables needed by the content toggling function
	//var magnify_tooltip = dojo.byId("magnify_tooltip");
	navigation_node = dojo.byId("slideshow_navigation");
	// define some bogus object to be assigned to these variables. They are used for tracking connect/disconnect events relative to the magnify button
	core_contentpane = dijit.byId("the_content");
};



// define some bogus object to be assigned to these variables. They are used for tracking connect/disconnect events relative to the magnify button


//function to use to toggle the content, icons, navigation, and so on
var togglecontent = function() {

	if (content_fadestate == "1") {

		core_contentpane.domNode.style.opacity = "0";
		core_contentpane.domNode.style.display = "block";

		var hidenavigation = dojo.animateProperty({node: navigation_node, duration: 500, properties: {opacity: { end: 0 }}, onEnd: function(){ dojo.style(navigation_node, "display", "none");} });
		var hidehiddencontent = dojo.animateProperty({node: hidden_content, duration: 500, properties: {opacity: { end: 0 }}, onEnd: function(){ dojo.style(hidden_content, "display", "none");} });
		var showcontent = dojo.animateProperty({node: core_contentpane.domNode, duration: 500, properties: {opacity: { end: 1 }} });



		var combinehider = dojo.fx.chain([hidenavigation,hidehiddencontent,showcontent]);

		combinehider.play();

		content_fadestate = "0";


	}
	else if (content_fadestate == "0") {


		hidden_content.style.opacity = "0";
		hidden_content.style.display = "block";
		navigation_node.style.opacity = "0";
		navigation_node.style.display = "block";	

		var hidecontent = dojo.animateProperty({node: core_contentpane.domNode, duration: 500, properties: {opacity: { end: 0 }}, onEnd: function(){ dojo.style( core_contentpane.domNode, "display", "none");} });
		var showhiddencontent = dojo.animateProperty({node: hidden_content, duration: 500, properties: {opacity: { end: 1 }} });
		var shownavigation = dojo.animateProperty({node: navigation_node, duration: 500, properties: {opacity: { end: 1 }} });



		var combineshower = dojo.fx.chain([hidecontent,showhiddencontent,shownavigation]);

		combineshower.play();

		content_fadestate = "1";
	}
};



var slideshowNaviDisplacer = function(){
	//DISPLACE SLIDESHOW NAVIGATION
	var initnavmenuxpos = dijit.getViewport().w / 2 + 60;
	var initnavmenuypos = dijit.getViewport().h - 40;

	//we have to do this twice otherwise its not positioned correctly the first time.
	dijit.placeOnScreen(navigation_node, {x: initnavmenuxpos, y: initnavmenuypos}, ["TR", "BL"]);
	dojo.style(navigation_node, "z-index", 4);
	dojo.style(navigation_node, "opacity", 0);

	dojo.connect(window,"onresize", function(){
		var navmenuxpos = dijit.getViewport().w / 2 + 60;
		var navmenuypos = dijit.getViewport().h - 40;
		dijit.placeOnScreen(navigation_node , {x: navmenuxpos, y: navmenuypos}, ["TR", "BL"]);
	});
};



var connectNewsLetterTooltip = function() {

	var postForm = function() {
	  dojo.xhrPost({ // The page that parses the POST request
                                        url: '/email/mail.cgi',
                                        handleAs: 'text',
                                        // Name of the Form we want to submit
                                        form: dojo.byId('newsletterForm'),
                                        // Loads this function if everything went ok
                                        load: function() { // Put the data into the appropriate <div>
                                                dojo.byId('i18n_newsletter_tooltip').innerHTML = 'Thank you!';
                                                setTimeout("dijit.popup.close(dijit.byId('tt'))", 500);
                                        },
                                        // Call this function if an error happened
                                        error: function(error) {
                                                console.error('Error: ', error);
                                  		       }
 	            	                   });
           	                   };


                dojo.query("#newsletter_tooltip_connector").forEach(function(n) { // do more stuff for these (this) node: n

                        dojo.query("#tt div").addClass("dijitInline");
                        var myDialog = new dijit.TooltipDialog({},"tt");
                        myDialog.startup();

                        var coords = dojo.coords(dojo.byId("newsletter_tooltip_connector"));

                        var placenewslettertt = function(){coords = dojo.coords(dojo.byId("newsletter_tooltip_connector")); };

                        dojo.connect(window, "onresize", placenewslettertt);




                        dojo.connect(n, "onclick", function(e) { // stop the native click 
                                e.preventDefault();
                                if(dojo.byId("i18n_newsletter_tooltip").innerHTML != 'Thank you!') {
                                coords = dojo.coords(dojo.byId("newsletter_tooltip_connector"));


                                var mypopup =   dijit.popup.open({
                                        popup: myDialog,
                                        around: dojo.byId("newsletter_tooltip_connector"),
                                        orient: {'TR':'BR', 'TL':'BL', 'BL':'TL', 'BR':'TR'}
                                });

                                dijit.popup.close(myDialog);

                                mypopup = dijit.popup.open({
                                        popup: myDialog,
                                        around: dojo.byId("newsletter_tooltip_connector"),
                                        orient: {'TR':'BR', 'TL':'BL', 'BL':'TL', 'BR':'TR'}
                                });

}
                        });


dojo.query("#cancel").onclick(function(e){
        e.preventDefault(); // like return false; 
        dijit.popup.close(myDialog);
});

dojo.connect(dojo.byId('newsletterForm'), "onsubmit", dojo, "stopEvent");




                        dojo.query("#newsletterForm").onsubmit(function(e) {
                                e.preventDefault();
                              	postForm();
    	    	                });
	
dojo.connect(dojo.byId('newsletterFormSubmit'), "onclick", postForm);		
	
    		            });






};