jQuery(function($) {

if (typeof(fg) == 'undefined')

fg={};

if (typeof(fg.rg) == 'undefined')

fg.rg={};


fg.rg.setlistitems=function(list,names) {

var i=0;

var newhtml='';

names.sort();

if (names.length > 0) {

for (i=0; i<names.length; ++i) {

newhtml=newhtml+'<option value="'+names[i]+'">';

names[i].replace(/_/g,' ');

newhtml=newhtml+names[i]+'</option>';

}

} else {

newhtml='<option value="?">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';

}

$(list).html(newhtml);

};


fg.rg.moveselitems=function(srclist,destlist) {

var i=0;

var srcnames=new Array();

var destnames=new Array();

for (i=0; i<destlist.options.length; ++i) {

if (destlist.options[i].value != '?')

destnames[destnames.length]=destlist.options[i].value;

}

for (i=0; i<srclist.options.length; ++i) {

if (srclist.options[i].value != '?') {

if (srclist.options[i].selected)

destnames[destnames.length]=srclist.options[i].value;

else

srcnames[srcnames.length]=srclist.options[i].value;

}

}

fg.rg.setlistitems(srclist,srcnames);

fg.rg.setlistitems(destlist,destnames);

};


fg.rg.sethiddenval=function(name,srclist) {

var i=0;

var srcnames=new Array();

var valstring='';

var separator='';

for (i=0; i<srclist.options.length; ++i) {

if (srclist.options[i].value != '?') {

valstring=valstring+separator+srclist.options[i].value;

separator=' ';

}

}

$("#kb").append('<input type="hidden" name="'+name+'" value="'+valstring+'" />');

};

fg.rg.edit=function(idtoedit) {

if (fg.rg.editing) {

alert("You must finish or cancel your current edits before editing or inserting something else.");

return;

}

fg.rg.editing=true;

var editform=$('#ge').get(0);

var id=idtoedit.toString();

editform.editedid=id;

$('#de').val(id);

if (id == '0') {


$('#hd,#jd,#ld,#nd').val('');


$('#insert_button').after($(editform)).css('display','none');

$('#ge').css('display','inline');

$('#pd,#ae').css('display','none');

calcpos();

} else {

var cd={u:"hcnc",q:window.location.pathname,r:"d3",v:"resource"};

var url="/ajax.php";

cd.s='editsetup';

cd.t=0;

cd.id=id;

var options={url:url,data:cd,dataType:'html'};

options.success=function(formhtml,status) {

var editspec='#rep_'+id;

var edited=$(editspec).get(0);


$('#ge').html(formhtml);

$("#ce").click(fg.rg.editcancel);


$(editspec).after(editform).css('display','none');

$('#ge').css('display','inline');

calcpos();

};

options.error=function(xhr,status,exception) {

alert("network or software error occurred in getting edit data. status:"+status+"  exception:"+exception);

fg.rg.editing=false;

};


$.ajax(options);

}

};

fg.rg.remove=function(id) {

if (fg.rg.editing) {

alert("You must finish or cancel your current edits before deleting anything.");

return;

}

if (confirm("Warning! You are about to delete a resource guide entry.  There is no 'undo' for this action.  OK to proceed, Cancel to abort.")) {

var params={q:window.location.pathname, r:'d3', s:'', t:0, u:'hcnc', delid:id};

newlocation='/form.php?'+$.param(params);

document.location=newlocation;

}

};

fg.rg.editing=false;

$("#ob").replaceWith('<input id="ob" type="button" value="<<" onclick="fg.rg.moveselitems($(\'#ec\').get(0),$(\'#mb\').get(0));">');

$("#pb").replaceWith('<input id="pb" type="button" value=">>" onclick="fg.rg.moveselitems($(\'#mb\').get(0),$(\'#ec\').get(0));">');

$("#cc").replaceWith('<input id="cc" type="button" value=">>" onclick="fg.rg.moveselitems($(\'#ec\').get(0),$(\'#ac\').get(0));">');

$("#dc").replaceWith('<input id="dc" type="button" value="<<" onclick="fg.rg.moveselitems($(\'#ac\').get(0),$(\'#ec\').get(0));">');


$("#kb").submit(function() {

fg.rg.sethiddenval('jsfilters',$("#mb").get(0));

fg.rg.sethiddenval('jsexclusions',$("#ac").get(0));

return true;

});


fg.rg.editcancel=function() {

var editform=$('#ge').get(0);

var editspec=(editform.editedid == '0') ? '#insert_button' : '#rep_'+editform.editedid;

var edited=$(editspec).get(0);

$(edited).css('display','inline');

$(editform).css('display','none');

fg.rg.editing=false;

calcpos();

};


$("#ce").click(fg.rg.editcancel);



});

function be() {

$(document).ready(calcpos); $(window).resize(calcpos);

}

function calcpos() {

if (this.busy) {alert('reentry detected'); return;} this.busy=1;
$("#ma").css("left",Math.max(0,Math.floor(50*($("#ma").offsetParent().width())/100-($("#ma").width())*50/100+0.5+(0)))+"px");


$("#oa").height(Math.max(0,Math.floor($("#gb").position().top+100*($("#gb").height())/100+0.5+(-10))-$("#oa").position().top));


$("#pa").css("top",Math.floor($("#oa").position().top+100*($("#oa").height())/100+0.5+(0))+"px");


$("#ab").css("top",Math.floor($("#pa").position().top+100*($("#pa").height())/100+0.5+(0))+"px");


$("#ib").css("top",Math.floor($("#pa").position().top+100*($("#pa").height())/100+0.5+(-15))+"px");


var topmax=$("#cb").position().top+$("#cb").height()+(-10);

topmax=Math.max(topmax,$("#ib").position().top+$("#ib").height()+(-10));

$("#bb").css("top",topmax+"px");

delete topmax;


$("#ab").height(Math.max(0,Math.floor($("#bb").position().top+0*($("#bb").height())/100+0.5+(0))-$("#ab").position().top));


$("#he").css("left",Math.max(0,Math.floor($("#bb").position().left+100*($("#bb").width())/100-($("#he").width())*100/100+0.5+(0)))+"px");


$("#he").css("top",Math.max(0,Math.floor($("#bb").position().top+100*($("#bb").height())/100-($("#he").height())*100/100+0.5+(35)))+"px");


this.busy=0;

}
