var editorInitialised = false;
var sectionName = '';
$().ready(function(){

    //hidding header and footer
    $('#header').hide();
    $('#footer').hide();

    //convertin each section into editable section

    var section_tags = $().find("*[section]");

    $.each(section_tags,function (k,v){
        var t = $(v).html();
        var sec_type = $(v).attr('section_type');
        var sec_name = $(v).attr('section');
        var sec_id = $(v).attr('section_id');
        switch(sec_type)
        {
            case "text":
                t =  "<div class='edit_bag'><a class='edit_bag_a' href='javascript:void(0);' onclick='showEditBox(\""+sec_name+"\");'>"+buttonText+"</a></div>"+t;
                var parent_div = $(v).attr("class");
                if(parent_div=="verticalAdvanceAreaTextClass")
                {
                    var nh = $(v).css('height');
                    nh =  nh.replace('px','');
                    nh = parseInt(nh);
                    nh +=30;
                    $(v).css({
                        'height':nh
                    });
                }
                $(v).html(t);
                break;

            case "img":
                var src = $('img[section='+sec_name+']').attr('src');
                //                alert($('img[section='+sec_name+']').height());
                var img_height = $('img[section='+sec_name+']').css('height');
                var img_width = $('img[section='+sec_name+']').css('width');
                var img_class=$('img[section='+sec_name+']').attr('class');
                //                alert(src);
                //                alert(img_height);
                //                alert(img_width);
                t = "<div style='background: url("+src+") no-repeat; margin:0px; height:"+img_height+"; width:"+img_width+"' class='"+img_class+"'>";
                t +="<div class='edit_bag'><a class='edit_bag_a' href='javascript:void(0);' onclick='showImageEditBox(\""+sec_name+"\");'>"+buttonPhotoEdit+"</a></div>";
                t +="</div>";
                $($('img[section='+sec_name+']')).replaceWith(t);
                break;

            case 'credit':
                t =  "<div class='edit_bag'><a class='edit_bag_a' href='javascript:void(0);' onclick='showEditBox(\""+sec_name+"\");'>"+buttonCreditEdit+"</a></div>"+t;
                var parent_div = $(v).parent('div[class=verticalPhotoDiv]');
                if(parent_div.length ==0 )
                {
                    parent_div = $(v).parent('div[class=verticalPhotoDivSmall]');
                }
                var ht = $(parent_div).css('height');
                if(ht)
                {
                    ht =  ht.replace('px','');
                    ht = parseInt(ht);
                    ht +=30;
                    $(parent_div).css({
                        'height':ht
                    });
                }
                $(v).html(t);
                break;

            case 'gallery':
                t =  "<div class='edit_bag'><a class='edit_bag_a' href='javascript:void(0);' onclick='showGalleryEditBox(\""+sec_name+"\");'>"+buttonGalleryEdit+"</a></div>"+t;
                var ht = $(v).css('height');
                ht =  ht.replace('px','');
                ht = parseInt(ht);
                ht +=30;
                $(v).css({
                    'height':ht
                });
                $(v).html(t);
                break;

            case 'members':
                t =  "<div class='edit_bag'><a class='edit_bag_a' href='javascript:void(0);' onclick='showMembersEditBox(\""+sec_name+"\",\""+sec_id+"\");'>"+editMemberbuttonText+"</a></div>"+t;
                t =  "<div class='delete_bag'><a class='edit_bag_a' href='javascript:void(0);' onclick='showDeleteMembersEditBox(\""+sec_name+"\",\""+sec_id+"\",\"delMember\");'>"+deleteMemberbuttonText+"</a></div>"+t;
                $(v).html(t);
                break;

            case 'add':
                t =  "<div class='add_bag'><a class='edit_bag_a' href='javascript:void(0);' onclick='showMembersEditBox(\""+sec_name+"\",\"-1\");'>"+addMemberbuttonText+"</a></div>"+t;
                $(v).html(t);
                break;
        }
    });
});

