
function AudioPlayerObject (config) {
	var self = this;
	
	//-- Initial pre-DOM function
	var init = function () {
		self.config = config;
		
		JQ(window).load(self.load);
		JQ(window).unload(self.unload);
		
		setInterval(self.intervalCheck, 15);

		self.popup = self.check();
		
		self.closed = false;
		self.triedToOpen = false;
	};

	//-- Initial post-DOM function
	var postInit = function () {
		self.initialClick = true;
		
		//-- Catch the first click 
		JQ('body').mouseup(function () {
			if (self.initialClick && self.playing) {
				self.initialClick = false;
				self.open();
			}
			return true;
		});
	};
	
	//-- Open the audio player popup window
	self.open = function () {
		if (!self.check()) {
			self.triedToOpen = true;
			EventBridge.dispatchEvent({type:'tryCloseEmbeddedConsole'});
			window.open(self.config.url+'?ll='+document.location.pathname.split('/')[4], self.config.title, 'width='+self.config.width+',height='+self.config.height);
		}
	};
	
	//-- Check to see if a popup blocker stopped the popup
	self.popupBlockerCheck = function () {
		
		// Popup blocker must be present if:
		//  - You tried to open the popup
		//  - The popup was never closed
		//  - The popup isn't currently present		
		
		if (self.triedToOpen && !self.check() && !self.closed) {
			tracking.click({
				pageName:	tracker_obj.prefix + '>audio_player>pop_up_failed',
				prop18:		'audio_player'
			});
		}
		
	};
	
	//-- Stores that the audio is currently playing
	self.start = function () {
		self.playing = true;
	};
	
	//-- Stores that the audio is currently NOT playing
	self.stop = function () {
		self.playing = false;
	};
	
	//-- Checks to see if the popup is open
	self.check = function () {
		return (JQ.cookie(self.config.title)=='open');
	};

	//-- Returns the number of clients open
	self.checkClientCount = function () {
		var cookieValue = JQ.cookie('NIKEOS_AUDIO_PLAYER_LOADED');
		
		(isNaN(cookieValue)) ? cookieValue = 0 : cookieValue = parseInt(cookieValue, 10);
		
		return cookieValue;
	};
	
	//-- Returns the current domain for cookies
	self.getDomain = function () {
		var domain = document.domain;
		switch (domain) {
			case 'cs.ny.rga.com':
			case 'nike-dev2.ny.rga.com':
				return 'rga.com';
				break;
			case 'inside-staging.nike.com':
			case 'env1-brand.nike.com':
			case 'inside.nike.com':
			case 'www.nike.com':
				return 'nike.com';
				break;
			default:
				return '';
				break;
		}
	};
	
	//-- Fires on page load
	self.load = function () {
		// Get the number of open clients
		var cookieValue = self.checkClientCount();
		
		// If there are open clients, add 1 to the count; otherwise, create a new cookie with a count of 1
		if (cookieValue > 0) {
			JQ.cookie('NIKEOS_AUDIO_PLAYER_LOADED', (cookieValue + 1), {path:'/',domain:self.getDomain()});
		} else {
			JQ.cookie('NIKEOS_AUDIO_PLAYER_LOADED', 1, {path:'/',domain:self.getDomain()});
		}
	};
	
	//-- Fires on page unload
	self.unload = function () {
		// Get the number of open clients
		var cookieValue = self.checkClientCount();
		
		// If the count is greater than 1, decrement it; otherwise, delete the cookie altogether
		if (cookieValue > 1) {
			JQ.cookie('NIKEOS_AUDIO_PLAYER_LOADED', (cookieValue - 1), {path:'/',domain:self.getDomain()});
		} else {
			JQ.cookie('NIKEOS_AUDIO_PLAYER_LOADED', null, {path:'/',domain:self.getDomain()});
		}
		
		// Check for popup blocker
		// self.popupBlockerCheck();
		
		EventBridge.dispatchEvent({type:'tryCloseEmbeddedConsole'});
	};
	
	//-- Checks to see if the popup window has closed
	self.intervalCheck = function () {
		
		// If self.check() [current popup status] is false and self.popup [last recorded popup status] is true, the window has recently been closed.
		if (!self.check() && self.popup) {
			self.popup = false;
			self.closed = true;
			EventBridge.dispatchEvent({type:'closeConsole'});
		} else {
			self.popup = self.check();
		}
		
	};
		
	//-- Call the initial functions
	init();
	JQ(postInit);
}

var audioPlayer = new AudioPlayerObject ({
	title:		'NIKEOS_AUDIO_PLAYER',
	height:		165,
	width:		415,
	url:		NIKEOS.BASE[NIKEOS.site_mode] + '/nikeos/global/modules/audio_player/popup_window.html'
});