// JavaScript Document

//add provide
	var HEADHT = "60px";
	var IMGPATH = "img/";
	var aspRatio = 1.5625; // 961/615; Fixme
	var animations = {};
	
		function setVRules() {
				for (i in vruleX) {
				vrule[i]= dojo.doc.createElement('div');
				vrule[i].className= 'tile vrule';
				dojo.style(vrule[i] ,{
						left: vruleX[i] + 'px',
						height: vruleH[i] + 'px',
						bottom: vruleB + 'px'
					});
				dojo.byId('borderShell').appendChild(vrule[i]);
				}
		}
		function setHRules() {
				for (i in hruleX) {
				hrule[i]= dojo.doc.createElement('div');
				hrule[i].className= 'tile hrule';
				dojo.style(hrule[i] ,{
						left: hruleX[i] + 'px',
						width: hruleW[i] + 'px',
						top: hruleY[i] + 'px'
					});
				dojo.byId('borderShell').appendChild(hrule[i]);
				}
		}
		function setText(theDiv,theUrl) {
			dojo.xhrGet( { 
			url: theUrl, 
			handleAs: "text",
			timeout: 5000,
			load: function(response, ioArgs) {
          dojo.byId(theDiv).innerHTML = response; 
          return response; 
        },
        error: function(response, ioArgs) {
          console.error("HTTP status code: ", ioArgs.xhr.status);
          return response; 
          }
        });
      }

	function setTopNav(){
		var tnId = {
			exhibitions:		{ label: 'EXHIBITIONS', url: 'intersections' },
			education:			'EDUCATION',
			events:				'EVENTS',
			press:				'PRESS',
			membership:			'MEMBERSHIP',
			shop:				'SHOP',	
			information:		'INFORMATION',
/*			project_space:		'PROJECT SPACE',*/
			aim:				'AIM',
			index:				{ label: 'HOME', url: 'index' }
		};
		var nav = dojo.doc.createElement('div');
		dojo.addClass(nav, 'topNav');
		nav.appendChild(dojo.doc.createElement('span'));	// separator
		for (i in tnId) {
			
			if (i == 'index') {
				var s = dojo.doc.createElement('span');
				dojo.addClass(s, 'spacer');
				nav.appendChild(s);
			}
			b = dojo.doc.createElement('a');
			b.id= i;
			if (i == thisPage) {
				dojo.addClass(b, 'selected');
			}
			b.innerHTML = tnId[i].label ? tnId[i].label : tnId[i];
			dojo.attr(b, 'href', (tnId[i].url ? tnId[i].url : i) + '.html');
			nav.appendChild(b);
			var s = dojo.doc.createElement('span');
			nav.appendChild(s);	// separator
		}
		dojo.byId('tnavBtns').appendChild(nav);
		dojo.byId('logo').innerHTML = '<a href="index.html"><img src="img/logoBXM.gif" /></a>';
	}

	function setLNav(){
		var wrapper = dojo.byId('lnShell');
		for (i in lnBtn) { 
			var e = dojo.doc.createElement('div');
			e.innerHTML = lnBtn[i];
			e.id = lnLnk[i];
			e.className = 'ln';
			if (lnBtn[i]== thisPane) {
				dojo.addClass(e, 'lnCur');
			};
			if (i< lnBtn.length-1){ //hack
				dojo.connect(e,'onclick', onButton);
				/*
				dojo.connect(e,'mouseover',(function(evt) {dojo.addClass(evt.target,'lnHover') }));
				dojo.connect(e,'mouseout',(function(evt) {dojo.removeClass(evt.target,'lnHover') }));
				*/
			}
			wrapper.appendChild(e);
		}
	}
	
	function setRollovers(query) {
		if (! query) {
			query = '.topNav a, #lnShell .ln, #lnShell .lnCur';
		}
		var links = dojo.query(query);
		// Make sure all links have ids
		links.forEach(function(node) {
			if (!node.id) {
				node.id = dojo.generateId();
			}
		});
		links.connect('onmouseover', function(evt) {
			if (animations[evt.target.id] && animations[evt.target.id].status() != 'stopped') {
				animations[evt.target.id].stop();
			}
			animations[evt.target.id] = dojo.animateProperty({
				node: evt.target,
				duration: 300,
				accel: 1,
				properties: {
					color: { end: "#CCFF00" }
				}
			});
			animations[evt.target.id].play();
		});
		links.connect('onmouseout', function(evt) {
			if (dojo.hasClass(evt.target, 'selected') || dojo.hasClass(evt.target, 'lnCur')) {
				var targetColor = "#CCFF99";
			} else {
				targetColor = "#00FF00";
			}
			var myFade = dojo.animateProperty({
				node: evt.target,
				duration: 700,
				properties: {
					color: { end: targetColor }
				},
				onEnd: function() {
					dojo.style(evt.target, 'color', '');
				}
			});
			if (animations[evt.target.id] && animations[evt.target.id].status() != 'stopped') {
				// We need to wait until the fadeIn is complete.
				dojo.connect(animations[evt.target.id], 'onEnd', function() {
					animations[evt.target.id] = myFade;
					myFade.play();
				});
			} else {
				animations[evt.target.id] = myFade;
				myFade.play();
			}
 		});
	}	
	
 	function onButton(){
		var main_pane = dijit.byId('mainShell');
		newPane = this.id;
		var n= dojo.query('.lnCur');
		dojo.removeClass(n[0], 'lnCur');
		dojo.addClass(this, 'lnCur');
		newPath= thePath+newPane+'.html';
		main_pane.setHref(newPath);
	}
		
	  function sizeBackground() {
	    var n = dojo.byId('backDiv');
	  	var v = dijit.getViewport();
		var nh= n.style.height;//ie hack\
		//dojo.style('myBGimg','visibility','hidden');
		if ((v.w/v.h) > (aspRatio)) {
			n.style.width= v.w + 'px';
			n.style.height= v.w/aspRatio + 'px';
			} else {
					n.style.height= v.h + 'px';
					n.style.width= v.h*aspRatio+'px';
					}		
	  }
	  function setBackground(bgimg) {
			var myBG= dojo.doc.createElement("img");
			myBG.src= IMGPATH + bgimg;
			myBG.id= "myBGimg";
			n= dojo.byId('backDiv');
			//n.innerHTML ='<img src="' + myBG.src + '" width = "100%" height = "100%" >' ;
			dojo.place(myBG,dojo.byId('backDiv'),'first');
			myBG.style.width= '100%';
			myBG.style.height= '100%';
			//sizeBackground();
	  }
	  		function setPageTitle(){
				n= dojo.byId('pt');
				i = dojo.doc.createElement('img');
				i.src= IMGPATH+'pageTitle_'+thisPage+'.png';
				i.id= "ptimg";
				i.className= 'transimg';
				n.appendChild(i);
			}
	function setBorder() {
			id = dojo.byId('borderShell')
			var nw= dojo.style(id, "width");
			var nh= dojo.style(id, "height");
			var v = dijit.getViewport();
			if (nw < v.w ){dojo.style(id, 'left', (v.w-nw)/2 +'px')} else {dojo.style(id, 'left', '0px')} 
			if (nh < v.h ){dojo.style(id, 'top', (v.h-nh)/2 +'px')} else {dojo.style(id, 'top', HEADHT)}		
			}
			
// This provides us with dojo.generateId()
(function(d){
  // wrapped in this function to keep a local copy of some vars:
  var globalId, id_count = 0, base = "djid_";
  // the function:
  d.generateId = function(altBase){
     do{ globalId = (altBase || base) + (++id_count) }
     while(d.byId(globalId));
     return globalId;
  }
})(dojo);