if (typeof(WWDC) == "undefined") WWDC = {};




Event.onDOMReady(function() {
    new Ajax.Request('/wwdc/data/sessions.json', {
        method: 'get',
        evalJSON: 'force',
        onSuccess: function(request) {
            WWDC.SessionsData = request.responseJSON.SessionsData;
            WWDC.Init();
        }
    });
});




WWDC.Local = {
    Descriptions: 'Descriptions',
    Filters: {
        level: {
            'Beginner': { id:'beginner', title:'Beginner' },
            'Intermediate': { id:'intermediate', title:'Intermediate' },
            'Expert': { id:'expert', title:'Expert' }
        },
        focus: {
            'iPhone': { id:'iphone', title:'iPhone' },
            'Mac': { id:'mac', title:'Mac' },
            'IT': { id:'it', title:'IT' }
        },
        type: {
            'Lab': { id:'lab', title:'Lab' },
            'Session': { id:'session', title:'Session' }
        }
    },
    Focus: {
        sort: function(a, b) {
            a = a.toLowerCase();
            b = b.toLowerCase();
            if ((a=='iphone' && b=='it') || (a=='iphone' && b=='mac') || (a=='mac' && b=='it') || (a=='session' && b=='lab')) {
                return -1;
            }
            if ((a=='mac' && b=='iphone') || (a=='it' && b=='mac') || (a=='it' && b=='iphone') || (a=='lab' && b=='session')) {
                return 1;
            }
            return a<b;
        }
    }
}




WWDC.Permutations = function(array) {
    array.sort(WWDC.Local.Focus.sort);

    var permutations = [];
    for (var i=array.length-1; i>=0; i--) {
        for (var j=0; j<permutations.length; j++) {
            permutations[j] = array[i]+permutations[j];
        }
        permutations.push(array[i]);
    }

    permutations = permutations.concat(array);
    permutations = permutations.uniq().compact();
    return permutations;
}




var isLessThanSafari3 = navigator.userAgent.match(/Safari/) && !navigator.userAgent.match(/Version/);
WWDC.UpdateUrl = function(data, evt) {
    if (evt) var link = evt.findElement('a');

    for (name in data) {
        var value = data[name];
        if (!value && link && link.className) {
            value = link.className;
            if (value == 'on') {
                value = 'off';
            } else if (value == 'off') {
                value = 'on';
            }
        }
        if (!value) return;

        // what is the hash right now
        var hash = {};
        if (document.location.hash) var hash = document.location.hash.replace('#', '').toQueryParams();
        if (link && link.href) var hash = link.href.replace(/.*#/, '').toQueryParams();

        // filters add on to the value with a csv
        var addFlag = false;
        for (filter in WWDC.Local.Filters) {
            if (name == filter) {
                addFlag = true;
                break;
            }
        }

        // add the current one to the hash object
        if (addFlag && hash[name]) {
            if (hash[name].match(value)) {
                hash[name] = hash[name].replace(value, '');
            } else {
                hash[name] += ','+value;
            }
            hash[name] = hash[name].strip().replace(/^,/, '').replace(/,$/, '');
        } else {
            hash[name] = value;
        }

        // remove the
        if (hash[name] == '') {
            delete hash[name];
        }

        // turn the object into a string
        var queryString = '';
        for (id in hash) {
            queryString += id+'='+hash[id]+'&';
        }
        queryString = queryString.strip().replace(/\&$/, '');
        if (queryString == '') {
            queryString = '=';
        }

        // set the document.location, or the link href (to prevent safari2 constant loading state)
        if (isLessThanSafari3) {
            if (link) link.href = '#'+queryString;
        } else {
            document.location.hash = queryString;
        }
    }
};
