/* ### THIS IS A CUT DOWN COPY OF http://www.rsvp.com.au/js/global.js ### */

/* for Perisher build of RSVP site, all initial functions except browser sniffer code and 'addEvent', written by Adrian Neilson Hall, 160206; both ANH and David Connard have since added several refinements and new functions; on DOMContentLoaded code by Edwards, Millar and Resig with IE adaptiation by ANH */
/* note re ad imports - IE5/6/7 does not understand 'setAttribute' method with certain attributes, e.g.  iFrame.setAttribute('marginwidth','0'); it also passes over 'getAttribute('class')' */
/* note re 'length' - in an array of 7 items, the length is '7' which is one more than the number corresponding to the last item in the array */

/* browser sniffer code */
var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();

var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);

var is_opera = (agt.indexOf("opera") != -1);
var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
var is_opera6 = (agt.indexOf("opera 6") != -1 || agt.indexOf("opera/6") != -1);
var is_opera6down = (is_opera && (is_opera2 || is_opera3 || is_opera4  || is_opera5 || is_opera6));

var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
   is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
   is_major = parseInt(is_minor);
}

var is_ie   = ((iePos!=-1) && (!is_opera));
var is_ie5   = (is_ie && is_major == 5);
var is_ie5_5  = (is_ie && (agt.indexOf("msie 5.5") !=-1));
var is_ie6 = (is_ie && is_minor == 6);
var is_ie6down = (is_ie && is_minor <= 6);

/* extra - less often used - sniffer code */

var is_mac = (agt.indexOf("mac")!=-1);
if (iePos !=-1) {
	 if(is_mac) {
			 iePos = agt.indexOf('msie');
			 is_minor = parseFloat(agt.substring(iePos+5,agt.indexOf(';',iePos)));
	 }
	 else is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)));
	 is_major = parseInt(is_minor);
}

var is_konq = false;
var kqPos   = agt.indexOf('konqueror');
if (kqPos !=-1) {
	 is_konq  = true;
	 is_minor = parseFloat(agt.substring(kqPos+10,agt.indexOf(';',kqPos)));
	 is_major = parseInt(is_minor);
}

var is_safari = (agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1);
var is_khtml  = (is_safari || is_konq);

var is_gecko = (!is_khtml)&&(navigator.product)&&(navigator.product.toLowerCase()=="gecko");
var is_gver  = 0;
if (is_gecko) is_gver=navigator.productSub;

var is_moz   = ((agt.indexOf('mozilla/5')!=-1) && (agt.indexOf('spoofer')==-1) &&
								(agt.indexOf('compatible')==-1) && (agt.indexOf('opera')==-1)  &&
								(agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)     &&
								(is_gecko) &&
								((navigator.vendor=="")||(navigator.vendor=="Mozilla")||(navigator.vendor=="Debian")));

var dhtmlBrowser = document.getElementById && !is_opera6down;

/* a DHTML library */

/* shortcut method of accessing an element by its id */
function $id(elementId) {
	return document.getElementById(elementId);
}
/* create an array of all elements which share a tag name; must have at least one argument - tagName (string);
   search for elements can be narrowed by passing a 2nd optional argument - element (var) */
function $tag() {
	return ($tag.arguments[1]) ? $tag.arguments[1].getElementsByTagName($tag.arguments[0]) : document.getElementsByTagName($tag.arguments[0]);
}
/* create an array of all elements which share a class name; (note - IE5 doesn't understand wildcard div indicator '*';)
	- 1 mandatory argument - class name (string);
	search for elements can be narrowed by passing:
	- a 2nd optional argument - container element (var)
	- a 3rd optional argument - tag name (string) */
function $class() {
	var classArray = new Array;
	var tag = ($class.arguments[2]) ? $class.arguments[2] : '*';
	var elementNodes;
	if ($class.arguments[1]) {
		elementNodes = (is_ie5) ? $class.arguments[1].all : $tag(tag,$class.arguments[1]);
	}
	var allNodes = (is_ie5) ? document.all : $tag(tag);
	var nodes = ($class.arguments[1]) ? elementNodes : allNodes;
	// replace any instances of '-' in class name with '\\-'
	var classString = $class.arguments[0].replace(/\-/g, "\\-");
	// create a regular expression which consists of optional space + class name + optional space
	// this prevents elements with similar class names (which contain the targeted class string), from being included in returned 'classArray'
	var myRegExp = new RegExp("(^|\\s)" + classString + "(\\s|$)");
	for (var i=0; i<nodes.length; i++) {
		if (myRegExp.test(nodes[i].className)) {
			classArray[classArray.length] = nodes[i]; //NB - 'push' not supported by IE5
		}
	}
	return classArray;
}

/* create an array of all elements which share a type attribute;
	- 1 mandatory argument - type attribute (string);
	optional arguments: parent element (var) & tag name (string) */
