var NOTEPAD_MAX_SIZE = 60;
var COOKIE_MAX_SIZE = 4095//4095;
var tooltip, tooltip_text;
document.write("<div id='tooltip' onclick='hideTooltip();'><div class='tooltip_top'></div><div class='tooltip_bottom'></div><div class='tooltip_line1'></div><div class='tooltip_line2'></div></div>");
function toggleToNotepad(adv_type, adv_id, long_link_flag)
{
 var cookie_str = getCookie('notepad_'+adv_type);
 var notepad_size = getCookie('notepad_'+adv_type).split(';').length-1;
 var prefix_add = "", prefix_remove = "";
 if (long_link_flag == 1) {
   prefix_add = "добавить "; prefix_remove = "удалить ";
 }
 if ((cookie_str.indexOf(';'+adv_id+';') == -1)&&(cookie_str.indexOf(adv_id+';') != 0)) {
   if (notepad_size >= NOTEPAD_MAX_SIZE) {
     alert('В блокноте не может храниться более '+NOTEPAD_MAX_SIZE+' объявлений');
   }
   else if (document.cookie.length > COOKIE_MAX_SIZE) {
     alert('Превышен размер хранимых объявлений в блокноте.\nУдалите ненужные объявления из блокнота.');
   }
   else {
     setCookie('notepad_'+adv_type, cookie_str+adv_id+';', '/');
     if (document.getElementById('id-notepad-'+adv_type+'-'+adv_id) != null)
       document.getElementById('id-notepad-'+adv_type+'-'+adv_id).innerHTML = prefix_remove+"из блокнота";
   }
 }
 else {
   if (cookie_str.indexOf(';'+adv_id+';') != -1)
     setCookie('notepad_'+adv_type, cookie_str.replace(';'+adv_id+';', ';'), '/');
   else if (cookie_str.indexOf(adv_id+';') == 0)
     setCookie('notepad_'+adv_type, cookie_str.replace(adv_id+';', ''), '/');
   if (document.getElementById('id-notepad-'+adv_type+'-'+adv_id) != null)
     document.getElementById('id-notepad-'+adv_type+'-'+adv_id).innerHTML = prefix_add+"в блокнот";
 }
 var notepad_size = getCookie('notepad_'+adv_type).split(';').length-1;
 if (document.getElementById('id-notepad-'+adv_type+'-num-1') != null)
   document.getElementById('id-notepad-'+adv_type+'-num-1').innerHTML = notepad_size;
 if (document.getElementById('id-notepad-'+adv_type+'-num-2') != null)
   document.getElementById('id-notepad-'+adv_type+'-num-2').innerHTML = notepad_size;

 notepad_size = getCookie('notepad_'+adv_type).split(';').length-1;
 if (document.getElementById('id-notepad-'+adv_type+'-clear-1') != null)
 {
  if (notepad_size == 0)
    document.getElementById('id-notepad-'+adv_type+'-clear-1').style['visibility'] = 'hidden';
  else
    document.getElementById('id-notepad-'+adv_type+'-clear-1').style['visibility'] = 'visible';
 }
 if (document.getElementById('id-notepad-'+adv_type+'-clear-2') != null)
 {
   if (notepad_size == 0)
     document.getElementById('id-notepad-'+adv_type+'-clear-2').style['visibility'] = 'hidden';
   else
     document.getElementById('id-notepad-'+adv_type+'-clear-2').style['visibility'] = 'visible';
 }
 return false;
}
function setToNotepad(adv_type, ads_list)
{
 var ads_array = ads_list.split(';');
 if ((ads_list.length > 1) && (document.cookie.length < COOKIE_MAX_SIZE))
   setCookie('notepad_'+adv_type, ads_array.join(';')+';', '/');
 else if ((ads_list.length > 1) && (document.cookie.length > COOKIE_MAX_SIZE))
   alert('Превышен размер хранимых объявлений в блокноте.\nУдалите ненужные объявления из блокнота.');
 else
   setCookie('notepad_'+adv_type, '', '/');
 return false;
}
function clearNotepad(adv_type)
{
 var ads_array = getCookie('notepad_'+adv_type).split(';');
 for (var i = 0; i < ads_array.length-1; i++)
   if (document.getElementById('id-notepad-'+adv_type+'-'+ads_array[i]) != null)
     document.getElementById('id-notepad-'+adv_type+'-'+ads_array[i]).innerHTML = "в блокнот";
 setCookie('notepad_'+adv_type, '', '/');
 if (document.getElementById('id-notepad-'+adv_type+'-num-1') != null)
   document.getElementById('id-notepad-'+adv_type+'-num-1').innerHTML = '0';
 if (document.getElementById('id-notepad-'+adv_type+'-num-2') != null)
   document.getElementById('id-notepad-'+adv_type+'-num-2').innerHTML = '0';
 if (document.getElementById('id-notepad-'+adv_type+'-clear-1') != null)
  document.getElementById('id-notepad-'+adv_type+'-clear-1').style['visibility'] = 'hidden';
 if (document.getElementById('id-notepad-'+adv_type+'-clear-2') != null)
   document.getElementById('id-notepad-'+adv_type+'-clear-2').style['visibility'] = 'hidden';
 return false;
}