function showEditBox(section_name)
{
    $('#editorButtonDiv').hide();
    $('#editorDiv').hide();
    $('#imageDiv').hide();
    $('#loadingTextSave').html(waitingMessage);
    $('#loadingDiv').show('');
    $('#popup').show();
    //centering with css
    centerPopup();
    loadPopup();
    if(editorInitialised==false){
        showEditor();
    }
    sectionName = section_name;
    loadSectionContent(section_name);
}


function showImageEditBox(section_name)
{
    $('#editorButtonDiv').hide();
    $('#editorDiv').hide();
    $('#imageDiv').show();
    $('#loadingTextSave').html(waitingMessage);
    $('#loadingDiv').show('');
    $('#popup').show();
    centerPopup();
    loadPopup();
    sectionName = section_name;
    loadImageSectionContent(section_name);
}


function showMembersEditBox(section_name,section_id)
{
    $('#editorButtonDiv').hide();
    $('#editorDiv').hide();
    $('#imageDiv').show();
    $('#loadingTextSave').html(waitingMessage);
    $('#loadingDiv').show('');
    $('#popup').show();
    centerPopup();
    loadPopup();
    sectionName = section_name;
    sectionId=section_id;
    loadMembersSectionContent(section_name,section_id);
}

function showDeleteMembersEditBox(section_name,section_id,extra_sec)
{
    $('#editorButtonDiv').hide();
    $('#editorDiv').hide();
    $('#imageDiv').hide();
    $('#loadingTextSave').html(deleteMemberbuttonText);
    $('#loadingDiv').show('');
    $('#popup').show();
    centerPopup();
    loadPopup();
    sectionName = section_name;
    sectionId=section_id;
    deleteMembersSectionContent(section_name,section_id,extra_sec);
}

function showAddMemberEditBox()
{
    $('#editorButtonDiv').hide();
    $('#editorDiv').hide();
    $('#imageDiv').show();
    $('#loadingTextSave').html(waitingMessage);
    $('#loadingDiv').show('');
    $('#popup').show();
    centerPopup();
    loadPopup();
    addMemberSectionContent();
}

function showEditor()
{
    // O2k7 skin (silver)
    tinyMCE.init({
        // General options
        mode : "exact",
        elements : "textEditor,textEditorInfo",
        theme : "advanced",
        skin : "o2k7",
        skin_variant : "black",
        plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",

        // Theme options
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect,|,forecolor,backcolor,|,cut,copy,paste,pastetext,pasteword",
        theme_advanced_buttons2 : "search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,|,hr,removeformat,visualaid,|,sub,sup,|,charmap",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "none",
        theme_advanced_resizing : false,
        force_br_newlines : true,
        forced_root_block : '',
        // Example content CSS (should be your site CSS)
        content_css : "css/content.css," +css_url+"main.css",
        content_type :'',
        width : "100%",
        height: "200px",
        // Drop lists for link/image/media/template dialogs
        template_external_list_url : "lists/template_list.js",
        external_link_list_url : "lists/link_list.js",
        external_image_list_url : "lists/image_list.js",
        media_external_list_url : "lists/media_list.js",

        // Replace values for the template plugin
        template_replace_values : {
            username : "Some User",
            staffid : "991234"
        }
    });

    editorInitialised = true;
}

$().ready(function(){
    
    $("#frmSectionValues").submit(function(){
        $('#editorButtonDiv').hide('slow');
        $('#editorDiv').hide('slow');
        $('#loadingTextSave').html(savingChangesMsg);
        $('#loadingDiv').show('slow');
        var section_data = tinyMCE.activeEditor.getContent();
        saveSection(sectionName, section_data);
        return false;
    });

    $("#frmSectoinValueCancel").click(function(){
        disablePopup();
        return false;
    });
});

function refreshMe()
{
    location.reload(true);
}
function refreshPage()
{
    submitPageToEdit();
}


function submitPageToEdit()
{
    $('#frmMerchandise').submit();
}