/**
 * @fileoverview Venda.Widget.Features - Create an inpage popup for common features such as tell a friend, emwbis, write and read a review.
 *
 * The information displayed in the popup div is retrieved using AJAX. This information is pulled
 * from the usual invt template used for the standard tell a friend, emwbis, write and read a review functionality.
 * 
 * @requires /venda-support/js/ajax.js
 * @requires /venda-support/js/external/yui/build/yahoo/yahoo-min.js
 * @requires /venda-support/js/external/yui/build/dom/dom-min.js
 * @requires /venda-support/js/external/yui/build/event/event-min.js
 * @requires /venda-support/js/external/yui/build/connection/connection-min.js
 * @requires /venda-support/js/external/yui/build/yahoo-dom-event/yahoo-dom-event.js  
 * @requires /venda-support/js/external/yui/build/dragdrop/dragdrop-min.js
 * @requires /venda-support/js/external/yui/build/container/container-min.js
 * @author Hayley Easton <heaston@venda.com>
 */

//create Features namespace
var features = Venda.namespace('Widget.Features');

/**
 * Construct a new features object
 * @class This class creates a features object
 * @constructor
 * @param {object} 	settings set settings for features (draggable: boolean, modal:boolean )
 * @tags {object} 	tags pass venda tags into js functions (loadmessage:string)
 */	
features.create = function(settings, tags) {
	features.settings = settings;
	features.tags = tags;
	
	var ids = ["tellafriend_link", "writereview_link", "writeownreview_link", "readreview_link", "emwbis_link"]; // these ids must be added to links
	//register listener for objects in 'ids' array
	YAHOO.util.Event.addListener(ids, "click", features.interceptLink);
	
	//window onload events
	YAHOO.util.Event.addListener(window, "load", features.popupInvtFeature);
};


/**
 * Show product detail feature panel
 * Will show/hide generated feature popup
 * @param {event} e used to suppress default link behaviour
 */
features.interceptLink = function(e) {
	YAHOO.util.Event.preventDefault(e); // suppress default link behaviour
	var tl = '<div class="tl"></div><span>'; // optional top left curve
	var tr = '</span><div class="tr"></div>'; // optional top right curve
	htmlEl = document.getElementById(this.id);
	featurePanel.setHeader(tl + htmlEl.innerHTML + tr); // set heading of popup div - uses link text within <a></a> tags
	featurePanel.show(); // show popup div
	ajaxFunction(this.href+'&layout=noheaders','popupcontent',null,features.loadInvtScript); //fill div with relevant content via ajax
};

/**
 * Insert javascript
 * Adds an external javascript tag to the main page so that javascript belonging to popup will run
 */
features.loadInvtScript = function() {
	insertScript(htmlEl.href+'_script&layout=noheaders','popupcontent');
};

/**
 * Initialise product detail features panel
 * Creates div containers which will display either tell a friend, emwbis, read or write a review
 */
features.popupInvtFeature = function() {
	// Instantiate a Panel from script
	featurePanel = new YAHOO.widget.Panel("popupcontent_panel", { constraintoviewport: true, visible: false, draggable: features.settings.drag, modal: features.settings.modal } );
	featurePanel.setBody("<div id='popupcontent'><p>" + features.tags.loadmessage + "</p></div>");
	featurePanel.render("invt_popup");
	featurePanel.hideEvent.subscribe(features.emptyContent);
};

/**
 * Hide features popup
 * Closes the popup if 'Back to Product Details' link is clicked
 */
features.featureHide = function(e) {
	YAHOO.util.Event.preventDefault(e); // suppress default link behaviour
	featurePanel.hide();
};

/**
 * Reset popup contents
 * When popup is closed, reset the contents so loading message appears
 */
features.emptyContent = function(e) {
	document.getElementById('popupcontent').innerHTML = "<p>" + features.tags.loadmessage + "</p>";
};

//Legacy support - allows calls to function that predate Venda namespacing to work
featureHide = Venda.Widget.Features.featureHide;
