$(document).ready(function() {
    
    WikiPage.ready();

    //changed($("#colors")[0].value);
    //option 11
    //apply(["#282828", "#EEEEEE", "#8C0009", "#000000", "#703511"], i);

});


var WikiPage = {

    ready: function() {

        var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;

        var myConfig = {
            height: '100%',
            width: '100%',
            animate: true,
            dompath: true,
            focusAtStart: true
        };

        var myEditor = new YAHOO.widget.Editor('id_content', myConfig);

        $("#toggleEditor").click(function() {
            toggleEditor();
            $("#toggleEditor").toggle();
            $("#content").toggle();
            $("#wikiform").toggle();
        });

        $("#save").click(function() {
            toggleEditor();
            $("#toggleEditor").toggle();
            $("#content").html(myEditor.getEditorHTML());
            $("#content").toggle();
            $("#wikiform").toggle();
        });

        var state = false;
        function toggleEditor() {
            if (state) {
                //turn it off
                state = !state;
                myEditor.saveHTML();
                //YAHOO.log('Save the Editors HTML', 'info', 'example');
                var stripHTML = /<\S[^><]*>/g;
                myEditor.get('textarea').value = myEditor.getEditorHTML(); //myEditor.get('textarea').value.replace(/<br>/gi, '\n').replace(stripHTML, '');
                //YAHOO.log('Strip the HTML markup from the string.', 'info', 'example');
                //YAHOO.log('Set Editor container to position: absolute, top: -9999px, left: -9999px. Set textarea visible', 'info', 'example');
                Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position', 'absolute');
                Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top', '-9999px');
                Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left', '-9999px');
                myEditor.get('element_cont').removeClass('yui-editor-container');
                Dom.setStyle(myEditor.get('element'), 'visibility', 'visible');
                Dom.setStyle(myEditor.get('element'), 'top', '');
                Dom.setStyle(myEditor.get('element'), 'left', '');
                Dom.setStyle(myEditor.get('element'), 'position', 'static');
            } else {
                //turn it on
                myEditor.render();
                state = !state;
                //YAHOO.log('Set Editor container to position: static, top: 0, left: 0. Set textarea to hidden', 'info', 'example');
                Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'position', 'static');
                Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'top', '0');
                Dom.setStyle(myEditor.get('element_cont').get('firstChild'), 'left', '0');
                Dom.setStyle(myEditor.get('element'), 'visibility', 'hidden');
                Dom.setStyle(myEditor.get('element'), 'top', '-9999px');
                Dom.setStyle(myEditor.get('element'), 'left', '-9999px');
                Dom.setStyle(myEditor.get('element'), 'position', 'absolute');
                myEditor.get('element_cont').addClass('yui-editor-container');
                //YAHOO.log('Reset designMode on the Editor', 'info', 'example');
                //myEditor._setDesignMode('on');
                //YAHOO.log('Inject the HTML from the textarea into the editor', 'info', 'example');
                //myEditor.setEditorHTML($("#content").html().replace(/\n/g, '<br>'));
                //myEditor.get('textarea').value.replace(/\n/g, '<br>'));
            }
        }
    }
}


var i = 0;
$("#colors").change(function() {
    i = 0;
    changed(this.value);
});

function rotate() {
    if (i == 4) {
        i = 0;
    } else {
        i++;
    }
    changed($("#colors")[0].value);
}

function changed(value) {
    switch (value) {
        case "1":
            //http://www.colourlovers.com/palette/727035/%5Bw%5D_empty_sidewalks
            apply(["#AEB87D", "#91332A", "#CA2C1E", "#8B955C", "#C7CDA7"], i);
            break;
        case "2":
            //http://www.colourlovers.com/palette/44133/Night_Ground
            apply(["#30322A", "#818A71", "#516B5C", "#181E1E", "#B3B09A"], i);
            break;
        case "3":
            //http://www.colourlovers.com/palette/655315/Prior_Walter
            apply(["#141A14", "#852222", "#EDF2F1", "#3D4F4F", "#69B3B5"], i);
            break;
        case "4":
            //http://www.colourlovers.com/palette/733565/Prisengracht
            apply(["#181800", "#303030", "#604848", "#C25530", "#ABABF5"], i);
            break;
        case "5":
            //http://www.colourlovers.com/palette/726910/Marine
            apply(["#121E1C", "#0F1918", "#091515", "#32605E", "#78A4AF"], i);
            break;
        case "6":
            //http://www.colourlovers.com/palette/683931/icy_love
            apply(["#E3FAF7", "#CAFAF3", "#212928", "#131716", "#870000"], i);
            break;
        case "7":
            //http://www.colourlovers.com/palette/727958/Me_and_My_Soul
            apply(["#373B3B", "#455252", "#4E6161", "#BFB6A6", "#B5832B"], i);
            break;
        case "8":
            //http://www.colourlovers.com/palette/648830/Conformity
            apply(["#242B29", "#3B4743", "#141310", "#595947", "#789C90"], i);
            break;
        case "9":
            //http://www.colourlovers.com/palette/730315/r_e_m_a_i_n
            apply(["#000000", "#084B78", "#1C1C1C", "#076CB0", "#007FD4"], i);
            break;
        case "10":
            //http://www.colourlovers.com/palette/710831/My_Umpteenth_Palette
            apply(["#807D50", "#4B5E50", "#0D2C30", "#0A0802", "#323B38"], i);
            break;
        case "11":
            //http://www.colourlovers.com/palette/733631/Don%C2%B4t_play_with_fir
            apply(["#282828", "#0F0E0D", "#8C0009", "#000000", "#703511"], i);
            break;
        case "12":
            //http://www.colourlovers.com/palette/729285/couragedlovegoesdeep
            apply(["#0A0313", "#74000D", "#210503", "#C42A08", "#D49C11"], i);
            break;
        case "13":
            //http://www.colourlovers.com/palette/699730/Life
            apply(["#072A3A", "#075182", "#26011B", "#FEFCFF", "#A58C31"], i);
            break;
    }
}
function color(html, hd, hdul, docborder, ft) {
    $("html").css("background-color", html);
    $("#hd").css("background-color", hd);
    $("#hd ul").css("background-color", hdul);
    $("#doc").css("background-color", "White");
    $("#doc").css("border", "solid 3px " + docborder);
    $("#ft").css("background-color", ft);
}
function apply(colors, offset) {


    //        color(colors[offset],
    //        colors[offset + 1],
    //        colors[offset + 2],
    //        colors[offset + 3],
    //        colors[offset + 4]);

    color(colors[offset > 4 ? offset - 5 : offset],
        colors[offset + 1 > 4 ? offset + 1 - 5 : offset + 1],
        colors[offset + 2 > 4 ? offset + 2 - 5 : offset + 2],
        colors[offset + 3 > 4 ? offset + 3 - 5 : offset + 3],
        colors[offset + 4 > 4 ? offset + 4 - 5 : offset + 4]);

    //0 1 2 3 4
    //1 2 3 4 0
    //2 3 4 0 1

}
