/*
    Copyright 2007 Peter Rowell
    BSD License: use as you wish but don't lose the copyright.

    Editable implements class-based behaviors using jQuery to select
    'editable' objects and attach mouseover, mouseout, and dblclick
    events to them.

    When the user double-clicks one of two things happens:
	If edit_in_place == false, we change the whole page
	to the admin edit page for the related object.
    
	If edit_in_place == true, we call thickbox with the
	edit page's url + _popup=true. This gives us a more
	abbreviated edit page (this is standard admin code).
	On cancel, we simply remove the thickbox.
	On save, we have to reload the originating page to
	display any changes.
 */

var edit_in_place; // You set this in editable/templates/editable.html
var undefined;

function ParseEditableTarget(classesIn, include_field) {
    // Our target is encoded as a class attribute.
    // E.g., <p class="your_classes editable content.article.23">
    // So grab the string immediately after "editable" 
    target = classesIn.replace(
	    /^.*\beditable\s+([A-Za-z0-9_\.]+).*$/, '$1');
    tmp = target.split(/_/);
    objref = tmp[0].replace(/\./g, '/'); // convert . to /
    field = tmp[1]
    url = '/edit/' + objref + '/';
    urlField = (field == undefined) ? url : (url + '#id_' + field);
    // alert(classesIn+" => "+ urlField); return false;
    if (! include_field)
	return url;
    return urlField;
} // Parse EditableTarget

$(document).ready(function(){
    $(".editable")
	.mouseover(function(){
	    $(this).addClass("editable_mouseover");
	    })
	.mouseout(function(){
	    $(this).removeClass("editable_mouseover");
	    })
	.dblclick(function(){
	    if (edit_in_place != undefined && edit_in_place) {
		url = ParseEditableTarget(this.className);
		url += '?_popup=true&TB_iframe=true&height=550&width=1000';
		caption = "Editing " + target;
		tb_show(caption, url, false);
		// this.tb_blur();
	    } else {
		url = ParseEditableTarget(this.className, true);
		window.location.assign(urlField);
	    }
	    return false;
	    })
	;
    if (false) {
    $("//div.editable//p")
	.dblclick(function(){
	    return false;
	    })
	.mouseover(function(){
	    $(this).addClass("editable_child_mouseover");
	    })
	.mouseout(function(){
	    $(this).removeClass("editable_child_mouseover");
	    })
	;
    }
});


