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("dojox.data.CouchDBRestStore");
dojo.require("dijit.form.Textarea");
dojo.require("dijit.form.Button");
dojo.require("dojox.encoding.base64");


//global variables  
// catalogs

//create a globally scoped object for the store, and give it a relative path in order not to break XD requests coming from www.addplus.eu and  addplus.eu
var couchdbstoregeneral = new dojox.data.CouchDBRestStore({target:"/couchdb/addplus_eu/"});

//create a globally scoped object which will contain the data coming out of the store, for the moment objects are copied herein
couchdbstorearray = new Object();
var rndPool = new Object(eval((new XMLHttpLoader()).getSync("./data/content/common_stuff/solutions.json")));
var titleCycleDelay = 5000;	

//end of global variables  



//dojo.byId("hidemeautomagically").style.display = "none";

//end global variables

	

dojo.addOnLoad(function(){






		
		var buildUI = function(){



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




			var slideshow = new dojox.image.SlideShow({
				startwidth: 1440,
				startHeight: 900,
				slideShowInterval: 10,
				loop: true,
				imageLargeAttr: "unescapedUrl",
				autoLoad: true,
				id: 'defaultslideshow',
				autoStart: false,
				dynamicScale: true,
				noLink: 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:3 },
     		{
        imageThumbAttr: "thumb",
        imageLargeAttr: "large"
      		}
    	);
				
		
			
		
			// the main BC
			var outer = new dijit.layout.BorderContainer({
				gutters: false,
				id: 'outer'
			}).placeAt(dojo.body());
			


			
			dojo.style(dijit.byId("outer").domNode, {
				"height": "100%",
            	"width": "100%"
        	});

			
			// top region:
			var topregion = new dojox.layout.ContentPane({
				region:"top",
				style:"height:52px",
				href:"../../../pages/index_comingsoon/header.html",
				splitter:false,
				id:'header_container'
			}).placeAt(outer);
			
			
			var bottomregion = new dojox.layout.ContentPane({
				region:"bottom",
				style:"height:52px;",
				href: "../../../pages/index_comingsoon/footer_content.html",
				splitter:false,
				id: "bottom_region"
			}).placeAt(outer);
			
			
			var core = new dijit.layout.BorderContainer({ 
				region:"center", 
				style:"overflow: hidden;", 
				design: "sidebar", 
				gutters: false,
				id: 'core'
			}).placeAt(outer);


		

		var core_contentpane = new dojox.layout.ContentPane({
				region:"center",
				style:"margin: auto;",
				href:"../../../pages/index_comingsoon/content.html",
				id:'the_content'
			}).placeAt(core);
			
		core_contentpane.domNode.style.opacity=0;










var statuscheck = function(){	

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

	};

		


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

	//internationalize footer content
	//we pull this data out of the copy of the data coming from the couchdb store
//	nls.regCatId("i18n_magnify_tooltip", couchdbstorearray["i18n_magnify_tooltip"]);
//	nls.regCatId("i18n_slideshow_playpayse_tt", couchdbstorearray["i18n_slideshow_playpayse_tt"]);
//	nls.regCatId("i18n_slideshow_prev_tt", couchdbstorearray["i18n_slideshow_prev_tt"]);
//	nls.regCatId("i18n_slideshow_next_tt", couchdbstorearray["i18n_slideshow_next_tt"]);
	nls.regCatId("i18n_newsletter_tooltip", couchdbstorearray["i18n_newsletter_tooltip"]);
	nls.regCatId("i18n_contact_tooltip", couchdbstorearray["i18n_contact_tooltip"]);				

       //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", slideshow, 'toggleSlideShow');
	dojo.connect(dojo.byId("slideshow_next"), "onclick", slideshow, '_next');
	dojo.connect(dojo.byId("slideshow_prev"), "onclick", slideshow, '_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");
});
  
	

	//connect to the click event of the magnify button and act show the hidden contentpane
	dojo.connect(magnify_node, "onclick", togglecontent);

	//connect to the newsletter click and fire popup
	connectNewsLetterTooltip();

};


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 (outer._contentBox.h === 0) { location.reload();}
	
	// catalogs
//	nls.regCatId("i18n_index_content_profile_title", couchdbstorearray["i18n_index_content_profile_title"]);
//	nls.regCatId("i18n_index_content_profile_content", couchdbstorearray["i18n_index_content_profile_content"]); 

//	nls.regCatId("i18n_index_content_title_competences", couchdbstorearray["i18n_index_content_title_competences"]); 
//	nls.regCatId("i18n_index_content_subtitle_1_competences", couchdbstorearray["i18n_index_content_subtitle_1_competences"]); 
//	nls.regCatId("i18n_index_content_title_work", couchdbstorearray["i18n_index_content_title_work"]); 
//	nls.regCatId("i18n_index_content_subtitle_1_work", couchdbstorearray["i18n_index_content_subtitle_1_work"]); 




//lets reposition the content dynamically, centering it and moving it up by 52px
    function repositionContent() {
    var lastX = 0;
    var lastY = 0;
        var divInfo = dojo.coords(dojo.byId("core"), true);
        // use true to get the x/y relative to the document root
        var span = dojo.query(".noopacity")[0];  
        var spanInfo = dojo.coords(dojo.query(".noopacity")[0], true);
        lastY += ((divInfo.h - spanInfo.h) / 2) - 52;
        if (lastY <= 52) {lastY = 52;}
//		console.log(dojo.byId("core"));
//		console.log(divInfo);
//		console.log(span);
//      console.log(spanInfo);
//      console.log("contenbox: " + core._contentBox.h );
//      console.log("x:" + lastX + "  y:" + lastY);
        dojo.style(dojo.byId("the_content"), {
            marginTop: lastY + "px"
        });
    }

	repositionContent();
	
	dojo.connect(window,"resize",repositionContent);






//Throw a nice message if user is using a retarded browser
if ((dojo.isIE < 7)) {
	
	var ienode = dojo.create("div", { innerHTML:'Dear Visitor, <br /><br />We have designed this website to give you a compelling and innovative experience. <br />Furthermore, this website uses technologies which are not supported by your version of Internet Explorer.<br /> In order to experience this website the way it should be, we require you to use one of the following browsers to view this site: <br /> <a style="color: black" target="_blank" href="http://www.mozilla.com/firefox/firefox.html">Firefox</a>, <a style="color: black" target="_blank" href="http://www.apple.com/safari/download/">Safari </a>, <a style="color: black" target="_blank" href="http://www.google.com/chrome"> Chrome</a>, <a style="color: black" target="_blank" href="http://www.opera.com/"> Opera</a>, or <a style="color: black" target="_blank" href="http://www.microsoft.com/windows/internet-explorer/" >Internet Explorer 8</a>. <br /><br /> Thank you.' });
	
	
	
		
	var IEWarning;
			function createDialog() {
				if(!IEWarning){	
					IEWarning = new dijit.Dialog({
						id: "dialog3",
						refocus:false,
						modal: true,
						title: "PLEASE SWITCH BROWSER"
					},ienode);
				}
				IEWarning.show(); 
				IEWarning._onKey = function() {};
			}

	createDialog();
	
	};
	
  setTimeout(function() {	dojo.animateProperty({
	            node: dijit.byId("the_content").domNode,
	            duration: 1500,
	            properties: {
	                opacity: {
	                    end: 1
	                }
	            }
	        }).play();
}, 2000);


	};


