var selectWidget = function (widgetId)
{
    // Remove all selections
    $('#widgetList .selected').removeClass('selected');
        
    // Select the widget
    $('#widget' + widgetId).addClass('selected');
    
    // Load the widget
    if ( $.browser.msie && $.browser.version < 9 ) {
        var parent = $("#widgetFrame").parent();
        $('#widgetFrame').remove();
        parent.append("<iframe frameborder=\"0\" scrolling=\"no\" allowtransparency=\"true\" id=\"widgetFrame\" src=\"" + mariaSkeleton_jsVars['widgetServer'] + '#'+widgetId+'/microsite'+"\" width=\"320\" height=\"415\"></iframe>");
    } else {
        $('#widgetFrame').attr("src",mariaSkeleton_jsVars['widgetServer']+'#'+widgetId+'/microsite');
    }
    
    //set href
    location.href = "#"+widgetId;
    
    return false;
}

var widgetClicked = function (widgetId)
{
    selectWidget(widgetId);
    
    return false;
}

$(document).ready(function()
{
    // Check whether a hashcode could be found
    var hash = location.hash;
    
    // Check whether the hashcode is not empty
    if (hash.length > 0)
    {
        // Remove useless stuff
        hash = hash.replace('#', '');
        
        tryOpenFooterOverlay(hash);
    }
    
    //if the hash points to a widget that is not listed here -> move on to an existing widget..
    if (hash.length == 4 && $("#widget"+hash).length != 1) {
        hash = "";
    }
    
    // Load the first widget
    if (hash.length != 4)
    {
        
        var widgetId = '';
        
        // Get the first widget id
        if ($('#widgetList li:first-child').length > 0) {
            widgetId = $('#widgetList li:first-child').attr('id').replace('widget', '');
        }
        
        // Append the id
        hash = widgetId;
    }
        
    // Select the widget
    selectWidget(hash);
    
    
    startHashSupervisor();
});

