var landingPages = {
    'overview':     'at_a_glance.xhtml',
    'faculty':      'academic.xhtml',
    'partners':     'program_partners.xhtml',
    'locations':    'munich.xhtml',
    'admission':    'application.xhtml',
    'students':     'alumni.xhtml',
    'team':         'program.xhtml',
    'media':        'pressreleases.xhtml'
};

var flashItems = {
    'overview':     'overview.swf',
    'faculty':      'faculty.swf',
    'partners':     'partners.swf',
    'locations':    'locations.swf',
    'admission':    'admission.swf',
    'students':     'our_students.swf',
    'team':         'team.swf',
    'media':        'media.swf'
};

var headerImages = {
    'default':      'overview.jpg',
    'overview':     'overview.jpg',
    'faculty':      'faculty.jpg',
    'partners':     'partners.jpg',
    'locations':    'locations.jpg',
    'admission':    'admission.jpg',
    'students':     'students.jpg',
    'team':         'team.jpg',
    'media':        'media.jpg'
};

// Mapping between groups and navigation links
// Image file name is "<group name>_<key>.jpg"
// null value for link target inserts image without hyperlink
var navigationItems = {
    'overview':     {
        '01':  null,
        '02':  'at_a_glance.html',
        '03':  'learning_modules.html',
        '04':  'project_modules.html',
        '05':  'silicon_valley.html',
        '06':  null
    },
    'faculty':      {
        '01':   null,
        '02':   'academic.html',
        '03':   'corporate.html',
        '04':   'scientific.html',
        '05':   null
    },
    'partners':     {
        '01':   null,
        '02':   'program_partners.html',
        '03':   'tum.html',
        '04':   'unternehmertum.html',
        '05':   'hhl.html',
        '06':   'berkeley.html',
        '07':   'mckinsey.html',
        '08':   null
    },
    'locations':    {
        '01':   null,
        '02':   'munich.html',
        '03':   'silicon_valley.html',
        '04':   null
    },
    'admission':    {
        '01':   null,
        '02':   'application.html',
        '03':   'fees.html',
        '04':   null
    },
    'students':     {
        '01':   null,
        '02':   'alumni.html',
        '03':   'projects.html',
        '04':   'impressions.html',
        '05':   'profiles.html',
        '06':   null
    },
    'team':         {
        '01':   null,
        '02':   'program.html',
        '03':   'advisors.html',
        '04':   null
    },
    'media':        {
        '01':   null,
        '02':   'pressreleases.html',
        '03':   'clippings.html',
        '04':   null
    }
};

var defaultNavItem = "bar.jpg";

function disableFlash(){
    document.cookie = 'noflash=true;';
    window.location.reload();
}

function enableFlash(){
    document.cookie = 'noflash=false;';
    window.location.reload();
}

function isFlashEnabled(){
    if (document.cookie){
        var a = document.cookie.split('; ');
        for (var i = 0; i< a.length; i++){
            var b = a[i].split('=');
            if (b[0] == 'noflash'){
                if (b[1] == 'true'){
                    return false;
                }
            }
        }
    }

    return true;
}

// Returns the base name for a view-id
// Ex: /foo/bar.xhtml -> bar.html
function basename(path) {
    if (!path) {
        return '';
    }
    return path.split('/').reverse()[0];
}

// Returns the first-level navigation group for a view-id
// Ex: /foo/bar.xhtml -> foo
function navGroup(path) {
    if (!path) {
        return '';
    }
    return path.split('/').reverse()[1];
}

function adjustPath(path){
    if(!path){
        return '';
    }
    var pathArray = path.split('.');
    return pathArray[0].concat(".x",pathArray[1]);
}

function highlightNavigation(item) {
    var element = jQuery(item).children('img');
    var src = jQuery(element).attr('src');
    jQuery(element).attr('src', src.replace(/[.]([^.]+)/, "_over.$1"));
}

function unhighlightNavigation(item) {
    var element = jQuery(item).children('img');
    var src = jQuery(element).attr('src');
    jQuery(element).attr('src', src.replace("_over.", "."));
}
               
function buildNavigation(group) {
    if (navigationItems[group] == undefined) {
        jQuery("#nav_links").append("<div style='float:left;'><img style='width: 896px;' src='" + NAV_IMAGE_PATH + defaultNavItem + "' alt='' /></div>");
        return;
    }

    for (var key in navigationItems[group]) {
        var item = navigationItems[group][key];
        var image = NAV_IMAGE_PATH + group + '/' + group + '_' + key + '.jpg';

        if (item != null) {
            jQuery("#nav_links").append("<div style='background-image:url("+image+");float:left;'><a href='" + item + "'><img src='" + image + "' alt='' /></a></div>");
        } else {
            jQuery("#nav_links").append("<div style='float:left;'><img src='" + image + "' alt='' /></div>");
        }
    }
}
