/**
 * Popup block
 * (requires jQuery)
 *
 * @author Stepan Reznikov [stepan.reznikov@gmail.com]
 * @version 2.0 (2008-07-07)
 *
 * @param {Object} options Object with options
 */
 
var req=null;
var reqIns=null;
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;
 

function objav_no_publish(id_obj,code)
{
    var url_base = String(window.location);
    if (window.XMLHttpRequest) {
        req_1 = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req_1 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req_1) {
        req_1.onreadystatechange = processNoPublish;
        req_1.open('GET', '/__ajax/make_no_publish.php?url_base='+url_base+'&id_obj='+id_obj+'&code='+code, true);
        req_1.send(null);
    }
}

function processNoPublish()
{
    var ready_1=req_1.readyState;
    var data_1=null;

    if(ready_1 == READY_STATE_COMPLETE){
        data_1 = req_1.responseText;
        var Div_body=document.getElementById("prolongation_div");
        if(data_1 != ''){
            Div_body.style.display = 'none';
            $.prompt(data_1);
        } else {
            Div_body.style.display = 'none';
            $.prompt('<div id="error_ltbx"><div class="tkst_b">Ошибка снятия с публикации</div><div class="tkst">Не совпадает код, либо номер объявления.</div><div id="ok_but_2"><a onclick="$(\'.jqiclose\').click();"><img class="lite_ok" src="/f/1/global/ok.jpg" width="97" height="28" border="0" /></a></div><ins class="cn tl"></ins><ins class="cn tr"></ins><ins class="cn bl"></ins><ins class="cn br"></ins><div class="border top"></div><div class="border bottom"></div><div class="border left"></div><div class="border right"></div>');
        }
    }
}

function objav_prolong(id_obj,code)
{
    var url_base = String(window.location);
    if (window.XMLHttpRequest) {
        req_2 = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req_2 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req_2) {
        req_2.onreadystatechange = processProlong;
        req_2.open('GET', '/__ajax/make_prolong.php?url_base='+url_base+'&id_obj='+id_obj+'&code='+code, true);
        req_2.send(null);
    }
}

function processProlong()
{
    var ready_2=req_2.readyState;
    var data_2=null;

    if(ready_2 == READY_STATE_COMPLETE){
        data_2 = req_2.responseText;
        var Div_body=document.getElementById("prolongation_div");
        if(data_2 != ''){
            Div_body.style.display = 'none';
            $.prompt(data_2);
        } else {
            Div_body.style.display = 'none';
            $.prompt('<div id="error_ltbx"><div class="tkst_b">Ошибка продления</div><div class="tkst">Не совпадает код, либо номер объявления.</div><div id="ok_but_2"><a onclick="$(\'.jqiclose\').click();"><img class="lite_ok" src="/f/1/global/ok.jpg" width="97" height="28" border="0" /></a></div><ins class="cn tl"></ins><ins class="cn tr"></ins><ins class="cn bl"></ins><ins class="cn br"></ins><div class="border top"></div><div class="border bottom"></div><div class="border left"></div><div class="border right"></div>');
        }
    }
}



function make_automerge(id_obj)
{
    var url_base = String(window.location);
    if (window.XMLHttpRequest) {
        req___ = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req___ = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req___) {
        req___.onreadystatechange = processMakeAutomerge;
        if(document.getElementById("automerge_").checked == true) {
            flag_check=1;
        } else {
            flag_check=0;
        }
        req___.open('GET', '/__ajax/make_automerge.php?url_base='+url_base+'&id_obj='+id_obj+'&merge='+flag_check, true);
        req___.send(null);
    }
}

function processMakeAutomerge()
{
    var ready___=req___.readyState;
    var data___=null;

    if(ready___ == READY_STATE_COMPLETE){
        data___ = req___.responseText;
        var Div_body=document.getElementById("make_hot_v_");
        if(data___ != ''){
            Div_body.innerHTML = data___;
        }
    }
}


