/*=======================================================================================================
 * Class: Helper
 * Author: Kris Young
 *=======================================================================================================*/
 
function Helper(data, scope){
		
	var _scope = scope;
	var _data =	data;
	var _tooltip = $('<div id="tooltip"></div>');
	
	var __construct = (function(){
		
		_tooltip.css({position:'absolute', 
					  zIndex:10000, 
					  background:"#FFA", 
					  color:'#000', 
					  padding:"10px", 
					  border:"#CCC solid 1px", 
					  opacity:0.9
					  })
				.hide()
				.appendTo(_scope);
				
		_scope.bind('mouseover', _mouseover)
			  .bind('mouseout', _mouseout)
			  .bind('mousemove', _mousemove);
	})();
	
	function _getMessage(id){
		for(var i = 0; i<_data.length; i++){
			var o = _data[i];
			if(typeof o == 'object' && o.id == id) return o.message;
		}
	};	
	
	function _mouseover(event) {
		// get the img element that triggered the event (as events are delegated to the 'scope')
		var $el = $(event.target).closest("img.helper").css({cursor:'help'});
		//check if the event was triggered by a helper icon
		if ($el.length) {
			var msg = _getMessage($el.attr('id'));
			_tooltip.css({top: event.pageY + 5, left: event.pageX + 10 })
					.html('<div><p>' + msg + '</p></div>')
					.fadeIn(200)
					.css({width:function(){return ($(this).outerWidth() < 220)? '' : '220px';}});
		}
	};
	
	function _mouseout(event) {
		// get the img element that triggered the event (as events are delegated to the 'scope')
		var $el = $(event.target).closest("img.helper");
		//check if the event was triggered by a helper icon
		if ($el.length) {
			_tooltip.fadeOut(200, function(){$(this).css({width:''})});
		}
	};
	
	function _mousemove(event) {
		//check if the event was triggered by a helper icon
		if ($(event.target).closest("img.helper").length) {
			_tooltip.css({top: event.pageY + 5, left: event.pageX + 10});
		}
	};
	
	return this;
};

Helper.prototype = new Object();
Helper.prototype.constructor = Helper; 

/*=======================================================================================================*/
// initialise on page load

var data = [
{id:"msg-org-details", message:"Type in organisation details here, all inputs marked with an asterisk must be filled in."},
{id:"msg-financial-year", message:"Enter the start date for the financial year (official financial year begins 5th April)"},
{id:"msg-filter-category", message:"Add a new filter category to the list"},
{id:"msg-filter-value", message:"Define filter category values here"},
{id:"msg-user-details", message:"Enter personal details here"},
{id:"msg-role", message:"Select Portfolio Manager if the user is responsible for overseeing all buildings. Select Building Manager if they will be entering data for only buildings they are responsible for."},
{id:"msg-address", message:"Enter contact details here. All inputs with an asterisk must be filled in."},
{id:"msg-user-search", message:"Search the list of current users"},
{id:"msg-user-showall", message:"Displays all the users of the current portfolio"},
{id:"msg-user-delete", message:"Remove the currently selected user from the portfolio"},
{id:"msg-user-add", message:"Create a new user for this portfolio"},
{id:"msg-bld-showall", message:"Displays all the buildings currently in the portfolio"},
{id:"msg-bld-search", message:"Search the buildings in the portfolio"},
{id:"msg-bld-delete", message:"Remove the currently selected building from the portfolio"},
{id:"msg-bld-add", message:"Add a new building to the portfolio"},
{id:"msg-bld-address", message:"Enter the building address details here. All inputs marked with an asterisk must be filled in."},
{id:"msg-bld-manager", message:"Select the building manager from the drop-down list of users"},
{id:"msg-bld-exclude", message:"Selecting this option will unconditionally exclude the building from the carbon emissions assessment."},
{id:"msg-lease-type", message:"Select the type of lease for this building"},
{id:"msg-lease-expiry", message:"Enter the lease expiry date if applicable"},
{id:"msg-last-renovation", message:"Input the date of the last renovation"},
{id:"msg-next-renovation", message:"Input the date of the next renovation if known"},
{id:"msg-part-occupation", message:"Select Yes if the building is leased on a part-time basis"},
{id:"msg-repair-liability", message:"Select Yes if your organisation is responsible for repairs to the building"},
{id:"msg-crc-affected", message:"Select Yes if the inclusion of sub-tenants will affect your organisations carbon footprint."},
{id:"msg-fuel-type", message:"Select the fuel type from the drop-down menu"},
{id:"msg-meter-id", message:"Enter the MPR or MPAN number, or a bespoke meter reference."},
{id:"msg-fltr-startdate", message:"Enter the start date for the energy billing information displayed below. Click the calendar icon to select the date from a calendar"},
{id:"msg-fltr-enddate", message:"Enter the end date for the energy billing information displayed below. Click the calendar icon to select the date from a calendar."},
{id:"msg-fltr-fuel", message:"Select the fuel type(s) that you want to view billing information for."},
{id:"msg-fltr-btn", message:"Applies the filter criteria on the left to the energy billing information below."},
{id:"msg-usage-edit", message:"Edit the currently selected billing information input"},
{id:"msg-usage-add", message:"Add new energy billing information to the list"},
{id:"msg-usage-delete", message:"Delete the currently selected billing information input"},
{id:"msg-evidence-browse", message:"Navigate to where the file containing energy billing evidence is saved."},
{id:"msg-evidence-upload", message:"Upload the selected file containing energy billing evidence."},
{id:"msg-custom-category", message:"The filters below define those that will be available in the &lsquo;Review Portfolio&rsquo; section. Filters marked with a padlock symbol are non-editable and will always be available. You can add your own filter by typing the name in the field below and clicking &lsquo;Add&rsquo;.  An example custom filter would be &lsquo;Building location.&rsquo;"},
{id:"msg-custom-value", message:"Define the filter category values for any bespoke filters you have specified. The values must reflect the filter category. For example, if the filter category is defined as &lsquo;Building location&rsquo; then suitable filter category values could be: UK, USA, Australia."},
{id:"msg-client-select", message:"If you do not see a client listed please contact us"},
{id:"msg-dec-assessor-name", message:"Refered to as 'Person Name' in Scotland"},
{id:"msg-dec-score", message:"Enter asset rating as calculated by approved software"},
{id:"msg-epc-score", message:"Enter EPC asset rating as calculated by approved software"},
{id:"msg-epc-benchmark-new", message:"Enter Newly Built benchmark Asset Rating as calculated by approved software"},
{id:"msg-epc-benchmark-existing", message:"Enter Existing Building benchmark Asset Rating as calculated by approved software"},

];

$(function(){
var helper = new Helper(data, $('body'));
});

