var c4LayoutControl = {
	LAYOUT_INTERVAL: 50,
	RESIZE_ELEMENT_ID: 'tResizeControl',
	ELEMENTS: {
		'NAVIGATION':'navigation',
		'CONTAINER':'cont2',
		'HERO_IMAGE':'heroImage',
		'SKYSCRAPER':'sky',
		'RHC':'cont3',
		'LEFT':'cont1',
		'FOOTER':'c4footer',
		'SKIPS':'skipLinks',
		'SPONSOR':'sponsor',
		'MAP':'cont4',
		'BREADCRUMB':'breadcrumb'
	},

	layoutIntervalObj: null,
	lastChange: 0,
	lastWidth: 0,
	lastHeight: 0,
	textSizeEl: null,

	init: function() {
		c4LayoutControl.createTextSizeObject();
		c4LayoutControl.setLayoutMonitor();
	},

	createTextSizeObject: function() {
		this.textSizeEl = document.createElement('span');
		this.textSizeEl.id = this.RESIZE_ELEMENT_ID;
		this.textSizeEl.innerHTML = '&nbsp;';
		this.textSizeEl.style.position = 'absolute';
		this.textSizeEl.style.left = '-9999px';
		document.body.appendChild(this.textSizeEl);
	},

	setLayoutMonitor: function() {
		this.layoutIntervalObj = setInterval(this.getSetLayout(), this.LAYOUT_INTERVAL);
	},

	getSetLayout: function() {
		me = this;
		return function() {
			if (me.textSizeEl == null) return;
			if (me.getSize()!=me.lastChange) {
				me.lastChange = me.getSize();
				me.setLayout();
			}
		}
	},

	// For FF/MOZ use offsetHeight, for IE use offsetWidth
	getSize: function() {
		cH = this.textSizeEl.offsetHeight;
		cW = this.textSizeEl.offsetWidth;
		if(this.lastChange==cH) {
			return this.textSizeEl.offsetWidth;
		} else {
			return this.textSizeEl.offsetHeight;
		}
	},

	setLayout: function() {
		var $ = C4.DOM.$; // from c4utils.js
		var navigation = $(this.ELEMENTS.NAVIGATION);
		var lhc = $(this.ELEMENTS.LEFT);
		var container = $(this.ELEMENTS.CONTAINER);
		var heroImage = $(this.ELEMENTS.HERO_IMAGE);
		var skyscraper = $(this.ELEMENTS.SKYSCRAPER);
		var rhc = $(this.ELEMENTS.RHC);
		var footer = $(this.ELEMENTS.FOOTER);
		var skips = $(this.ELEMENTS.SKIPS);
		var sponsor = $(this.ELEMENTS.SPONSOR);
		var map = $(this.ELEMENTS.MAP);
		var breadcrumb = $(this.ELEMENTS.BREADCRUMB);
		
		var topCounter=0;

		if (navigation)	topCounter = navigation.offsetTop + navigation.offsetHeight;
		if (breadcrumb)	topCounter = topCounter + breadcrumb.offsetHeight;
		if (lhc) lhc.style.top = (topCounter + 99) + 'px';
		if (rhc) rhc.style.top = (topCounter + 159) + 'px';
		if (skyscraper) skyscraper.style.top = (topCounter + 144) + 'px';
		if (heroImage){
			heroImage.style.top = topCounter + 'px';
			if (container) {
				container.style.top = (topCounter + 128) + 'px';
				if (map) map.style.top = (topCounter + container.offsetHeight + 128) + 'px';
			}
		} else {
			if (container) {
				container.style.top = (topCounter) + 'px';
				if (map) map.style.top = (topCounter + container.offsetHeight) + 'px';
			}
		}
		if (skips) skips.style.top = topCounter + 'px';
		if (sponsor) sponsor.style.top = topCounter + 'px';

		//To place footer, get largest bottom offset of lhc, rhc, and container
		if (footer) {
			belowThese = [lhc,container,skyscraper,rhc,map];
			for (var i=0;i<belowThese.length;i++) {
				if (belowThese[i]==null) continue;
				topCounter = Math.max(topCounter, belowThese[i].offsetTop + belowThese[i].offsetHeight);
			}
			footer.style.width = '100%';
			footer.style.top = (topCounter + 10) + 'px';
		}

	}

};

var c4PageTools = {

	TOOLS_CLASS: "pagetools",
	toolEls:[],

	init: function() {
		c4PageTools.addPrintLink();
		c4PageTools.formatEmailLink();
	},

	addPrintLink: function() {
		this.toolEls = C4.DOM.getElementsByClassName(this.TOOLS_CLASS);
		if(this.toolEls.length) {
			for (var i=0; i<this.toolEls.length; i++) {
				var printEl = document.createElement('a');
				printEl.href = '#';
				var printElText = document.createTextNode('Print');
				var afterText = document.createTextNode(' | ');
				printEl.appendChild(printElText);
				C4.BOM.addEvent(printEl, 'click', function(e) {print();});
				this.toolEls[i].insertBefore(afterText, this.toolEls[i].childNodes[0]);
				this.toolEls[i].insertBefore(printEl,afterText);
			}
		}
	},
	//this additional function won't be required in phase 2;
	formatEmailLink: function() {
		this.toolEls = C4.DOM.getElementsByClassName(this.TOOLS_CLASS);
		if(this.toolEls.length) {	
			for (var i=0; i<this.toolEls[0].getElementsByTagName('a').length;i++) {
				emailEle = this.toolEls[0].getElementsByTagName('a')[i];
				if(emailEle.href.indexOf("mailto:")!=-1) {
					str1 = emailEle.href.substring(emailEle.href.indexOf("?subject=")+9,emailEle.href.lastIndexOf("&"))
					str2 = document.location.href;
					str1 = "Check this out on 4Food: "+str1;
					str2 = "\n\n"+str2;
					emailEle.href="mailto:?subject=" + escape(str1)+"&body="+escape(str2);
				}
			}
		}
	}
};

// Initialise only when DOM is loaded 
C4.BOM.addDOMLoadEvent(c4LayoutControl.init);
C4.BOM.addDOMLoadEvent(c4PageTools.init);