/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

// Position the pointer image for the FBM network strip
function positionSnookerPointers() {
	var snookerpointers = getElementsByClassName(document, "span", "snookerpointer");
	for(var i=0;i<snookerpointers.length;i++) {
		snookerpointers[i].style.left = ((snookerpointers[i].offsetParent.offsetWidth/2) - (snookerpointers[i].offsetWidth/2)) + "px";
		snookerpointers[i].style.bottom = (0 - (snookerpointers[i].offsetHeight)) + "px";
		snookerpointers[i].style.display = "block";
	}
}

function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

//Need to check for existing array.push in user browser. 
//Use the array.push implementation below it works with Nielsen webstats javascript.
if (!Array.prototype.push)
{
	Array.prototype.push = function()
	{
		for (var i = 0; i < arguments.length; i++)
		{
			this[this.length] = arguments[i];
		}
	};
}

 


// ---
/*
	Examples of how to call the function:
	
	To get all a elements in the document with a "info-links" class:
    getElementsByClassName(document, "a", "info-links");
    
	To get all div elements within the element named "container", with a "col" and a "left" class:
    getElementsByClassName(document.getElementById("container"), "div", ["col", "left"]);
*/

/*
	Site Selection Box code
*/

function showhideSelectionBoxes(boxid) {
	sectionBoxes = getElementsByClassName(document, "div", "sitesectionboxcontent");
	for (var i=0;i<sectionBoxes.length;i++) {	
		if (sectionBoxes[i].id == boxid) {
			sectionBoxes[i].style.display = "block";
			sectionBoxes[i].style.visibility = "visible";
		}
		else {
			sectionBoxes[i].style.display = "none";
		}
	}
}

function selectSiteSection(element) {
	if (element.offsetParent.className != "selected") { 
		elementLink = element.href.split("#");
		sectionBox = elementLink[1];
		pointerCode = '<span class="snookerpointer">&nbsp;<\/span>';
		splitter1 = "<\/a>";
		splitter2 = "<\/A>";
		listElements = getElementsByClassName(document.getElementById("sectionbox1"), "li", "selected");

		for (var i=0;i<listElements.length;i++) {
			listElements[i].className = "";
			splitter1Pos = listElements[i].innerHTML.indexOf(splitter1);
			splitter2Pos = listElements[i].innerHTML.indexOf(splitter2);
						
			if (splitter1Pos >= 0) { splitter = splitter1; }
			else if (splitter2Pos >= 0) { splitter = splitter2; }

			listElementCode = listElements[i].innerHTML.split(splitter);
			listElements[i].innerHTML = listElementCode[0] + splitter;
		}
										
		element.offsetParent.className = "selected"
		element.offsetParent.innerHTML = element.offsetParent.innerHTML + pointerCode;
		positionSnookerPointers();
		showhideSelectionBoxes(sectionBox);					
	}
}

/* FBM Brands Slideshow */

var slideCount;

function initFBMSlideShow() {			
	FBMBoxes = getElementsByClassName(document, "div", "fbmfeaturedbox");
	
	for (slideCount=1;slideCount<FBMBoxes.length+1;slideCount++) {
		FBMBox = document.getElementById("slide" + slideCount);
		if (FBMBox.id != "slide1") {
			if (FBMBox.className == "contentbox fbmfeaturedbox hidden") {
				FBMBox.className = "contentbox fbmfeaturedbox";
				FBMBox.style.display = "none";
			}
		}
	}
	if (slideCount > 1) {
		var curr_frame;
		start_slideshow(1, slideCount-1, 6000);
	}    
}
	
function start_slideshow(start_frame, end_frame, delay) {
	startshowTO = setTimeout(switch_slides(start_frame,start_frame,end_frame, delay), delay);
}
	           
function switch_slides(frame, start_frame, end_frame, delay) {
	return (function() {
    	Effect.Fade('slide' + frame);
	    if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
		curr_frame = frame;
	    setTimeout("Effect.Appear('slide" + frame + "');", 850);
    	switchslidesTO = setTimeout(switch_slides(frame, start_frame, end_frame, delay), delay + 850);
	})
}
	