function make_hot(id_obj,id_user)
{
    var url_base = String(window.location);
    if (window.XMLHttpRequest) {
        req_ = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req_ = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req_) {
        req_.onreadystatechange = processMakeHot;
        if(document.getElementById("automerge").checked == true){
            flag_check=1;
        } else {
            flag_check=0;
        }
        req_.open('GET', '/__ajax/make_hot.php?url_base='+url_base+'&id_obj='+id_obj+'&id_user='+id_user+'&merge='+flag_check, true);
        req_.send(null);
    }
}

function processMakeHot()
{
    var ready=req_.readyState;
    var data_=null;

    if(ready == READY_STATE_COMPLETE){
        data_ = req_.responseText;
        var Div_body=document.getElementById("make_global_hot");
        if(data_ != ''){
            Div_body.innerHTML = data_;
        }
    }
}



function my_schet_litebox(id_obj,id_user)
{
    var url_base = String(window.location);
    //alert('/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj);
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req) {
        req.onreadystatechange = processReqChange_start_my_schet;
        if (id_user) {
            req.open('GET', '/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj+'&id_user='+id_user, true);
        } else {
            req.open('GET', '/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj, true);
        }
        req.send(null);
    }
}

function my_schet_litebox_test(id_obj,id_user)
{
    var url_base = String(window.location);
    //alert('/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj);
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req) {
        req.onreadystatechange = processReqChange_start_my_schet;
        if (id_user) {
            req.open('GET', '/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj+'&id_user='+id_user+'&switch_to_schet=1', true);
        } else {
            req.open('GET', '/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj+'&switch_to_schet=1', true);
        }
        req.send(null);
    }
    $('.jqiclose').click();
}

function my_schet_litebox_test2(id_obj,id_user)
{
    var url_base = String(window.location);
    //alert('/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj);
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req) {
        req.onreadystatechange = processReqChange_start_my_schet;
        if (id_user) {
            req.open('GET', '/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj+'&id_user='+id_user+'&switch_to_sms=1', true);
        } else {
            req.open('GET', '/__ajax/get_my_schet.php?url_base='+url_base+'&id_obj='+id_obj+'&switch_to_sms=1', true);
        }
        req.send(null);
    }
    $('.jqiclose').click();
}



function processReqChange_start_my_schet()
{
    var ready=req.readyState;
    var data=null;

    if(ready == READY_STATE_COMPLETE){
        data = req.responseText;
        if(data != ''){
            $.prompt(data);
            document.getElementById('jqi').style.top = '15%';
            //document.getElementById('jqibuttons').style.display = 'none';
            // var Div_Schet=document.getElementById("jqimessage");
            //Div_Schet.innerHTML = data;
            //$.prompt(Div_Schet.innerHTML);
        }
        //document.getElementById('vschet_hot_show').style.display = 'block';
        //document.getElementById('div_first').style.display = 'none';
    }
}


function my_schet_litebox_automerge(id_obj)
{
    var url_base = String(window.location);
    if (window.XMLHttpRequest) {
        req__ = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req__ = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req__) {
        req__.onreadystatechange = processReqChange_start_my_schet_automerge;
        req__.open('GET', '/__ajax/get_my_schet_automerge.php?url_base='+url_base+'&id_obj='+id_obj, true);
        req__.send(null);
    }
}


function processReqChange_start_my_schet_automerge()
{
    var ready=req__.readyState;
    var data=null;

    if(ready == READY_STATE_COMPLETE){
        data = req__.responseText;
     
        if(data != ''){
            $.prompt(data);
            // document.getElementById('jqi').style.top = '1%';
        }
    }
}


function hot_hide_sms()
{
    document.getElementById('make_global_hot').style.display = 'block';
    document.getElementById('make_hot_sms').style.display = 'none';
    document.getElementById('ok_v_2').style.display = 'none';
    document.getElementById('schet').checked = true;
}


function hot_hide_schet()
{
    document.getElementById('make_global_hot').style.display = 'none';
    document.getElementById('make_hot_sms').style.display = 'block';
    document.getElementById('ok_v_2').style.display = 'block';
    document.getElementById('sms2').checked = true;
}


function bot_text_hide()
{
    document.getElementById('bot_text_v').style.display = 'none';
    document.getElementById('bot_text_show').style.display = 'block';
    document.getElementById('ok_v').style.padding = '35px 0 0';
    document.getElementById('jqi').style.top = '15%';
}


function bot_text_show()
{
    document.getElementById('bot_text_v').style.display = 'block';
    document.getElementById('bot_text_show').style.display = 'none';
    document.getElementById('ok_v').style.padding = '10px 0 0';
    document.getElementById('jqi').style.top = '5%';
}


function redir_pay(url)
{
    document.location = url;
}


function show_schet(arg)
{
    var fio = document.getElementById('fio').value;
    var summa = document.getElementById('summa').value;
    window.open('/html_schet/get_html_schet.php?fio='+fio+'&summa='+summa+'&username='+arg,'','width=1000,height=800,menubar=yes');
    return false;
}


function litebox_hot_show()
{
    document.getElementById('litebox_hot').style.display = 'block';
    document.getElementById('hot_show').style.display = 'none';
    document.getElementById('jqi').style.top = '5%';
}


/* function sms_hot_show() {  
 document.getElementById('sms_div_general').style.display = 'block';
 document.getElementById('div_first').style.display = 'none';
 }
 
  function vschet_hot_show(id_obj) {
  my_schet_litebox(id_obj);
  }*/


function litebox_hot_hide()
{
    document.getElementById('litebox_hot').style.display = 'none';
    document.getElementById('hot_show').style.display = 'block';
    document.getElementById('jqi').style.top = '15%';
}


var popup_block = function (options) {

	var that = {};

	var keep = false,
	    documentClickHandler,
	    documentKeyDownHandler;

	that.onResize = options.onResize || function() {};
	that.container = options.container;
	that.link = options.link;

	that.hide = function (event) {

		if (keep) {
			keep = false;
			return;
		} else {
			if (options.onBlockHide) {
				options.onBlockHide(options);
			}
		}

		if (options.fader) {
			options.fader.addClass("hidden");
			$("select").css("visibility","visible");
		}
		options.container.addClass("hidden");

		$(document).unbind("click", documentClickHandler);
		$(document).unbind("keydown", documentKeyDownHandler);
	};


	that.cancel = function (event) {
		var code = event.keyCode ? event.keyCode : event.which ? event.which : null;
		if (code === 27) {
			that.hide(event);
		}
	};


	that.show = function (event) {

		options.container.removeClass("hidden");
		if (options.onBlockShow) {
			options.onBlockShow(options);
		}

		if (options.fader) {
			options.fader.removeClass("hidden");
			//$("select").css("visibility","hidden");
			options.fader.css("height",$(document).height());
		}

		documentClickHandler = function (event) {
			that.hide(event);
		};

		documentKeyDownHandler = function (event) {
			that.cancel(event);
		};

		//$(document).click(documentClickHandler);
		$(document).click(
			function(evt){
				if( (keep || options.keep) && (!evt.which || evt.which === 1) ){
					that.hide();
				}
			}
		);
		$(document).keydown(documentKeyDownHandler);
	};


	that.toggle = function (event) {
		event.preventDefault();
		event.stopPropagation();

		if (options.container.hasClass("hidden")) {
			that.show(event);
		} else {
			that.hide(event);
		}
	};


	options.container.click(function (event) {
		keep = true;
	});

	options.link.click(function (event) {
		that.toggle(event);
	});

	if (options.close) {
		options.close.click(function (event) {
			that.toggle(event);
		});
	}

	return that;
};
