if (document.compatMode == "BackCompat") {
document.write('<style type="text/css">'+
'#krible { width:1px; height:1px; position:absolute; bottom:0px; z-index:1000; display:block; overflow:hidden; }'+ // todo: change block to none
'body > div#krible { position: fixed; }'+
'.right { right:0px; }'+
'.left { left:0px; }'+
'</style>'+
'<!--[if IE]>'+
'<style type="text/css">'+
'div#krible.right {'+
'  position:absolute !important; right: auto; bottom: auto;'+
'  left: expression( ( 0 - krible.offsetWidth + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + "px" );'+
'  top: expression( ( 0 - krible.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + "px" );'+
'}'+
'div#krible.left {'+
'  position:absolute !important; left:0px; bottom: auto;'+
'  top: expression( ( 0 - krible.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + "px" );'+
'}'+
'</style>'+
'<![endif]-->');
} else {
document.write('<style type="text/css">'+
'#krible { width:250px; height:370px; position:fixed; bottom:-400px; z-index:1000; display:block; }'+
'.right { right:0px; }'+
'.left { left:0px; }'+
'</style>');
}



var Krible = new Object();
Krible.callCount = 0;  // маркер вызова статистики


// функции определения домена и работы с куками
function kribleGetDomain(url){if(url){return url.match(/:\/\/(.[^/]+)/)[1];}else{return"undefined";}}
function kribleGetCookie(name){cookie_name=name+"=";cookie_length=document.cookie.length;cookie_begin=0;while(cookie_begin<cookie_length){value_begin=cookie_begin+cookie_name.length;if(document.cookie.substring(cookie_begin,value_begin)==cookie_name){var value_end=document.cookie.indexOf(";",value_begin);if(value_end==-1){value_end=cookie_length;}return unescape(document.cookie.substring(value_begin,value_end));}cookie_begin=document.cookie.indexOf(" ",cookie_begin)+1;if(cookie_begin==0){break;}}return null;}
function kribleSetCookie(name,value,days){expires=new Date();if(days!=0){expires.setTime(expires.getTime()+(1000*86400*days));document.cookie=name+"="+escape(value)+"; expires="+expires.toGMTString()+"; path=/";}else{document.cookie=name+"="+escape(value)+"; expires=0; path=/";}}


