// Fathomer
// Author: Charles Lawrence 1/2009
// Licensed under Mozilla Public License 1.1
// v0.5
var fathomer = {
	account:'',
	message:"Hey! It looks like you're a member of Twitter too! Join our conversation, you can follow us here!",
	urlcheck:'http://twitter.com/account/settings',
	target:'body',
	loadcss:true,
	altbadge:'',

	checkcookie: function(){
		if( document.cookie.match(/fathomer=closed/i) ){
			return true;
		}
	},
	setcookie: function(){
		document.cookie = 'fathomer=closed; expires=Thu, 21 Dec 2012 20:00:00 UTC; path=/';
	},

	showbadge: function(){

		$('#fathomerdiscover').remove(); //cleanup from discovery process

		if(typeof this.altbadge != 'function'){

			$(this.target).append( '<div id="fathomer"><div id="fathomerclose"></div><span id="fathomermessage">'+ this.message +'</span><span id="fathomerfollow"><a href="http://twitter.com/'+this.account+'" target="_blank">http://twitter.com/'+this.account+'</a></span></span>');
			
			//sets cookie not to show badge if user closes
			$('#fathomerclose').bind('click', function(){ $('#fathomer').fadeOut(1000); fathomer.setcookie(); });

			if(this.loadcss){
				$('head').append('<style type="text/css">#fathomer{display:none;position:absolute;top:20px;right:20px;width:175px;padding:15px;background-color:#FF0000;border-radius:10px;-moz-border-radius:10px;-webkit-border-radius:10px;border:5px solid #FFFFFF;color:#FFFFFF;font-family:arial;font-size:14px;}#fathomerclose{width:10px;height:10px;border:1px solid #FFFFFF;position:absolute;top:5px;right:5px;}#fathomerclose:hover{background-color:#FFFFFF;cursor:pointer;}#fathomermessage{width:50%;}#fathomerfollow{margin:5px 0 0 0;display:block;}#fathomerfollow a, #fathomerfollow a:visited{font-size:15px;font-weight:bold;color:#FFFFFF;}</style>');
			}
			
			$('#fathomer').fadeIn(1000);
			
		}else if(typeof this.altbadge == 'function'){
			//load user-defined badge
			this.altbadge();
		}
	},

	discover: function(){
		if( !this.checkcookie() ){

			$('body').append('<div id="fathomerdiscover"><a href="'+this.urlcheck+'">twit</a><style>#fathomerdiscover{display:none;}#fathomerdiscover a{color:#cc0000;}#fathomerdiscover a:visited{color:#0000cc;}</style>');
			matchcolor = this.rgbhex( $('#fathomerdiscover a').css('color') );
			matchcolor=='#0000cc'? this.showbadge():'';

		}//end cookie check 
	},

    //convert rgb color values to hex
    rgbhex: function(rgbval){
	var s = rgbval.match(/rgb\s*\x28((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*,\s*((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*,\s*((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*\x29/);

	if(s){ s=s.splice(1); }
        if(s && s.length==3){
            d='';
            for(i in s){
                e=parseInt(s[i],10).toString(16); 
                e == "0" ? d+="00":d+=e;
            } return '#'+d;
        }else{ return rgbval; }
    },
    
    init:function(){
	window.onload = function(){
		if(typeof jQuery == 'undefined'){
			a=document.createElement('script');
			a.type='text/javascript';
			a.src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js';
			document.getElementsByTagName('html')[0].appendChild(a);

			t=setInterval(function(){ if(typeof jQuery == 'function'){clearInterval(t);fathomer.discover();} },1);
		}else{
			fathomer.discover();	
		}
	}
    }
}