﻿jQuery.fn.fadeToggle = function(speed, easing, callback) { 
	return this.animate({opacity:'toggle'}, speed, easing, callback); 
};


// Replaces all instances of the given substring.
String.prototype.replaceAll = function(  
    strTarget, // The substring you want to replace
    strSubString // The string you want to replace in.
    )
{
    var strText = this;
    var intLastIndexOfMatch = 0;
    var intIndexOfMatch = strText.indexOf( strTarget );
     
    var result="";

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1)
    {
        result += strText.substring( intLastIndexOfMatch, intIndexOfMatch ) + strSubString;
    
        intLastIndexOfMatch = intIndexOfMatch + strTarget.length;

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf( strTarget, intLastIndexOfMatch );
    }

    result += strText.substring( intLastIndexOfMatch, strText.length );

    return result;
}



/* -------- NOTES -------- */

var helpNoteTop	 = '<div style="display:none;" class="helpnote">\n<h3 class="note-top">Help <a class="note-close">&times;</a></h3>\n<div class="note-middle">\n'
var errorNoteTop = '<div style="display:none;" class="errornote">\n<h3 class="note-top">Oops! <a class="note-close">&times;</a></h3>\n<div class="note-middle">\n'
var adviceNoteTop = '<div style="display:none;" class="advicenote">\n<h3 class="note-top">Advice <a class="note-close">&times;</a></h3>\n<div class="note-middle">\n'

var noteBottom = '\n</div>\n<div class="note-bottom"></div>\n</div>'

function hideNote(what) {
	var theNote = $(what).parents('div:eq(0)');
	$(theNote).fadeOut(300, function() {
		$(theNote).remove();
	});
}

function hideNotes(what) {
	$('div.helpnote,div.advicenote,div.errornote,div.comparehelpnote,div.table.tr.helpnote,div.table.tr.advicenote').each(function(i,name) {
		if( $(name).css('display') == 'block' && name.id != what) {
			$(name).fadeOut(300, function() {
				$(name).remove();
			});
		}
	});
}

function manageNotes(clickedLabel, noteTop, displayHelpText, divsToManage, climbHeight) {
	var helpText = $(clickedLabel).attr('title').replaceAll('\n','<br>');
    var divsToAttachTo = divsToManage + " h3 a.note-close"; 

	if (helpText == null) {
		helpText = displayHelpText;
	}						
	var helpNote = noteTop + helpText + noteBottom;
	
	var helpField;
	if(climbHeight > 0)
	{
	    for(var i = 0; i < climbHeight; i++)
	         helpField = $(clickedLabel).parent();
	}
	else
	{
	    helpField = $(clickedLabel);
	
	}
	if (helpField.children(divsToManage).css('display') == 'block') {
		helpField.children(divsToManage).fadeOut(300, function() {
			$(divsToAttachTo).click(function() {
				hideNote(this);
			});
		});
	} else {
		hideNotes();
		helpField.children(divsToManage).remove();
		helpField.prepend(helpNote);
		helpField.children(divsToManage).fadeToggle(300, function() {
			$(divsToAttachTo).click(function() {
				hideNote(this);
			});
		});
	}
}

