function rewards() {
	this.contentType = arguments[0];
	this.show = arguments[1];
	this.content = arguments[2];
	this.cms = arguments[3];

	this.auth();
	var self=this;
	var actions = new Array(); // This is where we are adding odd Jquery binding requests for rewards like comments, or URL's
	jqN('#comment_submit_button').live('click', function() {
		actions.push('comment_make');
		if (self.show) {
			actions.push('comment_make_' + self.show);
		}
		if (content) {
			actions.push('comment_make_' + self.show + '_' + self.content);
		}
		rewards.logAction(actions);
	});
	jqN('.share_profile').live('click', function() { rewards.shareProfile(); });


	


	nitroLibsVersion = "4.1.0";
	var connectionParams = {};
	connectionParams.apiKey = "b243ff316f1b441f92acaa1e236639f1";
	connectionParams.server = "http://syfyrewards.nitro.bunchball.net/nitro/json";
	connectionParams.userId = encodeURIComponent(socialConnect.syfyuid);
	connectionParams.timeStamp = this.timestamp;
	connectionParams.signature = this.sessionKey;
	var nitro = new Nitro(connectionParams);
	if(this.show == 'ghosthunters')
		nitro.callAPI('method=user.getLevel', 'ghostInit');
	
	nitro.showPendingNotifications();
	if (jqN('nitro:widget')) {
		nitro.refreshNML();
	}
	jqN("a[href*='rewards=syfy']").live('click', function() {
		url = String(this);
		r = url.split('return=');
		if (r) re = r[r.length - 1].split('&');
		pieces = url.split('action=');
		pie = pieces[pieces.length - 1].split('&');
		actions.push(pie[0]);
		rewards.logAction(actions);
		if (re[0] == 'false'){
			return false;
		}else{
			return true;
		}
	}); // End the oddness
	
	
	
	
jqN(".play_link").bind('click', function() {
	path = this.href.split('/');
	show = path[4];
	content = path[path.length - 1];
	var actions = [];
	actions.push('shortform_video_view');
	if (show) {
		actions.push('shortform_video_view_' + show);
	}
	if (content) {
		actions.push('shortform_video_view_' + show + '_' + content.replace("v", ""));
	}
	rewards.logAction(actions);
});

function onOutletEvent(e) {
	var player = Outlet.getOutletExtension("embeddedPlayer");
	if (e.type == "outletInited") {
		var actions = [];
		path = window.location.pathname.split('/');
		if (window.location.host == 'video.syfy.com') {
			show = path[2];
			content = videoID;
			actions.push('shortform_video_view');
			if (show) {
				actions.push('shortform_video_view_' + show);
			}
			if (content) {
				actions.push('shortform_video_view_' + show + '_' + content);
			}
		}

		player.addEventListener("Player.start", rewards.logAction(actions));
	}
}


	
}

