var srfp;
if ( srfp == undefined ) {
	srfp = {};
}

if ( !srfp.initialized ) {

	srfp.byId = function(id) {
		return document.getElementById(id);
	};

	srfp.add_css = function(css_content) {
		var head = document.getElementsByTagName('head')[0];
		var style = document.createElement('style');
		style.type = 'text/css';
		
		if ( style.styleSheet ) {
			style.styleSheet.cssText = css_content;
		} else {
			rules = document.createTextNode(css_content);
			style.appendChild(rules);
		}
		head.appendChild(style);
	};

	srfp.clearTimeout = function() {
		if ( srfp.timeout_id ) {
			clearTimeout(srfp.timeout_id);
			srfp.timeout_id = 0;
		}
	};

	srfp.initialized = true;
	srfp.timeout_id = 0;
}

srfp.widget = function(options) {

	var object = this;

	this.is_ssl = ("https:" == document.location.protocol);
	if ( ( navigator.userAgent.indexOf('Safari') != -1 ) && ( navigator.userAgent.indexOf('Chrome') == -1 ) )
		this.is_safari = true;
	else
		this.is_safari = false;
	
	// set default options
	if ( !options.clear_previous )		options.clear_previous = false;
	if ( !options.display )				options.display = "overlay";
	if ( !options.button_style )		options.button_style = "";
	if ( !options.button_icon_size )	options.button_icon_size = "24";
	if ( !options.button_text )			options.button_text = "Request Proposal";
	if ( !options.button_text_style )	options.button_text_style = "";
	if ( !options.rfp_type )			options.rfp_type = "";
	if ( !options.start_rfp )			options.start_rfp = "";
	
	// match button identifiers with button types
	if ( !options.button ) options.button = "a";
	else {
		options.button = options.button.toLowerCase();
		if ( options.button != "a" && options.button != "b" && options.button != "custom" ) {
			if ( options.button == "request proposal" ) options.button = "a";
			else if ( options.button == "send a speedrfp" ) options.button = "b";
			else options.button = "a";	// default
		}
	}
	
	this.options = options;
	
	
	// hooks
	if ( !this.options.populateUid ) { this.options.populateUid = ""; }		// called before load()
	else if ( !window[this.options.populateUid] ) { alert("Error: the populateUid function '" + this.options.populateUid + "' does not exist"); return false; }
	else this.options.populateUid = window[this.options.populateUid];

	if ( this.is_ssl ) {
		this.base_url = this.ssl_base_url;
	} else {
		this.base_url = this.base_url;
	}

	this.refreshUrl();

	this.initial_iframe_url = this.getEmptyUrl();

	if ( this.options.display == 'overlay' ) {
		this.options.width = "100%";
		this.options.height = "";
	} else {
		if ( !this.options.width) { this.options.width = "100%"; }
		
		// if container exists, and it has a height, then speedrfp_container will fill it 100% from defined style.  otherwise, use 600px.
		if ( this.options.container ) {
			var container = srfp.byId(this.options.container);
			if ( !container.style.height && !this.options.height ) this.options.height = "600px";
		}
		else if ( !this.options.height ) this.options.height = "600px;";
	}

	this.iframe_html =
		'<iframe id="speedrfp_iframe" name="speedrfp_iframe" allowTransparency="true" scrolling="auto" frameborder="0" class="loading"'
		+ ' src="' + this.initial_iframe_url + '"'
		+ ' style="width: '	+ this.options.width + ';"></iframe>';

	if ( this.options.button == "custom" ) {
		this.button_html = '<span id="speedrfp_button" class="speedrfp_button_custom_' + this.options.button_icon_size + '" style="' + this.options.button_style + '">'
		+ '<a href="#" onclick="return false;" id="speedrfp_button_text" style="' + this.options.button_text_style + '">' + this.options.button_text + '</a>'
		+ '</span>';
	} else {
		this.button_html = '<span id="speedrfp_button" class="speedrfp_button_' + this.options.button + '"></span>';
	}

	this.loading_html = '<div id="speedrfp_loading"><div id="speedrfp_loading_content">Loading <i>Speed</i>RFP...<div id="speedrfp_reload">Taking too long?  Click <span id="speedrfp_reload_link">here</span> to reload.</div></div></div>';

	this.overlay_html =
		'<div id="speedrfp_overlay" style="display:none">'
		+ '<div id="speedrfp_container">'
		+ '<a href="#" id="speedrfp_close"></a>'
		+ '<table id="speedrfp_table" border="0" cellpadding="0" cellspacing="0">'
		+ '<colgroup><col style="width:25px;" /><col /><col style="width:25px;" /></colgroup>'
		+ '<tbody><tr><td id="speedrfp_tl"></td><td id="speedrfp_t"></td><td id="speedrfp_tr"></td></tr>'
		+ '<tr><td id="speedrfp_l"></td><td id="speedrfp_content">'
		+ this.loading_html
		+ this.iframe_html
		+ '</td><td id="speedrfp_r"></td></tr>' // end content
		+ '<tr><td id="speedrfp_bl"></td><td id="speedrfp_b"></td><td id="speedrfp_br"></td></tr>'
		+ '</tbody></table>'	
		+ '</div>'	// end container
		+ '<div id="speedrfp_background"></div>'
		+ '</div>'; // end overlay

	if ( this.options.display == 'overlay' ) {
	
		// replace http with https if needed
		var raw_css = "#speedrfp_overlay {\n\n	width:100%;\n\n	height:100%;\n\n	position:absolute;\n\n	top:0;\n\n	left:0;\n\n	z-index:100000;\n\n	text-align:center;\n\n}\n\n\n\n#speedrfp_background {\n\n	width:100%;\n\n	top:0;\n\n	left:0;\n\n	z-index:1;\n\n	position:absolute;\n\n	background-color:#000000;\n\n	opacity:0.5;\n\n	-moz-opacity:0.5;\n\n	filter:alpha(opacity=50);\n\n}\n\n\n\n#speedrfp_container {\n\n	width:890px;\n\n	height:600px;\n\n	z-index:2;\n\n	position:relative;\n\n	margin:0 auto;\n\n}\n\n\n\n#speedrfp_content {\n\n	background-color:#ffffff;\n\n	height:100%;\n\n}\n\n\n\n#speedrfp_loading {\n\n	font:bold 18px Arial;\n\n	color:#535353;\n\n	text-align:center;\n\n	height:100%;\n\n}\n\n\n\n#speedrfp_loading_content {\n\n	position:relative;\n\n	top:45%;\n\n}\n\n\n\n#speedrfp_reload {\n\n	margin-top:10px;\n\n	font:bold 11px Arial;\n\n	color:#535353;\n\n	text-align:center;\n\n}\n\n\n\n#speedrfp_reload_link { text-decoration:underline; cursor:pointer; }\n\n\n\n#speedrfp_container iframe {\n\n	width:100%;\n\n	background:transparent;\n\n}\n\n\n\n#speedrfp_container iframe.loading {\n\n	/* display:none; causes ff bug with dojo 1.6 */\n\n	visibility:hidden;\n\n	height:1px;\n\n}\n\n\n\n#speedrfp_container iframe.loaded { height:100%; }\n\n\n\n#speedrfp_table	{ width:100%; height:100%; border:none; padding:0px; margin:0px; }\n\n#speedrfp_table	tr td { border:none; padding:0px; margin:0px; }\n\n#speedrfp_tl, #speedrfp_tr, #speedrfp_bl, #speedrfp_br {\n\n	line-height:1px; \n\n	background:url(http://www.speedrfp.com/images/widget/corners.png); \n\n	width:25px; \n\n	height:25px; \n\n	-background:url(http://www.speedrfp.com/images/widget/corners_ie.png); \n\n}\n\n#speedrfp_tl	{ background-position:0px 0px; }\n\n#speedrfp_tr	{ background-position:-25px 0px; }\n\n#speedrfp_bl	{ background-position:0px -25px; }\n\n#speedrfp_br	{ background-position:-25px -25px; }\n\n\n\n#speedrfp_t, #speedrfp_b {\n\n	line-height:1px; \n\n	background:url(http://www.speedrfp.com/images/widget/top_bottom.png) 0px 0px repeat-x; \n\n	height:25px; \n\n	-background:url(http://www.speedrfp.com/images/widget/top_bottom_ie.png) 0px 0px repeat-x; \n\n}\n\n#speedrfp_b {\n\n	background-position:0px -25px;\n\n}\n\n\n\n#speedrfp_l, #speedrfp_r {\n\n	line-height:1px; \n\n	background:url(http://www.speedrfp.com/images/widget/left_right.png) 0px 0px repeat-y; \n\n	width:25px; \n\n	-background:url(http://www.speedrfp.com/images/widget/left_right_ie.png) 0px 0px repeat-y; \n\n}\n\n#speedrfp_r {\n\n	background-position:-25px 0px;\n\n}\n\n\n\na#speedrfp_close {\n\n	width:31px;\n\n	height:32px;\n\n	top:-9px;\n\n	right:-9px;\n\n	-top:-12px;\n\n	-right:-12px;\n\n	z-index:4;\n\n	position:absolute;\n\n	cursor:pointer;\n\n	outline:none;\n\n	overflow:hidden;\n\n	background-image:url(http://www.speedrfp.com/images/widget/close.png);\n\n	-background:none;\n\n	-filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://www.speedrfp.com/images/widget/close.png', sizingMethod='crop');\n\n}\n\n\n\na#speedrfp_close:hover { background-position:0px -32px; }\n\n\n\n#speedrfp_button { display:inline-block; vertical-align:middle; cursor:pointer; }\n\n.speedrfp_button_custom_24 {\n\n	padding:4px 5px 4px 33px;\n\n	background:url(http://www.speedrfp.com/images/widget/speedrfp_24.png) 3px center no-repeat;\n\n	-background:url(http://www.speedrfp.com/images/widget/speedrfp_24_ie.png) 3px center no-repeat;\n\n}\n\n\n\n#speedrfp_button_text { }\n\n\n\n.speedrfp_button_a { \n\n	width:182px;\n\n	height:29px;\n\n	background:url(http://www.speedrfp.com/images/widget/button_a.png) no-repeat;\n\n	-background:url(http://www.speedrfp.com/images/widget/button_a_ie.png) no-repeat;\n\n}\n\n\n\n.speedrfp_button_b { \n\n	width:182px;\n\n	height:29px;\n\n	background:url(http://www.speedrfp.com/images/widget/button_b.png) no-repeat;\n\n	-background:url(http://www.speedrfp.com/images/widget/button_b_ie.png) no-repeat;\n\n}\n\n";
		replacer_regex = new RegExp("http://www.speedrfp.com", "g");
		translated_css = raw_css.replace(replacer_regex, this.base_url);
		srfp.add_css(translated_css);

		var is_inserted = false;
		if ( this.options.container ) {
			var container = srfp.byId(this.options.container);
			if ( container ) {
				container.innerHTML = this.button_html;
				is_inserted = true;
			}
		}

		// if not inserting button into a container, insert at this spot
		if ( !is_inserted ) {
			document.write(this.button_html);
		}

		// create new node for overlay because cropping could happen if inserted into small parent (IE only)
		var overlay = document.createElement("span");
		document.body.insertBefore(overlay, document.body.firstChild);
		overlay.innerHTML = this.overlay_html;

		srfp.byId("speedrfp_button").onclick = function( ) { object.show(); return false; }
		srfp.byId("speedrfp_close").onclick = function( ) { object.hide(); return false; }
		srfp.byId("speedrfp_reload_link").onclick = function( ) { object.show(); return false; }
		srfp.byId("speedrfp_iframe").setAttribute("src", object.getEmptyUrl());

	} else {

		// replace http with https if needed
		var raw_css = "#speedrfp_loading {\n\n	position:absolute;\n\n	left:0px;\n\n	top:0px;\n\n	background-color:white;\n\n	font:bold 18px Arial;\n\n	color:#535353;\n\n	text-align:center;\n\n	height:100%;\n\n	width:100%;\n\n}\n\n\n\n#speedrfp_loading_content {\n\n	position:relative;\n\n	top:200px;\n\n}\n\n\n\n#speedrfp_reload {\n\n	margin-top:10px;\n\n	font:bold 11px Arial;\n\n	color:#535353;\n\n	text-align:center;\n\n}\n\n\n\n#speedrfp_reload_link { text-decoration:underline; cursor:pointer; }\n\n\n\n#speedrfp_container {\n\n	height:100%;\n\n	position:relative;\n\n}\n\n\n\n#speedrfp_container iframe.loading { }\n\n\n\n#speedrfp_container iframe.loaded { height:100%; }\n\n";
		replacer_regex = new RegExp("http://www.speedrfp.com", "g");
		translated_css = raw_css.replace(replacer_regex, this.base_url);
		srfp.add_css(translated_css);

		var html = '<div id="speedrfp_container" style="height: ' + this.options.height + ';">' + this.loading_html + this.iframe_html + '</div>';
		
		var is_inserted = false;
		if ( this.options.container ) {
			var container = srfp.byId(this.options.container);
			if ( container ) {
				container.innerHTML = html;
				is_inserted = true;
			} else {
				alert("An error occured.  Container " + this.options.container + " was not found");
			}
		}

		if ( !is_inserted ) {
			document.write(html);
		}

		srfp.byId("speedrfp_reload_link").onclick = function( ) { object.show(); return false; }
		srfp.byId("speedrfp_iframe").setAttribute("src", object.getEmptyUrl());
		object.show();
	}

};

