var maudit_domain_id;
var maudit_maudit_domain = 'maudit.study.ru';
var maudit_count_false_load = 0;
var maudit_max_false_load = 100;
var maudit_doc_ref = document.referrer;
var maudit_href = window.top.location.href;
var maudit_hostname = window.location.hostname;
var maudit_callback_loaded = null;
var maudit_div_class = null;
//var maudit_out_div_id = null;
var maudit_Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = maudit_Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = maudit_Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

//if ((typeof this.window['maudit_function_exists'] != 'function') || window.opera ){
 function maudit_function_exists(function_name){
  return false;
 }
//}

if(! maudit_function_exists('maudit_Get_Cookie')) {
function maudit_Get_Cookie( maudit_check_name ) {
	var maudit_a_all_cookies = document.cookie.split( ';' );
	var maudit_a_temp_cookie = '';
	var maudit_cookie_name = '';
	var maudit_cookie_value = '';
	var maudit_b_cookie_found = false; // set boolean t/f default f
	var maudit_i = '';
	
	for ( maudit_i = 0; maudit_i < maudit_a_all_cookies.length; maudit_i++ )
	{
		maudit_a_temp_cookie = maudit_a_all_cookies[maudit_i].split( '=' );
		maudit_cookie_name = maudit_a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( maudit_cookie_name == maudit_check_name )
		{
			maudit_b_cookie_found = true;
			if ( maudit_a_temp_cookie.length > 1 )
			{
				maudit_cookie_value = unescape( maudit_a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return maudit_cookie_value;
			break;
		}
		maudit_a_temp_cookie = null;
		maudit_cookie_name = '';
	}
	if ( !maudit_b_cookie_found ) 
	{
		return null;
	}
}
}

if(! maudit_function_exists('maudit_Set_Cookie')) {
function maudit_Set_Cookie(  maudit_name,  maudit_value,  maudit_expires,  maudit_path,  maudit_domain,  maudit_secure ) {
	var maudit_today = new Date();
	 maudit_today.setTime(  maudit_today.getTime() );
	if (  maudit_expires )
	{
		 maudit_expires =  maudit_expires * 1000 * 60 * 60 * 24;
	}
	var  maudit_expires_date = new Date(  maudit_today.getTime() + ( maudit_expires) );
	document.cookie =  maudit_name + "=" +escape(  maudit_value ) +
		( (  maudit_expires ) ? ";expires=" +  maudit_expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( (  maudit_path ) ? ";path=" + path : "" ) + 
		( (  maudit_domain ) ? ";domain=" + domain : "" ) +
		( (  maudit_secure ) ? ";secure" : "" );
}
}

if(! maudit_function_exists('maudit_Delete_Cookie')) {
function maudit_Delete_Cookie( maudit_name, maudit_path, maudit_domain ) {
	if ( Get_Cookie( maudit_name ) ) document.cookie = maudit_name + "=" +
			( ( maudit_path ) ? ";path=" + maudit_path : "") +
			( ( maudit_domain ) ? ";domain=" + maudit_domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
}

if (! maudit_function_exists('maudit_in_page')){
function maudit_in_page(){
  var maudit_in_page_img  = new Image();
  maudit_in_page_img.src = 'http://'+maudit_maudit_domain+'/show/'+maudit_domain_id+'/'+maudit_n_date+'/'+maudit_uid+'?ref='+escape(maudit_Base64.encode(maudit_doc_ref))+'&hostname='+escape(maudit_Base64.encode(maudit_hostname))+'&url='+escape(maudit_Base64.encode(maudit_href))+'&counter=1';
  maudit_in_page_img.onload = function(){
  //setTimeout(function() { maudit_in_page();}, 30000);
 }
}
}

if (! maudit_function_exists('maudit_show_div')){
function maudit_show_div(){ 
 div = document.getElementById('maudit_div'+maudit_n_date);
 img = document.getElementById('maudit_img'+maudit_n_date);
 if (img.height > 2){  
 if (div){
  div.style.width = img.width+'px';
  div.style.height = img.height+'px';  
 }
  if (maudit_callback_loaded != null){
   maudit_callback_loaded();
  }
 };
}
}


if (! maudit_function_exists('maudit_get_id')){
function maudit_get_id(o_stat){
 var self = document.getElementById('maudit_img'+maudit_n_date); 
  maudit_random = Math.round(Math.random()*100000);
  self.onload = function(){
   if ((self.width <= 2) && (self.height <=1)) {
    self.style.display = 'none';
    if ((self.width < 2) && (maudit_count_false_load < maudit_max_false_load)){
     setTimeout(function() { maudit_get_id(o_stat); }, 1000);
    }
   } else {
    maudit_uid = maudit_n_date+''+maudit_random;
    maudit_Set_Cookie('maudit_uid',maudit_uid,365);
    maudit_show_div();    
    //setTimeout(function() { maudit_in_page();}, 30000);
   };
  };  
  self.src='http://'+maudit_maudit_domain+'/show/'+maudit_domain_id+'/'+maudit_n_date+'/'+maudit_n_date+''+maudit_random+'?ref='+escape(maudit_Base64.encode(maudit_doc_ref))+'&hostname='+escape(maudit_Base64.encode(maudit_hostname))+'&url='+escape(maudit_Base64.encode(maudit_href))+o_stat;
  self.style.display = '';  
}
}



if (! maudit_function_exists('maudit_get_image')){
function maudit_get_image(domain_id,only_stat){
 var o_stat = '';
 if (only_stat){ o_stat = '&only_stat=true';};
 maudit_domain_id = domain_id * 1;
 if (maudit_uid * 1 > 0){
  document.write('<div '+( ( maudit_div_class == null) ? '' : 'class="'+maudit_div_class+'"') +' id="maudit_div'+maudit_n_date+'" style="display:block !important; width:0px; height:0px; overflow:hidden !important;"><img onload="maudit_show_div();" id="maudit_img'+maudit_n_date+'" src="http://'+maudit_maudit_domain+'/show/'+maudit_domain_id+'/'+maudit_n_date+'/'+maudit_uid+'?ref='+escape(maudit_Base64.encode(maudit_doc_ref))+'&hostname='+escape(maudit_Base64.encode(maudit_hostname))+'&url='+escape(maudit_Base64.encode(maudit_href))+o_stat+'" /></div>');
  maudit_Set_Cookie('maudit_uid',maudit_uid,365);
  setTimeout(function() { maudit_in_page();}, 30000);
 } else {
  document.write('<div '+( ( maudit_div_class == null) ? '' : 'class="'+maudit_div_class+'"') +' id="maudit_div'+maudit_n_date+'" style="display:block !important; width:0px; height:0px; overflow:hidden !important;"><img id="maudit_img'+maudit_n_date+'" /></div>');
  maudit_get_id(o_stat);
 }
}
}
var maudit_uid = maudit_Get_Cookie('maudit_uid');
var maudit_n_date = new Date().getTime();
var maudit_random;