function prevslide(slidePos) {
	if (isNaN(slidePos) == true) { slidePos = 1; }
	clearTimeout(switchslidesTO);
	clearTimeout(startshowTO);
	if (slidePos > 1) {
		curr_frame = slidePos-1;
	}
	else {
		curr_frame = slideCount-1;
	}
	Effect.Fade('slide' + slidePos);
	setTimeout("Effect.Appear('slide" + curr_frame + "');", 850);
	return false;
}
	
function nextslide(slidePos) {
	if (isNaN(slidePos) == true) { slidePos = 1; }
	clearTimeout(switchslidesTO);
	clearTimeout(startshowTO);
	if (slidePos == slideCount-1) {
		curr_frame = 1;
	}
	else {
		curr_frame = slidePos+1;
	}
	Effect.Fade('slide' + slidePos);
	setTimeout("Effect.Appear('slide" + curr_frame + "');", 850);
	return false;
}

/* rotating divs */

/*function rotate() {
  if( nextprevhit == 0 ) {	
  var delay = 7;
  if (!document.getElementById) {
    return;
  }
  var divToRotate = document.getElementById("fbmfeaturedboxcontainer");
  var divs = getElementsByClassName(document, "div", "fbmfeaturedbox");
  var validDivs = 0;
  for (var i=0; i < divs.length; i++ ) {
    var div = divs[i];
    if ( (div.id != "") && (div.nodeType == 1) ) {
        div.style.display = "none";
		validDivs++;
    }
  }
  if (rotationCount > validDivs) {
        rotationCount = 1;
  }
  if (rotationCount > 0) {
    var divIdToReveal  = rotationCount;
    var divToRevealObj = document.getElementById("slide"+divIdToReveal);
	fadeToDiv(divToRevealObj);
    rotationCount++;
  } else {
    var randDivId = Math.ceil( Math.random() * (validDivs) );
	var firstDivToRevealObj = document.getElementById("slide"+randDivId);
    firstDivToRevealObj.style.display = "block";
	rotationCount = randDivId;
	rotationCount++;
  }
  setTimeout('rotate()', delay*1000);
  }
}
function fadeToDiv(divToRevealObj) {
  setOpacity(divToRevealObj.id, 0);
  divToRevealObj.style.display = "block";
  fadeInDiv(divToRevealObj.id,0);
}
function fadeInDiv(divId, opacity) {
  if (opacity <= 100) {
	setOpacity(divId, opacity);
	opacity += 5;
	window.setTimeout("fadeInDiv('"+divId+"',"+opacity+")", 100);
  }
}
function setOpacity(divId, opacity) {
  var div = document.getElementById(divId);
  opacity = (opacity == 100)?99.999:opacity;
  div.style.filter = "alpha(opacity:"+opacity+")";
  div.style.KHTMLOpacity = opacity/100;
  div.style.MozOpacity = opacity/100;
  div.style.opacity = opacity/100;
}
function nextslide() {
  var delay = 7;
  if (!document.getElementById) {
    return;
  }
  var divToRotate = document.getElementById("fbmfeaturedboxcontainer");
  var divs = getElementsByClassName(document, "div", "fbmfeaturedbox");
  var validDivs = 0;
  for (var i=0; i < divs.length; i++ ) {
    var div = divs[i];
    if ( (div.id != "") && (div.nodeType == 1) ) {
        div.style.display = "none";
                validDivs++;
    }
  }
  rotationCount++;
  if (rotationCount > validDivs) {
        rotationCount = 1;
  }
  if (rotationCount > 0) {
    var divIdToReveal  = rotationCount;
    var divToRevealObj = document.getElementById("slide"+divIdToReveal);
        fadeToDiv(divToRevealObj);
  } else {
    var randDivId = Math.ceil( Math.random() * (validDivs) );
        var firstDivToRevealObj = document.getElementById("slide"+randDivId);
    firstDivToRevealObj.style.display = "block";
        rotationCount = randDivId;
  }
  nextprevhit = 1;
}
function prevslide() {
  var delay = 7;
  if (!document.getElementById) {
    return;
  }
  var divToRotate = document.getElementById("fbmfeaturedboxcontainer");
  var divs = getElementsByClassName(document, "div", "fbmfeaturedbox");
  var validDivs = 0;
  for (var i=0; i < divs.length; i++ ) {
    var div = divs[i];
    if ( (div.id != "") && (div.nodeType == 1) ) {
        div.style.display = "none";
                validDivs++;
    }
  }
  if(nextprevhit == 0) {
	  rotationCount--;
  }
  rotationCount--;
  if (rotationCount < 1) {
        rotationCount = validDivs;
  }
  if (rotationCount > 0) {
    var divIdToReveal  = rotationCount;
    var divToRevealObj = document.getElementById("slide"+divIdToReveal);
        fadeToDiv(divToRevealObj);
  } else {
    var randDivId = Math.ceil( Math.random() * (validDivs) );
        var firstDivToRevealObj = document.getElementById("slide"+randDivId);
    firstDivToRevealObj.style.display = "block";
        rotationCount = randDivId;
  }
  nextprevhit = 1;
}*/