function $type() {
	var typeArray = new Array;
	var tag = ($type.arguments[2]) ? $type.arguments[2] : '*';
	var elementNodes;
	if ($type.arguments[1]) {
		elementNodes = (is_ie5) ? $type.arguments[1].all : $tag(tag,$type.arguments[1]);
	}
	var allNodes = (is_ie5) ? document.all : $tag(tag);
	var nodes = ($type.arguments[1]) ? elementNodes : allNodes;
	for (var i=0; i<nodes.length; i++) {
		if (nodes[i].getAttribute('type')&&(nodes[i].getAttribute('type')==$type.arguments[0])) {
			typeArray[typeArray.length] = nodes[i];//NB - 'push' not supported by IE5
		}
	}
	return typeArray;
}

/* create an array of child nodes of designated element which have designated tag */
function getChildrenByTag(element,tag) {
	var nodes = new Array;
	for (var i=0; i<element.childNodes.length; i++) {
		if ((element.childNodes[i].tagName)&&(element.childNodes[i].tagName.toLowerCase()==tag)) {
			nodes[nodes.length] = element.childNodes[i];//NB - 'push' not supported by IE5
		}
	}
	return nodes;
}

/* create an array of child nodes of designated element which have designated class */
function getChildrenByClass(element,myClass) {
	var nodes = new Array;
	// for explanation of reg expression test, see $class() above
	var classString = myClass.replace(/\-/g, "\\-");
	var myRegExp = new RegExp("(^|\\s)" + classString + "(\\s|$)");
	for (var i=0; i<element.childNodes.length; i++) {
		var child = element.childNodes[i];
		if ((child.className) && (myRegExp.test(child.className))) {
			nodes[nodes.length] = child; // NB - 'push' not supported by IE5
		}
	}
	return nodes;
}

/* ### CREATE NEW ELEMENT ### creates and inserts a new element into the current page; takes between 3 and 10 arguments:
0- tagName, 1- id, 2- class name, 3- destination node, 4- innerHTML, 5- handler, 6- function, 7- attribute, 8- value, 9- insertBefore;
note - arguments which aren't required can simply be left out, if they are sequential and the last of the arguments to be left out also happens to be the last possible argument; however if the arguments which are not required are non-sequential or do not include the last possible argument, assign a value of 0 to each of these; e.g. if an attribute is required but a function isn't - assign value of 0 to arguments [5] & [6] ; additional attributes/value and handler/function pairs can be added to the new element, but this must be done  separately (i.e. after calling this function, get the new element by its id, then add additional handler+function or attribute+value) */
function insertNewElement() {
	var element = document.createElement(insertNewElement.arguments[0]);
	if (insertNewElement.arguments[1]!=0) {
		element.id = insertNewElement.arguments[1];
	}
	if (insertNewElement.arguments[2]) {
		element.className = insertNewElement.arguments[2];
	}
	if (insertNewElement.arguments[4]&&(insertNewElement.arguments[4]!=0)) {
		element.innerHTML = insertNewElement.arguments[4];
	}
	if (insertNewElement.arguments[5]&&(insertNewElement.arguments[5]!=0)) {
		eval('element.' + insertNewElement.arguments[5] + ' = ' + insertNewElement.arguments[6]);
	}
	if (insertNewElement.arguments[7]) {
		element.setAttribute(insertNewElement.arguments[7],insertNewElement.arguments[8]);
	}
	insertNewElement.arguments[3].appendChild(element);
	if (insertNewElement.arguments[9]) {
		insertNewElement.arguments[3].insertBefore(element,insertNewElement.arguments[9]);
	}
}

/* initialise 2 variables for 'showActiveTabs()' below */
var titlesInitialised = false;
var primaryTitle;
var secondaryTitle;
var firstLastTagged = false;

/* adjust following javascript so that secondaryTitle is null (and thus ignored for title bar) if there is no h5.pageTitle and the first h2's parent node isn't #rsvpcontent; also write class="pageTitle" to h1 if secondary title is null; replace all id="secondary" with class="pageTitle"; also - remove unique ids form h1 tags and place in footer as a variable to be filled by serverside code which reads ofroma table  */

function initialiseTitles() {
	if (!titlesInitialised)	{
		// if there's an element with id #primary, make it the primary title, otherwise make the first <h1> in #rsvpcontent the  primary title
		primaryTitle = ($id('primary')) ? $id('primary') : $tag('h1',content)[0];
		// if there's an element with id #secondary, make it the secondary title
		if ($id('secondary')) {
			secondaryTitle =$id('secondary');
		}
		titlesInitialised = true;
	}
}

/* ### CREATE AN IFRAME ### */
// 5 possible parameters are: 0 - src, 1 - width, 2 - height, 3 - node to append to, 4 - node to insert before
// NB: parameters 3 & 4 above are optional
function doIframe() {
	var newIframe = (is_ie) ? document.createElement('<iframe frameborder="0" marginwidth="0" marginheight="0" width="' + doIframe.arguments[1] + '" height="' + doIframe.arguments[2] + '"></iframe>') : document.createElement('iframe');
	if (!is_ie) {
		newIframe.setAttribute('frameborder',0);
		newIframe.setAttribute('marginwidth',0);
		newIframe.setAttribute('marginheight',0);
		newIframe.setAttribute('width',doIframe.arguments[1]);
		newIframe.setAttribute('height',doIframe.arguments[2]);
	}
	newIframe.setAttribute('src',doIframe.arguments[0]);
	newIframe.setAttribute('scrolling','no');
	if (doIframe.arguments[3]) {
		var node = doIframe.arguments[3];
		node.innerHTML = '';
		node.appendChild(newIframe);
		if (doIframe.arguments[4]) {
			node.insertBefore(newIframe,doIframe.arguments[4]);
		}
	}
}

