$(document).ready(function(){	
	
var tmp='12345';

setInterval(function() {
    $.ajax({
	url: '/chat_sys/getstatus.php',
	    success: function (response) {
		if (tmp != response) {
		    drawConsult(response);
		    tmp = response;
		}
	    }
    });
}, 9000);


function drawConsult(val){
var x = getConsultantStatusCookie('consultant_state');

    if (val == 1){
	document.getElementById('consult-h-off').style.display = "none";

	if (x == null){
	    $('#consult').show('slide', { direction: 'right' }, 500);		
    	    document.getElementById('consult-h').style.display = "none";
        };

	if (x == 1){
	    $('#consult').show('slide', { direction: 'right' }, 500);		
            document.getElementById('consult-h').style.display = "none";
        };

        if (x == 0){
	    document.getElementById('consult').style.display = "none";
            $('#consult-h').show('slide', { direction: 'right' }, 500);		
        };
    };

    if (val == 0){
	document.getElementById('consult').style.display = "none";
	document.getElementById('consult-h').style.display = "none";
	$('#consult-h-off').show('slide', { direction: 'right' }, 500);		
    }
};



	$('.cons-close-button').click(function(){		
		$('#consult').hide();
		$('#consult-h').show('slide', { direction: 'right' }, 500);		
// hide
		setConsultantStatusCookie(0);
	});
	
	$('#consult-h').click(function(){		
		$('#consult').show('slide', { direction: 'right' }, 500);		
		$('#consult-h').hide();
// show
		setConsultantStatusCookie(1);		
	});
});

function setConsultantStatusCookie(val){
    var exdate=new Date();
        exdate.setDate(exdate.getDate()+365);
                document.cookie="consultant_state="+val + ";expires="+exdate.toGMTString()+";path=/"
};

function getConsultantStatusCookie(name){
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
	
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    	    }
    return null;
};
