/****************************************************************************
	Description: 
 	Author: Ivanov Kirill
	Created: 13.07.2008
 	Version: 1.0.0

	Changes info: 
		= 13.07.2008	(Ivanov Kirill):	создан
		+ 29.08.2008    (Kecheor):          метод ServerArgEvent
		+ 04.09.2008    (Kecheor):          добавлена проверка передается ли объект или id в вызове события
		+ 17.09.2008    (Ivanov Kirill):    outerHTML
****************************************************************************/
function StartWait()
{
	$("#status_div").fadeIn("fast");
}
function StopWait()
{
	$("#status_div").fadeOut("slow");
}
function WorkSpaceSubmit()
{
    document.forms['WorkSpace'].submit();
}

/*
    обработка ответа сервера
*/
function ParseServerResponse(_response)
{
    //alert(_response);
    var workspace_output_start = '[->]';
    var index_of_workspace_output = _response.indexOf(workspace_output_start);
    if (index_of_workspace_output != -1)
    {
	    var w_string = _response.substring(0, index_of_workspace_output);
	    _response = _response.substring(index_of_workspace_output + workspace_output_start.length); 
	    if (w_string != "")
	    {
	      alert(w_string);
	    }
    }
    else
    {
       // alert(_response);
    }
    while (_response.indexOf('{component_end}') != -1)
    {
        var component_end_string = '{component_end}';
	    var index_of_end = _response.indexOf(component_end_string);
	    var component_string = _response.substring(0, index_of_end);
	    _response = _response.substring(index_of_end + component_end_string.length); 
	    
	    ParseComponentString(component_string);
	}
    StopWait();
}
/*
    обработка строки компонента из ответа сервара
*/
function ParseComponentString(_component_string)
{
    var index_of_separator = _component_string.indexOf('~');
	var component_parent_name = _component_string.substring(0, index_of_separator);
	var component_body = _component_string.substring(index_of_separator + 1); 

    index_of_separator = component_body.indexOf(':');
	var component_container_name = component_body.substring(0, index_of_separator);
	component_body = component_body.substring(index_of_separator + 1); 
    
    //alert(component_parent_name);
    
	var component_contaner = document.getElementById(component_container_name);

    if ($("#" + component_container_name).length)
    {
        if (component_body == "")
        {
            component_body = " ";
        }
	    $("#" + component_container_name).outerHTML(component_body);
	}
	else
	{
	    if ($("#" + component_parent_name).length)
	    {
	        $("#" + component_parent_name).append(component_body);
	    }
	}
	
	run_js(component_container_name);
}
/*
    функция отправляет сообщение о событии на сервер 
*/
function ServerEvent(_component, _event)
{
    if (typeof(_component) != "object")
    {
        _component = document.getElementById(_component);
    }
	var value = _component.value;
	
	if (_component.type == "checkbox" && !_component.checked)
	{
	    value = 0;
	}

    var c_id = _component.id;
    StartWait();
    $.post("index.php", {action: "single_send", event_arg: value, event: _event, event_component: c_id}, function(data) {ParseServerResponse(data);});
}
/*
    функция отправляет сообщения о событии на сервер и содержания родительской формы
*/
function ServerEventWithFormData(_component, _event, _container_id, _arg)
{
    // tineMCE section (this project)
    tinyMCE.triggerSave();
    // *tineMCE section (this project)

    if (typeof(_component) != "object")
    {
        _component = document.getElementById(_component);
    }

    StartWait();

    var r_data = new Object();
    r_data.action = "single_send";
    $(r_data).attr("event", _event);
    $(r_data).attr("event_component", _component.id);
    $(r_data).attr("event_arg", _arg);
    $("#" + _container_id + "").find(":input").each(function() {$(r_data).attr($(this).attr("id"), $(this).attr("value"));});
    $("#" + _container_id + "").find(":input[type='checkbox']").each(function() {$(r_data).attr($(this).attr("id"), $(this).attr("checked"));});

    $.post("index.php", r_data, function(data) {ParseServerResponse(data);});
}
function ServerArgEvent(_component, _event, _arg)
{
    StartWait();
    if (typeof(_component) != "object")
    {
        _component = document.getElementById(_component);
    }
    var c_id = _component.id;

    var value = _component.value;

   	if (_component.type == "checkbox" && !_component.checked)
    {
   	    value = 0;
    }
    var r_data = new Object();
    r_data = {action: "single_send", event_arg: _arg, event: _event, event_component: c_id};
    $(r_data).attr(c_id, value);
    $.post("index.php", r_data, function(data) {ParseServerResponse(data);});
}
function SendContainerData(_container_id)
{
    var r_data = new Object();
    r_data.action = "single_send";
    $("#" + _container_id + "").find(":input").each(function() {$(r_data).attr($(this).attr("id"), $(this).attr("value"));});
    $.post("index.php", r_data, function(data) {ParseServerResponse(data);});
}
function run_js(name)
{
    $("#" + name + " script").each(function() {
        $(this).attr("src", $(this).attr("src"));
    });
}
function ping()
{
    var r_data = new Object();
    r_data = {action: "single_send"};
    $.post("index.php", r_data, function(data) {ParseServerResponse(data);});
    
    setTimeout("ping()",60000);
}

$(document).ready(function() {ping();});

jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
}

function ajaxFileUpload2(componentname, file_field_name)
{
    StartWait();
    
    alert('1');
	$.ajaxFileUpload
	(
		{
			url:'index.php', 
			secureuri:false,
			dataType:'mayral',
			fileElementId:'fileToUpload',
			component:componentname,
			success:function (data, status)
			{
			    ParseServerResponse(data);
			}
		}
	)
	StopWait();
	return false;
}  

function ajaxFileUpload(componentname, file_field_name)
{

    StartWait();
	$.ajaxFileUpload
	(
		{
		    
			url:'index.php', 
			secureuri:false,
			dataType:'mayral',
			fileElementId:file_field_name,
			component:componentname,
			success:function (data, status)
			{
			    ParseServerResponse(data);
			}
		}
	)
	
	StopWait();
	return false;
} 