rewards.prototype.auth = function() {
	var self;
	self = this;
	if (!jqN.cookie('syfyuid')) {
		return;
	}
	if (!jqN.cookie('sessionKey')) { 
		//console.log('no token userid --' + socialConnect.syfyuid);
		url = 'http://api.syfy.com/bunchball/' + encodeURIComponent(socialConnect.syfyuid) + '/load';
		
		//console.log(url);
		jqN.ajax({
			url: url,
			dataType: 'jsonp',
			data: {
				firstName: socialConnect.nickname,
				lastName: socialConnect.photoURL
			},
			jsonp: 'callback',
			jsonpCallback: 'jsonpCallback',
			success: function(data) {
				jqN.cookie('sessionKey', data.Login.sessionKey, {
					path: '/',
					domain: ".syfy.com",
					secure: false
				});
				jqN.cookie('timestamp', data.timestamp, {
					path: '/',
					domain: ".syfy.com",
					secure: false
				});
				self.timestamp = data.timestamp;
				//console.log(self.timestamp);
				self.sessionKey = data.Login.sessionKey;
				//console.log('got new--' + self.sessionKey);
				self.logAction();
				if(jqN.cookie('syfyuid').search("anon_") == -1){
					rewards.actionSubmit([ 'register_'+self.show]);
				}
			},
			error: function(jqXHR, status, error) {
				//console.log(error);
			}
		});
	} else {
		if (jqN.cookie('sessionKey')) {
			self.sessionKey = jqN.cookie('sessionKey');
			self.timestamp = jqN.cookie('timestamp');
			//console.log('we got token--' + socialConnect.syfyuid + '--' + jqN.cookie('sessionKey'));
			this.logAction();
		} else {
			//console.log('something went very wrong lets retry');
			gigya.services.socialize.getUserInfo(conf, {
				callback: socialConnect.checkLoggedIn
			});
		}
	}
}
rewards.prototype.logAction = function(actions) { //Need to figure out where we are at.  Assuming this will get pretty long. 
	var content;
	path = window.location.pathname.split('/');
	if (typeof actions === 'undefined') {
		var actions = []; //This section is only for the CMS
		if (cms == 'publisher') {
			if (!this.contentType) contentType = path[1];
			else contentType = this.contentType;
			if (!this.show) show = path[0];
			else show = this.show;
			if (this.content) content = '_' + this.content;
			else content = '';
			if (contentType == 'cast' || contentType == 'team') contentType = 'bio'; // Create the actions
			if (show) {
				actions.push(show + '_visit');
				show = '_' + show;
			} else {
				show = '';
			}
			if (contentType) {
				actions.push(contentType + '_view' + show);
			}
			if (content && (this.content != this.contentType && this.content != this.show)) {
				actions.push(contentType + '_view' + show + content);
			}
		} else if (cms == 'rewind') {
			actions.push('longform_view');
			if (this.show) {
				actions.push('longform_view_' + show);
				if (this.content) {
					actions.push('longform_view_' + show + '_' + this.content);
				}
			}
		}else if (cms == 'video') {
			actions.push('shortform_video_view');
			content = path[path.length - 1];

			if (show) {
				actions.push('shortform_video_view_' + show);
			}
			if (content) {
				actions.push('shortform_video_view_' + show + '_' + content.replace("v", ""));
			}
		}
	}
	if (actions) {
		this.actionSubmit(actions);
	}
}
rewards.prototype.actionSubmit = function(actions) {
	if (actions.length > 0) {
		var action;
		jqN.each(actions, function(key, val) {
			if (val) {
				if (key == 0 && val) action += val;
				else action += ',' + val;
			}
		});
		url = 'http://api.syfy.com/bunchball/' + this.sessionKey + '/execute/' + actions;
		//console.log(url);
	    if( (window['console'] !== undefined) ){
			console.log(actions);
	   }

		jqN.ajax({
			url: 'http://api.syfy.com/bunchball/' + this.sessionKey + '/execute/' + actions,
			dataType: 'jsonp',
			data: {
				uid: socialConnect.syfyuid
			},
			jsonp: 'callback',
			jsonpCallback: 'jsonpCallback',
			success: function(data) {
				//console.log(data);
			},
			error: function(jqXHR, status, error) {
				//console.log(error);
			}
		});
	}
}
rewards.prototype.shareProfile = function(){
    // Constructing a UserAction Object  
    var act = new gigya.services.socialize.UserAction();  
      
    // Setting the User Message  
     
    // Setting the Title  
    act.setTitle("Check out my Team Ghost Profile!");  
      
    // Adding a Link Back  
    act.setLinkBack(document.URL);  
      
    // Setting the Description  
    act.setDescription("I have been hunting ghosts, watching exclusive behind the scenes Ghost Hunters clips and hair raising full episodes! Oh and I've also been earning points, badges and prizes!");  
      
    // Adding a Media (image)  
    act.addMediaItem( {   
        type: 'image',      // Type of the media (image/flash/mp3)  
        src: 'http://www.syfy.com/home/images/ghaffinity/ui/facebook_icon.jpg',   // URL to the image source  
        href: document.URL    // URL to redirect the user when he clicks the image  
     });  
      
    // Activate the Share Plugin  
    gigya.services.socialize.showShareUI(conf, {  userAction:act });  
}
var rewards;