srfp.widget.prototype = {
	base_url:		"http://www.speedrfp.com",
	ssl_base_url:	"https://www.speedrfp.com",

	assetUrl: function(asset) {
		return this.base_url + asset;
	},

	getEmptyUrl: function() {
		return this.assetUrl("/images/dot.png");
	},

	refreshUrl: function() {
		this.query = [];

		if ( this.options.uid ) {
			this.query.push("uid=" + encodeURIComponent(this.options.uid));
		}

		if ( this.options.clear_previous ) {
			this.query.push("clear_previous=1");
		}

		if ( this.options.rfp_type ) {
			this.query.push("rfp_type=" + options.rfp_type);
		}

		if ( this.options.start_rfp ) {
			this.query.push("start_rfp=" + options.start_rfp);
		}

		this.query_string = "?" + this.query.join("&");
		var controller = "";

		this.speedrfp_url = this.base_url + "/irfps/singlefacility/choose/" + this.options.key + "/" + this.query_string;
	},

	setUid: function(uid) {
		this.options.uid = uid;
	},

	setRfpType: function(rfp_type) {
		if ( rfp_type == "meeting" || rfp_type == "wedding" || rfp_type == "special event" )
			this.options.rfp_type = rfp_type;
		else
			this.options.rfp_type = "";
	},

	setPosition: function() {
		this.scroll_top = document.documentElement.scrollTop || document.body.scrollTop;
		this.scroll_height = ( window.ActiveXObject ) ? document.body.scrollHeight : document.documentElement.scrollHeight;
		this.client_height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
		this.client_width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		this.background_height = Math.max(this.scroll_height, this.client_height);

		var height = this.client_height - 125;
		var top = this.scroll_top + 50;
		if ( height < 600 ) {
			top = this.scroll_top + 15;
			height = 600;
		}

		if ( window.ActiveXObject ) srfp.byId("speedrfp_overlay").style.width = this.client_width + "px"; // ie centering fix
		srfp.byId("speedrfp_background").style.height = this.background_height + "px";
		srfp.byId("speedrfp_background").style.width = "100%";
		srfp.byId("speedrfp_container").style.top = top + "px";
		srfp.byId("speedrfp_container").style.height = height + "px";
		srfp.byId("speedrfp_content").style.height = (height - 50) + "px"	// for ie
	},

	showReload: function() {
		srfp.clearTimeout();
		srfp.byId("speedrfp_reload").style.display = "";
	},

	load: function() {
		srfp.clearTimeout();
		if ( this.options.populateUid ) this.setUid(this.options.populateUid());
		this.refreshUrl();
		srfp.timeout_id = setTimeout("srfp_widget.showReload()", 10000);	// show reload link after 10 seconds
		
		if ( this.is_safari )
		{
			// safari cross-domain cookie fix
			form = srfp.byId("speedrfp_form");
			if ( !form )
			{
				form = document.createElement('form');
				form.id = "speedrfp_form";
				form.action = this.speedrfp_url
				form.method = "POST";
				form.target = "speedrfp_iframe";
				document.body.appendChild(form);
			}
			else
			{
				form.action = this.speedrfp_url;
			}
			
			form.submit();
		}
		else
		{
			// all other browsers
			srfp.byId("speedrfp_iframe").setAttribute("src", this.speedrfp_url);
		}
	},

	show: function() {
		srfp.byId("speedrfp_reload").style.display = "none";
		srfp.byId("speedrfp_loading").style.display = "";
		srfp.byId("speedrfp_iframe").className = "loading";

		if ( srfp.byId("speedrfp_iframe").addEventListener ) {
			srfp.byId("speedrfp_iframe").addEventListener("load", this.loaded, false);
		} else if ( srfp.byId("speedrfp_iframe").attachEvent ) {
			srfp.byId("speedrfp_iframe").attachEvent("onload", this.loaded);
		}

		if ( this.options.display == "overlay") {
			this.setPosition();
			srfp.byId("speedrfp_overlay").style.display = "block";
		}
		
		this.load();
	},

	hide: function() {
		srfp.clearTimeout();

		if ( srfp.byId("speedrfp_iframe").addEventListener ) {
			srfp.byId("speedrfp_iframe").removeEventListener("load", this.loaded, false);
		} else if ( srfp.byId("speedrfp_iframe").attachEvent ) {
			srfp.byId("speedrfp_iframe").detachEvent("onload", this.loaded);
		}

		srfp.byId("speedrfp_overlay").style.display = "none";
		srfp.byId("speedrfp_iframe").setAttribute("src", this.getEmptyUrl());
		srfp.byId("speedrfp_iframe").className = "loading";
	},

	loaded: function() {
		srfp.clearTimeout();
		srfp.byId("speedrfp_loading").style.display = "none";
		srfp.byId("speedrfp_iframe").className = "loaded";
	}

}

