// wrap in a function to restrict scope
var MHJobs = function() {
   // supress errors
    var errorHandler = window.onerror;
    window.onerror = function() { return false; };

    function urlEncode(val) {
        if (typeof (encodeURIComponent) == 'function') {
            return encodeURIComponent(val);
        } else {
            return escape(val);
        }
    }

	 function uniqid()
	{
		var newDate = new Date;
		return newDate.getTime();
	}
   // preset dimensions
    var dimensions = {
    	'vBanner': { w: 160, h: 320 },
		'vTower' : { w: 160, h: 600 },
		'vSearch' : { w: 160, h: 240 }
    };

    // Set default transparency
    var Transparent = false;

    // selected dimensions
    var size = dimensions[MHJobParams.Type];

	//Evaluate Optional User Params;
    if (typeof MHJobParams['Width'] == "number"){
    	size.w = MHJobParams['Width'];
    }
    if (typeof MHJobParams['Height'] == "number"){
    	size.h = MHJobParams['Height'];
    }
    if (typeof MHJobParams['Transparent'] == "boolean"){
    	Transparent = MHJobParams['Transparent'];
    }

    // get widget url
    var url = 'http://www.medhealthjobs.com/affiliates/widgets/jobRoll.cfm';
    if(MHJobParams.Type == 'vSearch'){
    	url = 'http://www.medhealthjobs.com/affiliates/widgets/jobSearch.cfm';
    }

    // build query string array
    var qs = [];
    for (var key in MHJobParams) {
        if (MHJobParams.hasOwnProperty(key)) {
            qs.push(key + "=" + urlEncode(MHJobParams[key]));
        }
    }

    // combine url and query string
    url += "?" + qs.join("&");
    frameID = 'MHJobs' + uniqid()

    // write iframe tags
	//document.write("<ifr" + "ame name='" + frameID + "' id='" + frameID + "' src='" + url + "' width='" + size.w + "' height='" + size.h + "' frameborder='0' scrolling='no' allowtransparency='" + Transparent + "' onload='resizeFrame(this.id,\"" + MHJobParams.Type + "\",\"" + typeof MHJobParams['Height'] + "\")'></ifr" + "ame>");
    document.write("<ifr" + "ame name='" + frameID + "' id='" + frameID + "' src='" + url + "' width='" + size.w + "' height='" + size.h + "' frameborder='0' scrolling='no' allowtransparency='" + Transparent + "'></ifr" + "ame>");


   // restore original error handler
    window.onerror = errorHandler;

    // clean up
    errorHandler = null;
    dimensions = null;
    size = null;
    url = null;
    qs = null;
    MHJobParams = null;


} ();        // invoke function

function resizeFrame(frameID,widgetType,height) {
 	if(widgetType != 'vSearch' && height != "number"){
		var f = document.getElementById(frameID);
		f.style.height = f.contentWindow.document.body.offsetHeight + 'px';
	}
}

