/**
 * @fileoverview
 * This file provides font definitions for sIFR 3 and performs the appropriate
 * replacements for standard pages within the iiNet Web site. Further
 * page-specific replacements may be performed by defining your own domready
 * handler and calling sIFR.replace as seen below.
 */

parseSelector.pseudoClasses = {
  'not': function(nodes, selector) {
    var result = [];
    each: for(var i = 0, node; i < nodes.length; i++) {
      node = nodes[i];
      var ignore = parseSelector(selector, node.parentNode);
      for(var j = 0; j < ignore.length; j++) {
        if(ignore[j] == node) continue each;
      }
      result.push(node);
    }
    return result;
  }
};

/** The sIFR configuration for the Bernhardt Light font. */
var bernLight = {
	src: "/ii-inc/inc/bernhardt-light.swf",
	fitExactly: true,
	transparent: true,
	css: [ "a { color: #d52b1e; text-decoration: none; }", "a:hover { color: #d52b1e; text-decoration: none; }", ".sIFR-root { letter-spacing: 0.3; }" ]
};

/** The sIFR configuration for the Bernhardt Medium font. */
var bernMedium = {
	src: "/ii-inc/inc/bernhardt-medium.swf",
	fitExactly: true,
	transparent: true,
	css: [ "a { color: #d52b1e; text-decoration: none; }", "a:hover { color: #d52b1e; text-decoration: none; }", ".sIFR-root { letter-spacing: 0.3; }" ]
};

/** The sIFR configuration for the Bernhardt Bold font. */
var bernBold = {
	src: "/ii-inc/inc/bernhardt-bold.swf",
	fitExactly: true,
	transparent: true,
	css: [ "a { color: #d52b1e; text-decoration: none; }", "a:hover { color: #d52b1e; text-decoration: none; }", ".sIFR-root { letter-spacing: 0.3; }" ]
};

/** Somewhat ugly status variable for the forceReflow function. */
var isForcingReflow = false;

/**
 * Function to force a browser reflow. Horrifyingly enough, this is actually
 * required not only with IE, but also with Gecko-based browsers. Presumably
 * it's something to do with how sIFR 3 does its height auto-sizing.
 */
var forceReflow = function() {
	/* sIFR sometimes seems to lie about whether the replacement has actually
	 * taken place when onReplacement is called, so the 100 ms delay gives it a
	 * bit more time to work its magic. The delay is, admittedly, pulled
	 * completely out of thin air, but it seems to work pretty reliably. */
	if (!isForcingReflow) {
		isForcingReflow = true;
		window.setTimeout(function() { 
			var footer = document.getElementById("footer");
			footer.style.display = "none";
			footer.style.display = "block";
		}, 100);
	}
};

/* This was a last minute hack to avoid excessive downloading in the customer
 * retention area. The main customer retention portal page has a lot of
 * headings, and sIFR was bypassing the browser cache no matter what
 * combination of options, pre-loading and Apache mod_expires magic was used.
 * (There are reports about this behaviour on the sIFR forums -- nobody seems
 * to be able to track down exactly what causes it.)
 *
 * As such, we just kill sIFR for the customer area at present. Somebody should
 * check the behaviour of sIFR 3 next time it gets a major release to see if
 * it's improved. */
if (window.location.pathname.substr(0,11) != "/customers/") {
	sIFR.activate(bernLight, bernMedium, bernBold);

	// Let's perform the replacements once we have a DOM tree to work with.
	$(function() {
		sIFR.replace(bernBold, { selector: "#productSelector h1, #productSelector h2, #productSelector h3, h1.bold, h2.bold, h3.bold", onReplacement: forceReflow });
		sIFR.replace(bernMedium, { selector: "h1.medium, h2.medium, h3.medium", onReplacement: forceReflow });
		sIFR.replace(bernLight, { selector: "#left h2, #left h3, #side .side_heading", css: ".sIFR-root { color: #ffffff; }", onReplacement: forceReflow });
		sIFR.replace(bernMedium, { selector: "#main h1:not(.nosIFR), #main h2:not(.nosIFR), #main h3:not(.nosIFR)", onReplacement: forceReflow });
		sIFR.replace(bernLight, { selector: "#right h2, #right h3", onReplacement: forceReflow });
		sIFR.replace(bernMedium, { selector: ".customers #content h1, .customers #content h2, .customers #content h3", onReplacement: forceReflow });
	});
}
else if (window.location.pathname.substr(0,17) == "/customers/iinews") {
	sIFR.activate(bernLight, bernMedium);
	// iinews headings
	sIFR.replace(bernMedium, { selector: "#main h1", onReplacement: forceReflow });
	sIFR.replace(bernMedium, { selector: "#iinews h1, #iinews h2, #iinews h3", onReplacement: forceReflow });
}
