function addWidget(widgetClass)
{
    $.post("/admin/theme/Ajax", { action: "add", widget: widgetClass },

    function(data){

        window.location = '/admin/widgets';
    });
}


function removeWidget(widgetID)
{
    if(confirm('Are you sure you wish to remove this widget and delete all associated data?')) {
        $('#cc_enabled-widget-' + widgetID).hide();

        $.post("/admin/theme/Ajax", { action: "remove", widget: widgetID },
        function(data){
            //add back into available
            $('#cc_available-widgets').html(data);
        });
    }
}

function deleteBanner(bannerID)
{
    if(confirm('Are you sure you wish to delete this banner?')) {
        $.post("/admin/theme/Ajax", { action: "delete-banner", banner: bannerID },

        function(data){
            $('#banner-' + bannerID).hide('slow');
        });
    }
}

function hoverWidget(widgetID)
{
    var y = findPositionWithScrolling(document.getElementById('cc_widget' + widgetID))[1];

    console.log(y);

    if(y < 45) {
        $('#cc_widget-bubble' + widgetID).css('margin-top', '0px');
    }

    $('#cc_widget-bubble' + widgetID).show();
    $('#cc_widget-bubble' + widgetID).css('cursor', 'pointer');
    $('#cc_widget-bubble' + widgetID).css('z-index', '10');

}

function findPosition( oElement ) {
    if( typeof( oElement.offsetParent ) != 'undefined' ) {
        for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        return [ posX, posY ];
    } else {
        return [ oElement.x, oElement.y ];
    }
}

function findPositionWithScrolling( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    var originalElement = oElement;
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
      if( oElement != originalElement && oElement != document.body && oElement != document.documentElement ) {
        posX -= oElement.scrollLeft;
        posY -= oElement.scrollTop;
      }
    }
    return [ posX, posY ];
  } else {
    return [ oElement.x, oElement.y ];
  }
}

function unhoverWidget(widgetID)
{
    $('#cc_widget-bubble' + widgetID).hide();
}

function loadTemplate(select)
{
    selectedValue = select.options[select.selectedIndex].value;

    if(selectedValue != 0) {
        $.post("/admin/theme/Ajax", { action: "load-template", template: selectedValue },

        function(data){
            document.getElementById('html').value = data;
        });
    }
}

function deleteTheme(themeID)
{
    $.post("/admin/theme/Ajax", { action: "delete-theme", theme: themeID },

    function(data){
        $('#theme' + themeID).hide('slow');
    });

}