function FBAppTracker() {
	this.FBAppHost = 'fb.thebreastcancersite.com';
	this.isSet = function(variable) { return( typeof( variable ) != 'undefined' );};
	this.error = function(err){
        try{
		if(this.isSet(err)&&this.isSet(navigator)&&this.isSet(navigator.userAgent)&&this.isSet(navigator.userAgent.indexOf)&&(navigator.userAgent.indexOf('Safari')>-1)){
			console.log('FBAppTracker: ' + err);
		}
        } catch ( err ){ /* do nothing */ }
	};               /*   */
	this.detectFacebookId = function(){
		try{
			var ref = this.getParam("uid");
			if(this.isSet(ref)&&(ref != '')){
				if((this.isSet(window))&&(this.isSet(window.location))&&(this.isSet(window.location.host))){
					this.setCookie('facebookId',ref,1,null,window.location.host,null);
				}
			}
		} catch ( err ){ this.error(err); }
        return true;
	};
	this.getParam = function(strParamName){
		var strReturn = "";
        try{
		var strHref = window.location.href;
		if ( strHref.indexOf("?") > -1 ){
			var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
			var aQueryString = strQueryString.split("&");
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
				if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
					var aParam = aQueryString[iParam].split("=");
					strReturn = aParam[1];
					break;
				}
			}
		}
        }catch(err){this.error(err);}
		return strReturn;
	};
	this.trackBcsThankYouPage = function(){try{var fbid=this.getCookie('facebookId');if(this.isSet(fbid)&&(fbid !='')){this.track('donate',fbid);}}catch(err){this.error(err);}return true;};
	this.trackBcsSendAnEcard = function(){try{var fbid=this.getCookie('facebookId');if(this.isSet(fbid)&&(fbid !='')){this.track('send-ecard',fbid);}}catch(err){this.error(err);}return true;};
	this.trackBcsSearch = function(){try{var fbid=this.getCookie('facebookId');if(this.isSet(fbid)&&(fbid !='')){this.track('search',fbid);}}catch(err){this.error(err);}return true;};
	this.trackBcsSignPetition = function(){try{var fbid=this.getCookie('facebookId');if(this.isSet(fbid)&&(fbid !='')){this.track('sign-petition',fbid);}}catch(err){this.error(err);}return true;};
	this.trackBcsShareStory = function(){try{var fbid=this.getCookie('facebookId');if(this.isSet(fbid)&&(fbid !='')){this.track('share-story',fbid);}}catch(err){this.error(err);}return true;};
	this.trackBcsJoinNewsletter = function(){try{var fbid=this.getCookie('facebookId');if(this.isSet(fbid)&&(fbid !='')){this.track('join-newsletter',fbid);}}catch(err){this.error(err);}return true;};
	this.trackBcsLikeApp = function(){try{var fbid=this.getCookie('facebookId');if(this.isSet(fbid)&&(fbid !='')){this.track('new-recruit',fbid);}}catch(err){this.error(err);}return true;};
	this.trackBcsNewRecruit = function(){try{var fbid=this.getCookie('facebookId');if(this.isSet(fbid)&&(fbid !='')){this.track('like-app',fbid);}}catch(err){this.error(err);}return true;};
	this.track = function(action,facebookId){
		try{
			if(!(this.isSet(facebookId))){
				facebookId = this.getCookie('facebookId');
				if((!(this.isSet(facebookId)))||(facebookId == '')){return;}
			}
			var isSecure = false;
			if((this.isSet(window))&&(this.isSet(window.location))&&(this.isSet(window.location.protocol))&&(window.location.protocol=='https')){
				isSecure = true;
			}
			var host = this.FBAppHost;
			if(!(this.isSet(action))){return;}
			if(!(this.isSet(facebookId))){return;}
			var hasValidAction = false;
			if('donate' == action){hasValidAction=true;}
			else if('send-ecard' == action){hasValidAction=true;}
			else if('search' == action){hasValidAction=true;}
			else if('sign-petition' == action){hasValidAction=true;}
			else if('share-story' == action){hasValidAction=true;}
			else if('new-recruit' == action){hasValidAction=true;}
			else if('join-newsletter' == action){hasValidAction=true;}
			else if('like-app' == action){hasValidAction=true;}
			if(!hasValidAction){return;}
			var IEWIN = false;
			if(this.isSet(navigator)&&this.isSet(navigator.userAgent)&&this.isSet(navigator.userAgent.indexOf)&&(navigator.userAgent.indexOf('MSIE')>-1)){
				IEWIN = true;
			}
			var img = IEWIN ? new Image() : document.createElement('img');
    		if(this.isSet(img)&&this.isSet(document.body)){
				img.src = (isSecure ? "https" : "http") + '://' + host + '/clicks/img/' + action + '.gif?facebook_id=' + facebookId;
				img.width = '1';
				img.height = '1';
				document.body.appendChild(img);
			}
		} catch( err ){ this.error(err); }
	};
	this.setCookie = function Set_Cookie( name, value, expires, path, domain, secure ){
		try{
			if(!(this.isSet(name))){return;}
			if(!(this.isSet(value))){return;}
			if(!(this.isSet(domain))){
                if((this.isSet(window))&&(this.isSet(window.location))&&(this.isSet(window.location.host))){domain=window.location.host}
            }
			if(!(this.isSet(document))){return;}
			var today = new Date();
			today.setTime( today.getTime() );
			var expires_date = null;
			if ( expires ){
				expires = expires * 1000 * 60 * 60 * 24;
				expires_date = new Date( today.getTime() + (expires) );
			}
			document.cookie = name + "=" + encodeURI( value ) +
				( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
				( ( path ) ? ";path=" + path : "" ) +
				( ( domain ) ? ";domain=" + domain : "" ) +
				( ( secure ) ? ";secure" : "" );
		} catch( err ){ this.error(err); }
	};
	this.getCookie = function( check_name ) {
		if((!(this.isSet(check_name)))||(!(this.isSet(document)))||(!(this.isSet(document.cookie)))){return '';}
		try{
			var a_all_cookies = document.cookie.split( ';' );
			var a_temp_cookie = '';
			var cookie_name = '';
			var cookie_value = '';
			for ( var i = 0; i < a_all_cookies.length; i++ ){
				a_temp_cookie = a_all_cookies[i].split( '=' );
				cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
				if(cookie_name == check_name ){
					if( a_temp_cookie.length > 1 ){
						cookie_value = decodeURI( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
					}
					return (this.isSet(cookie_value) ? cookie_value : '');
				}
				a_temp_cookie = null;
				cookie_name = '';
				cookie_value = '';
			}
		} catch( err ){ this.error(err); }
		return '';
	};
}

var fbTracker = new FBAppTracker();




