var isMSIE = /*@cc_on!@*/false;
var current;
var timeout;

var menus = new Array ( "membership", "insurance", "loans", "travel", "directory", "services", "magazine", "e-news", "card", "forms", "benefits", "onlineShop" );

var addEvent = function() {
    if ( window.addEventListener ) {
        return function(el, type, fn) {
            el.addEventListener(type, fn, false);
        };
    } else if ( window.attachEvent ) {
        return function(el, type, fn) {
            var f = function() {
                fn.call(el, window.event);
            };
            el.attachEvent('on'+type, f);
        };
    } else {
        return function(el, type, fn) {
            element['on'+type] = fn;
        }
    }
}();

function init() {
	if (arguments.callee.done) return;

	arguments.callee.done = true;

	if (_timer) clearInterval(_timer);

	if (initWindow) initWindow();

}

{
var initAttached = false;

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
        initAttached = true;
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=//:></script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init(); // call the onload handler
		}
	};
        initAttached = true;
/*@end @*/

/* for Safari */
if (/KHTML|WebKit/i.test(navigator.userAgent)) // sniff
{ 
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
        initAttached = true;
}

if (!initAttached)
{
    window.onload = init;
}

};

function initWindow(  )
{
    for (var i = 0; i < menus.length; i++)
    {
        addEvent( $(menus[i]), "mousemove", menuOver );
        addEvent( $(menus[i]+"Menu"), "mousemove", cancel );
    }
    //addEvent( $( "benefits" ), "click", benefits );
    //addEvent( $( "benefits2" ), "click", benefits );
    //$( "benefits" ).href = "#";
    //$( "benefits2" ).href = "#";
    addEvent( document, "mousemove", closeMenusSoon );

    setInterval( rotateBanner, 30000 );

}

function createObject( src, width, height )
{

    var parentObj  = document.createElement( "object");
    parentObj.type = "application/x-shockwave-flash";
    parentObj.id   = "banner";
    parentObj.data = src;

    parentObj.width  = width;
    parentObj.height = height;

    var param     = document.createElement( "param" );
    param.name    = "movie";
    param.value   = src;
    param.id      = "bannerParam";

    parentObj.appendChild( param );

    return parentObj;

}

function rotateBanner()
{
    var param     = $( "bannerParam" );
    var banner    = $( "banner" );
    var container = $( "rightContainer" );
    var newSrc  = "";

    switch (param.value)
    {
        case "":
        case "http://www.apilifesmart.com.au/cms/upload_temp/rotate1.swf":
           newSrc = "rotate2.swf";
        break;

        case "http://www.apilifesmart.com.au/cms/upload_temp/rotate2.swf":
           newSrc = "rotate3.swf";
        break;

        case "http://www.apilifesmart.com.au/cms/upload_temp/rotate3.swf":
           newSrc = "rotate4.swf";
        break;

        case "http://www.apilifesmart.com.au/cms/upload_temp/rotate4.swf":
           newSrc = "rotate5.swf";
        break;

	case "http://www.apilifesmart.com.au/cms/upload_temp/rotate5.swf":
           newSrc = "rotate1.swf";
        break;
    }

    if (!isMSIE)
    {
    	container.replaceChild( createObject( "http://www.apilifesmart.com.au/cms/upload_temp/" + newSrc, 597, 76 ), banner );
    }
    else
    {

        container.innerHTML = "<object type=\"application/x-shockwave-flash\" data=\"http://www.apilifesmart.com.au/cms/upload_temp/"+newSrc+"\" id=\"banner\" width=\"597\" height=\"76\"><param name=\"movie\" value=\"http://www.apilifesmart.com.au/cms/upload_temp/"+newSrc+"\" id=\"bannerParam\" /></object>";
    }

    //banner.replaceNode( createObject( "newSrc", 597, 76 ) );
    
}

function cancel( e )
{
    //.stopPropagation();
    if (timeout)
    {
        clearTimeout( timeout );
	timeout = 0;
    }
    e.cancelBubble = true;
    return false;
}

function menuOver( e )
{
    current = this.id;
    if (this.time)
    {
        clearTimeout( this.time );
    }
    if (timeout)
    {
        clearTimeout( timeout );
	timeout = 0;
    }
    this.time    = setTimeout( openMenu, 200 );

    //e.stopPropagation();
    e.cancelBubble = true;
    return false;
}

function getOffsetTop(elm) {

  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetTop;
}

function getOffsetLeft(elm) {

  var mOffsetLeft = elm.offsetLeft;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetLeft;
}

function closeMenusSoon( e )
{
   if (!timeout)
   {
       timeout = setTimeout( closeMenus, 400 );
   }
}

function closeMenus( e )
{
    for (var i = 0; i < menus.length; i++)
    {
        var menu = $( menus[i]+"Menu" );
        menu.style.display = "none";
        menu.style.visibility = "hidden";
    }
   
    
}

function benefits( e )
{
    window.open( "http://apilifesmart.com.au/cms/frame.php?action=content&id=35", "", "menubar=no,location=no,resizable=no,scrollbars=yes,status=yes,width=800,height=555" );
    return false;   
}

function openMenu( e )
{
    closeMenus( null );

    var menu = $( current + "Menu" );
    var insurance = $( current );
    menu.style.top = getOffsetTop( insurance ) + "px";
    menu.style.left = (getOffsetLeft( insurance ) + 185) + "px";
    menu.style.display = "block";
    menu.style.visibility = "visible";
   
}


function $( e )
{
    return document.getElementById( e );
}

function $n( e )
{
    return document.getElementsByTagName( e );
}

function popupThumbnail( e )
{
    window.open( "./html/popup.php?image=" + this.id, "_blank", "menubar=no, location=no, scrollbars=yes, status=no, toolbar=no" );
}

//addEvent( window, "load", load );
