var reloadState = false;
var ajaxPath = "/ajax.php";

$(document).ready(function(){
    $("input, textarea").live("focusin focusout", function() {  $(this).toggleClass("ui-state-highlight"); });
});

    function errorDialog(error, title)
    {
        if(title == undefined) title = "Hiba";
        $("#dialog-error").dialog("destroy");
        $("#dialog-error #error").html(error);
        $("#dialog-error").dialog({
            modal: true,
            title: title,
            hide: "explode",
            buttons: {
                Ok: function() {
                    $(this).dialog('close');
                }
            }
        });
        return asux = false;
    }

    function messageDialog(message, title)
    {
        if(title == undefined) title = "Üzenet";
        $("#dialog-highlight").dialog("destroy");
        $("#dialog-highlight #highlight").html(message);
        $("#dialog-highlight").dialog({
            modal: true,
            title: title,
            hide: "explode",
            buttons: {
                Ok: function() {
                    $(this).dialog('close');
                    
                    if(reloadState) 
                    {
                        document.location.href = document.location.href;
                        reloadState = false;
                    }
                }
            }
        });
        return true;
    }


function StatusMessage(text)
{
    $.jGrowl.defaults.closerTemplate = '<div>[ mindet bezár ]</div>';
    $.jGrowl(text, {
        theme: 'statusMessage'
        /*,easing: 'easeInOutExpo'
        ,beforeClose: function() {
                        return false;
                    }*/
    }); 
}
function sendTask(data, responseType)
{
    var ap = ajaxPath;
    var taskResponse;
    if(responseType == undefined)
        responseType = 'html';
        
    if(data.ajaxPath != undefined)
    {
        ap = data.ajaxPath;
        delete data.ajaxPath;
    }    
    if(data.process != undefined)
    {
        ap = data.ajaxPath;
        delete data.ajaxPath;
    }
    
    var retVal = true;
    $.ajax({
       type: "POST",
       url: ajaxPath,
       /*dataType: "json",*/
        data: data,
        async:false,
        success: function(resp)
        {
            taskResponse = resp;
            // json expected, es feldolgozza
            if(responseType != 'html')
            {
                asux = true;
                /*
                if(resp.error) 
                    errorDialog(resp.error);
                    */
                if($.isPlainObject(resp))
                {
                    if(resp._statusType != undefined && resp._statusType == 1)
                    {
                        //StatusMessage(resp._message);
                        messageDialog(resp._message);
                    }
                    else
                    {
                        errorDialog(resp._message);
                    }
                }
                else 
                    errorDialog("Rossz válasz érkezett a szervertől.");
            }
        }
     });     
     return taskResponse;
} 

    
function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;
    
    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";
    
    if(typeof(arr) == 'object') { //Array/Hashes/Objects 
        for(var item in arr) {
            var value = arr[item];
            
            if(typeof(value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}    

function countProperties(obj) {
    var count = 0;

    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
                ++count;
    }

    return count;
}

function arrayCompare(a1, a2) {
    if (a1.length != a2.length) return false;
    var length = a2.length;
    for (var i = 0; i < length; i++) {
        if (a1[i] !== a2[i]) return false;
    }
    return true;
}

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(typeof haystack[i] == 'object') {
            if(arrayCompare(haystack[i], needle)) return true;
        } else {
            if(haystack[i] == needle) return true;
        }
    }
    return false;
}

