// copyright 2010 codeHorse, Inc.  all rights reserved

function ajax_DirectAssignments( data) 
{
    jQuery("#jQueryDirectAssignments", data).children(':first').children().each(function() {
        var targetSelector = jQuery(this).children(':eq(0)').text();
        var htmldata = jQuery(this).children(':eq(1)').html();
        jQuery(targetSelector).html( htmldata);
    });
}
function ajax_IndirectAssignments( data) 
{
    jQuery("#jQueryIndirectAssignments", data).children(':first').children().each(function() {
        var targetSelector = jQuery(this).children(':eq(0)').text();
        var sourceSelector = jQuery(this).children(':eq(1)').text();
        jQuery(targetSelector).html( jQuery(sourceSelector, data).html());
    });
}

// used in ticket edit form where the contents of rows changes depending on the fields values but where the number of rows remains the same 
function ajax_html_update( data) 
{
    //alert( "done " + data.substring(1,200));
    var newjs = jQuery("#changed_js", data).html();
    // globalEval was necessary to change previously defined javascript variables, but I think it does NOT have access to the 'data' argument
    jQuery.globalEval( newjs);
   
    //alert( "var pcid200300 = " + pcid200300 + " hello");
    
    jQuery("#changed_rows", data).children(':first').children().each(function() {
        var t = jQuery(this);
        var trid = t.attr('id');
        var tridkey = "#" + trid;
        if( trid ) {
            var target = jQuery(tridkey);
            var source = jQuery(tridkey, data);
            target.html( source.html());
            target.css('display', source.css('display'));
        }
        
    });
    ajax_DirectAssignments( data);
    ajax_IndirectAssignments( data);
    jQuery(".result").html( jQuery("#changed_result", data).html());
    var newlocaljs = jQuery("#localjs", data).html();
    eval( newlocaljs);
}



// fieldGroups --------------------------------
//  add and remove whole sections of table rows to edit or view groups of fields 

function clear_fieldGroup_feedback( fieldGroup) 
{
    jQuery("#save_result_" + fieldGroup).html("");
    jQuery("#save_error_" + fieldGroup).html("");
    jQuery("#save_message_" + fieldGroup).html("");
}

function ajax_fieldGroup( data, fieldGroup, editOrView) 
{
    var allFieldGroups = getAllFieldGroups();
    if (editOrView == 'view') {
        jQuery("#button_view_" + fieldGroup).hide();
        jQuery("#button_edit_" + fieldGroup).show();
        for (var i in allFieldGroups) {
            var otherGroup = allFieldGroups[i];
            if (otherGroup == fieldGroup) {
            } else {
                jQuery("#button_edit_" + otherGroup).show();
            }
        }
    } else {
        jQuery("#button_edit_" + fieldGroup).hide();
        jQuery("#button_view_" + fieldGroup).show();
        // we also have to hide all the edit buttons of all the other groups
        var allFieldGroups = getAllFieldGroups();
        for (var i in allFieldGroups) {
            var otherGroup = allFieldGroups[i];
            if (otherGroup == fieldGroup) {
            } else {
                jQuery("#button_edit_" + otherGroup).hide();
            }
        }
    }
    jQuery(".change" + fieldGroup).remove();
    jQuery("#row_" + fieldGroup + "buttons").after( jQuery(".change" + fieldGroup, data));
}
function jeov_fieldGroup( jurl, fieldGroup, editOrView) {
    clear_fieldGroup_feedback( fieldGroup);
    jQuery.get(jurl,({"change" : fieldGroup }), function(data){
        ajax_fieldGroup( data, fieldGroup, editOrView);
    });
}
function ajax_save_result( data) 
{
    // reedit if errors or view if success
    var ajax_method = jQuery('#ajax_method', data).text();
    // alert( "save returned ajax_method " + ajax_method);
    eval( ajax_method);
    ajax_DirectAssignments( data);
    var newlocaljs = jQuery("#localjs", data).html();
    eval( newlocaljs);
}
function jsave_fieldGroup(jurl,fields) {
    jQuery.get(jurl,(fields),ajax_save_result );
}


// Input Hints as content of text fields that disappear when you start to fill them in
// use like this:
// 
// <script>
// jQuery(document).ready(function() {
//   var hints = {
//     "search": "search our stories",
//   };
//   setupInputHints( hints)
// });
// </script>
// 
function hideInputHint(field, text) {
    if (jQuery(field).val() === text) {
        jQuery(field).val("");
        jQuery(field).removeClass('hint');
    }
}
function showInputHint(field, text){
    if (jQuery(field).val() === "") {
        jQuery(field).val(text);
        jQuery(field).addClass('hint');
    }
}
function setupInputHints( hintsByField) {
  var text;
  for (fieldName in hintsByField) {
        text = hintsByField[fieldName];
        jQuery("input[name='"+fieldName+"']").val(text).focus((function(text) {
            return function() {hideInputHint(this, text)};
        })(text)).blur((function(text) {
            return function() {showInputHint(this, text)};
        })(text)).addClass('hint');
    }
}


