FootballPlayerWidget = Class.create();
FootballPlayerWidget.prototype = {
	
	initialize: function(uid) {
	
		this.uid = uid;
		this.numberOfSkins = 11;
		
		this.feedUrl = "/resource/application/service/footballplayer.php?Action=GetFeedItems&Uid=" + uid;
		this.comingUpUrl = "http://www.bbc.co.uk/5live/sport/commentaries/";
		this.listenLiveUrl = "http://www.bbc.co.uk/iplayer/playlive/bbc_radio_five_live/";
				 
		this.listenLiveConsole = { 
			url: "http://www.bbc.co.uk/iplayer/console/",
			width: 512,
			height: 270
		};
			
		this.currentFeedItem = -1;	
		this.tickerItems = [];
		this.feedItems = [];
		this.comingUpItems = [];
		
		this.initializeInterface();
		this.initializeComingUp();
		this.initializeTicker();
		this.initializeFeedItems();
		this.initializeSetting();
		
		this.cycleTicker();
		//this.updateFeed();		
		
		// Tempoary Updater
		new PeriodicalExecuter(this.refreshAll.bind(this), 4800);
	},
	
	initializeInterface: function() {
		
		// Default visual states
		this.expanded = true;
		this.settingsVisible = false;
		this.sharePanelExpanded = false;
		this.helpPanelExpanded = false;
		this.settingsPanelExpanded = false;
		this.feedbackPanelExpanded = false;
		
		// Setup access to interface buttons
		this.playNextButton = $("play-next");
		
		if ($A($("settings").getElementsBySelector("a")).length > 0) {
			this.settingsButton = $A($("settings").getElementsBySelector("a")).first()
			this.settingsButton.onclick = this.onSettingsClick.bindAsEventListener(this);
			this.settingsVisible = true;
		}
		
		this.feedbackButton = $A($("feedback").getElementsBySelector("a")).first();		
		this.feedbackCloseButton = $("feedback-close-button");
		this.feedbackSendButton = $("feedback-send-button");
		this.feedbackForm =	$A($("feedback-form").getElementsBySelector("form")).first();
		new RumbleUI.Form.AutoClear("comment", "Enter your comment here.");
		
		this.helpButton = $A($("help").getElementsBySelector("a")).first();
		this.helpCloseButton = $("help-close-button");
		this.empCloseButton = $("emp-close-button");
		this.shareButton = $A($("share").getElementsBySelector("a")).first();		
		this.shareCloseButton = $("share-close-button");
		
		this.embedPanel = $("embed");
		
		this.sendEmailForm = $("send-email-form");
		this.settingsForm = $("settings-form");
		
		if (this.emailPanel = $("email-friends")) {
			this.emailButton = $A($("email-button").getElementsBySelector("a")).first();
			this.emailCloseButton = $("send-close-button");
			this.emailSendButton = $("send-button");
			this.emailButton.onclick = this.onEmailClick.bind(this);
			this.emailCloseButton.onclick = this.onEmailCloseClick.bind(this);
			this.emailSendButton.onclick = this.onEmailSendClick.bind(this);
		}
		
		this.mobilePanel = $("mobile-panel");
		this.mobileButton = $("mobile-button");
		this.mobileButton.onclick = this.onMobileClick.bind(this);
		this.mobileCloseButton = $("mobile-close-button");
		this.mobileCloseButton.onclick = this.onMobileCloseButtonClick.bind(this);
		
		this.embedButton = $A($("embed-button").getElementsBySelector("a")).first();
		this.embedCloseButton = $("embed-close-button");
		this.embedCopyButton = $("copy-button");
		$("embed-show-fixed-settings-button").onclick = this.onShowFixedSettingsClick.bind(this);
				
		// Link events
		this.playNextButton.onclick = this.onPlayNextClick.bind(this);
		
		
		this.shareButton.onclick = this.onShareClick.bindAsEventListener(this);
		this.shareCloseButton.onclick = this.onShareCloseClick.bind(this);
		this.helpButton.onclick = this.onHelpClick.bind(this);
		this.helpCloseButton.onclick = this.onHelpCloseClick.bind(this);

		$$(".feedback").each((function(element) {
			element.onclick = this.onFeedbackClick.bind(this);	
		}).bind(this));		
		
		this.feedbackCloseButton.onclick = this.onFeedbackCloseClick.bind(this);
		this.feedbackSendButton.onclick = this.onFeedbackSendClick.bind(this);
		this.empCloseButton.onclick = this.onEmpCloseClick.bind(this);
		
		this.embedButton.onclick = this.onEmbedClick.bind(this);
		this.embedCloseButton.onclick = this.onEmbedCloseClick.bind(this);
		
		// Setup access to interface elements
		this.tickerItemsElement = $("tickeritems");
		this.feedItemsElement = $("feeditems");
		this.settingsElement = $("settings-panel");
		this.empPanelElement = $("emp-panel");
		this.empElement = $("emp");
		this.comingUpPanel = $("comingup-items");
	},
	
	/** 
	 * Global Widget Functions
	 */
	showWidget: function() {
		new Effect.BlindDown($("main-content"), { duration: 0.5 });	
	},

	hideWidget: function() {
		this.hideEmpPanel();
		new Effect.BlindUp($("main-content"), { duration: 0.5 });
	},
	
	toggleWidget: function() {
		if (this.expanded) {			
			this.hideWidget();
			this.expanded = false;			
		} else {
			this.showWidget();
			this.expanded = true;			
		}
	},
	
	/**
	 * Settings
	 */
	initializeSetting: function() {
		if (this.settingsVisible) {
			$A(this.settingsElement.getElementsByClassName("filter-panel")).each((function(element) {
				var filterFieldsetElement = $A(element.getElementsBySelector("fieldset")).first();
				filterFieldsetElement.hide();
				$A(element.getElementsBySelector("h4")).first().onclick = this.onFilterHeaderClick.bind(this, filterFieldsetElement);
				
			}).bind(this));
			
			new RumbleUI.Carrousel("skins", "previous-skins", "more-skins", { fadeWrap: true, direction: RumbleUI.Carrousel.Horizontal } );
			new RumbleUI.Selector("Skin", "skin-choice", null, { useId: true, type: RumbleUI.Selector.Type.Radio, onSelect: this.onSelectSkin.bind(this) } );
			
			$("settings-save-button").onclick = this.onSettingsSaveClick.bind(this);
			$("settings-cancel-button").onclick = this.onSettingsCancelClick.bind(this);
		}
	},
	
	showSettingsPanel: function() {
		if (this.settingsVisible) {
			this.hideEmpPanel();
			$("all-settings").style.overflow = "hidden";
			new Effect.BlindDown($("settings-panel"), { duration: 0.5, afterFinish: function() {
				$("all-settings").style.overflow = "auto";
			}});		
			this.settingsPanelExpanded = true;
		}
	},

	hideSettingsPanel: function() {
		if (this.settingsVisible) {
			$("all-settings").style.overflow = "hidden";
			new Effect.BlindUp($("settings-panel"), { duration: 0.5 });
				
			this.settingsPanelExpanded = false;
		}
	},

	toggleSettingsPanel: function() {
		if (this.settingsPanelExpanded) {
			this.hideSettingsPanel();
		} else {
			this.showSettingsPanel();
		}
	},
	
	loadSettings: function() {
	},
	
	saveSettings: function() {
		this.settingsForm.request({
			parameters: { ContentType: "text/json" }, 
		  onSuccess: (function(transport) {
		  	this.refreshAll();
		  }).bind(this)
		});
	},
	
	previewSkin: function(value) {
		for (var i = 0; i < this.numberOfSkins + 1; i++) {
			$("container").removeClassName("skin-" + i);
		}
		$("container").addClassName("skin-" + value);
	},	
	
	/**
	 * Share
	 */
	showSharePanel: function() {
		this.hideEmpPanel();
		this.hideSettingsPanel();
		this.hideHelpPanel();		
		this.hideFeedbackPanel();
		new Effect.BlindDown($("share-panel"), { duration: 0.5 });
		this.sharePanelExpanded = true;
	},

	hideSharePanel: function() {
		new Effect.BlindUp($("share-panel"), { duration: 0.5 });
		this.hideEmbedPanel();
		this.hideEmailPanel();
		this.sharePanelExpanded = false;			
	},

	hideEmailPanel: function() {
		if (this.emailPanel) {
			Effect.Fade(this.emailPanel, { duration: 0.3 });
			
		}
	},

	showEmailPanel: function() {
		if (this.emailPanel) {
			Effect.Appear(this.emailPanel, { duration: 0.3 });
			this.sendEmailForm.reset();
		}
	},
	
	hideEmbedPanel: function() {
		if (this.embedPanel) {
			Effect.Fade(this.embedPanel, { duration: 0.3 });
		}
	},
	
	hideMobilePanel: function() {
		if (this.mobilePanel) {
			Effect.Fade(this.mobilePanel, { duration: 0.3 });
		}
	},
		
	toggleSharePanel: function() {
		if ($("share-panel").visible()) {
			this.hideSharePanel();
		} else {
			this.showSharePanel();
		}
	},
	
	sendEmail: function() {
		this.hideSharePanel();
		this.sendEmailForm.request({
			parameters: { ContentType: "text/json" }, 
		  onSuccess: (function(transport) {
		  	
		  }).bind(this)
		});
	},
	
	/**
	 * Help
	 */
	showHelpPanel: function() {
		this.hideEmpPanel();
		this.hideSharePanel();
		this.hideFeedbackPanel();
		this.hideSettingsPanel();		
		new Effect.BlindDown($("help-panel"), { duration: 0.5 });
		this.helpPanelExpanded = true;
	},

	hideHelpPanel: function() {
		new Effect.BlindUp($("help-panel"), { duration: 0.5 });
		this.helpPanelExpanded = false;			
	},
		
	toggleHelpPanel: function() {
		if ($("help-panel").visible()) {
			this.hideHelpPanel();			
		} else {
			this.showHelpPanel();			
		}
	},
	
	
	/**
	 * Feedback
	 */
	showFeedbackPanel: function() {
		this.hideEmpPanel();
		this.hideSharePanel();
		this.hideHelpPanel();
		this.hideSettingsPanel();		
		this.feedbackForm.reset();
		new Effect.BlindDown($("feedback-panel"), { duration: 0.5 });
		this.feedbackPanelExpanded = true;
	},

	hideFeedbackPanel: function() {
		new Effect.BlindUp($("feedback-panel"), { duration: 0.5 });
		this.feedbackPanelExpanded = false;			
	},
		
	toggleFeedbackPanel: function() {
		if ($("feedback-panel").visible()) {
			this.hideFeedbackPanel();			
		} else {
			this.showFeedbackPanel();			
		}
	},
	
	sendFeedback: function() {
		this.hideFeedbackPanel();
		this.feedbackForm.request({
			parameters: { ContentType: "text/json" }, 
		  onSuccess: (function(transport) {
		  }).bind(this)
		});
	},
	
	
	/**
	 *  Coming Up/Listen Live Functions
	 */
	initializeComingUp: function() {
		this.comingUpPanel.innerHTML = "";
	},
	
	addComingUp: function(comingUpItem) {
			
		// Remove temp item when real items are added
		if ((this.comingUpItems.length == 1) && (this.comingUpItems[0].id == 0)) {
			this.comingUpItems[0].remove = true;
		}
		
		this.comingUpItems.push(comingUpItem);
	
		var element = document.createElement("div");
		var dateSpan = document.createElement("span");
		var contentSpan = document.createElement("span");
		Element.extend(element);			
		Element.extend(dateSpan);
		Element.extend(contentSpan);
		
		if (comingUpItem.StartTime.formatted != "") {
			element.appendChild(dateSpan);
		}
		
		element.appendChild(contentSpan);
		element.id = "comingup-item-" + comingUpItem.Id.data;
		if (this.comingUpItems.length > 1) {
			element.style.display = "none";				
		} else {
			this.setLiveLabel(comingUpItem.live);
		}
		
		element.className = "comingup-item";
		if (comingUpItem.live) {
			element.addClassName("live");
		}		
		
		dateSpan.className = "date";
		contentSpan.className = "content";
		dateSpan.innerHTML = comingUpItem.StartTime.formatted;
		contentSpan.innerHTML = comingUpItem.Title.formatted;
		comingUpItem.element = element;
		this.comingUpPanel.appendChild(element);
		
		comingUpItem.remove = false;
		comingUpItem.element = element;
		this.setLiveLabel(this.comingUpItems[0].live);
	},
	
	removeComingUp: function(comingUpItem) {
		this.comingUpItems.splice(this.comingUpItems.indexOf(comingUpItem), 1);	
		comingUpItem.element.remove();		
	},	
	
	refreshComingUp: function() {			
		//this.comingUpElements.each(Element.hide);
	},
	
	hasComingUpItemExpired: function(comingUpItem) {
	
		var startTime = new Date();
		var endTime = new Date();
		var now = new Date();
		
		utcTimestamp = now.getTime() + 60000 * now.getTimezoneOffset();
		
		startTime.setFromTimestamp(comingUpItem.StartTime.data);	
		endTime.setFromTimestamp(comingUpItem.EndTime.data);
		
		if (((comingUpItem.StartTime.data == "") || (utcTimestamp >= startTime.getTime())) && ((comingUpItem.EndTime.data == "") || (utcTimestamp <= endTime.getTime()))) {
			comingUpItem.live = true;
			comingUpItem.element.addClassName("live");
		} else {
			comingUpItem.live = false;
			comingUpItem.element.removeClassName("live");
		}
				
		if ((comingUpItem.EndTime.data) && (utcTimestamp > endTime.getTime())) {

			comingUpItem.remove = true;
		}
		
		if (comingUpItem.remove) {
			this.removeComingUp(comingUpItem);
			return true;
		}
		return false;
	},
	
	getNextComingUp: function() {
		var index = 0;
	
		if (this.comingUpCurrentItem != undefined) {
			index = this.comingUpItems.indexOf(this.comingUpCurrentItem) + 1;
			if (index >= this.comingUpItems.length) {
				index = 0;
			}
		}
		if (this.comingUpItems.length == 0) {
			this.addComingUp({"Id":{"formatted":"0","data":"0"},"Title":{"formatted":"Listen to 5 Live Now","data":"Listen to 5 Live Now"},"Description":{"formatted":"","data":""},"StartTime":{"formatted":"","data":""},"EndTime":{"formatted":"","data":""},"Link":{"formatted":"","data":""},"PresenterLink":{"formatted":"","data":""},"ProgrammeLink":{"formatted":"","data":""},"ListenAgainLink":{"formatted":"","data":""},"ListenLiveLink":{"formatted":"http:\/\/www.bbc.co.uk\/iplayer\/console\/fivelive","data":"http:\/\/www.bbc.co.uk\/iplayer\/console\/fivelive"},"Tags":{"formatted":"Schedule\/Coming Soon","data":"Schedule\/Coming Soon\n"},"DateCreated":{"formatted":"20 Aug - 23:26","data":"2008-08-20 22:26:01.531"},"live":true});
		}
		return this.comingUpCurrentItem = this.comingUpItems[index]; 
	},
	
	
	cycleComingUp: function() {		
		if (this.comingUpTimer == undefined) {
			this.showComingUpNext();
		} else {
			this.comingUpTimer.stop();
			this.scrollOut(this.comingUpCurrentItem.element, this.showComingUpNext.bind(this));
		}
	},
	
	showComingUpNext: function() {
		var index = 0;
	
		if (this.comingUpCurrentItem != undefined) {
			this.comingUpCurrentItem.element.hide();	
			this.comingUpCurrentItem.element.style.left = "0px";
			index = this.comingUpItems.indexOf(this.comingUpCurrentItem) + 1;
			if (index >= this.comingUpItems.length) {
				index = 0;
			}
		}
		
		while (this.hasComingUpItemExpired(this.comingUpItems[index]));
		
		if (this.comingUpItems.length == 0) {
			this.addComingUp({"Id":{"formatted":"0","data":"0"},"Title":{"formatted":"Listen to 5 Live Now","data":"Listen to 5 Live Now"},"Description":{"formatted":"","data":""},"StartTime":{"formatted":"","data":""},"EndTime":{"formatted":"","data":""},"Link":{"formatted":"","data":""},"PresenterLink":{"formatted":"","data":""},"ProgrammeLink":{"formatted":"","data":""},"ListenAgainLink":{"formatted":"","data":""},"ListenLiveLink":{"formatted":"http:\/\/www.bbc.co.uk\/iplayer\/console\/fivelive","data":"http:\/\/www.bbc.co.uk\/iplayer\/console\/fivelive"},"Tags":{"formatted":"Schedule\/Coming Soon","data":"Schedule\/Coming Soon\n"},"DateCreated":{"formatted":"20 Aug - 23:26","data":"2008-08-20 22:26:01.531"},"live":true});
		}
		
		this.setLiveLabel(this.comingUpItems[index].live);
		this.comingUpItems[index].element.show();
		
		this.comingUpCurrentItem = this.comingUpItems[index]; 
		if (this.comingUpCurrentItem.live) {				
			this.comingUpTimer = new PeriodicalExecuter(this.cycleComingUp.bind(this), 90);
		} else {
			this.comingUpTimer = new PeriodicalExecuter(this.cycleComingUp.bind(this), 20);
		}
		
		return this.comingUpCurrentItem;
	},
	
	scrollOut: function(element, afterFinish) { 	
		element = $(element); 
		new Effect.Morph(element, { style: "left: " + -(element.getWidth() + 25) + "px", duration: 3, afterFinish: afterFinish.bind(this), transition: Effect.Transitions.linear });
	},
	
	scrollOutComingUp: function(comingUpItem) {
		this.scrollingTransition = true;
		this.scrollOut(comingUpItem.element, (function(element) {
			this.scrollingTransition = false;
			this.cycleComingUp();			
		}).bind(this));
	},
	
	setLiveLabel: function(live) {
		if (live) {
	 		$("comingup-label").innerHTML = "Listen live";
	 		$("comingup-label").href = this.listenLiveUrl;
	 		$("comingup-label").onclick = this.openEmpConsole.bind(this);
	 	} else {
	 		$("comingup-label").innerHTML = "Coming up";
	 		$("comingup-label").href = this.comingUpUrl;
	 		$("comingup-label").onclick = null;
	 	}
	},
	
	openEmpConsole: function(event, episode) {
		if (episode == undefined) {
			episode = "fivelive";
		}
		
		url = "";
		if ((this.comingUpCurrentItem.ListenLiveLink) && (this.comingUpCurrentItem.ListenLiveLink.data)) {
			url = this.comingUpCurrentItem.ListenLiveLink.data;
		} else {
			url = this.listenLiveConsole.url + episode;
		}
						
		window.open(url, "5live", "width=" + this.listenLiveConsole.width + "px, height=" + this.listenLiveConsole.height + "px");
		return false;
	},

	/**
	 * Ticker Functions 
	 */	
	initializeTicker: function() {
		this.tickerItemsElement.innerHTML = "";
	},
	
	refreshTicker: function() {
				
		this.tickerItems.each(function(tickerItem) {
			tickerItem.element.removeClassName("new");
		});
		
		this.getFeed("/proxy.php?Url=" +  escape("http://www.bbc.co.uk/dna/606/xml/articlesearchphrase?s_xml=rss&phrase=Football&contenttype=-1&articlesortby=DateUploaded&show=20"),
			this.onTickerData.bind(this));
	},
	
	addToTicker: function(tickerData) {
		
		// Only add new ticker items
		for (var i = 0; i < this.tickerItems.length; i++) {
			if (this.tickerItems[i].link == tickerData.link) {
				return;
			}
		}
		
		var tickerItemElement = document.createElement("span");
		tickerData.id = this.tickerItems.length > 0 ? this.tickerItems[this.tickerItems.length - 1].id + 1 : 0;
		tickerItemElement.id = "tickeritem-" + tickerData.id;
		tickerItemElement.className = "tickeritem new";
		if (this.tickerItems.length > 0) {
			tickerItemElement.style.display = "none";
		}
		tickerItemElement.innerHTML = "<a target=\"_blank\" href=\"" + tickerData.link + "\">" + tickerData.title + "</a>";
		
		this.tickerItemsElement.insertBefore(tickerItemElement, this.tickerItemsElement.firstChild);		
		tickerData.element = tickerItemElement;
		
		this.tickerItems.push(tickerData);
		
		if (this.tickerItems.length > 20) {
			var cropCount = this.tickerItems.length - 20;
			for (var i = 0; i < cropCount; i++) {
				this.tickerItems[i].element.remove();
			}
			this.tickerItems = this.tickerItems.slice(cropCount);
		}
	},

	cycleTicker: function() {
		if (this.tickerTimer == undefined) {
			this.tickerCurrentItem = -1;
			this.tickerTimer = new PeriodicalExecuter(this.cycleTicker.bind(this), 20);
		}
	
		if (this.tickerItems.length == 0) {
			return;
		}				

		this.tickerCurrentItem++;
		
		if (!this.tickerScrollingTransition && this.tickerItems[this.tickerCurrentItem]) {
			$("tickeritem-" + this.tickerLastItem) && $("tickeritem-" + this.tickerLastItem).hide();
			this.tickerLastItem = this.tickerItems[this.tickerCurrentItem].id;
			new Effect.Appear(this.tickerItems[this.tickerCurrentItem].element);
		}		
	
		if (this.tickerCurrentItem >= this.tickerItems.length) {
			this.refreshTicker();
			this.tickerCurrentItem = -1;
		} 	
	},
	
	/**
	 * Feed Item Function
	 */
	initializeFeedItems: function() {
		
		
		$A(this.feedItemsElement.getElementsByClassName("feeditem")).each((function(element) {
		
			var id = element.id.substring(element.id.search(/-/) + 1);
			
			// Make all expand buttons work
			var expandButton = $A(element.getElementsByClassName("feeditem-expand")).first();
			var titleText = $A(element.getElementsByClassName("feeditem-title")).first();

			if (titleText.getWidth() < titleText.scrollWidth) {
				titleText.onmouseover = this.scrollText.bind(this, titleText);
			}
			titleText.onclick = expandButton.onclick = this.onFeedItemExpandClick.bindAsEventListener(this, element, id);
						
			// Make all action buttons work
			$A(element.getElementsByClassName("feeditem-action")).each((function(actionElement) {
				actionElement.onclick = this.onFeedItemActionClick.bind(this, element, id, expandButton);
			}).bind(this));
						
		}).bind(this));
		
		// Find all Listen Again Links
		$A(this.feedItemsElement.getElementsByClassName("listen-again")).each(this.setupListenAgainLink.bind(this));
	},
	
	setupListenAgainLink: function(element) {
		if (element.href) {
			var episode = element.href.match(/[^/]*?$/);
			element.onclick = this.openEmpConsole.bind(this, episode)
		}
	},	
	
	updateFeed: function() {
		this.getFeed(this.feedUrl, this.onFeedItemData.bind(this));
	},	
	
	addFeedItem: function(feedItem) {
		this.feedItems.push(feedItem);
	},
	
	removeFeedItem: function() {
	},
	
	
	clearNowPlaying: function() {
		$A(this.feedItemsElement.getElementsByClassName("feeditem")).each((function(element) {
			element.removeClassName("now-playing");
		}).bind(this));
	},
	
	playFeedItemById: function(id) {
		var feedItem = this.findFeedItem(id);
		if (feedItem) {
			return this.playFeedItem(feedItem);
		} else {
			return false;
		}
	},
	
	playFeedItem: function(feedItem) {
		
		this.clearNowPlaying();
		
		$("feeditem-" + feedItem.Id.data).addClassName("now-playing");
		
		this.showNextButton();
		this.currentFeedItem = this.feedItems.indexOf(feedItem);
		
		var returnValue = true;
		switch (feedItem.MediaType.formatted) {
			case "audio/mpeg":
			case "audio/playlist":
			case "audio/podcast":
				this.playAudio(feedItem.MediaLink.data);
				returnValue = false;
				break;
			case "video/playlist":
			case "video/x-flv":
				this.playVideo(feedItem.MediaLink.data);
				returnValue = false;
				break;
		}
		return returnValue;
	},
	
	showNextButton: function() {
		$A(this.playNextButton.getElementsBySelector("a")).first().innerHTML = "Next";
		this.playNextButton.addClassName("skip");
	},
	
	showPlayButton: function() {
		$A(this.playNextButton.getElementsBySelector("a")).first().innerHTML = "Play";
		this.playNextButton.removeClassName("skip");
	},
	
	playNext: function() {
	
		while ((++this.currentFeedItem < this.feedItems.length) && (this.playFeedItem(this.feedItems[this.currentFeedItem]))) {
		}
	},
	
	updateUpcoming: function() {
	},
	
	getFeed: function(url, onSuccess) {
		new Ajax.Request(url, {
			method: "get",
			onSuccess: (function(transport) {
				try {
					onSuccess(transport);
				} catch (e) {
					// console.debug(e);
				}				
			}).bind(this)
		}) 
	},
	
	refreshAll: function() {
		var s = location.search.toQueryParams();
		s.Play = false;
		location.search = $H(s).toQueryString();
	},
	
	findFeedItem: function(id) {
		var returnItem = false;
		this.feedItems.each(function(feedItem) {
			if (id == feedItem.Id.data) {
				returnItem = feedItem;
			}
		});
		return returnItem;
	},
	
	expand: function(element, parent) {
		if (!element.transition) {
			element.transition = true;
			parent.addClassName("expanded");
			new Effect.BlindDown(element, {
				duration: 0.3,
				afterFinish: this.finishTransition.bind(this, element) 
			});
		}
	},
	
	hide: function(element, parent) {
		if (!element.transition) {
			element.transition = true;
			parent.removeClassName("expanded");
			new Effect.BlindUp(element, {
				duration: 0.3, 
				afterFinish: this.finishTransition.bind(this, element)
			});		
		}
	},
	
	finishTransition: function(element) {
		element.transition = false;
	},	
	
	/**
	 * EMP Functions
	 */
	playAudio: function(playlist) {
		this.openEmpPanel(188, "audio", (function() {
			var emp = new bbc.Emp();
			emp.setDomId("emp");
			emp.setWidth("100%");
			emp.setHeight("90");
			emp.set("config_settings_displayMode", "audio");
			emp.set("config_settings_autoPlay", "true");
			emp.set("config_settings_skin", "black");
			emp.set("config_settings_suppressRelatedLinks", "true");
			emp.set("config_settings_showShareButton", "false");
			
			emp.setPlaylist(playlist);
			emp.write();		
			this.empPanelExpanded = "audio";
		}).bind(this));
	},
	
	playVideo: function(playlist) {
		this.openEmpPanel(0, "video", (function() {
			var emp = new bbc.Emp();
	
			emp.setDomId("emp");
			emp.setWidth("100%");
			emp.setHeight("284");
			emp.set("config_settings_displayMode", "video");
			emp.set("config_settings_autoPlay", "true");
			emp.set("config_settings_skin", "black");
			emp.set("config_settings_showShareButton", "false");
			emp.set("config_settings_suppressRelatedLinks", "true");
			
			emp.setPlaylist(playlist);
			emp.write();
			
			this.empPanelExpanded = "video";
		}).bind(this));
	},
	
	openEmpPanel: function(height, className, afterFinish) {
		if (this.empPanelElement.visible()) {
			this.closeEmpPanel(this.openEmpPanel.bind(this, height, className, afterFinish));
		} else {
			if (RumbleUI.EmbeddedObjects.Flash.isVersion(9, 0, 0)) {
				this.empElement.innerHTML = "";
			} 
			this.empPanelElement.className = className;
			
			new Effect.Morph("feeditems-display", { style: "height: " + height + "px", duration: 0.5 });
			new Effect.BlindDown(this.empPanelElement, { duration: 0.5, afterFinish: afterFinish });
		}
	},
	
	closeEmpPanel: function(afterFinish) {
		this.empElement.innerHTML = "";
		new Effect.Morph("feeditems-display", { style: "height: 301px", duration: 0.5 });
		new Effect.BlindUp(this.empPanelElement, { duration: 0.5, afterFinish: afterFinish });
		this.clearNowPlaying();
	},
	
	hideEmpPanel: function() {
		if (this.empPanelExpanded) {
			$("feeditems-display").style.height = "";
			if (RumbleUI.EmbeddedObjects.Flash.isVersion(9, 0, 0)) {
				this.empElement.innerHTML = "";
			} 
			this.empPanelExpanded = false;
			this.empPanelElement.hide();
		}
	},
	
	/** 
	 * Generic Interface Functions
	 */	
	scrollText: function(element) {
		element = $(element);
		if (element.isScrolling) {
			return;
		}
		
		element.isScrolling = true;
		var scrollWidth = element.scrollWidth;
		new PeriodicalExecuter(function() {
			var x = 0;
			if (element.style.textIndent == "") {
				element.style.textIndent = "0px";  
			} else {
				x = element.style.textIndent.substring(0, element.style.textIndent.length - 2);
			}
			element.style.textIndent = (x - 3) + "px";
			if (x < -scrollWidth - 25) {				
				element.setStyle({ textIndent: "0px" });
				this.stop();
				element.isScrolling = false;
			}
		}, 0.06);
	},	
	
	/**
	 * Events 
	 */
	onTickerData: function(transport) {
		var nodes = transport.responseXML.getElementsByTagName("item");
		var i = 0;
		for (var node in nodes) {		
			if (nodes[node].attributes && nodes[node].attributes[0]) {
				try {
					this.addToTicker({ 
						uid: nodes[node].attributes[0].nodeValue,
						link: nodes[node].attributes[0].nodeValue,
						title: nodes[node].getElementsByTagName("title")[0].firstChild.nodeValue,
						description: nodes[node].getElementsByTagName("description")[0].firstChild.nodeValue
					});
				} catch (e) {
					//console.debug(e);
				}
			}
		}	
	},
		
 	onFeedItemActionClick: function(element, id) {
 		return this.playFeedItem(this.findFeedItem(id));
	},	
	
	onFeedItemData: function(transport) {
		this.feedItems = transport.responseText.evalJSON().feedItems;	
	},
	
	onFeedItemExpandClick: function(event, element, id) {

		var descriptionElement = $A(element.getElementsByClassName("feeditem-description")).first();

		if (element.hasClassName("expanded")) {
			this.hide(descriptionElement, element);
		} else {
			this.expand(descriptionElement, element);
		}
		
		Event.stop(event);
		return false;
	},
	
	onSelectSkin: function(value) {
		this.previewSkin(value);
	},
	
	onPlayNextClick: function() {
		this.playNext();
	},

	onTickerItemClick: function() {
	},

	onEditPrefenecesClick: function() {
	},

	onScheduleClick: function() {
	},

	onShareClick: function() {
		this.toggleSharePanel();
		return false;
	},
	
	onShareCloseClick: function() {
		this.toggleSharePanel();
		return false;
	},
	
	onHelpClick: function() {
		this.toggleHelpPanel();
		return false;
	},

	onHelpCloseClick: function() {
		this.hideHelpPanel();
		return false;
	},
	
	onFeedbackClick: function() {
		this.toggleFeedbackPanel();
		return false;
	},

	onFeedbackCloseClick: function() {
		this.hideFeedbackPanel();
		return false;
	},

	onFeedbackSendClick: function() {
		this.sendFeedback ();
		return false;
	},

	onEmpCloseClick: function() {
		this.closeEmpPanel();
		this.showPlayButton();
		return false;
	},

	onSettingsClick: function() {
		this.toggleSettingsPanel();
		return false;
	},

	onSettingsSaveClick: function() {
		this.saveSettings();
		//this.hideSettingsPanel();
		return false;
	},

	onSettingsCancelClick: function() {
		this.hideSettingsPanel();
		return false;
	},

	onFilterHeaderClick: function(element) {
		new Effect.toggle(element, "blind", {
			duration: 0.3
		});
	},
	
	/**
	 * Embed Events
	 */
	onEmbedClick: function() {
		Effect.Appear("embed", { duration: 0.3 });
		return false;
	},
	
	onMobileClick: function() {
		Effect.Appear("mobile-panel", { duration: 0.3 });
		return false;
	},
	
	onMobileCloseButtonClick: function() {
		this.hideMobilePanel();
		return false;
	},	
	
	onShowFixedSettingsClick: function() {
		this.embedCodeElement = $("embed-code");
		this.embedCodeFixedElement = $("embed-code-fixed");
		if (this.embedCodeElement.visible()) {
			this.embedCodeElement.hide();
			this.embedCodeFixedElement.show();
			$("embed-show-fixed-settings-button").down("strong").innerHTML = "Hide";
		} else {
			this.embedCodeElement.show();
			this.embedCodeFixedElement.hide();
			$("embed-show-fixed-settings-button").down("strong").innerHTML = "Show";
		}
		return false;
	},

	onEmailClick: function() {
		
		this.showEmailPanel();
		return false;
	},

	onEmbedCloseClick: function() {
		this.hideEmbedPanel();
		return false;
	},

	onEmailCloseClick: function() {
		this.hideEmailPanel();
		return false;
	},
	
	onEmailSendClick: function() {
		this.sendEmail();
		return false;
	}
}