/* ### IMPORT ADS ### */

var bust = Math.floor(1000000*Math.random());
var adLoadDelay = 125;

function doFFXIFrameAd(parentNodeName, ifWidth, ifHeight, ffxAdUrl) {
	var parentNode = $id(parentNodeName);
	if (parentNode) {
		window.setTimeout('doIframe("' + ffxAdUrl + '", ' + ifWidth + ', ' + ifHeight +', ' + '$id("' + parentNodeName + '"))', adLoadDelay);
	}
}

function getFFXAdUrl(ifWidth, ifHeight, otherRestrictors) {
	var localBust = Math.floor(1000000*Math.random());

	initialiseTitles();

	// enforce appropriate case on these tags...
	var primaryTitleText = 'BLOGS';
	var secondaryTitleText = !secondaryTitle || secondaryTitle == null ? 'na' : adTagFormat(secondaryTitle.innerHTML);
	var areaTag = "DATING.RSVP." + primaryTitleText + "." + secondaryTitleText;

	var areaStr = "/AREA=" + areaTag.toUpperCase();
	var catStr = "/CAT=" + primaryTitleText.toUpperCase();
	var siteStr = "/SITE=ONL.CL.RSVP.DATING";
	var subCatStr = "";
	if (secondaryTitleText != 'na')
		subCatStr = "/SUBCAT=" + secondaryTitleText.toUpperCase();

	var otherRestrictorsStr = otherRestrictors ? otherRestrictors : "";
	if (otherRestrictorsStr.length > 0 && !otherRestrictorsStr.indexOf('/') == 0)
		otherRestrictorsStr = "/" + otherRestrictorsStr;

	var fcam_segvars=areaStr + catStr + subCatStr + siteStr + targetting + otherRestrictorsStr + '/AAMSZ=' + ifWidth + 'X' + ifHeight + '/acc_random='+localBust;
	var adUrl = 'http://direct.fairfax.com.au/hserver' + fcam_segvars;

	// for some ad classes, we go via an intermediary file located our own domain, so that we can dynamically resize the IFRAME without cross site scripting issues 
	var useIntermediary = adUrl.indexOf("/ADTYPE=PANORAMA") > -1;
	if (useIntermediary) {
		adUrl = '/adIM.html?ad=' + escape(adUrl.replace('hserver', 'jserver'));
	}
	return adUrl;
}

function adTagFormat(tagStr) {
	// start by treating the same as the menu...
	tagStr = menuFormat(tagStr);
	// next, strip all &amp; etc. characters
	var re = /&(\w+;)/g
	while (tagStr.match(re))
		tagStr = tagStr.replace(re, '');
	// strip all single quotes...
	re = /'/g
	while (tagStr.match(re))
		tagStr = tagStr.replace(re, '');
	// strip all non-alpha-space chars...
	re = /[^\w^\s]+/g
	while (tagStr.match(re))
		tagStr = tagStr.replace(re, '');
	// covert all whitespace down to a single "_" character...
	re = /\s+/g
	while (tagStr.match(re))
		tagStr = tagStr.replace(re, '_');
	return tagStr;
}

/* in a given text string, replaces line breaks and/or line breaks+hyphens, with a space */
function removeLineBreaks(myString) {
	var brs = new Array('<br>','<br/>','<br />','<BR>','<BR/>','<BR />');
	for (var i=0; i<brs.length; i++) {
		var br = '-' + brs[i];
		while (myString.indexOf(br) != -1) {
			myString = myString.replace(br,'');
		}
		while (myString.indexOf(brs[i]) != -1) {
			myString = myString.replace(brs[i],'');
		}
	}
	return myString;
}

/* in a given text string, replaces line breaks and/or line breaks+hyphens, with a space */
function menuFormat(myString) {
	var strings = new Array('<strong>','<STRONG>','</strong>','</STRONG>');
	for (var i=0; i<strings.length; i++) {
		while (myString.indexOf(strings[i]) != -1) {
			myString = myString.replace(strings[i],'');
		}
	}
	return myString;
}

/* in a given text string, removes all leading spaces */
function leftTrim(myString) {
	while (myString.substring(0,1) == ' ') {
		myString = myString.substring(1, myString.length);
	}
	return myString;
}


//var header;
var content;
var footer;
var blog = true;

function setVars() {
	// note - 'header' isn't used anywhere at 150807
	// header = $class('header',document.body,'div')[0];
	content = $id('rsvpcontent');
	footer = $class('footer',document.body,'div')[0];
}

setVars();