jQuery(function() {

	_pm.pageInit();
	
	jQuery(".playMusic").click( function() {
		_pm.addTrackBM( this.id, true );
		return false;
	});
});

_pm = new PlayerManager();

function PlayerManager() {
	this.jsReady = false; // True after Javascript is init by body onload
	this.asReady = false; // True after Flash is ready
	
	this.pagePlayerId = "OoizitPlayer"; // Id of the Player in the main page
	this.popupPlayerId = "OoizitPlayerPopup"; // Id of the Popup Player in the spawned window
	this.popupWindowId = "OOiZiT_Player"; // Id of the spawned popup window
	
	this.popupWindow = null; // Pointer to the popup window
	this.popupWindowOptions = "width=270,height=425,top=0,left=0,location=0,menubar=0,resizable=0,status=0";
	
	this.popupIsReadyInterval = null;
	
	this.popupWindowURL = "/wp-content/themes/ooizit/swf/player/popup"; // URL of the popup window.
	
	this.bookmarkURL = {
		root: "/music-api-438u9gt489gt4hj89gt4?action=",
		getPlaylistBM: "bp_getPlaylist",
		addTrackBM: "bp_addTrack",
		newLocationPlaylistBM: "bp_newLocationPlaylist",
		newPlaylistBM: "bp_newPlaylist",
		newFeaturedPlaylistBM: "bp_newFeaturedPlaylist",
		newLatestPlaylistBM: "bp_newLatestPlaylist",
		newTop40PlaylistBM: "bp_newTop40Playlist",
		newRandomPlaylistBM: "bp_newRandomPlaylist"
	};
	
	this.flashId = [];
	
	this.pageInit = function () {
		this.jsReady = true;
		if(this.getMovie(this.popupPlayerId)) {
			this.popupInit();
			window.onbeforeunload = function () {
				if(_pm.checkMainWindowExists()) {
					// send to MAIN window
					window.$parent._pm.sendAction(_pm.pagePlayerId, "pause_click", "0");
				}
			}
		}

	};
	this.popupInit = function () {
		var self = this;
		window.setTimeout(function() { self.popupInit(); }, 500);
		try{
			window.$parent._pm.siteInit(window);
		}catch(exception){
		}
	};
	this.siteInit = function (popup) {
		this.popupWindow = popup;
	};
	this.isReady = function () {
		return this.jsReady;
	};
	this.flashReady = function () { // Called from Flash only!
		// Called when the flash has the BM playlist ready
		
		if(!this.asReady && this.getMovie(this.pagePlayerId)) {
			this.addCallWhenPopupReady("broadcastActionCallBack", ["get_track", "0"]);
		}
		
		this.asReady = true;
	};
	this.commsReady = function () {
		return (this.asReady && this.jsReady);
	};
	this.popupIsReady = function (self, callBackFunction, callBackArgObj) {
		if(self.popupWindow && self.popupWindow._pm && self.popupWindow._pm.commsReady()) {
			window.clearInterval(self.popupIsReadyInterval);
			self[callBackFunction](callBackArgObj);
		}
	};
	this.getMovie = function (id) {
 		if (navigator.appName.indexOf("Microsoft") != -1) {
 			return window[id];
 		} else {
 			return document[id];
 		}
	};
	this.checkMainWindowExists = function () {
		if(window.$parent && window.$parent.open && !window.$parent.closed && window.$parent._pm && window.$parent._pm.commsReady()) {
			return true;
		} else {
			return false;
		}
	};
	this.checkPopupWindowExists = function () {
		if(this.popupWindow && this.popupWindow.open && !this.popupWindow.closed && this.popupWindow._pm && this.popupWindow._pm.commsReady()) {
			return true;
		} else {
			return false;
		}
	};
	this.initPopupWindow = function (callBackFunction, callBackArgObj) {
		if(!this.checkPopupWindowExists()) {
			this.spawnPopupWindow();
			this.addCallWhenPopupReady(callBackFunction, callBackArgObj);
		}
	};
	this.addCallWhenPopupReady = function (callBackFunction, callBackArgObj) {
		window.clearInterval(this.popupIsReadyInterval);
		if(callBackFunction != null) {
			var self = this;
			this.popupIsReadyInterval = window.setInterval(function() { self.popupIsReady(self, callBackFunction, callBackArgObj); }, 100);
		}
	}
	this.spawnPopupWindow = function () {
		this.popupWindow = window.open(this.popupWindowURL, this.popupWindowId, this.popupWindowOptions);
		if(this.popupWindow) {
			this.popupWindow.$parent = window;
		}
	};
	this.executeAction = function (action, playlistIndex) {
		// Popup window --> Main page
		if(this.checkMainWindowExists()) {
			// send to MAIN window
			window.$parent._pm.sendAction(this.pagePlayerId, action, playlistIndex);
		}
	};
	this.broadcastAction = function (action, playlistIndex) {
		// Main page --> Popup window
		if(!this.checkPopupWindowExists()) {
			this.initPopupWindow("broadcastActionCallBack", arguments);
		} else {
			// send to POPUP window
			this.popupWindow._pm.sendAction(this.popupPlayerId, action, playlistIndex);
		}
	};
	this.broadcastActionCallBack = function (callBackArgObj) {
		var action = callBackArgObj[0];
		var playlistIndex = callBackArgObj[1];
		this.broadcastAction(action, playlistIndex);
	};
	this.sendAction = function (id, action, playlistIndex) {
		this.getMovie(id).fl_receiveAction(action, playlistIndex);
	};
	this.sendAddTrack = function (id, xml, play) {
		this.getMovie(id).fl_receiveAddTrack(xml, play);
	};
	this.sendNewPlaylist = function (id, xml, play) {
		this.getMovie(id).fl_receiveNewPlaylist(xml, play);
	};
	this.sendLoadProgress = function (id, ratio) {
		this.getMovie(id).fl_receiveLoadProgress(ratio);
	};
	this.sendPlayProgress = function (id, ratio) {
		this.getMovie(id).fl_receivePlayProgress(ratio);
	};
	this.sendCartRef = function (id, ref, add) {
		this.getMovie(id).fl_receiveCartRef(ref, add);
	};
	
	this.broadcastAddCart = function (ref, add, cTotal) {
		if(_pm.getMovie(_pm.pagePlayerId)) {
			// Main page --> Popup Window
			if(this.checkPopupWindowExists()) {
				_pm.popupWindow._pm.executeAddCart(ref, add, cTotal);
			}
		} else if (_pm.getMovie(_pm.popupPlayerId)) {
			// Popup Window --> Main page
			if(this.checkMainWindowExists()) {
				window.$parent._pm.executeAddCart(ref, add, cTotal);
			}
		}
		this.executeAddCart(ref, add, cTotal);
	};
	this.executeAddCart = function (ref, add, cTotal) {
		jQuery("#cartTotal").text((cTotal < 10) ? "0" + cTotal : cTotal);
		// Send to all flash on this page
		for(var i in this.flashId) {
			_pm.sendCartRef(this.flashId[i], ref, add);
		}
	};
	
	this.mainPage = function (url) {
		if(_pm.getMovie(_pm.pagePlayerId)) {
			window.location = url;
		} else if (_pm.getMovie(_pm.popupPlayerId)) {
			window.$parent.location = url;
		}
	};
	
	// Bookmark System

	this.getPlaylistBM = function () {
		// Main page <-- onPageOpen --> Popup window
		jQuery.post(this.bookmarkURL.root + this.bookmarkURL.getPlaylistBM, null, this.getPlaylistBMCallBack, "text");
	};
	this.getPlaylistBMCallBack = function (data) {
		// AJAX call back points to the _pm object
		
		if(_pm.getMovie(_pm.pagePlayerId)) {
			_pm.sendNewPlaylist(_pm.pagePlayerId, data);
		} else if (_pm.getMovie(_pm.popupPlayerId)) {
			_pm.sendNewPlaylist(_pm.popupPlayerId, data);
		}
	};
	
	
	this.addTrackBM = function (trackRef, play) { // Works on main page
		// SINGLE TRACK --> Bookmarks
		var self = this;
		jQuery.post(this.bookmarkURL.root + this.bookmarkURL.addTrackBM, {track: trackRef}, function(data) { self.addTrackBMCallBack(data, trackRef, play); }, "text");
	};
	this.addTrackBMCallBack = function (data, trackRef, play) {
		// AJAX call back points to the _pm object
		
		if(!_pm.checkPopupWindowExists()) {
			if(play) {
				// Start another call back to send the play action
				_pm.initPopupWindow("broadcastActionCallBack", ["add_track", trackRef]);
			} else {
				_pm.initPopupWindow();
			}
			_pm.sendAddTrack(_pm.pagePlayerId, data, play);// send to MAIN window
			//In this case, the new popup gets the playlist from getPlaylistBM
		} else {
			// send to MAIN and POPUP windows
			
			_pm.sendAddTrack(_pm.pagePlayerId, data, play);// send to MAIN window
			_pm.popupWindow._pm.sendAddTrack(_pm.popupPlayerId, data, play);
		}
	};

	this.newPlaylistBM = function (playlistRef, play) {
		// POPUP --> Bookmarks
		var self = this;
		jQuery.post(this.bookmarkURL.root + this.bookmarkURL.newPlaylistBM, {playlist: playlistRef}, function(data) { self.newPlaylistBMCallBack(data, playlistRef, play); }, "text");
	};
	this.newPlaylistBMCallBack = function (data, playlistRef, play) {
		// AJAX call back points to the _pm object
		// Enable calling and redirection from either window. Normal use is from the popup window.

		if(_pm.getMovie(_pm.pagePlayerId)) {
			_pm.sendNewPlaylist(_pm.pagePlayerId, data, play);
			if(!_pm.checkPopupWindowExists()) {
				if(play) {
					// Start another call back to send the play action
					_pm.initPopupWindow("broadcastActionCallBack", ["play_click", "0"]);
				} else {
					_pm.initPopupWindow();
				}
				//In this case, the new popup gets the playlist from getPlaylistBM
			} else {
				_pm.popupWindow._pm.sendNewPlaylist(_pm.popupPlayerId, data, play);
			}
		} else if (_pm.getMovie(_pm.popupPlayerId)) {
			if(_pm.checkMainWindowExists()) {
				window.$parent._pm.sendNewPlaylist(_pm.pagePlayerId, data, play);
			}
			_pm.sendNewPlaylist(_pm.popupPlayerId, data, play); // Update the popup last
		}
	};
	this.newLocationPlaylistBM = function (locationRef, play) {
		// POPUP --> Bookmarks
		var self = this;
		jQuery.post(this.bookmarkURL.root + this.bookmarkURL.newLocationPlaylistBM, {location: locationRef}, function(data) { self.newPlaylistBMCallBack(data, "", play); }, "text");
	};
	this.newFeaturedPlaylistBM = function (play) {
		// POPUP --> Bookmarks
		var self = this;
		jQuery.post(this.bookmarkURL.root + this.bookmarkURL.newFeaturedPlaylistBM, null, function(data) { self.newPlaylistBMCallBack(data, "", play); }, "text");
	};
	this.newLatestPlaylistBM = function (play) {
		// POPUP --> Bookmarks
		var self = this;
		jQuery.post(this.bookmarkURL.root + this.bookmarkURL.newLatestPlaylistBM, null, function(data) { self.newPlaylistBMCallBack(data, "", play); }, "text");
	};
	this.newTop40PlaylistBM = function (play) {
		// POPUP --> Bookmarks
		var self = this;
		jQuery.post(this.bookmarkURL.root + this.bookmarkURL.newTop40PlaylistBM, null, function(data) { self.newPlaylistBMCallBack(data, "", play); }, "text");
	};
	this.newRandomPlaylistBM = function (play) {
		// POPUP --> Bookmarks
		var self = this;
		jQuery.post(this.bookmarkURL.root + this.bookmarkURL.newRandomPlaylistBM, null, function(data) { self.newPlaylistBMCallBack(data, "", play); }, "text");
	};
	
	this.insertPlayer = function (src, id, w, h, bg, wmode, vars) {
		AC_FL_RunContent(
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
			'width', w,
			'height', h,
			'src', src,
			'quality', 'high',
			'pluginspage', 'http://www.adobe.com/go/getflashplayer',
			'align', 'middle',
			'play', 'false',
			'loop', 'false',
			'scale', 'showall',
			'wmode', wmode,
			'devicefont', 'false',
			'id', id,
			'bgcolor', bg,
			'name', id,
			'menu', 'false',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'movie', src,
			'salign', '',
			'flashvars', vars
		);
		this.flashId[this.flashId.length] = id;
	};
}
