
// Custom utility functions

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

function click() {
    SWFAddress.setValue(this.rel);
    this.blur();
    return false;
}

function mouseOver() {
    MM_swapImage(this.rel.replace(/\//g, ''),'','images/menu_' + this.rel.replace(/\//g, '') + '-hover.gif',1);
}

function mouseOut() {
    //MM_swapImageRestore();
    var ima = document.getElementById(this.rel.replace(/\//g, ''));
    ima.src = 'images/menu_' + this.rel.replace(/\//g, '') + '-link.gif';
}

function formatTitle(title) {
    title = title.replace(/\//g, '');
    return 'Kristiansand baseballklubb Suns - '+toTitleCase(title);
}

// 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;
    var property = content.filters ? 'filter' : 'opacity';
    if (value == 100) {
        if (content.style.removeAttribute) {
            content.style.removeAttribute(property);
        } else {
            content.style[property] = 1;
        }
    } else {
        content.style[property] = content.filters ? 'alpha(opacity=' + value + ')' : value/100;
        setTimeout(function () {appear(content, value + 20)}, 50);
    }
}

function updateChange(xhr,div,update) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            var content = document.getElementById(div);
            content.innerHTML = xhr.responseText;
            if(update==1) {
              appear(content); }
            /*var links = document.getElementById('social').getElementsByTagName('a');
            for (var i = 0, link, parts, pairs; link = links[i]; i++) {
                parts = link.href.split('?');
                pairs = parts[1].split('&');
                for (var j = 0, params; j < pairs.length; j++) {
                    params = pairs[j].split('=');
                    if (params[0] == 'url' || params[0] == 'u' || params[0] == 'bkmk')
                        pairs[j] = params[0] + '=' + SWFAddress.getBaseURL() + SWFAddress.getValue();
                    if (params[0] == 'title' || params[0] == 't')
                        pairs[j] = params[0] + '=' + SWFAddress.getTitle();'

                }
                link.href = parts[0] + '?' + pairs.join('&');
            }
            */
        } else {
            alert('Error: ' + xhr.status + '!');
        }
    }
}

function updateChangeBg(xhr) {
    if (xhr.readyState == 4) {
        if (xhr.status == 200) {
            document.body.style.backgroundImage="url('images/bakgrunnsbilder/"+xhr.responseText+"')";
        } 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 = '';
    var param_array = new Array();
    for (var p in event.parameters) {
        parameters += '&' + p + '=' + event.parameters[p];

    }
    if(event.path=='/') {
        event.path='/aktuelt/';
    }

    // change the menu
    var id = event.path.replace(/\//g, '')+'_link';
    var link = document.getElementById(id);
    link.onclick = '';
    link.onmouseover = '';
    link.onmouseout = '';
    var im = document.getElementById(event.path.replace(/\//g, ''));
    im.src = 'images/menu_'+event.path.replace(/\//g, '')+'-active.gif';
    im.height = 32;
    //restore old image-link
    var old_swf = document.getElementById('info_div');
    if(old_swf.title!='_blank' && old_swf.title!=event.path.replace(/\//g, '') ) {
        var old_im = document.getElementById(old_swf.title);
        old_im.height = 27;
        old_im.src = 'images/menu_'+old_swf.title+'-link.gif';
        var old_id = old_swf.title+'_link';
        var old_link = document.getElementById(old_id);
        old_link.onmouseover = mouseOver;
        old_link.onclick = click;
        old_link.onmouseout = mouseOut;
        old_link.rel = '/'+old_swf.title+'/';
    }
    old_swf.title = event.path.replace(/\//g, '');
    
    // change the content
    var xh1 = getTransport();
    xh1.onreadystatechange = function() {
        updateChange(xh1,'mainContent',1);
      }
    xh1.open('get', 'datasource.php?div=mainContent&swfaddress=' + event.path + parameters, true);
    xh1.send('');
    
    var xh2 = getTransport();
    xh2.onreadystatechange = function() {
        updateChange(xh2,'header',0);
      }
    xh2.open('get', 'datasource.php?div=header&swfaddress=' + event.path + parameters, true);
    xh2.send('');
    
    /*if(event.path.replace(/\//g, '') == 'aktuelt') {
      document.getElementById('banner').style.height = '329px';
      //document.getElementById('banner').innerHTML = '<img src="images/bannere/header_aktuelt.jpg" width="886" height="320" alt="aktuelt" />';
      document.getElementById('kampprogram').style.height = '329px';
      //document.getElementById('kampprogram').innerHTML = '<img src="images/bannere/header_aktuelt.jpg" width="886" height="320" alt="aktuelt" />';
      document.getElementById('treningstider').style.height = '329px';
      //document.getElementById('treningstider').innerHTML = '<img src="images/bannere/header_aktuelt.jpg" width="886" height="320" alt="aktuelt" />';

    } else {
      document.getElementById('banner').innerHTML = '&nbsp';
      document.getElementById('banner').style.height = '0px';

      document.getElementById('kampprogram').style.backgroundImage = 'none';
      //document.getElementById('kampprogram').innerHTML = '&nbsp';
      document.getElementById('kampprogram').style.height = '0px';
      document.getElementById('kampprogram').style.backgroundPosition = '0px';
      //document.getElementById('kampprogram').style.visibility = 'hidden';
      document.getElementById('treningstider').style.backgroundImage = 'none';
      //document.getElementById('treningstider').innerHTML = '&nbsp';
      document.getElementById('treningstider').style.height = '0px';
      //document.getElementById('treningstider').style.visibility = 'hidden';
      document.getElementById('header').style.paddingBottom = '9px';

    }                                                                 */
    
    //var DivHeight = document.getElementById('header').offsetHeight; //329
    //alert(DivHeight);
    
    var xh3 = getTransport();
    xh3.onreadystatechange = function() {
        updateChangeBg(xh3);
      }
    xh3.open('get', 'datasource.php?div=bg&swfaddress=' + event.path + parameters, true);
    xh3.send('');


    SWFAddress.setTitle(formatTitle(event.path));
}

function copyLink() {
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('Text', SWFAddress.getBaseURL() + SWFAddress.getValue());
    } else {
        alert('Unsupported browser.');
    }
}

// in_Array
function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array
    //
    // version: 911.718
    // discuss at: http://phpjs.org/functions/in_array    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {                return true;
            }
        }
    }
     return false;
}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
