/*************** The Construction Index News Widget
* © 2010 The Construction Index
*/
var IECheck = {
  version: function() {
    var version = false; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}
// our main class
TCINewsWidget = new function() {
    var baseUrl = 'http://www.theconstructionindex.co.uk/';
    var stylesheet = baseUrl + 'widgets/news_widget.css';
    var contentUrl = baseUrl + 'news/widgetjson';
    var rootDomElement = 'tciNewsWidget';
	var urlVars;
	var options = [{'headerHeight':25, 'footerHeight':60, 'width':175, 'height':300, 'backgroundColour':'#0f3156', 'borderColour':'#0f3156', 'headerColour':'#ffffff'}];

	// function to append the stylesheet for the widget to the HEAD section of the page
    function requestStylesheet(cssUrl) {
        var s = document.createElement("link");
        s.rel = "stylesheet";
        s.type = "text/css";
        s.href = cssUrl;
        s.media = "all";
		document.getElementsByTagName('head')[0].appendChild(s);
    }

	// function to add the source JSON data file to the HEAD section of the page
    function requestContent(local) {
        var s = document.createElement('script');
        s.src = contentUrl + '?from=' + escape(location.href);
        document.getElementsByTagName('head')[0].appendChild(s);
    }

    this.init = function() {
		if(typeof(tciNewsWidgetOptions) == 'object') {
			if(tciNewsWidgetOptions[0].width != undefined) {
				options[0].width = tciNewsWidgetOptions[0].width;
			}
			if(tciNewsWidgetOptions[0].height != undefined) {
				options[0].height = tciNewsWidgetOptions[0].height;
			}
			if(tciNewsWidgetOptions[0].headerColour != undefined) {
				options[0].headerColour = tciNewsWidgetOptions[0].headerColour;
			}
			if(tciNewsWidgetOptions[0].backgroundColour != undefined) {
				options[0].backgroundColour = tciNewsWidgetOptions[0].backgroundColour;
			}
			if(tciNewsWidgetOptions[0].borderColour != undefined) {
				options[0].borderColour = tciNewsWidgetOptions[0].borderColour;
			}
		}
		this.serverResponse = function(data) {
	   		if (!data) return;
	        var containerDiv = document.getElementById(rootDomElement);
	        var txt = '<div id="tciWidgetHeader"><h1>Construction News</h1></div><div id="tciWidgetContent"><ul id="tciWidgetItems">';
			for(var i in data) {
				if(data[i]['title'] != undefined) {
					txt += '<li class="tciWidgetItem"><a href="' + baseUrl + data[i]['link'] + '" target="_blank">' + data[i]['title'] + '</a></li>';
				}
			}
			if(IECheck.version() == 6) {
				var logoExtension = 'gif';
			} else {
				var logoExtension = 'png';
			}
			txt += '</ul></div><div id="tciWidgetFooter"><a href="' + baseUrl + '" target="_blank"><img src="' + baseUrl + 'widgets/tci-logo.' + logoExtension + '" alt="The Construction Index" border="0" id="tciWidgetLogo" /></a><form id="tciWidgetSearchForm" class="tciSearch" method="get" action="' + baseUrl + '/search/" target="_blank"><input type="text" name="q" value="Construction Search..." id="tciWidgetFormQ" onfocus="this.value=\'\'" /><input type="image" src="' + baseUrl + 'widgets/go-btn.gif" name="submit" value="Search" alt="Search" id="tciWidgetFormSubmit" /></form></div>';
			// populate our widget data
	        containerDiv.innerHTML = txt;
			// set dimensions of widget
			var headerDiv = document.getElementById('tciWidgetHeader');
			var contentDiv = document.getElementById('tciWidgetContent');
			var footerDiv = document.getElementById('tciWidgetFooter');
			headerDiv.style.height = options[0].headerHeight + 'px';
			headerDiv.childNodes[0].style.color = options[0].headerColour;
			footerDiv.style.height = options[0].footerHeight + 'px';
			contentDiv.style.height = options[0].height - options[0].footerHeight - options[0].headerHeight + 'px';
	        containerDiv.style.display = 'block';
			containerDiv.style.width = options[0].width + 'px';
			containerDiv.style.height = options[0].height + 'px';
			containerDiv.style.backgroundColor = options[0].backgroundColour;
			containerDiv.style.borderColor = options[0].borderColour;
	        // make element visible
	        containerDiv.style.visibility = 'visible';
	    }
        requestStylesheet(stylesheet);
        document.write("<div id='" + rootDomElement + "' style='display: none'></div>");
        requestContent();
        var no_script = document.getElementById('no_script');
        if (no_script) {
            no_script.style.display = 'none';
        }
    }
}
// lights up!
TCINewsWidget.init();
