// Custom utility functions

function toTitleCase(str) {
    return str.substr(0,1).toUpperCase() + str.substr(1).toLowerCase();
}      

function formatTitle(title) {
    return 'Välkommen till Evermarks Original, den multikreativa reklambyrån i Karlstad' + (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 index, rel, links = document.getElementsByTagName('a');
    for (var i = 0, l; link = links[i]; i++) {
        index = link.rel.indexOf('?');
        rel = (index > -1) ? link.rel.substr(0, index) : link.rel;
        link.className = rel == event.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));    
}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
