// Custom utility functions

function toTitleCase(str) {
    return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
}      

function formatTitle(title) {
    return 'ACCA ' + (title != '/' ? ' / ' + toTitleCase(title.substr(1, title.length - 2).replace(/\//g, ' / ')) : '');
}


// Custom SWFAddress and Ajax handling

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}

function appear(content, value) {
    if (typeof value == 'undefined') value = 0;
    if (value > 1) return;
    var property = content.filters ? 'filter' : 'opacity';
    content.style[property] = content.filters ? 'alpha(opacity=' + value*100 + ')' : value;
    setTimeout(function () {appear(content, value + .1)} , 50);
}

function updateChange(xhr) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var content = document.getElementById('content');
            if (com.asual.util.Browser.isIE()) {
                var _content = document.getElementById('_content');
                if (_content == null) {
                    var _content = content.cloneNode(false);
                    _content.id = '_content';
                    _content.style.position = 'absolute';
                    _content.style.top = content.offsetTop;
                    _content.style.left = '50%';
                    _content.style.width = content.offsetWidth;
                    _content.style.marginLeft = -content.offsetWidth/2;
                    document.body.appendChild(_content);
                }
                _content.innerHTML = xhr.responseText;
                content.style.height = _content.offsetHeight;
                appear(_content);                
            } else {
                content.innerHTML = xhr.responseText;
                appear(content);            
            }
        } else {
            alert('Error: ' + xhr.status + '!');
        }
    }
}

function handleChange(event) {
	/*
    var links = document.getElementsByTagName('a');
    for (var i = 0, link; link = links[i]; i++) {
        link.className = link.rel == event.value ? 'selected' : '';
    }
    var parameters = '';
    for (var p in event.parameters) {
        parameters += '&' + p + '=' + event.parameters[p];
    }
    var xhr = getTransport();
    xhr.onreadystatechange = function() {
        updateChange(xhr);
    }
    xhr.open('get', 'datasource.php?swfaddress=' + event.path + parameters, true);
    xhr.send('');
    SWFAddress.setTitle(formatTitle(event.path));    
	*/

	//alert('datasource.php?swfaddress=' + event.path);
	url = 'datasource.php?swfaddress=' + event.path;
	containerid = 'content';
	ajaxpage(url, containerid);
}
function ajaxpage(url, containerid){
	
	var page_request = false;


	if (window.XMLHttpRequest) // if Mozilla, Safari etc
			page_request = new XMLHttpRequest()
		
		else if (window.ActiveXObject){ // if IE

		try {
		
		page_request = new ActiveXObject("Msxml2.XMLHTTP")
			} 
		catch (e){

		try{
			page_request = new ActiveXObject("Microsoft.XMLHTTP")
		}
		catch (e){}
		}
	}
else

	return false	
	page_request.onreadystatechange=function(){
	loadpage(page_request, containerid)
	


}



page_request.open('GET', url, true)
page_request.send(null)  

return false;


}



function loadpage(page_request, containerid){


	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
		document.getElementById(containerid).innerHTML=page_request.responseText
	
		
}


SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);