function rewards_init() {

	if (typeof path === 'undefined') {
		path = window.location.pathname.split('/');
		if (path[1] == 'rewind') {
			show = path[2];
			ct = 'rewind';
			path = path[3];
			cms = 'rewind';
		}

         if (window.location.host == 'video.syfy.com') {
               show = path[2];
               ct = videoID;
               cms = 'video';
		}

	} else {
		path = path.split('/');
		path = path[path.length - 1];
	}
	rewards = new rewards(ct, show, path, cms);
}

function ghostInit(data) {
	var points = parseInt(data.Nitro.users.User.SiteLevel.points);

	var ghosts = [
		'/_images/ghosthunters/ghost_1.png',
		'/_images/ghosthunters/ghost_2.png',
		'/_images/ghosthunters/ghost_3.png'
	]

	var chance = 0;
	if (points < 25) {
		chance = 10;
	} else if (points < 75) {
		chance = 15;
	} else if (points < 100) {
		chance = 20;
	} else {
		chance = 9999;
	}

	if (window.location.search == '?ghost') {
		chance = 1;		
	}
	var fadeInterval = 3000;

	if (Math.floor(Math.random() * (chance - 1 + 1)) + 1 == 1) {
		//console.log('spawning ghost');
		var ghost = jqN('<img />');
		ghost.attr('src', ghosts[(Math.floor(Math.random()*3+1))-1]);
		ghost.css('position', 'fixed');
		ghost.css('bottom', '0px');
		ghost.css('right', '0px');
		ghost.css('z-index', '9990');
		ghost.click(function() {
			// fire off nitro action here??
			rewards.actionSubmit([ 'ghosts_find' ]);
			ghost.fadeOut('slow', function(e) { jqN(this).remove(); })
			
		});
		ghost.appendTo('body');
		//var barf = window.setInterval(function() { ghost.fadeTo(3000, Math.random()); }, 3000);
	}

}

var parse_url = function(uri) {
	if (typeof uri == 'undefined') {
		uri = location.href;
	} else if (uri[0] == '/') {
		uri = location.host + uri;
	}
	var url = uri.match(/^([^:]*:\/\/)?([^:]*:[^@]*@)?([^\/:]*\.[^\/:]*)?(:[^\/]*)?(\/[^?#]*)?(\?[^#]*)?(#.*)?$/i);
	delete url.input;
	url.protocol = ((url[1]) ? url[1] : 'http://').split('://')[0];
	url.user = (url[2]) ? url[2].split(':')[0] : undefined;
	url.password = (url[2]) ? url[2].split(':')[1].split('@')[0] : undefined;
	url.host = (url[3]) ? url[3] : location.host;
	url.hostname = url.host;
	url.port = (url[4]) ? ((isNaN(parseInt(url[4].split(':')[1]))) ? 80 : parseInt(url[4].split(':')[1])) : 80;
	url.path = (url[5]) ? url[5] : '/';
	url.pathname = url.path;
	url.search = (url[6]) ? url[6].split('?')[1] : undefined;
	url.query = url.search;
	url.fragment = (url[7]) ? url[7].split('#')[1] : undefined;
	url.hash = url.fragment;
	url.href = '' + url.protocol + '://' + ((url.user) ? url.user + ':' + url.password + '@' : '') + url.host + ((url.port != 80) ? ':' + url.port : '') + url.path + ((url.search) ? '?' + url.search : '') + ((url.fragment) ? '#' + url.fragment : '');
	return url;
} // /parse_url()

