(function() {
// Localize jQuery variable
var jQuery;
var useNoConflict = true;
var mercuryURL = "https://mercury.connective.com.au";
var mercuryCalculatorsURL = mercuryURL + "/calculators/";
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.11.3') {
useNoConflict = false;
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("src", "https://code.jquery.com/jquery-2.2.4.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function() { // For old versions of IE
if (this.readyState === 'complete' || this.readyState === 'loaded') {
scriptLoadHandler();
}
};
} else { // Other browsers
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(useNoConflict);
// Call our main function
main();
}
/******** Receive an post message from each iframe of body size ******/
window.addEventListener('message', function(event) {
if (event.origin === mercuryURL){
jQuery(document).ready(function($) {
var iFrame = event.data;
var iFrameValue = iFrame.split("|");
// Resize the iframes when the window is resized
$(window).resize(function() {
$("#" + iFrameValue[0]).height(iFrameValue[1] + "px");
}).resize();
if (event.origin !== mercuryURL) return;
});
}
}, false);
/******** Our main function ********/
function main() {
jQuery(document).ready(function($) {
var css_custom = $("", {
rel: "stylesheet",
href: mercuryCalculatorsURL+"css/custom.min.css",
type: "text/css"
});
var css_colorPicker = $("", {
rel: "stylesheet",
href: mercuryCalculatorsURL+"css/colorpicker.min.css",
type: "text/css"
});
css_custom.appendTo('head');
css_colorPicker.appendTo('head');
var applyColorsFn = function(responseObj) {
var colors = responseObj.colors;
colors = colors.split('|');
var m4Colors = false;
if (colors[0].indexOf('#') === -1) {
m4Colors = true;
for (var i = 0; i < colors.length; i++) {
colors[i] = '#' + (parseInt(colors[i])).toString(16);
for (var j = colors[i].length; j < 7; j++) {
colors[i] = colors[i] + '0';
}
}
}
var headerColor = null;
var bgColor = null;
var labelColor = null;
var resultsBgColor = null;
if (m4Colors) {
headerColor = colors[0];
bgColor = colors[2];
labelColor = colors[3];
resultsBgColor = colors[4];
} else {
headerColor = colors[0];
labelColor = colors[1];
resultsBgColor = colors[2];
bgColor = colors[3];
}
// Find all iframes
var $iframes = $("iframe");
var iframeCalculatorCount = 0;
// Apply a custom stylesheet to all iframes individually, based on the colors retrieved from Mercury
$iframes.each(function() {
var src = $(this).attr('src');
if( src ) {
if(src.substring(0,mercuryURL.length) === mercuryURL && iframeCalculatorCount < 3){
var iframe = $(this)[0];
var iframeDocument = $(this)[0].contentWindow;
var url = mercuryURL;
var i = 0;
iframe.onload = function() {
var setColors = setInterval(function () {
var message = headerColor + "|" + labelColor + "|" + resultsBgColor + "|" + bgColor;
iframeDocument.postMessage(message, url);
if (i > 5) {
window.clearInterval(setColors);
} else {
i++;
}
}, 200);
}
}
}
});
}
var cbFunction = function(responseObj) {
if (responseObj.success === true) {
// Color Picker
if ($('#connective-color-picker-container').length > 0) {
$.getScript( mercuryCalculatorsURL+"js/jscolor.min.js", function() {
$('#connective-color-picker-container').html(
'
' +
'
'
);
});
}
// Property Fees
if ($('#connective-property-fees-calculator').length > 0) {
$('#connective-property-fees-calculator').html(
''
);
}
// Borrowing Capacity
if ($('#connective-borrowing-capacity-calculator').length > 0) {
$('#connective-borrowing-capacity-calculator').html(
''
);
}
// Repayments
if ($('#connective-repayments-calculator').length > 0) {
$('#connective-repayments-calculator').html(
''
);
}
applyColorsFn(responseObj);
} else {
if ($('#connective-property-fees-calculator').length > 0 ) {
$('#connective-property-fees-calculator').html("' +
'
' +
'Main heading: ' +
'' +
'
' +
'Labels: ' +
'' +
'
' +
'Results background: ' +
'' +
'
' +
'Background: ' +
'' +
'
' +
'' +
'' +
'' +
'
' +
'The calculators have not been configured correctly. " +
"Please contact the Connective Mercury Helpdesk.
");
}
if ($('#connective-borrowing-capacity-calculator').length > 0 ) {
$('#connective-borrowing-capacity-calculator').html("The calculators have not been configured correctly. " +
"Please contact the Connective Mercury Helpdesk.
");
} else if ($('#connective-repayments-calculator').length > 0 ) {
$('#connective-repayments-calculator').html("The calculators have not been configured correctly. " +
"Please contact the Connective Mercury Helpdesk.
");
}
}
}
$.getScript( mercuryCalculatorsURL+"js/CommonFunctions.min.js", function() {
$.getScript( mercuryCalculatorsURL+"js/bootstrap.min.js", function() {
connective_htmlcw_common_functions.authenticate(cbFunction);
});
});
});
}
})(); // We call our anonymous function immediately