function setCookie (c_name, value, path, domain, secure) {
 var exdate=new Date();          //expiredays
 exdate.setDate(exdate.getDate()+9999);
 document.cookie = c_name + "=" + escape(value) + ";expires=" + exdate.toUTCString() + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}
function getCookie(c_name)
{
 if (document.cookie.length>0)
 {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
  {
   c_start=c_start + c_name.length+1;
   c_end=document.cookie.indexOf(";",c_start);
   if (c_end==-1) c_end=document.cookie.length;
   return unescape(document.cookie.substring(c_start,c_end));
  }
 }
 return "";
}

function overXV(obj, cl) {
 obj.className='icon' + cl + 'over';
 showTooltip(obj.parentNode, obj.getAttribute('tooltip_text'), 1);
}

function outXV(obj, cl) {
 obj.className='icon' + cl;
 hideTooltip();
}

function ge() {
 var ea;
 for (var i = 0; i < arguments.length; i++) {
   var e = arguments[i];
   if (typeof e == 'string')
     e = document.getElementById(e);
   if (arguments.length == 1)
     return e;
   if (!ea)
     ea = new Array();
   ea.push(e);
 }
 return ea;
}

function getXY(obj) {
 if (!obj || obj == undefined) return;
 var left = 0, top = 0;
 if (obj.offsetParent) {
  do {
   left += obj.offsetLeft;
   top += obj.offsetTop;
  } while (obj = obj.offsetParent);
 }
 return [left,top];
}

function showTooltip(obj, text, direction) {
 text = text.replace(/ /g, "&nbsp;");
 tooltip = ge('tooltip');
 var coords = getXY(obj.childNodes[0]); //pavkorev_mod
 tooltip.childNodes[0].innerHTML = text;
 tooltip_text = text;
 tooltip.style.left = (coords[0])+'px';
 tooltip.style.top = (coords[1]-24)+'px';
 tooltip.style.visibility = 'hidden';
 tooltip.style.display = 'block';
 tooltip.style.top = (coords[1]-tooltip.offsetHeight)+'px';
 tooltip.childNodes[1].style.width = '0px';
 tooltip.childNodes[2].style.width = '0px';
 tooltip.childNodes[3].style.width = '0px';
 var w = tooltip.offsetWidth;
 tooltip.childNodes[2].style.width = w+'px';
 tooltip.childNodes[3].style.width = w+'px';
 tooltip.childNodes[1].style.width = w+'px';
 tooltip.childNodes[1].style.display = 'none';
 if (direction)
   tooltip.style.left = (coords[0]-w+obj.childNodes[0].offsetWidth)+'px';
 tooltip.style.visibility = 'visible';
}
function hideTooltip() {
 if (!tooltip)
   return;
 tooltip.style.display = 'none';
}

