/**
 * @fileoverview Venda.Widget.InvtPopups - 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 InvtPopups namespace
Venda.namespace('Widget.InvtPopups');

/**
 * Stub function is used to support JSDoc.
 * @class Venda.Widget.InvtPopups
 * @constructor
 */
Venda.Widget.InvtPopups = 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, fixedCenter: boolean, fade: duration (in a whole or decimal number) )
 * @tags {object} 	tags pass venda tags into js functions (loadmessage:string)
 */	
Venda.Widget.InvtPopups.create = function(featureHook, settings, tags) {
	Venda.Widget.InvtPopups.settings = settings;
	Venda.Widget.InvtPopups.tags = tags;
	
	//window onload events
	YAHOO.util.Event.addListener(window, "load", Venda.Widget.InvtPopups.popupInvtFeature);
	
	//register listener for objects in 'featureHook' array
	YAHOO.util.Event.addListener(featureHook, "click", Venda.Widget.InvtPopups.interceptLink);
};

/**
 * Show product detail feature panel
 * Will show/hide generated feature popup
 * @param {event} e used to suppress default link behaviour
 */
Venda.Widget.InvtPopups.interceptLink = function(e) {
	YAHOO.util.Event.preventDefault(e); // suppress default link behaviour
	// If the link clicked has rel attributes e.g. rel="390 180", use the values provided to set the width and height of the popup div
	// If no rel attributes are provided at all, the css will control the width & height
	// If one link uses the rel attributes, it is best to provide rel attributes to all links
	if(jQuery("#productset").length > 0) {
		var top = jQuery(document).scrollTop() + jQuery("#productset").position().top;
		jQuery("#invt_popup").css("top",top+"px");
	};
/*** Addad Kellwood - Greg Starek ***/
	if(jQuery("#productdetail").length > 0) {
		var top  = locY(document.getElementById("productdetail")) - 17;
		var left = locX(document.getElementById("productdetail")) - 35;
		jQuery("#invt_popup").css("position","absolute");
		jQuery("#invt_popup").css("top",top+"px");
		jQuery("#invt_popup").css("left",left+"px");
	};
/*** End Kellwood add ***/	
	var relEl = document.getElementById(this.id).rel.split(" ");
	if (relEl[0]) {
		YAHOO.util.Dom.setStyle('popupcontent_panel', 'width', relEl[0]+'px');
	};
	
	if (relEl[1]) {
		YAHOO.util.Dom.setStyle('popupcontent', 'height', relEl[1]+'px');
	};

	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);
	
	hasHeaderID = document.getElementById(this.id+'_header');
	if(hasHeaderID){
		// If there is an ID in the productdetail to use for the header, use it
		headerEl = document.getElementById(this.id+'_header').innerHTML;
	} else {
		// If there no ID in the productdetail use the HTML within the <a></a> tags that called the popup
		headerEl = htmlEl.innerHTML;
	}
	
	featurePanel.setHeader(tl + headerEl + tr); // set heading of popup div
	featurePanel.show(); // show popup div
	ajaxFunction(this.href+'&layout=noheaders','popupcontent',null,Venda.Widget.InvtPopups.loadInvtScript); //fill div with relevant content via ajax
/*** Addad Kellwood - Greg Starek ***/
	var isMobileDev = ( (navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1) || (navigator.userAgent.indexOf('Android') != -1) ) ? true : false;
	if (isMobileDev) makeSizechartBig();
/*** End Kellwood add ***/		
};

/*** Addad Kellwood - Greg Starek ***/
makeSizechartBig = function() {
	if (document.getElementById('sizechart')==undefined) {
		setTimeout("makeSizechartBig()",100);
	} else {
		YAHOO.util.Dom.setStyle('sizechart', 'height', '886px');
		var theHTML = document.getElementById('popupcontent').innerHTML;
		theHTML = theHTML.replace('<div ','<div onclick="featurePanel.hide();" ');
		document.getElementById('popupcontent').innerHTML=theHTML;
		var theHeight = (Math.max(Math.max(document.body.scrollHeight,document.documentElement.scrollHeight),Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),Math.max(document.body.clientHeight, document.documentElement.clientHeight))+50);
		if (theHeight<1150) theHeight=1150;
		YAHOO.util.Dom.setStyle('popupcontent_panel_mask','height',''+theHeight+'px');
		setTimeout("YAHOO.util.Dom.setStyle('popupcontent_panel_mask','height','"+theHeight+"px')",1000);
	}
};
/*** End Kellwood add ***/		


/**
 * Insert javascript
 * Adds an external javascript tag to the main page so that javascript belonging to popup will run
 */
Venda.Widget.InvtPopups.loadInvtScript = function() {
	//only load script if the popup contains an invt template
	if (htmlEl.href.match('/invt')!=null){
		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.InvtPopups.popupInvtFeature = function() {
	// Instantiate a Panel from script
	featurePanel = new YAHOO.widget.Panel("popupcontent_panel", { constraintoviewport: true, visible: false, draggable: Venda.Widget.InvtPopups.settings.drag, modal: Venda.Widget.InvtPopups.settings.modal, fixedcenter: Venda.Widget.InvtPopups.settings.fixedCenter, effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration: Venda.Widget.InvtPopups.settings.fade} } );
	featurePanel.setBody("<div id='popupcontent'><p>" + Venda.Widget.InvtPopups.tags.loadmessage + "</p></div>");
	featurePanel.render("invt_popup");
	featurePanel.hideEvent.subscribe(Venda.Widget.InvtPopups.emptyContent);
};

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

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