if (!FYREBALL) {

var FYREBALL = {
baseUrl : 'http://media.fyreball.com/',
fyreUrl : 'http://www.fyreball.com/',
versionNum : '0',
css : false,
maxZIndex : (2147483648 - 1),
arrayPageSize : null,
arrayPageScroll : null,
lightboxTop : null,
fyreitElements : null,
fadeElements : null,
fyreitIdx : 0,
opacityRefreshTimer : null,
gimpedBrowser : null,

checkBrowser : function() {
	var is_safari = (navigator.vendor == "Apple Computer, Inc.");
	var is_mac_firefox = (navigator.appCodeName == "Mozilla" && navigator.userAgent.search("Macintosh") != -1);
	//room to expand gimped list to other browsers
	this.gimpedBrowser = (is_safari || is_mac_firefox);
},

_getSelection : function() {
	return(!!document.getSelection) ? document.getSelection() :
	(!!window.getSelection) ? window.getSelection() :
	document.selection.createRange().text;
},

getSelectedText : function() {
	var text = this._getSelection();      

	if (text && text.length > 0) {
		text = text.toString();
		text = text.replace(/^\s+/, "");
		text = text.replace(/\s+$/, "");
		text = text.replace(/\r/g, "");
		text = text.replace(/\n/g, " ");
		text = text.replace(/\s+/, " ");
	} else {
		text = "";
	}
	
	return text;
},

getPageScroll : function() {
	var yScroll; if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = ['',yScroll];
	return arrayPageScroll;
},

getPageSize : function() {
	var xScroll, yScroll; if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth; yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight; if (self.innerHeight) {
		windowWidth = self.innerWidth; windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight;
	}
	if (yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if (xScroll < windowWidth) {
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = [pageWidth,pageHeight,windowWidth,windowHeight];
	return arrayPageSize;
},

createShadowBox : function() {
	var fyre_base=document.body.appendChild(document.createElement('div')); 
	this.fyreitElements.add(fyre_base);

	var value = 6.0; 
	fyre_base.onclick = this.cleanup; 
	fyre_base.href = "javascript:void(0);"; 
	fyre_base.id = "grey_base"; 
	fyre_base.style.position = (this.isIE()) ? "absolute" : "fixed"; 
	fyre_base.style.background = "#000000"; 
	fyre_base.style.top = "0"; 
	fyre_base.style.left = "0"; 
	fyre_base.style.width = "100%"; 
	fyre_base.style.height = (this.arrayPageSize[1] + 'px'); 
	fyre_base.style.zIndex = this.maxZIndex - 1; 
	fyre_base.style.opacity = value/10; 
	fyre_base.style.filter = 'alpha(opacity=' + value*10 + ')';
},

hookIFrameEvents : function(iframe) {
	var win = iframe.contentWindow;
	var origIFrameLocation = null;

	var onLoadFunc = function() {
		if (origIFrameLocation == null) {
			origIFrameLocation = win.location;
		} else {
			window.setTimeout(FYREBALL.fadeFrame, 1);
		}
	};

	if (!!iframe.addEventListener) {
		iframe.addEventListener('load', onLoadFunc, false);
	} else if (!!iframe.attachEvent) {
		iframe.attachEvent('onload', onLoadFunc);
	}
},

createIFrame : function(url) {
	var iframe = document.createElement('iframe'); 
	this.fyreitElements.add(iframe);
	this.fadeElements.add(iframe);

	iframe.id = "fyreit_frame"; 
	iframe.style.opacity = 1.0;
	iframe.style.position = (this.isIE())?"absolute":"fixed"; 
	iframe.style.height = "90%"; 
	iframe.style.width = "900px"; 
	iframe.style.top = (this.isIE())? this.lightboxTop + "px":"5%"; 
	iframe.style.left = "50%"; 
	iframe.style.margin = "0px 0 0 -450px"; 
	iframe.style.background = "#000000"; 
	iframe.style.zIndex = this.maxZIndex; 
	iframe.style.display = "block"; 
	iframe.style.border = "5px solid #42819A"; 
	iframe.style.visibility = "visible"; 
	iframe.style.frameborder = "0"; 
	iframe.style.padding = "0";
	iframe.style.overflow = "auto";

	document.body.appendChild(iframe); 

	iframe.src = url;
	
	this.opacityRefreshTimer = window.setInterval(
		function() { iframe.style.opacity = 0.99; },
		500);

	this.hookIFrameEvents(iframe);
},

createCloseButton : function() {
	var close_button=document.createElement('a'); 
	this.fyreitElements.add(close_button);
	this.fyreitElements['close_button'] = close_button;

	this.fadeElements.add(close_button);
	
	close_button.id = "close_button"; 
	close_button.onclick = this.cleanup; 
	close_button.innerHTML = '<img src="'+this.fyreUrl+'img/buttons/delete.gif" alt="[close]" title="click to close" style="border:0;padding:0;background:none;margin:0;"/>'; 
	close_button.href = "javascript:void(0);"; 
	close_button.style.position = (this.isIE())?"absolute":"fixed"; 
	close_button.style.top = (this.isIE())? this.lightboxTop + "px":"5%"; 
	close_button.style.left = "50%"; 
	close_button.style.background = "#000000"; 
	close_button.style.zIndex = this.maxZIndex; 
	close_button.style.border = "1px solid #42819A"; 
	close_button.style.margin = (this.isIE())?"2px 0 0 451px":"2px 0 0 461px";
	close_button.style.padding = "0"; 
	close_button.style.fontSize = "9pt"; 
	
	document.body.appendChild(close_button);
},

fadeFrame : function() {

	if (FYREBALL.opacityRefreshTimer != null) {
		window.clearInterval(FYREBALL.opacityRefreshTimer);
	}

	if (!FYREBALL.isIE()) {
		var fadePos = 0.99; 
		var fadeTimeout = 20;
		var fadeStep = fadePos/15.0;

		var fadeFunc = function() {
			fadePos -= fadeStep;
			if (fadePos > 0.0) {
				opacity = Math.sin(fadePos);

				for (var i = 0; i < FYREBALL.fadeElements.length; i++) {
					FYREBALL.fadeElements[i].style.opacity = opacity;
				}
			} else {
				clearInterval(intervalId);  
				FYREBALL.cleanup();
			}
		};

		var intervalId = window.setInterval(fadeFunc, fadeTimeout);
	} else {
		var fadeTimeout = 0.5;

		for (var i = 0; i < FYREBALL.fadeElements.length; i++) {
			FYREBALL.fadeElements[i].style.filter = "blendTrans(duration=" + fadeTimeout + ")";
		}

		for (var i = 0; i < FYREBALL.fadeElements.length; i++) {
			var el = FYREBALL.fadeElements[i];
			if (el.filters.blendTrans.status != 2) {
				el.filters.blendTrans.apply();
				el.style.visibility="hidden";
				el.filters.blendTrans.play();
			}
		}

		window.setTimeout(FYREBALL.cleanup, fadeTimeout * 1000);
	}
},

cleanup : function() {

	for (var i = 0; i < FYREBALL.fyreitElements.length; i++) {
		var el = FYREBALL.fyreitElements[i];
		if (!!el) {
			el.parentNode.removeChild(el); 
		}
	}

	FYREBALL.initFyreitElements();
},

isIE : function() {
	if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
		var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, ''); 
		var iever = (rslt != null && Number(rslt[1]) >= 5.0);
	}

	return iever;
},

createFrameElements : function(url) {
	this.createShadowBox(); 
	this.createIFrame(url); 
	this.createCloseButton();
},

initFyreitElements : function() {
	this.fyreitElements = [];
	this.fyreitElements.add = function(el) {
		this[this.length] = el;
	}

	this.fadeElements = [];
	this.fadeElements.add = function(el) {
		this[this.length] = el;
	}
},

fyreit : function(props) {
	props = typeof(props) != 'undefined' ? props : {};
	var url = this.createFyreitUrl(props);

	this.arrayPageSize = this.getPageSize(); 
	this.arrayPageScroll = this.getPageScroll(); 

	this.lightboxTop = (this.arrayPageScroll[1] + 15 + ((this.arrayPageSize[3] - (0.9*this.arrayPageSize[1])) / 2)); 
	if (this.lightboxTop < 0) {
		this.lightboxTop = "25";
	}

	if (!document.getElementById('fyre_frame')) {
		if ((typeof(url) != 'string') || (url == '')) {
			url = this.createFyreitUrl();
		}
		this.createFrameElements(url); 
	} else {
		this.cleanup();
	}
},

createFyreitUrl : function(props) {
	props = typeof(props) != 'undefined' ? props : {};

	var raw_base_url = 'url' in props ? props.url : location.href;
	var raw_link_list = 'link_list' in props ? props.link_list : [];
	var raw_title = 'title' in props ? props.title : document.title;
	var raw_text = 'description' in props ? props.description : this.getSelectedText();

	var url = this.fyreUrl+'fyreball/add?v=3';
	url += '&url=' + encodeURIComponent(raw_base_url);
	url += '&title=' + encodeURIComponent(raw_title);
	if (!this.gimpedBrowser) {
		url += "&ifr=true";
	}
	if (raw_text != "") {
		url += '&description=' + encodeURIComponent(raw_text);
	}
	if (raw_link_list.length > 0) {
		url += '&lnklst=' + encodeURIComponent(raw_link_list.join(','));
	}

	return url;
},

createFyreitLink : function(props) {
	props = typeof(props) != 'undefined' ? props : {};

	var url = 'url' in props ? props.url : '';
	var buttonText = 'buttonText' in props ? props.buttonText : 'Fyre This';
	var buttonClass = 'buttonClass' in props ? props.buttonClass : 'fb_button fbico_default';

	var o=document.createElement('a');
	o.className = buttonClass;
	o.title = "Fyre This!";
	o.innerHTML = buttonText;
	if (this.gimpedBrowser) {
		o.href = this.createFyreitUrl(props)
		o.target = "_blank";
	}
	else {
		o.href = "javascript:void(0);";
		o.onclick = function() { FYREBALL.fyreit(props); };
	}

	return o;
},

attachCSS: function() {
	var css_url = this.baseUrl+'css/widget'+this.versionNum+'.css';
	
	var cssNode = document.createElement('link');
	cssNode.setAttribute('rel', 'stylesheet');
	cssNode.setAttribute('type', 'text/css');
	cssNode.setAttribute('href', css_url);
	document.getElementsByTagName('head')[0].appendChild(cssNode);
	
	this.css = true;
},

create : function(props) {
	if (!this.css)	{
			this.attachCSS();
	}
	
	if (this.gimpedBrowser == null)  {
		this.checkBrowser();
	}

	props = typeof(props) != 'undefined' ? props : {};

        var id = 'fyreit_' + this.fyreitIdx++;
        document.write('<span id="' + id + '"></span>');
        var o = document.getElementById(id);
        if (o) {
		o.appendChild(this.createFyreitLink(props));
	}
}
};

} // if (!FYREBALL)

FYREBALL.initFyreitElements();