function toggleNote(adv_type, adv_id, mode)
{
 if (document.getElementById('id-note-input-'+adv_type+'-'+adv_id+'') == null)
   showNoteInput(adv_type, adv_id, mode);
 else
   setNote(adv_type, adv_id, mode);
 return false;
}
function showNoteInput(adv_type, adv_id, mode)
{
 var user_guid = getCookie("myautouid");
 if (user_guid.length == 0) {
   user_guid = Math.uuid();
   setCookie('myautouid', user_guid, '/');
 }
 var margin_bottom = '';
 if (mode == 2) margin_bottom = ' margin-bottom:10px;';
 if (document.getElementById('id-note-'+adv_type+'-'+adv_id) != null)
 {
   document.getElementById('id-note-'+adv_type+'-'+adv_id).innerHTML =
     '<table style="border: 1px solid #d4bc4c; background-color: #f9f6e7;'+margin_bottom+' font: 11px bold verdana,arial; color:#333;"><tr><td class="padding10">'+
     '<input type=text maxlength=180 style="width:100%;margin:0px 2px 0px 2px;" tooltip_text="Ваша заметка будет видна только вам!"'+
     'onfocus="showTooltip(this.parentNode, this.getAttribute(\'tooltip_text\'), 0);" onblur="hideTooltip();" id="id-note-input-'+adv_type+'-'+adv_id+'"'+
     'onkeydown="if(event.keyCode == 13 || event.keyCode == 27) { this.blur(); hideTooltip(); setNote(\''+adv_type+'\', '+adv_id+', '+mode+'); }"></td>'+
     '<td width=19 class=valign_middle><img id="id-note-loading-'+adv_id+'" style="display:none; width:16px; height:16px;" src="/images/loading_16.gif" align="absmiddle"><span><img id="id-note-OK-'+adv_id+'" class="iconSover" onclick="hideTooltip(); return setNote(\''+adv_type+'\', '+adv_id+', '+mode+');" src="/images/one.gif" align="absmiddle"></span></td>'+
     '<td width=8></td></tr></table>';
 }
 if (document.getElementById('id-note-loading-'+adv_id)) {
  document.getElementById('id-note-OK-'+adv_id).style.display = 'none';
  document.getElementById('id-note-loading-'+adv_id).style.display = 'inline';
 }
 xmlhttp = createRequest();
 xmlhttp.onreadystatechange = function()
 {
   if (xmlhttp.readyState == 4)// "complete"
   {
     if (xmlhttp.status == 200) // "OK"
     {
       var note_result = xmlhttp.responseText.substr(0, xmlhttp.responseText.length).split('|');
       if (document.getElementById('id-note-loading-'+adv_id)) {
         document.getElementById('id-note-loading-'+adv_id).style.display = 'none';
         document.getElementById('id-note-OK-'+adv_id).style.display = 'inline';
         document.getElementById('id-note-input-'+adv_type+'-'+adv_id).value = note_result[3];
         document.getElementById('id-note-input-'+adv_type+'-'+adv_id).focus();
       }
     }
   }
 };
 xmlhttp.open("GET", 'http://'+location.host+'/common_fns/_adv_note.php?user_guid='+encodeURIComponent(user_guid)+'&action=get_note&adv_type='+encodeURIComponent(adv_type)+'&adv_id='+encodeURIComponent(adv_id), true);
 xmlhttp.send(null);
 return false;
}
function setNote(adv_type, adv_id, mode, delete_note_flag)
{
 var user_guid = getCookie("myautouid");
 if (delete_note_flag == 1)
   var note = "";
 else
   var note = document.getElementById('id-note-input-'+adv_type+'-'+adv_id).value;

 if (user_guid.length == 0) {
   user_guid = Math.uuid();
   setCookie('myautouid', user_guid, '/');
 }
 if (document.getElementById('id-note-loading-'+adv_id)) {
  document.getElementById('id-note-OK-'+adv_id).style.display = 'none';
  document.getElementById('id-note-loading-'+adv_id).style.display = 'inline';
 }
 xmlhttp = createRequest();
 xmlhttp.onreadystatechange = function()
 {
   if (xmlhttp.readyState == 4)// "complete"
   {
     if (xmlhttp.status == 200) // "OK"
     {
       var note_result = xmlhttp.responseText.substr(0, xmlhttp.responseText.length).split('|');
       displayNote(adv_type, adv_id, note_result[3], mode);
       if (document.getElementById('id-note-loading-'+adv_id))
         document.getElementById('id-note-loading-'+adv_id).style.display = 'none';
       setCookie('notes_num_'+adv_type, note_result[5], '/');
       if (document.getElementById('id-notes-'+adv_type+'-num-1') != null)
         document.getElementById('id-notes-'+adv_type+'-num-1').innerHTML = note_result[5];
       if (document.getElementById('id-notes-'+adv_type+'-num-2') != null)
         document.getElementById('id-notes-'+adv_type+'-num-2').innerHTML = note_result[5];
       if (document.getElementById('id-notes-'+adv_type+'-clear-1') != null)
       {
         if (note_result[5] == 0)
           document.getElementById('id-notes-'+adv_type+'-clear-1').style['visibility'] = 'hidden';
         else
           document.getElementById('id-notes-'+adv_type+'-clear-1').style['visibility'] = 'visible';
       }
       if (document.getElementById('id-notes-'+adv_type+'-clear-2') != null)
       {
         if (note_result[5] == 0)
           document.getElementById('id-notes-'+adv_type+'-clear-2').style['visibility'] = 'hidden';
         else
           document.getElementById('id-notes-'+adv_type+'-clear-2').style['visibility'] = 'visible';
       }
     }
   }
 };
 xmlhttp.open("GET", 'http://'+location.host+'/common_fns/_adv_note.php?user_guid='+encodeURIComponent(user_guid)+'&action=set_note&adv_type='+encodeURIComponent(adv_type)+'&adv_id='+encodeURIComponent(adv_id)+'&note='+encodeURIComponent(note), true);
 xmlhttp.send(null);
 return false;
}
function clearNotes(adv_type)
{
 var user_guid = getCookie("myautouid");
 if (user_guid.length == 0)
   return false;
 xmlhttp = createRequest();
 xmlhttp.onreadystatechange = function()
 {
   if (xmlhttp.readyState == 4)// "complete"
   {
     if (xmlhttp.status == 200) // "OK"
     {
       var note_result = xmlhttp.responseText.substr(0, xmlhttp.responseText.length).split('|');
       var notes_array = note_result[1].split(';');
       setCookie('notes_num_'+adv_type, 0, '/');
       if (document.getElementById('id-notes-'+adv_type+'-num-1') != null)
        document.getElementById('id-notes-'+adv_type+'-num-1').innerHTML = '0';
       if (document.getElementById('id-notes-'+adv_type+'-num-2') != null)
        document.getElementById('id-notes-'+adv_type+'-num-2').innerHTML = '0';
       if (document.getElementById('id-notes-'+adv_type+'-clear-1') != null)
        document.getElementById('id-notes-'+adv_type+'-clear-1').style['visibility'] = 'hidden';
       if (document.getElementById('id-notes-'+adv_type+'-clear-2') != null)
        document.getElementById('id-notes-'+adv_type+'-clear-2').style['visibility'] = 'hidden';
       for (var i = 0; i < notes_array.length; i++)
         if (document.getElementById('id-note-'+adv_type+'-'+notes_array[i]) != null)
           document.getElementById('id-note-'+adv_type+'-'+notes_array[i]).innerHTML = "";
     }
   }
 };
 xmlhttp.open("GET", 'http://'+location.host+'/common_fns/_adv_note.php?user_guid='+encodeURIComponent(user_guid)+'&action=delete_all&adv_type='+adv_type, true);
 xmlhttp.send(null);
 return false;
}
function displayNote(adv_type, adv_id, note, mode)
{
 if (note.length > 0) {
   if (mode == 0)
     document.getElementById('id-note-'+adv_type+'-'+adv_id).innerHTML = note;
   else if (mode == 1)
     document.getElementById('id-note-'+adv_type+'-'+adv_id).innerHTML =
     '<table style="border: 1px solid #d4bc4c; background-color: #f9f6e7;"><tr><td style="padding:10px; font: 11px bold verdana,arial; color:#333;">'+note+'</td>'+
     '<td width=32 class=valign_middle><img id="id-note-loading-'+adv_id+'" style="display:none; width:16px; height:16px;" src="/images/loading_16.gif" align="absmiddle"><span id="id-note-OK-'+adv_id+'"><span><img class="iconE" onclick="hideTooltip();return showNoteInput(\''+adv_type+'\', '+adv_id+', '+mode+');" tooltip_text="Редактировать эту заметку" onmouseover="this.className=\'iconEover\';showTooltip(this.parentNode, this.getAttribute(\'tooltip_text\'), 1);" onmouseout="this.className=\'iconE\';hideTooltip();" src="/images/one.gif" align="absmiddle"></span>'+
     '<span><img class="iconX" onclick="hideTooltip();return setNote(\''+adv_type+'\', '+adv_id+', '+mode+', 1);" tooltip_text="Удалить эту заметку" onmouseover="this.className=\'iconXover\';showTooltip(this.parentNode, this.getAttribute(\'tooltip_text\'), 1);" onmouseout="this.className=\'iconX\';hideTooltip();" src="/images/one.gif" align="absmiddle"></span></span>'+
     '<td width=7></td></tr></table>';
   else if (mode == 2)
     document.getElementById('id-note-'+adv_type+'-'+adv_id).innerHTML =
     '<table style="border: 1px solid #d4bc4c; background-color: #f9f6e7; margin-bottom:10px;"><tr><td style="padding:10px; font: 11px bold verdana,arial; color:#333;">'+note+'</td>'+
     '<td width=32 class=valign_middle><img id="id-note-loading-'+adv_id+'" style="display:none; width:16px; height:16px;" src="/images/loading_16.gif" align="absmiddle"><span id="id-note-OK-'+adv_id+'"><span><img class="iconE" onclick="hideTooltip();return showNoteInput(\''+adv_type+'\', '+adv_id+', '+mode+');" tooltip_text="Редактировать эту заметку" onmouseover="this.className=\'iconEover\';showTooltip(this.parentNode, this.getAttribute(\'tooltip_text\'), 1);" onmouseout="this.className=\'iconE\';hideTooltip();" src="/images/one.gif" align="absmiddle"></span>'+
     '<span><img class="iconX" onclick="hideTooltip();return setNote(\''+adv_type+'\', '+adv_id+', '+mode+', 1);" tooltip_text="Удалить эту заметку" onmouseover="this.className=\'iconXover\';showTooltip(this.parentNode, this.getAttribute(\'tooltip_text\'), 1);" onmouseout="this.className=\'iconX\';hideTooltip();" src="/images/one.gif" align="absmiddle"></span></span>'+
     '<td width=7></td></tr></table>';
 }
 else {
   document.getElementById('id-note-'+adv_type+'-'+adv_id).innerHTML = '';
 }
}

//Math.uuid.js (v1.4) http://www.broofa.com mailto:robert@broofa.com
//Copyright (c) 2010 Robert Kieffer Dual licensed under the MIT and GPL licenses.
(function() {
  var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  Math.uuid = function () {
    var chars = CHARS, uuid = [];
    var r;
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
    uuid[14] = '4';
    for (var i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | Math.random()*16;
        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
      }
    }
    return uuid.join('');
  };
})();