// Custom utility functions
function formatTitle(title) {
    return 'Michelberger Hotel' + (title != '/' ? ' - ' + title.substr(1, title.length - 1).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 updateChange(xhr) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var content = document.getElementById('content');
            content.innerHTML = xhr.responseText;
			// reload pagesettings
			init();
        } 
    }
}
function handleChange(event) {

	if(event.path != "/rocket" && event.path != "/video" && event.path != "/gallery" && event.path != "/audioguestbook" && event.path != "/content" && event.path != "/for_rudi.php") 
	
	{
    	var index, rel, links = document.getElementsByTagName('a'), path = event.path;
   		if (path.substr(path.length - 1) != '/') {
    		path += '/';
    	}
    	for (var i = 0, l, link; link = links[i]; i++) {
        	index = link.rel.indexOf('?');
        	rel = (index > -1) ? link.rel.substr(0, index) : link.rel;
        	link.className = (rel == path) ? '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));
	}
	
	if(event.path != "/newsletter") {
		// load script asynchronously
		$.getScript("http://www.michelbergerhotel.com/js/mailchimp_signup.js");
	}

}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
