var ETV;
(function() {

var debug = function(arg) {
	if (console.log) {
		console.log(arguments);
	} else {
		debug = function() {};
	}
};


ETV = {

	contentTargets: {
		head2010: { id: 'etv-header', content: null, handled: false, el: null },
		foot2010: { id: 'etv-footer', content: null, handled: false, el: null }
	},
	head2010: 'etv-header',
	foot2010: 'etv-footer',
	path: null,
	handler: false,

	start: function(params) {

		if (typeof(params) == "string") {
			params = "?" + params;
		} else {
			params = "";
		}
	
		var scripts = document.getElementsByTagName('script');

		for(var i = 0; i < scripts.length; i++) {
			var s = scripts[i];
			if (s.src && s.src.match(/layout-loader-2010\.js/)) {
				this.path = s.src.replace(/layout-loader-2010.js.*/, '');
			}
		}

		var userAgent = navigator.userAgent.toLowerCase();
		var browser = {
			version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
			safari: /webkit/.test( userAgent ),
			opera: /opera/.test( userAgent ),
			msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
			mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
		};

		// only add styles for IE7 (and probably 5 as well)
		if (browser.msie && browser.version < 7) {
			this.appendStyle('../css/layout-loader/ie.css');
		}

		// load head and foot
		if (!this.contentTargets.head2010.content) {
			this.appendScript('../include/head2010' + params);
		}
		if (!this.contentTargets.foot2010.content) {
			this.appendScript('../include/foot2010' + params);
		}
	},
	updateContent: function() {

		if (this.handler) {
		
			return false;
		}
		var me = this;
		this.handler = setInterval(function() {

			// keep track
			var runAgain = false;

			// do not bother if we do not have a body yet
			if (!document.body) {
				return false;
			}

			var type;
			for(type in me.contentTargets) {
				if (typeof(me.contentTargets[type]) != 'function') {

					if (type === null) {
						continue;
					}
					if (typeof(me.contentTargets[type]) == "undefined") {
						continue;
					}
					if (me.contentTargets[type].handled) {
						continue;
					}
					var element = document.getElementById(me.contentTargets[type].id);
					if (element) {

						me.contentTargets[type].handled = true;
						element.innerHTML = me.contentTargets[type].content;
						/** remove content locally, saving a bit of memory **/

					} else {
						runAgain = true;
					}

					if (type == 'head2010') {
						jQuery('#navigation ul.level_1').addClass('sf-menu').superfish({
							pathClass: 'true',
							delay: 750,
							pathLevels: 0,
							animation: { speed: 'fast', height:'show'}
						});
						jQuery('#more h3 a').click(function() {
							$('#family-links').slideToggle();
							return false;
						});
					}
				}
			}

			clearInterval(me.handler);
			me.handler = false;
		}, 10);
	},
	appendStyle: function(path) {

		path 			= this.path + path;
		var link 		= document.createElement('link');
		link.type 		= 'text/css';
		link.href		= path;
		link.rel		= "stylesheet";

		document.getElementsByTagName('head')[0].appendChild(link);

	},
	appendScript: function(path) {

		path 			= this.path + path;
		var script 		= document.createElement('script');
		script.type 	= 'text/javascript';
		script.src 		= path;
		document.getElementsByTagName('head')[0].appendChild(script);

	},
	setContent: function(type, data) {
		if (data.status) {

			if (typeof(this.contentTargets[type]) != "undefined") {
				this.contentTargets[type].content = data.content;
				this.contentTargets[type].handled = false;

			}

			this.updateContent();

		}
	}
};

//ETV.start();

}());
