/*******************************************************************************
	Popup Window openers
*******************************************************************************/

var winReference = null;

// Open a window at a given position on the screen
function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {

	// ie 4.5 and 5.0 mac - windows are 2 pixels too short; if a statusbar is used, the window will be an additional 15 pixels short
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("mac") != -1 && agent.indexOf("msie") != -1 && (agent.indexOf("msie 4") != -1 || agent.indexOf("msie 5.0") != -1) ) {
		height += (status) ? 17 : 2;
	}

	// Adjust width if scrollbars are used (pc places scrollbars inside the content area; mac outside)
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;

	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
	var reference = openWindow(url, name, properties, openerName);

	return reference;
}


// Open a window at the center of the screen
function openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
	   y = (screen.availHeight - height) / 2;
   }
	if (!status) status = '';
	if (!openerName) openerName = '';
	var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
	return reference;
}


// Open a window at the center of the parent window
function openCenteredOnOpenerWindow(url, name, width, height, status, scrollbars, moreProperties, openerName) {
	var centerX = 0;
   var centerY = 0;
   if (window.screenX != null && window.outerWidth) {
      centerX = window.screenX + (window.outerWidth / 2);
      centerY = window.screenY + (window.outerHeight / 2);
   }
   else if (window.screenLeft) {
      if (document.documentElement) {
         centerX = window.screenLeft + (document.documentElement.offsetWidth / 2);
         centerY = window.screenTop + (document.documentElement.offsetHeight / 2);
      }
      else if (document.body && document.body.offsetWidth) {
         centerX = window.screenLeft + (document.body.offsetWidth / 2);
         centerY = window.screenTop + (document.body.offsetHeight / 2);
      }
   }

   if (centerX == 0) {
      openCenteredWindow(url, name, width, height, status, scrollbars, moreProperties, openerName);
   }
   var x = parseInt(centerX - (width / 2));
   var y = parseInt(centerY - (height / 2));
	if (!status) status = '';
	if (!openerName) openerName = '';
	var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
	return reference;
}


// Open a full-screen window (different from IE's fullscreen option)
function openMaxedWindow(url, name, scrollbars, openerName) {
	var x, y = 0;
	var width  = 600;
	var height = 800;
	if (screen) {
      if (screen.availLeft) {
         x = screen.availLeft;
         y = screen.availTop;
      }
      width  = screen.availWidth - 6;
	   height = screen.availHeight - 29;
   }
	var reference = openPositionedWindow(url, name, width, height, x, y, false, scrollbars, openerName);
	return reference;
}


// Open a full-chrome (all GUI elements) window
// This is like using a target="_blank" in a normal link but allows focussing the window
function openFullChromeWindow(url, name, openerName) {
	return openWindow(url, name, 'directories,location,menubar,resizable,scrollbars,status,toolbar');
}


// Open a sized full-chrome (all GUI elements) window
function openSizedFullChromeWindow(url, name, width, height, openerName) {
	return openCenteredWindow(url, name, width, height, true, true, 'directories,location,menubar,resizable,toolbar', openerName)
}


// Core utility function that actually creates the window and gives focus to it
function openWindow(url, name, properties, openerName) {

	// ie4.x pc can't give focus to windows containing documents from a different domain
	// in this case, initially load a local interstisial page to allow focussing before loading final url
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) == 4 && agent.indexOf("msie 5") == -1 && agent.indexOf("msie5") == -1 && agent.indexOf("win") != -1 && url.indexOf('http://') == 0) {
		winReference = window.open('about:blank', name, properties);

		setTimeout('if (winReference && !winReference.closed) winReference.location.replace("' + url + '")', 300);
	}
	else {
		winReference = window.open(url, name, properties);
	}

	// ie doesn't like giving focus immediately (to new window in 4.5 on mac; to existing ones in 5 on pc)
	setTimeout('if (winReference && !winReference.closed) winReference.focus()', 200);

	if (openerName) self.name = openerName;
	return winReference;
}

// Close a dialog
// Call from onunload event handler of any page that can create a dialog
function closeDialog(dialog) {
	if (dialog && dialog.closed != true) dialog.close();
}


