//<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
var qstop = 0;
var sq    = 0;

function ShowDelay(str1, str2, hint, ltp) {
	_v('hint1').style.display='none';
	_v('hint2').style.display='none';
	sq = 1;
	lt = (null != ltp) ? ltp : 0;
	setTimeout("quickSearch('"+str1+"', '"+str2+"', '"+hint+"', '"+lt+"')", 500);
}

function quickSearch(str1, str2, hint, ltp) {
    
	if ( _v('search1').value == str1 && _v('search2').value == str2 && !qstop && sq ) {
		var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4 && sq) {
		        _v('hint'+hint).innerHTML = req.responseJS.q; 
				_v('hint'+hint).style.display = 'block';
				//_v('out_inf').innerHTML = req.responseJS.q;			
            }
        }
        req.open(null, '/ajax.php', true);
		lt = (null != ltp) ? ltp : 0;
	    req.send( { action: 'quicksearch', str1: str1, str2: str2, hint: hint, lt: lt } );
	}
}

function fullSearch(str1, str2, xsort, page, kind, people, tag, show, subfamily)  {
    
	if ( !qstop ) {
	    ShowLoad(1);
		qstop = 1;
		sq    = 0;
		var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
		        _v('out_inf').innerHTML = req.responseJS.q;
				qstop = 0;
				ShowLoad(0);
            }
        }
        req.open(null, '/ajax.php', true);
	    
		kind      = ( null == kind) ? '' : kind;
		subfamily = ( null == subfamily) ? '' : subfamily;
		people    = ( null == people) ? '' : people;
		tag       = ( null == tag) ? '' : tag;
		page      = ( null == page) ? '' : page;
		show      = ( null == show) ? '' : show;
		req.send( { action: 'fullsearch', str1: str1, str2: str2, xsort : xsort, page : page, kind: kind,  people: people, tag: tag, show: show, subfamily: subfamily } );
	}
}

function fullS() {
    fullSearch(_v('search1').value, _v('search2').value, _v('sort').value, '', '', _v('people').value, _v('tag').value, '',  _v('subfamily').value);	
}

function ChgShowType(show) {
	if ( !qstop ) {
		qstop = 1;
		sq    = 0;
		var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
				qstop = 0;
				fullSearch( _v('search1').value, _v('search2').value, _v('sort').value, '{$pages[i].link}', _v('kind').value, _v('people').value, _v('tag').value, '', _v('subfamily').value );
            }
        }
        req.open(null, '/ajax.php', true);
	    req.send( { action: 'chgshow', show: show } );
	}	
}


function ChgCntType(pcnt) {
	if ( !qstop ) {
		qstop = 1;
		sq    = 0;
		var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
				qstop = 0;
				fullSearch( _v('search1').value, _v('search2').value, _v('sort').value, '{$pages[i].link}', _v('kind').value, _v('people').value, _v('tag').value, '', _v('subfamily').value );
            }
        }
        req.open(null, '/ajax.php', true);
	    req.send( { action: 'chgpcnt', pcnt: pcnt } );
	}	
}


function EditFav( id, act ) {
	if ( !qstop ) {
	    qstop = 1;
		sq    = 0;
		var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
				if (req.responseJS.q) {
					if (3 == act || 4 == act) {
						
						_v('but_'+id).innerHTML = '<input class="input_bg00'+((3 == act) ? '2' : '1')+'" type="submit" value="'+((3 == act) ? 'убрать' : 'отобрать')+'" onclick="EditFav('+id+', '+((3 == act) ? '4' : '3')+');" />'; 
						_v('favcnt').innerHTML  = req.responseJS.q2;						
					}					
					else if (1 == act || 2 == act) {
                        _v('but_'+id).innerHTML = '<input class="input_bg00'+((1 == act) ? '22' : '11')+'" type="submit" value="'+((1 == act) ? ' убрать ' : 'отобрать')+'" onclick="EditFav('+id+', '+((1 == act) ? '2' : '1')+');" />';
                        _v('favcnt').innerHTML  = req.responseJS.q2;
					}
				}
				qstop = 0;
            }
        }
        req.open(null, '/ajax.php', true);
		var vact = (2 < 1*act ) ? act - 2 : act;
	    req.send( { action: 'editfav', id: id, act: vact } );
	}	
}

function EditMain( id, act ) {
	if ( !qstop ) {
	    qstop = 1;
		sq    = 0;
		var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
				if (req.responseJS.q) {
					if (3 == act || 4 == act) {
						
						_v('buto_'+id).innerHTML = '<input class="input_bg00'+((3 == act) ? '2' : '1')+'" type="submit" value="'+((3 == act) ? 'с главной' : 'на главную')+'" onclick="EditMain('+id+', '+((3 == act) ? '4' : '3')+');" />'; 					
					}					
					else if (1 == act || 2 == act) {
                        _v('buto_'+id).innerHTML = '<input class="input_bg00'+((1 == act) ? '22' : '11')+'" type="submit" value="'+((1 == act) ? ' с главной ' : 'на главную')+'" onclick="EditMain('+id+', '+((1 == act) ? '2' : '1')+');" />';
					}
				}
				qstop = 0;
            }
        }
        req.open(null, '/ajax.php', true);
		var vact = (2 < 1*act ) ? act - 2 : act;
	    req.send( { action: 'editmain', id: id, act: vact } );
	}	
}

function GetFav() {
	if ( !qstop ) {
	    ShowLoad(1);
		qstop = 1;
		sq    = 0;
		var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
		        _v('out_inf').innerHTML = req.responseJS.q;
				qstop = 0;
				ShowLoad(0);
            }
        }
        req.open(null, '/ajax.php', true);
	    req.send( { action: 'getfav' } );
	}	
}

function LinkFav( res ) {
    if ( !qstop ) {
        _v( res ).style.display = 'none';
        ShowLoad(1);
        qstop = 1;
        sq    = 0;
        var req = new JsHttpRequest();
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                qstop = 0;
                ShowLoad(0);
                _v( res ).style.display = 'block';
            }
        }
        req.open(null, '/ajax.php', true);
        req.send( { action: 'linkfav' } );
    }   
}

function ChgCountry( country )
{
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            _v('places').innerHTML = req.responseJS.q;
        }
    }
    req.open(null, '/ajax.php', true);
    req.send( { action: 'chgcountry', country: country } );       
}


function ShowLoad(t)
{
    if (_v('load'))
	{
	    _v('load').style.display = (t == 1) ? 'block' : 'none';
	}
}

