/**
 * All intellectual property rights in this Software throughout the world belong to UK Radioplayer, 
 * rights in the Software are licensed (not sold) to subscriber stations, and subscriber stations 
 * have no rights in, or to, the Software other than the right to use it in accordance with the 
 * Terms and Conditions at www.radioplayer.co.uk/terms. You shall not produce any derivate works 
 * based on whole or part of the Software, including the source code, for any other purpose other 
 * than for usage associated with connecting to the UK Radioplayer Service in accordance with these 
 * Terms and Conditions, and you shall not convey nor sublicense the Software, including the source 
 * code, for any other purpose or to any third party, without the prior written consent of UK Radioplayer.
 *
 * Modal popup for the radioplayer
 *
 * @param name Used for the css class so styles can be overriden
 * @param title Text for the title
 * @param body Text for the body of the modal
 * @param primaryBttn Text for the primary button
 * @param primaryCallback Callback function for when the button is clicked
 * @param secondaryBttn Text for the secondary button
 * @param secondaryCallback Callback function for when the secondary button is clicked
 *
 * @author John Cleveley <john.cleveley@bbc.co.uk>
 */

radioplayer.models.modal = function(name, title, body, primaryBttn, primaryCallback, secondaryBttn, secondaryCallback) {
	this._name = name;
	this._title = title;
	this._body = body;
	this._primaryBttn = primaryBttn ? primaryBttn : "";
	this._primaryCallback = primaryCallback ? primaryCallback : null;
	this._secondaryCallback = secondaryCallback ? secondaryCallback : null;
	this._secondaryBttn = secondaryBttn ? secondaryBttn : "";
	this._modal = null;
	
};

radioplayer.models.modal.prototype = {

	"a.modal-primary-bttn click": function(e) {
		e.preventDefault();
		if (this._primaryCallback) {
			this._primaryCallback();
		} else {
			this.hide();
		}

	},

	"a.modal-secondary-bttn click": function(e) {
		e.preventDefault();
		if (this._secondaryCallback) {
			this._secondaryCallback();
		} else {
			this.hide();
		}
	},


	show: function() {

		var $overlay = this._getDom();

		var opts = { modal: true, closeOnMaskClick: false, anim: "fade", x:'50%', y:'38%' };
		this._modal = new glow.widgets.Overlay($overlay, opts);

		// bind the dialog
		this.bind(this._modal);

		// show the modal
		this._modal.show();

	},

	hide: function() {

		glow.events.addListener(this._modal, 'afterHide', function() {
			this._modal = null;
			this.unbind();
		}, this);

		this._modal.hide();

	},

	_getDom: function() {
		// return  modal dom structure
	
		var $overlay = glow.dom.create(
			"<div id=\"modal\" class=\"{name}\">" +
				"<div class=\"modal-top\"></div>" +
				"<div class=\"modal-main\">" +
					"<div class=\"modal-heading\">{title}</div>" +
					"<div class=\"modal-body\">{body}</div>" +
					"<a href=\"#\" class=\"modal-primary-bttn\">{primaryBttn}</a>" +
				"</div>" +
				"<div class=\"modal-bottom\"></div>" +
			"</div>"
			, {
			interpolate: {
				name: this._name,
				title: this._title,
				body: this._body,
				primaryBttn: this._primaryBttn
			}
		});

		var $primaryButton = $overlay.get(".modal-primary-bttn");

		// if modal has been passed confirm textcreate button
		if (this._secondaryBttn) {
			var $secondaryBttn = glow.dom.create(
				
					"<a href=\"#\" class=\"modal-secondary-bttn\">" +
						this._secondaryBttn +
					"</a>"
				);

			// insert  button in before the close button
			$secondaryBttn.insertAfter($primaryButton);
		}

		return $overlay;
	}

}

