var admin = {
    cms: {
        popup: false,
        events: function(firstuse, preview){
            if(firstuse){
                popup({
                    title: "Edit Web Content",
                    html: "<p>To edit web content sections, click on the <img src='"+ base.url +"/"+ base.context+"/img/icons_med/edit.png'/> icon in the upper right corner of a section.  You can browse to other pages as well to find editable content.  When you are finished, click 'Return to Administrator Panel' in the upper right hand of the screen.</p>",
                    height: "90px"
                });
            }
            if(preview){
                $('a').click(function(){
                    alert("All links are disabled in preview mode.");
                    return false;
                });
                $("<div class='cms'>Close Preview Window</div>").click(function(){
                    window.close();
                }).appendTo(document.body);
            }else{
                $("div[rel=editable]").each(function(){
                    $(this).css("min-height","50px");
                    var content = $("<img src='"+ base.context +"/img/icons/24x24/edit.png' class='edit' />").appendTo(this).get(0);
                    $(content).click(function(){
                        var w = window.open('about:blank','Report','width=750,height=630,scrollbars,resizable');
                        var bgcolor = 'a_bgwhite';
                        try{
                            bgcolor = /a_bg[\S]*/gi.exec($(this).parents("div.box").attr("class"))[0];
                        }catch(e){}
                        w.document.location = base.context +
                        "/a/admin/content/edit?id=" +
                        $(this).parent().attr('alt') +
                        "&bg="+bgcolor;
                        w.focus();
                    });
                });
                $("button[rel=editable]").each(function(){
                    var content = $("<img src='"+ base.context +"/img/icons/24x24/edit.png' class='edit' />").appendTo(this).get(0);
                    $(content).click(function(){
                        
                        var w = window.open('about:blank','Report','width=750,height=630,scrollbars,resizable');
                        var bgcolor = 'a_bgwhite';
                        try{
                            bgcolor = /a_bg[\S]*/gi.exec($(this).parents("div.box").attr("class"))[0];
                        }catch(e){}
                        w.document.location = base.context +"/a/admin/content/edit?id=" +
                        $(this).parent().attr('alt') +
                        "&bg="+bgcolor;
                        w.focus();
                    });
                });                
                $("<div class='cms'><strong>You are in Web Content editing mode:</strong> | <a href='" +
                    base.context +
                    "/a/admin/content/web/quit'>Stop Editing?</a><br/>" +
                    "Return To &rsaquo; <a href='"+base.context +"/a/admin/cms/list'>CMS Page List</a> " +
                    "</div>").appendTo(document.body);
            }
            setInterval("admin.cms.pingtimer();",900000);
        },
        pingtimer: function(){
            console.log("pingtimer");
            $.get("/Smartt/a/admin/cms/list");
        }
    },
    editSection: {
        events: function(){
            $("button[name=delete]").click(function(e){
                if($('#published').val()==1){
                    alert("You cannot delete live content. If you need to delete this entry, first publish a different version.");
                }else{
                    $('#delete').val(1);
                    $('#cmsEditorForm').attr('target','_self');
                    $('#cmsEditorForm').submit();
                }
            });
            $("button[name=save]").click(function(e){
                if($('#published').val() == 1){
                    alert("You have modified live content. If you save under the existing name this revision will be published.");
                }
                var name = "New Version";
                try{
                    name = $('#revisions').val().split('|')[1];
                }catch(ex){}
                popup({
                    title: "Save Revision?",
                    html: "Give your revision a name: <br/>(Hint: To 'Save As' new version, supply a new name)",
                    button1: "Save",
                    inputType: "text",
                    inputValue: name,
                    height: "70px",
                    callback: function(x){
                        $('#cmsEditorForm').attr('target','_self');
                        if($('#name').val() != x){
                            $('#published').val(0);
                        }
                        $('#name').val(x);
                        $('#saved').val(1);
                        $('#cmsEditorForm').submit();
                    }
                });
            });
            $("button[name=preview]").click(function(e){
                $('#cmsEditorForm').attr('target','_blank');
                $('#forwardURL').val(parent.document.location);
                $('#cmsEditorForm').submit();
            });
            
            $("button[name=publish]").click(function(e){
                $('#cmsEditorForm').attr('target','_self');
                $('#saved').val(1);
                $('#published').val(1);
                $('#cmsEditorForm').submit();
                window.close();
                opener.document.location.reload();
            });
        },
        load: function(){
            document.location=  base.context +'/a/admin/content/edit?id=' + $('#cms_id').val() + '&v='+ $('#revisions').val().split('|')[0]+'&bg='+$('#bg').val(); 
        }
    },
    manageSubscriberAccounts: {
        events: function() {
            $("tr td img[name=delete]").click(function(e){
                popup({
                    title: "Delete Subscription Account?",
                    html: "This will permanently remove this account. Are you sure you want to do this?",
                    button1: "Delete",
                    height: "60px",
                    callback: function(){

                    }
                });
            });
        }
    },
    techSpotlights: {
        events: function() {
            $("tr td img[name=delete]").click(function(e){
                popup({
                    title: "Delete Technology Spotlight?",
                    html: "This will permanently remove this spotlight. Are you sure you want to do this?",
                    button1: "Delete",
                    height: "60px",
                    callback: function(){

                    }
                });
            });
            $("tr td img[name=wp_delete]").click(function(e){
                popup({
                    title: "Delete Whitepaper(s)",
                    html: "Select a whitepaper you wish to delete and click continue below:",
                    inputType: "select",
                    inputValueNames: ["Whitepaper 1","Whitepaper 2"],
                    button1: "Continue",
                    height: "60px",
                    callback: function(){
                        popup({
                            title: "Delete Whitepaper?",
                            html: "This will permanently remove this whitepaper. Are you sure you want to do this?",
                            button1: "Delete",
                            height: "60px",
                            callback: function(){

                            }
                        });
                    }
                });
            });
        }
    },
    ads: {
        events: function() {
            $("tr td img[name=delete]").click(function(e){
                popup({
                    title: "Delete Advertisement?",
                    html: "This will permanently remove this advertisement. Are you sure you want to do this?",
                    button1: "Delete",
                    height: "60px",
                    callback: function(){

                    }
                });
            });
            $("tr td img[name=view]").click(function(e){
                function adjustLargeImage(img){
                    if(img.width > 800 || img.height > 500){
                        img.width = img.width/2;
                        img.height = img.height/2;
                    }
                    return img;
                }
                var f =  $(this).attr('alt') ;
                var img = new Image();
                img.src = "http://"+base.siteURL+"/cms/smartt/ads/"+f;
                if($.browser.msie){
                    setTimeout(function(){
                        img = adjustLargeImage(img);
                        popup({
                            title: f,
                            height: img.height+10+"px",
                            width: img.width+"px",
                            html: img
                        });
                    },500); //this value may need to be adjusted for ie
                }
                else {
                    img.onload = function(){
                        img = adjustLargeImage(img);
                        popup({
                            title: f,
                            height: img.height+10+"px",
                            width: img.width+"px",
                            html: img
                        });
                    };
                }
            });
        }
    },
    testimonial: {
        events: function() {
            $("tr td img[name=delete]").click(function(e){
                popup({
                    title: "Delete Testimonial?",
                    html: "This will permanently remove this testimonial. Are you sure you want to do this?",
                    button1: "Delete",
                    height: "60px",
                    callback: function(){
                      
                    }
                });
            });
        }
    },
    files: {
        events: function(subscribers) {
            $("tr td img[name=delete]").click(function(e){
                var f =  $(this).attr('alt') ;
                popup({
                    title: "Delete File?",
                    html: "This will permanently remove this file. Are you sure you want to do this?",
                    button1: "Delete",
                    inputValue: $(this).attr('alt'),
                    height: "60px",
                    callback: function(){
                        $('#filename').val(f);
                        $('#deletefileform').submit();
                    }
                });
            });
            $("tr td img[name=view]").click(function(e){
                function adjustLargeImage(img){
                    if(img.width > 800 || img.height > 500){
                        img.width = img.width/2;
                        img.height = img.height/2;
                    }
                    return img;
                }
                var f =  $(this).attr('alt') ;
                var img = new Image();
                if(subscribers) {
                    img.src = $(this).parent().next().next().text();
                } else {
                    img.src = "http://"+base.siteURL+"/cms/smartt/main/"+f;
                }
                if($.browser.msie){
                    setTimeout(function(){
                        img = adjustLargeImage(img);
                        popup({
                            title: f,
                            height: img.height+10+"px",
                            width: img.width+"px",
                            html: img
                        });
                    },500); //this value may need to be adjusted for ie
                }
                else {
                    img.onload = function(){
                        img = adjustLargeImage(img);
                        popup({
                            title: f,
                            height: img.height+10+"px",
                            width: img.width+"px",
                            html: img
                        });
                    };
                }
            });
        }
    }
};