
/*------------------------------------------------------------------------
 * url_params()
 * 
 * Parse any parameters specified with the URL.
 *------------------------------------------------------------------------*/

function url_params() {
	var url = window.document.URL.toString();
    var params = new Array;

    if (url.indexOf('?') > 0) {
        var args = url.split('?')[1].split('&');
		for (var i = 0; i < args.length; i++) {
			var tokens = args[i].split("=");
            params[tokens[0]] = tokens[1].length
                              ? unescape(tokens[1]) : '';
		}
	}

    return params;
}

var BLUR_TIMEOUT = 250;
var post_edit_elem;
var post_edit_timer;

function edit_elem(elem) {
    var that = $(elem);
    var save = that.html();
    if (elem.save) {
        // console.debug('got existing saved test: ' + elem.save);
    }
    else {
        elem.save = save;
    }
    that.addClass('edit');
    if (! elem.blurry ) {
        console.debug('binding blur function');
        that.unbind('blur');
        that.bind(
            'blur',
            function() {
//                console.debug('blurring');
                post_edit_elem  = elem;
                post_edit_timer = setTimeout(
                    function() { post_edit(elem,save) }, 
                    BLUR_TIMEOUT
                );
//                console.debug('set post_edit_timer'); // + post_edit_timer);
            }
        );
        elem.blurry = 1;
    }
}

function post_edit(elem,save) {
    var that = $(elem);
    var html = that.html();
    var text = that.text().replace(/^\s+/, '');
    that.removeClass('edit');
//    console.debug('post_edit');
    if (text.length) {
        html = html.replace(/1\/4/g, '&frac14;');
        html = html.replace(/1\/2/g, '&frac12;');
        html = html.replace(/3\/4/g, '&frac34;');
        that.html(html);
      
        // see if the text has changed
        if (html != elem.save) {
            that.addClass('dirty');
            console.debug('html changed, made dirty'); // + save + "\nvs\n" + text);
        }
        else {
            console.debug('html unchanged'); // + html);
        }
    }
    else {
        // empty element gets deleted
        that.replaceWith();
    }
}

function apply_style(cmd) {
    console.debug('applying style: ' + cmd);
    document.execCommand(cmd, null, null);
    if (post_edit_timer) {
//        console.debug('clearing blur timeout');
        clearTimeout(post_edit_timer);
        $(post_edit_elem).focus();
    }
    else {
//        console.debug('no blur timeout to clear');
    }
}

function new_para(elem, class) {
    $(elem).before('<p class="about" contenteditable="true" onfocus="edit_elem(this)">&nbsp;</p>');
    $(elem).prev().focus();
}

function new_li(elem, class) {
    $(elem).parent('li').before('<li contenteditable="true" onfocus="edit_elem(this)">&nbsp;</li>');
    $(elem).parent('li').prev('li').focus();
}

function OLD_new_para(elem, class) {
    var para = document.createElement('p');
    para.innerHTML = 'New para...';
//    document.body.insertBefore(para, elem);
    var pq = $(para);
    pq.addClass('about');
    pq.attr('contenteditable', 'true');
    pq.focus(function() { edit_elem(elem) });
    $(elem).before(para);
}