var contentscript_trigger = function(){return (dijit.byId("the_content").isLoaded === true) ? contentscript : dojo.connect(dijit.byId("the_content"),"onLoad", contentscript);};

contentscript_trigger();



var headerscript = function(){
		
		//detect layouting fail and correct the problem  
		if (outer._contentBox.h === 0) { location.reload();}
		
	
		//header stuff 	
		//internationalize stuff
		// animations
		new RndText(dojo.byId("i18n_introrndtext"), rndPool, titleCycleDelay);
		//static texts
		nls.regCatId("i18n_presents", couchdbstorearray["i18n_presents"]);
	    nls.regCatId("i18n_introtext", couchdbstorearray["i18n_introtext"]);
//		nls.regCatId("i18n_impressum", couchdbstorearray["i18n_impressum"]);
		nls.regCatId("i18n_pagetitle", couchdbstorearray["i18n_index_content_pagetitle"]);
		//tooltips
//		nls.regCatId("i18n_headerworkbutton_tooltip", couchdbstorearray["i18n_headerworkbutton_tooltip"]);
//		nls.regCatId("i18n_headernetworkbutton_tooltip", couchdbstorearray["i18n_headernetworkbutton_tooltip"]);
//		nls.regCatId("i18n_headerprojectsbutton_tooltip", couchdbstorearray["i18n_headerprojectsbutton_tooltip"]);
//		nls.regCatId("i18n_headerresearchbutton_tooltip", couchdbstorearray["i18n_headerresearchbutton_tooltip"]);


		getintrotextwidth();
		//end header stuff
				};


var headerscript_trigger = function(){return (dijit.byId("header_container").isLoaded === true) ? headerscript : dojo.connect(dijit.byId("header_container"),"onLoad", headerscript);};

headerscript_trigger();


		
	};


			
			// do all the layout etc
			outer.startup();

dojo.connect(outer, "onLoad",  statuscheck());



		};
		

		// function gets started on run, and calls the fetch method of the store object
			fetch = couchdbstoregeneral.fetch({
			   //call the uri of the view we want to view
			   query:"_design/internationalization_data/_view/index_nls_data",
			   //each item which is detected passes through this loop
			   onItem:function(item){
		//			handle the results
		//			console.log("ITEM OBJECT: " , item);
		//			console.log("ENGLISH VALUE OF EN PROPERTY: " , couchdbstore.getValue(item, "en"));
		//			console.log("ID OF OBJECT: " , couchdbstore.getValue(item, "id"));
					//we want to be able to call our data through a object.it way, so we need to find out the ID of the objects in the view
					var id = couchdbstoregeneral.getValue(item, "id");
					//and then we create them in the object using an associative array approach
					couchdbstorearray[id] = item;
					couchdbstorearray[id].en = couchdbstoregeneral.getValue(couchdbstorearray[id], "en");
					couchdbstorearray[id].de = couchdbstoregeneral.getValue(couchdbstorearray[id], "de");
					couchdbstorearray[id].it = couchdbstoregeneral.getValue(couchdbstorearray[id], "it");
					couchdbstorearray[id].fr = couchdbstoregeneral.getValue(couchdbstorearray[id], "fr"); 
					
				},
				onComplete: function(){buildUI();}
			});

		


  });
	


