/**
 * @fileoverview Venda.Widget.Features - This file is identical to Venda.Widget.InvtPopups and will be phased out when the code is upgraded. 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
Venda.namespace('Widget.Features');

/**
 * Stub function is used to support JSDoc.
 * @class Venda.Widget.Features
 * @constructor
 */
Venda.Widget.Features = function(){};

/**
 * Construct a new features object
 * @param {array} 	featureHook specify which anchors will trigger the popups
 * @param {object} 	settings set settings for features (draggable: boolean, modal:boolean )
 * @tags {object} 	tags pass venda tags into js functions (loadmessage:string)
 */	
Venda.Widget.Features.create = function(featureHook, settings, tags) {
	Venda.Widget.Features.settings = settings;
	Venda.Widget.Features.tags = tags;
	
	//register listener for objects in 'featureHook' array
	YAHOO.util.Event.addListener(featureHook, "click", Venda.Widget.Features.interceptLink);
	
	//window onload events
	YAHOO.util.Event.addListener(window, "load", Venda.Widget.Features.popupInvtFeature);
};


/**
 * Show product detail feature panel
 * Will show/hide generated feature popup
 * @param {event} e used to suppress default link behaviour
 */
Venda.Widget.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,Venda.Widget.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
 */
Venda.Widget.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
 */
Venda.Widget.Features.popupInvtFeature = function() {
	// Instantiate a Panel from script
	featurePanel = new YAHOO.widget.Panel("popupcontent_panel", { constraintoviewport: true, visible: false, draggable: Venda.Widget.Features.settings.drag, modal: Venda.Widget.Features.settings.modal } );
	featurePanel.setBody("<div id='popupcontent'><p>" + Venda.Widget.Features.tags.loadmessage + "</p></div>");
	featurePanel.render("invt_popup");
	featurePanel.hideEvent.subscribe(Venda.Widget.Features.emptyContent);
};

/**
 * Hide features popup
 * Closes the popup if 'Back to Product Details' link is clicked
 */
Venda.Widget.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
 */
Venda.Widget.Features.emptyContent = function(e) {
	document.getElementById('popupcontent').innerHTML = "<p>" + Venda.Widget.Features.tags.loadmessage + "</p>";
};

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