var currentDeeplink = null;


// 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, deeplink) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
			if(currentDeeplink != null){
				$("#content").fadeOut(600, function(){
					$("#content").html(xhr.responseText);
					$("#content").fadeIn(600);
				});
			}
			else{
				//$("#content").css('display', 'none');
				//$("#content").css('opacity', 0);
				$("#content").html(xhr.responseText);
				$("#content").fadeIn(600);
			}
			
			currentDeeplink = deeplink;

        } else {
            alert('Error: ' + xhr.status + '!');
        }
    }
}

function handleChange(event) {
    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) {
		if(parameters == ''){parameters += "?";}
		else{parameters += '&';}
        parameters += p + '=' + event.parameters[p];
    }
	
	
	var page = event.path;
	
	if(page == "" || page == "/"){page = "home";}

	try{stopSlideshow();}
	catch(e){;}
	
	if(timeouts != undefined){
		for(var i = 0; i < timeouts.length; i ++){
			try{clearTimeout(timeouts[i]);}
			catch(e){}
		}
		timeouts = [];
	}
	
    var xhr = getTransport();
    xhr.onreadystatechange = function() {
        updateChange(xhr, event.path);
    }
	
	xhr.open('get', "_content/" + page + ".php" + parameters, true);
	xhr.send('');	
	
	currentPage = page;
	showSubNav(currentPage);
	pageChanged(event.path + parameters);
}