// Close parent popup
// Call from onload event handler of any page that could be created from a dialog
function closeParentDialog() {
	if (top.opener && isWindowPopup(top.opener)) {
		root = top.opener.top.opener;
		top.opener.close();
		top.opener = root;
	}
}


// Check if a window is a popup
function isWindowPopup(win) {
	return ((win.opener) ? true : false);
}

/*******************************************************************************
	AUTO PAGE FUNCTION
*******************************************************************************/

//STANDARD WIDGET
function showautopage(pageid,footerid,colour) {
	hidePages();
	var mypage = document.getElementById(pageid);
	mypage.style.display = "block";

	var myfooter = document.getElementById(footerid);
	myfooter.style.color = colour;

}

function showpage(page,count){
        hideOther(count);
        document.getElementById(page).style.display = "block";
}

function hideOther(count){

         var p = 1;

         while (p <= count){
             try {
                 document.getElementById('autopage_'+p).style.display = "none";
             } catch(e) {
                 //do nothing
             }
             p++;
         }
}

/*******************************************************************************
	IMAGE PRELOADER
*******************************************************************************/

function preloadImages(AllImages) {

	for(loadall=0; loadall<AllImages.length; loadall++)
	{
		var LoadedImage=new Image();
		LoadedImage.src=AllImages[loadall];
	}

}

/*******************************************************************************
	MOO LOADER
*******************************************************************************/

/* slider
*************************************/

var slidefx = new Array;
var slidercount = 0;
function toggleReveal(i,img){
    var myImage = document.getElementById(img);
    var plus = myImage.src;
    var minus = myImage.src;
    plus = plus.replace("/minus.gif","/plus.gif");
    minus = minus.replace("/plus.gif","/minus.gif");
    var str = myImage.src;
    var pos = str.match("/images/stock/plus.gif");
    if (pos != null)
        myImage.src = minus;
    else
        myImage.src = plus;

        slidefx[i].toggle();
}

var textChange;
var textMode = "small";
window.onload=function(){

	/* setup dragable
    *************************************/
	try{
	    var draggables = $S('.drag');
	    draggables.each(function(el){
	    el.makeDraggable({handle: el.getElementsBySelector('.handle')[0]});
	    el.makeResizable({handle: el.getElementsBySelector('.resize')[0]});
        });
    } catch(e){}

    /* fontsize
    *************************************/
        textChange = new fx.Style('textContent', 'font-size', {unit: 'em'});

    /* setup tooltips
    *************************************/
	 var myTips = new Tips($S('.toolTipImg'), {
         maxTitleChars: 50,
         maxOpacity: .9
    });
    
    try {
    document.getElementById('spid').value='+61';
    } catch(e){}
}

function blisFontChanger(targ){
    if (textMode == "small"){
        textChange.custom(1,1.2);
        textMode = "large";
    } else {
        textChange.custom(1.2,1);
        textMode = "small";
    }
}

/*******************************************************************************
	AJAX FORM
*******************************************************************************/

var old,head,scrNum=0;

function subscriberpc(str){
 head=document.getElementsByTagName('head').item(0);
 script=document.createElement('script');
 script.id='scr'+scrNum;
 script.src='/ccmail/subscriptions2.php?'+str+'&s='+script.id;
 script.type='text/javascript';
 script.defer=true;
 scrNum++
 void(head.appendChild(script));
}

function formrpc(str){
 head=document.getElementsByTagName('head').item(0);
 script=document.createElement('script');
 script.id='scr'+scrNum;
 script.src='/common/public/submit.php?'+str+'&s='+script.id;
 script.type='text/javascript';
 script.defer=true;
 scrNum++
 void(head.appendChild(script));
}

function removeScript(scriptid){
 old=document.getElementById(scriptid);
 if(old)head.removeChild(old);
}

function subscribeToMailingList(){
    /*var addressform = document.getElementById('subscriptionform');
    var address = addressform.emailaddress.value;
    subscriberpc('func=subscribe&action=subscribe&address=' + address);*/
}

function sendFormData(){
    alert('debug-a');
    formrpc('func=form');
    return false;
}

/*******************************************************************************
	DOM READY
*******************************************************************************/

Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);

    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);

      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);

        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady();
            };
        /*@end @*/

        if (/WebKit/i.test(navigator.userAgent)) {
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady();
          }, 10);
        }

        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});