/*
 * silver.app main-javascript
 *
 * silver.solutions gmbh, Berlin, Germany
 * <rel@silversolutions.de>
 * 
 */
var progress_counter    = 0;
 
function svb_Request(form_object, div_array, workflow, params )
{
    /*
     * arguments
     *  0: form-object
     *  1: div IDs as array
     *  2: workflow-name as string
     *  3: (optional) params as object
     */
    var div_object;
    var child_nodes;
    var select_childes;
    var submit_object           = new Object();
    var JSON_string;
    var divname;    
    
    submit_object['workflow']   = workflow;
    submit_object['input_vars'] = new Object();

    if ( document.documentElement ) {
        // MSIE
        browserHeight = document.documentElement.clientHeight;
        browserWidth  = document.documentElement.clientWidth;
        scrollY       = document.documentElement.scrollTop;
    } else {
        // alle "normalen" Browser
        browserHeight = window.innerHeight;
        browserWidth = window.innerWidth;
        scrollY       = window.pageYOffset;
    }
    
    if ( progress_counter++ == 0 )
    {
        //$('BODY').append('<div id="ajax-progress" style="position: absolute; left: '+(browserWidth/2)+'px; top: '+(scrollY+browserHeight/2)+'px;"><img src="/extension/silver.app/design/silver.app/images/ajax-loader.gif" /></div>');
        $('BODY').append('<div id="ajax-progress" style="position: absolute; left: '+(browserWidth/2)+'px; top: '+(scrollY+browserHeight/2)+'px;"><img src="/extension/silver.project/design/silver.project/images/ajax-loader.gif" /></div>');
    }
    
    for ( key in params )
    {
        if ( key != 'svapp_remove_vars' )
        {
            divname = key;
        }
    }
    if ( divname != undefined )
    {
        $('#toggle_'+divname+' INPUT[type=checkbox]').attr('disabled', 'disabled');
    }

    /* go through all div-objects */
    if ( div_array != undefined )
    {
        for ( var i = 0; i < div_array.length; i++ )
        {
            div_object                                  = document.getElementById( div_array[i] );
            child_nodes                                 = div_object.childNodes;
            submit_object['input_vars'][div_array[i]]   = new Object();
            
            /* go through all child nodes of current div-object */
            for ( var j = 0; j < child_nodes.length; j++ )
            {
                /* handle input elements */
                if ( child_nodes[j].nodeName == "INPUT" )
                {
                    switch ( child_nodes[j].getAttribute("type") )
                    {
                        case "text":
                        case "hidden":
                            submit_object['input_vars'][div_array[i]][(child_nodes[j].getAttribute("name")).replace(/\[\]/g,'')] = child_nodes[j].value.replace(/\%/g,'&#37;').replace(/\//g,'&#47;');
                            break;
                        case "checkbox":
                        case "radio":
                            if ( typeof submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")] == "object" )
                            {
                                //submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")][child_nodes[j].value] = child_nodes[j].checked;
                                submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")][j] = child_nodes[j].value;
                            }
                            else
                            {
                                submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")] = new Object();
                                submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")][j] = child_nodes[j].value;
                                // child_nodes[j].checked;
                            }
                            break;
                    }
                }
                /* handle select elements */
                if ( child_nodes[j].nodeName == "SELECT" )
                {
                    select_childs = child_nodes[j].childNodes;
                    for ( var k = 0; k < select_childs.length; k++ )
                    {
                        if ( typeof submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")] == "object" )
                        {
                            submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")][select_childs[k].value] = select_childs[k].selected;
                        }
                        else
                        {
                            submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")] = new Object();
                            submit_object['input_vars'][div_array[i]][child_nodes[j].getAttribute("name")][select_childs[k].value] = select_childs[k].selected;
                        }
                    }
                }
            }
        }
    }
    if( params != undefined )
    {
        submit_object['input_vars']['params'] = params;
    }
   
    /* stringify submit_object into JSON */
    JSON_string = JSON.stringify(submit_object);
    /* perform AJAX-request */
    jQuery.ez('sva::application', {json: JSON_string}, callBack);
}


//function callBack( content, errorCode, errorText )
function callBack(data)
{
    //var JSON_return     = content;
    var JSON_return       = data.content;
    var return_object   = JSON.parse(JSON_return);
    //var div_object      = document.getElementById('returned');
    var tmp_str;
    
    //div_object.innerHTML = return_object;
    
    for ( var i = 0; i < return_object.length; i++ )
    {
        if ( return_object[i]['html'] && return_object[i]['block_id'] )
        {
            var dialog_spalten = return_object[i]['option_count']/25;
            if (dialog_spalten > Math.floor(dialog_spalten)) dialog_spalten = Math.ceil(dialog_spalten);
            if (dialog_spalten > 4) dialog_spalten = 4;
            var dialog_width = dialog_spalten*215;

            if(document.getElementById(return_object[i]['block_id']))
            {
                tmp_str = return_object[i]['html'];
                document.getElementById(return_object[i]['block_id']).innerHTML = tmp_str;
            }
            
            if ( return_object[i]['block_id'] == 'wiadok_popup' )
            {
                $('#wiadok_popup').dialog('destroy');
                $('#wiadok_popup').dialog({width: dialog_width, modal: true, title: "Weitere Auswahlmöglichkeiten", open: function(event, ui) {
                var browserHeight;
                // see: http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
                if ( document.documentElement )
                {
                    browserHeight = document.documentElement.clientHeight;
                }
                else
                {
                    browserHeight = window.innerHeight;
                }
                 var maxHeight = browserHeight-120;   // Höhe der Titelzeile inkl.
                 if ($(this).height() > maxHeight) {
                     $(this).height(maxHeight);
                 }
              }});
            }
        }
        if(return_object[i]['javascript'])
        {
            eval(return_object[i]['javascript']);
        }
    }
    if ( --progress_counter == 0 )
    {
        $('#ajax-progress').remove();
    }
}

function autosuggest_helper(form_object, div_array, workflow, params, text, delay)
{
    if ( text.length >= 2 )
    {
        svb_Request(form_object, div_array, workflow, params);
    }
    else
    {
        if ( $('#suggestion') != null )
        {
            $('#suggestion').hide();
        }
    }
}

function svappTemplate(code,template,block_id) {
    if ( code == '---' )
    {
        $('#' + block_id).html('');
        return;
    }
    
     p = new Object();
     p.svapp_remove_vars = "all";
     p.we_nummer = code;
     p.template = template;
     p.block_id = block_id;
     svb_Request(undefined , undefined , 'template', p);
}


