
var mediaplayer = {
	
	Version: '2.0',
		
	MediaPlayer: {
		ID: 'mediaPlayer',
		CONTAINER_ID: 'mpWrap', // wrapper div around entire mediaplayer
		SWF_URL: 'http://static.thehothits.com/mcm_as3_mediaplayer_thehothits.swf',
		width: 630,
		height: 470,
		vars: {
			location: 'thehothits.com.au',
			allowAddToPlaylist: true,
			allowAdvertising: true,
			allowAdvertisingSkip: false,
			allowTrackPlayOnInitialize: true,
			allowPopOutPlayer: true,
			allowShareMedia: true,
			allowSponsorPreRoll: true,
			allowTrackQuickStart: true,
			allowTracking: true,
			artistId: 0,
			countdownToggle: null,
			hasArtistPanel: true,
			hasPlaylistPanel: true,
			hasInternalPlaylist: false,
			interviewId: 0,
			isPopOutPlayer: false,
			mediaType: null,
			playlistId: 0,
			propertyId: 0,
			refreshAdvertisingInterval: 0,
			startPosition: null,
			suggestedTrackListSize: 20,
			clientId: 0,
			loggerLevel: null,
			localConnectionIds: null,
			zone: null,
			trackingLocation: null,
			numberOneTrackDay: null,
			numberOneTrackMonth: null,
			numberOneTrackYear: null
		},
		embed: function () {
			return swfobject.embedSWF(this.SWF_URL+'?version='+mediaplayer.Version, this.ID, this.width, this.height, mediaplayer.CommonFlashSettings.min_flash_ver, mediaplayer.CommonFlashSettings.expressinstall_url, this.vars, { allowScriptAccess:mediaplayer.CommonFlashSettings.allowscriptaccess, allowFullScreen:true, wmode:mediaplayer.CommonFlashSettings.wmode, bgcolor:mediaplayer.CommonFlashSettings.bgcolor }, null);
		}
	},
	
	ArtistPanel: {
		ID: 'artistPanel',
		SWF_URL: 'http://static.thehothits.com/mcm_as3_mediaplayer_panel_artist_thehothits.swf',
		width: 630,
		height: 156,
		vars: {
			loggerLevel: null,
			allowAudio: true,
			allowArtistLinks: true,
			allowPlayArtistTracks: true,
			localConnectionIds: null
		},
		embed: function () {
			return swfobject.embedSWF(this.SWF_URL+'?version='+mediaplayer.Version, this.ID, this.width, this.height, mediaplayer.CommonFlashSettings.min_flash_ver, mediaplayer.CommonFlashSettings.expressinstall_url, this.vars, { allowScriptAccess:mediaplayer.CommonFlashSettings.allowscriptaccess, bgcolor:mediaplayer.CommonFlashSettings.bgcolor, wmode:mediaplayer.CommonFlashSettings.wmode }, null);
		}
	},
	
	PlaylistPanel: {
		ID: 'playlistPanel',
		SWF_URL: 'http://static.thehothits.com/mcm_as3_mediaplayer_panel_playlist_thehothits.swf',
		width: 310,
		height: 366,
		vars: {
			loggerLevel: null,
			localConnectionIds: null
		},
		embed: function () {
			return swfobject.embedSWF(this.SWF_URL+'?version='+mediaplayer.Version, this.ID, this.width, this.height, mediaplayer.CommonFlashSettings.min_flash_ver, mediaplayer.CommonFlashSettings.expressinstall_url, this.vars, { allowScriptAccess:mediaplayer.CommonFlashSettings.allowscriptaccess, bgcolor:mediaplayer.CommonFlashSettings.bgcolor }, null);
		}
	},
	
	CommonFlashSettings: {
		min_flash_ver: '9.0.0',
		expressinstall_url: '/flash/mediaplayer/expressInstall.swf',
		wmode: 'opaque',
		allowscriptaccess: 'always',
		bgcolor: '#000000'
	},
	
	Popup: {
		PATH: '/pop-media-player',
		width: 970,
		height: 646,
		x: 40,
		y: 40
	},
	
	Ads: {
		initial_src: '/flash/syn_island_start.html',
		
		Island: {
			ID: 'mediaAd',
			CONTAINER_ID: 'mpIsland',
			PATH: '/action/mediaplayer/island?zone=media_player',
			width: 300,
			height: 250
		},
		
		Leaderboard: {
			ID: 'mediaAdLeaderboard',
			CONTAINER_ID: 'leaderboard',
			PATH: '/action/mediaplayer/leaderboard?zone=media_player',
			width: 728,
			height: 90
		},
		
		setup: function (id, container_id, width, height) {
			var iframe = '<iframe src="' + this.initial_src + '" width="' + width + '" height="' + height + '" marginheight="0" marginwidth="0" frameborder="0" scrolling="no" name="' + id + '" id="' + id + '" style="background-color:' + mediaplayer.CommonFlashSettings.bgcolor + '"></iframe>';
			var container = document.getElementById(container_id);
			if (container) { container.innerHTML = iframe; }
		}
	},
	
	
	
	
	/**
		@ INITIALISE MEDIAPLAYER
			Tests for mediaplayerVariables defined in media_player.jsp, embeds player if found
	*/
	init: function () {
		// if mediaplayerVariables exists from media_player.jsp, set vars from those and embed
		if (typeof mediaplayerVariables != 'undefined') {
			this.setupVarsFromPage();
			this.embed();
		}
	},
	
	
	
	/**
		@ SETUP VARS FROM PAGE
			Processes and applies vars passed in from mediaplayerVariables in media_player.jsp
	*/
	setupVarsFromPage: function () {
		pagevars = mediaplayerVariables;
		
		// playlist
		var playlist = pagevars.playlist;
		if (_flashVarExists(playlist)) {
	 		playlist = playlist.split(".");
			this.MediaPlayer.vars.playlistId = playlist[0];
			this.MediaPlayer.vars.startPosition = playlist[1];
			this.MediaPlayer.vars.countdownToggle = playlist[2];
		}
		
		// individual
		var track = pagevars.track;
		if (_flashVarExists(track)) {
			track = track.split(".");
			this.MediaPlayer.vars.propertyId = track[0];
			this.MediaPlayer.vars.mediaType = track[1];
		}
		
		// interview
		var interview = pagevars.interview;
		if (_flashVarExists(interview)) {
			interview = interview.split(".");
			this.MediaPlayer.vars.interviewId = interview[0];
			this.MediaPlayer.vars.startPosition = interview[1];
		}
		
		// artist
		var artist = pagevars.artist;
		if (_flashVarExists(artist)) {
			artist = artist.split(".");
			this.MediaPlayer.vars.artistId = artist[0];
			this.MediaPlayer.vars.mediaType = artist[1];
		}
		
		// numberone
		var numberone = pagevars.numberone;
		if (_flashVarExists(numberone)) {
			numberone = numberone.split(".");
			this.MediaPlayer.vars.mediaType = numberone[0];
			this.MediaPlayer.vars.propertyId = numberone[1];
		}
		
		// numberone year
		var searchyear = pagevars.searchYear;
		var year = pagevars.year;
		this.MediaPlayer.vars.numberOneTrackYear = (!_flashVarExists(year) && _flashVarExists(searchyear)) ? searchyear : year;
		
		// numberone day, month
		this.MediaPlayer.vars.numberOneTrackDay = pagevars.day;
		this.MediaPlayer.vars.numberOneTrackMonth = pagevars.month;
		
		// other MediaPlayer vars
		this.MediaPlayer.vars.clientId = pagevars.clientid;
		this.MediaPlayer.vars.loggerLevel = pagevars.loggerLevel;
		this.MediaPlayer.vars.localConnectionIds = 'playlistPanel_' + pagevars.ranNumber + '|artistPanel_' + pagevars.ranNumber;
		this.MediaPlayer.vars.trackingLocation = pagevars.trackingLocation;
		this.MediaPlayer.vars.zone = pagevars.zone;

		// ArtistPanel
		this.ArtistPanel.vars.loggerLevel = pagevars.loggerLevel;
		this.ArtistPanel.vars.localConnectionIds = 'artistPanel_' + pagevars.ranNumber;
		
		// PlaylistPanel
		this.PlaylistPanel.vars.loggerLevel = pagevars.loggerLevel;
		this.PlaylistPanel.vars.localConnectionIds = 'playlistPanel_' + pagevars.ranNumber;
		
		function _flashVarExists (value) {
			return !(value == 'null' || value == null);
		}
	},
	
	
	
	/**
		@ EMBED MEDIAPLAYER
			Performs swfobject embeds of each Flash component and ads 
	*/
	embed: function () {
		var container = document.getElementById(this.MediaPlayer.CONTAINER_ID);
		container.style.visibility = 'visible';
		
		_setupTrackingVars();
		this.PlaylistPanel.embed();
		this.ArtistPanel.embed();
		this.MediaPlayer.embed();
		
		this.Ads.setup(this.Ads.Island.ID, this.Ads.Island.CONTAINER_ID, this.Ads.Island.width, this.Ads.Island.height);
		this.Ads.setup(this.Ads.Leaderboard.ID, this.Ads.Leaderboard.CONTAINER_ID, this.Ads.Leaderboard.width, this.Ads.Leaderboard.height);
		
		window.addEvents({ 'beforeunload': function () { mediaplayer.cleanUp(); } });
		
		// sets trackingLocation and zone from TrackingLocation var in main_layout.jsp
		function _setupTrackingVars () {
			if (typeof TrackingVars == 'undefined') { return; }
			mediaplayer.MediaPlayer.vars.zone = TrackingVars.zone;
			mediaplayer.MediaPlayer.vars.trackingLocation = TrackingVars.trackingLocation;
		}
	},
	
	
	
	/**
		@ REFRESH ADS
		@ infostring [String] param string generated from Flash to pass to ad request
			e.g. 'genreName0=Pop;artistid=3667;mediaid=86419;label=Universal;mediatype=video;propertyid=28784;clientid=0;'
			Refreshes the island and leaderboard iframe srcs with ad request paths
	*/
	refreshIslandBanner: function (infostring) {
 		var ord = Math.floor((Math.random()*13756)*345);
		
		var island = document.getElementById(mediaplayer.Ads.Island.ID);
	 	if (island) { island.src = mediaplayer.Ads.Island.PATH + '&ord=' + ord + '&keyvalues=' + infostring; }
	
		var leaderboard = document.getElementById(mediaplayer.Ads.Leaderboard.ID);
		if (leaderboard) { leaderboard.src = mediaplayer.Ads.Leaderboard.PATH + '&ord=' + ord + '&keyvalues=' + infostring; }
	},
	
	
	
	/**
		@ OPEN POPUP PLAYER
		@ propertyid [String] media param passed as string e.g. 't=12345.3'
			Run when user clicks 'Pop-out player' from inside the mediaplayer
	*/
	popMediaPlayerFlash: function (propertyid) {
		var url = this.Popup.PATH + '?' + propertyid;
		var specs = 'toolbar=0,menubar=0,resizable=0,titlebar=0,status=no,scrollbars=0,location=0,left=' + this.Popup.x + ',top=' + this.Popup.y + ',width=' + this.Popup.width + ',height=' + this.Popup.height;
		popup = window.open(url, 'mediaPlayer', specs);
		popup.focus();
	},
	
	
	
	/**
		@ CLEAN UP
			Removes Flash elements and cleans up the mediaplayer area as the user leaves the page.
			This function solves the 'mootools (1.11) + EI on mediaplayer + IE7' problem.
			Fires on 'beforeunload' -- just before the browser leaves the window (via refresh or link).

			We do this to prevent problems caused by conflicts with mootools' garbage collector, Flash's
			unloading of externalinterface callbacks from the mediaplayer and IE7.
			
			Here are some URLs that help explain this phenomenon:
				http://bugs.adobe.com/jira/browse/FP-529
				http://flowplayer.org/forum/8/10632
				http://www.fusioncharts.com/forum/Topic1785-23-1.aspx
				http://swfupload.org/forum/generaldiscussion/809		
	*/
	cleanUp: function () {
		// remove flash <object>'s
		var els = document.getElementsByTagName('object');
		if (els && els.length) {
			for (var i=els.length-1; 0<=i; i--) {
				var parent = els[i].parentNode;
				parent.removeChild(els[i]);
			}
		}
		
		// remove wrapper, and set styles to parent to prevent visual jumble
		var wrapper = document.getElementById(this.MediaPlayer.CONTAINER_ID);
		if (wrapper) {
			var parent = wrapper.parentNode;
			parent.style.height = '626px';
			parent.style.background = '#000000';
			parent.removeChild(wrapper);
		}
	}
	
};



window.addEvents({ 'domready': function () { mediaplayer.init(); } });