/* Smooth Scrolling */

var ScrollLinks = {
    currentHash: false,
    start: function(){
        this.scroll = new fx.Scroll({duration: 800, onComplete: function(){ScrollLinks.end();}});
		this.allinks = document.getElementsByTagName('a');
        for (i=0; i<this.allinks.length; i++){
            var lnk = this.allinks[i];
			if ((lnk.href.indexOf('ss_') == -1) && (lnk.href.indexOf('mb_') == -1)) {
            if ((lnk.href && lnk.href.indexOf('#') != -1) && ( (lnk.pathname == location.pathname) || ('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {
                lnk.onclick = function(){
                    ScrollLinks.scroll.clearTimer();
                    this.initialHref = this.href;
                    this.initialHash = this.hash;
                    this.href = "javascript:void(0)";
                    setTimeout(function(){this.href = this.initialHref;}.bind(this), 200);
                    ScrollLinks.click(this);
                }
            }
			}
        }
    },

    click: function(link){
        this.currentHash = link.initialHash.substr(1);
        if (this.currentHash) {
            for (j=0; j<this.allinks.length; j++){
                if (this.allinks[j].id == this.currentHash){
                    if (!window.opera) this.scroll.scrollTo(this.allinks[j]);
                    else this.scroll.scrollTo(this.allinks[j].parentNode);
                    break;
                }
            }
        }
    },

    end: function(){
        window.location.href = "#"+this.currentHash;
        this.currentHash = false;
    }
}

/* Markets Box Selections */

function showhideMarketsBoxes(boxid) {
	sectionBoxes = getElementsByClassName(document, "div", "innerboxes");
	for (var i=0;i<sectionBoxes.length;i++) {
		
		if (sectionBoxes[i].id == boxid) {
			sectionBoxes[i].style.display = "block";
			sectionBoxes[i].style.visibility = "visible";
		}
		else {
			sectionBoxes[i].style.display = "none";
		}
	}
}

function selectMarketsBoxes(element) {
	if (element.offsetParent.className != "current") { 
		elementLink = element.href.split("#");
		sectionBox = elementLink[1];
		listElements = getElementsByClassName(document.getElementById("headinglinks"), "li", "current");

		for (var i=0;i<listElements.length;i++) {
			listElements[i].className = "";
		}
										
		element.offsetParent.className = "current"
		element.offsetParent.innerHTML = element.offsetParent.innerHTML;
		showhideMarketsBoxes(sectionBox);					
	}
}
// Controls the Network sites navigation select box
function surfto_networksites(site) {
	if (site == 10) {location.href= 'http://www.investsmart.com.au/';}
	if (site == 11) {location.href= 'http://www.smh.com.au/';}
	if (site == 12) {location.href= 'http://www.theage.com.au/';}
	if (site == 13) {location.href= 'http://www.tradingroom.com.au/';}
}