function kribleDetectBrowser ()
{
	Krible.browser = (function x(){})[-5]=='x'?'FF3':(function x(){})[-6]=='x'?'FF2':/a/[-1]=='a'?'FF':'\v'=='v'?'IE':/a/.__proto__=='//'?'Saf':/s/.test(/a/.toString)?'Chr':/^function \(/.test([].sort)?'Op':'Unknown'
	Krible.IE6 = /msie|MSIE 6/.test(navigator.userAgent);
}


function kribleGetMessage(event) 
{
	kribleOnOff(event.data);
	return;
}


function kribleOnOff(call) {  
//alert(call);
    // проверяем вызов для модных браузеров
    try {
        if (call.indexOf('krible') > -1)  Krible.callStr = call;
        } catch (e) {
        Krible.callStr = null;
    }


    // проверяем вызов для старых браузеров
    try {
        if (!Krible.callStr && location.hash.indexOf('krible') > -1) Krible.callStr = location.hash;
        } catch (e) {
         Krible.callStr = null;    
    }


    // парсим строку и определяем тип вызова
    if (Krible.callStr) {
        if (Krible.callStr == 'kribleOn' || Krible.callStr == '#kribleOn') { Krible.call = 'on' }
        if (Krible.callStr == 'kribleOff' || Krible.callStr == '#kribleOff') { Krible.call = 'off' }
        if (Krible.callStr == 'kribleBar' || Krible.callStr == '#kribleBar') { Krible.call = 'bar' }
    }
    
    
  if (Krible.call == 'on') {
   // if (Krible.callCoun == 0) {
 //   alert("ON");
        //Krible.windowChat.style.display = "block";
        Krible.windowChat.style.width = "250px";
        Krible.windowChat.style.height = "370px";
        Krible.windowChat.style.bottom = "0px";
        //Krible.windowChat.style.right = "0px";
        //kribleSendStatMessage('show');
        Krible.callCount = 1; // маркер на отправки статистики
   // }
  } 
  
  if (Krible.call == 'bar') {
   // if (Krible.callCoun == 0) {
    //alert("BAR");
        //Krible.windowChat.style.display = "block";
        Krible.windowChat.style.width = "250px";
        Krible.windowChat.style.height = "30px";
        Krible.windowChat.style.bottom = "0px";
        //kribleSendStatMessage('show');
        Krible.callCount = 1; // маркер на отправки статистики
   // }
  }
  
  if (Krible.call == 'off') {
	//if (Krible.callCount == 1) {
  //  alert("OFF");
	    //Krible.windowChat.style.display = "none";
        Krible.windowChat.style.width = "0px";
        Krible.windowChat.style.height = "0px";
        Krible.windowChat.style.bottom = "-400px";
        //Krible.windowChat.style.right = "-300px";
		//kribleSendStatMessage('close');
	    Krible.callCount = 0; // маркер на отправки статистики
	//}
  }
}



if (window.addEventListener) 
{
	if (typeof window.postMessage != 'undefined') 
	{
		window.addEventListener("message", kribleGetMessage, false);
	}
}

if (window.attachEvent)
{
	if (typeof window.postMessage != 'undefined') 
	{
		window.attachEvent('onmessage', kribleGetMessage);
	}
}



Krible.referrer = kribleGetCookie('kribleReferrer');

if(!Krible.referrer)
{
	var documentDomain = kribleGetDomain(document.URL);
	var referrerDomain = kribleGetDomain(document.referrer);

	if(documentDomain != referrerDomain && referrerDomain != 'undefined')
	{
	kribleSetCookie('kribleReferrer', document.referrer, 0);
	Krible.referrer = document.referrer;
	}
	else
	{
	Krible.referrer = 'none';
	}
}


function kribleSetPosition () 
{
	var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
	Krible.windowChat.style.top = 20 + iebody.scrollTop +'px';
}


function kribleSendStatMessage(message) {
	var params = 'status='+ message +'&arena='+ kribleCode +'&browser='+ Krible.browser;
	if (typeof window.postMessage == 'undefined') {
			Krible.windowProxyStat.contentWindow.location = 'http://chat.krible.ru/proxy_stat.html#'+ params;
			Krible.windowProxyStat.width = Krible.windowProxyStat.width > 50 ? 50 : 100;
	}
	else {
		if (window.addEventListener) {
		Krible.windowChat.contentWindow.postMessage(params, "http://chat.krible.ru/");
		}
		else {
			Krible.windowChat.contentWindow.postMessage(params, "*");
		}
	}
}



try {
	kriblePosition;
} catch (e) {  kriblePosition = 'right'; }

try {
    kribleOffline;
} catch (e) {  kribleOffline = false; }


document.write('<iframe id="kribleProxyStat" name="kribleProxyStat" style="position:absolute; left:-150px; top:-150px;" src="http://chat.krible.ru/proxy_stat.html" width="10" height="10" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');

document.write('<div id="krible" class="'+ kriblePosition +'"><iframe name="kribleChat" src="' +
		'http://chat.krible.ru/1.0.8/?'+
		'url=' + escape(document.URL) +
		'&domain=' + escape(document.domain) +
		'&ref='+ escape(Krible.referrer) +
		//'&title=' + escape(document.getElementsByTagName("title")[0].innerHTML) +
		'&code=' + kribleCode  +
		'&offline=' + escape(kribleOffline) +
		'&bot=general' +
	'" width="250" height="370" scrolling="no" frameborder="0" allowtransparency="true"></iframe></div>');


kribleDetectBrowser();

Krible.windowChat = document.getElementById('krible');
Krible.windowProxyStat = document.getElementById('kribleProxyStat');


if (typeof window.postMessage == 'undefined') {
	var kribleCallID = setInterval("kribleOnOff()", 1000);
}

try {
if (kribleAutorun) kribleOnOff('kribleBar');
} catch (e) {}

var kribleCallID = setInterval("kribleOnOff()", 1000);
//alert(Krible.windowProxy.contentWindow.location);
//setInterval("kribleGetMessageFromProxy()", 5000);
