// Function for initialising widget on publisher web page.
function init_geocast_widget()
{
    // Externally definable variables, customisable by publisher (variables might not be defined).
    window.geocastWidget_publisherKey = window.geocastWidget_publisherKey != undefined ? window.geocastWidget_publisherKey : '';
    window.geocastWidget_widgetId = window.geocastWidget_widgetId != undefined ? window.geocastWidget_widgetId : '';
    window.geocastWidget_frameWidth = window.geocastWidget_frameWidth != undefined ? window.geocastWidget_frameWidth : null;
    window.geocastWidget_frameHeight = window.geocastWidget_frameHeight != undefined ? window.geocastWidget_frameHeight : null;
    window.geocastWidget_frameBorder = window.geocastWidget_frameBorder != null ? window.geocastWidget_frameBorder : 0;
    window.geocastWidget_containerId = window.geocastWidget_containerId != undefined ? window.geocastWidget_containerId : "geocast_promotion_container";
    window.geocastWidget_host = window.geocastWidget_host != undefined ? window.geocastWidget_host : "www.bview.co.uk";
    window.geocastWidget_location = window.geocastWidget_location != undefined ? window.geocastWidget_location : "";
    window.geocastWidget_category_type = window.geocastWidget_category_type != undefined ? window.geocastWidget_category_type : "";
    window.geocastWidget_category = window.geocastWidget_category != undefined ? window.geocastWidget_category : "";
    window.geocastWidget_thirdparty_category_id = window.geocastWidget_thirdparty_category_id != undefined ? window.geocastWidget_thirdparty_category_id : "";
    
    // Internal variables, hidden to publisher.
    var geocastWidget_contentUrl = getUrl();
    var geocastWidget_frameWidthOpt = (window.geocastWidget_frameWidth != null && window.geocastWidget_frameWidth.length > 0) ? 'width="' + window.geocastWidget_frameWidth +'" ' : '';
    var geocastWidget_frameHeightOpt = (window.geocastWidget_frameHeight != null && window.geocastWidget_frameHeight.length > 0) ? 'height="' + window.geocastWidget_frameHeight + '" ' : '';
    var geocastWidget_iframeHtml = '<iframe id="frame'+ geocastWidget_widgetId +'" ' + geocastWidget_frameWidthOpt + geocastWidget_frameHeightOpt + ' frameborder="' + window.geocastWidget_frameBorder + '" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no"></iframe>';
    var geocastWidget_divContainer = document.getElementById(window.geocastWidget_containerId);
   
    
    if (geocastWidget_divContainer != null)
    {
    	geocastWidget_divContainer.innerHTML = geocastWidget_iframeHtml;
    	// inorder to refresh the iframe in firefox we need to do the following tricksie 
    	var iframe = document.getElementById("frame"+ geocastWidget_widgetId);
    	iframe.src = geocastWidget_contentUrl;
    }
    else
    {
        alert("BView error: Cannot locate HTML container with ID " + window.geocastWidget_containerId);
    }

    // Clear variables, mainly in case publisher wants to display multiple widgets within the same page.
    window.geocastWidget_publisherKey = null;
    window.geocastWidget_widgetId = null;
    window.geocastWidget_iframeHtml = null;
    window.geocastWidget_frameWidth = null;
    window.geocastWidget_frameHeight = null;
    window.geocastWidget_frameBorder = null;
    window.geocastWidget_containerId = null;
    window.geocastWidget_host = null;
    window.geocastWidget_location = null;
    window.geocastWidget_category_type = null;
    window.geocastWidget_category = null;
    window.geocastWidget_thirdparty_category_id = null;
}

function getUrl()
{
	var url = "http://" + geocastWidget_host + "/widget/promotions.do?pid=" + encodeURIComponent(window.geocastWidget_publisherKey) + "&wid=" + encodeURIComponent(window.geocastWidget_widgetId);

	// variables that may be changed altered.
	if(window.geocastWidget_host != undefined)
	{
		url+="&host=" + encodeURIComponent(window.geocastWidget_host);
	}
	if(window.geocastWidget_location != undefined && window.geocastWidget_location != "")
	{
		url+="&location=" + encodeURIComponent(window.geocastWidget_location);
	}
	if(window.geocastWidget_category_type != undefined && window.geocastWidget_category_type != "")
	{
		url+="&categoryType=" + encodeURIComponent(window.geocastWidget_category_type);
	}
	if(window.geocastWidget_category != undefined && window.geocastWidget_category != "")
	{
		url+="&category=" + encodeURIComponent(window.geocastWidget_category);
	}
	if(window.geocastWidget_thirdparty_category_id != undefined && window.geocastWidget_thirdparty_category_id !="")
	{
		url+="&externalCategory=" + encodeURIComponent(window.geocastWidget_thirdparty_category_id);
	}
	return url;
}

// Initialise widget.
init_geocast_widget();