var cmsReload;

$(document).ready(function() {

    var $cmsDialog = $('<div></div>')
        .dialog({
        autoOpen: false,
        width: 580,
        height: 600,
        modal: true,
        title: 'CMS InSite-Editor'
    });

    $(".cms_edit").hover(function(e) {
            $(this).parent().next().css({background: 'url(/images/cms/parent_bg.gif)'});
            $(this).parent().next().addClass('cms_editcontent_active');
            $(this).parent().next().children().css({ opacity: '0.5' });
        },
        function(e) {
            $(this).parent().next().css({background: 'none'});
            $(this).parent().next().removeClass('cms_editcontent_active');
            $(this).parent().next().children().css({ opacity: '1' });
        }
    );

    $(".cms_edit").click(function(e) {
        e.preventDefault();
        $cmsDialog.html('<div style="text-align:center;margin-top:50%"><img src="/images/cms/progress.gif" align="center" width="50" height="50" border="0"></div>');
        $cmsDialog.html('<iframe id="externalSite" style="border:0" width="555" height="540" class="externalSite" src="/cms/edit?fields='+$(this).attr('cms_fields')+'&code='+$(this).attr('cms_code')+'&key='+$(this).attr('cms_key')+'&rand='+Math.random()+'" />');
        /*
        $.post('/cms/edit', {
                fields: $(this).attr('cms_fields'),
                code: $(this).attr('cms_code'),
                key: $(this).attr('cms_key')
            }, function(data) {
                $cmsDialog.html(data);
            }
        );
        */
        $cmsDialog.dialog('open');
    });

    cmsReload = function(url,xhr)
    {
        if (xhr) {
            $('#content').load(url);
            $cmsDialog.dialog('close');
            $(document).trigger('ready');
        } else {
            document.location.reload();
        }
    }

});

