/* This is a comment. I love comments :-) */

var dumb = function() { };

/* I love phpBB, hmm...? */

var text_enter_url = "Введите полный адрес для ссылки.";
var text_enter_url_name = "Заголовок страницы.";
var text_enter_image = "Введите адрес картинки.";
var text_enter_email = "Введите e-mail.";
var error_no_url = "Введите URL.";
var error_no_title = "Введите описание.";
var error_no_email = "Введите e-mail.";
var error_no_width = "Введите ширину флешки.";
var error_no_height = "Введите высоту флешки.";

function sts_help()
 {
  alert('Вы можете использовать следующие теги: a, b, i, u, s, img, font, p, br, table, th, td, tr, ul, li, ol, cut');
 }

function sts_kolbaskin_styler(b, id)
 {
  if (!id) id = 'ta';

  sts_styler(b, id, "<font color=" + b.options[b.selectedIndex].value + ">", "</font>"); 
  b.selectedIndex = 0;
 }

function sts_kolbaskin_fonter(b, id)
 {
  if (!id) id = 'ta';

  sts_styler(b, id, "<font size=" + b.options[b.selectedIndex].value + ">", "</font>"); 
  b.selectedIndex = 0;
 }

function sts_bolder(b, id)
 {
  sts_tagger(b, id, "<b>", "</b>");
 }

function sts_italicer(b, id)
 {
  sts_tagger(b, id, "<i>", "</i>");
 }

function sts_cut(b, id)
 {
  if (!id) id = 'ta';

  sts_put(id, '<cut>');
 }

function sts_underliner(b, id)
 {
  sts_tagger(b, id, "<u>", "</u>");
 }

function sts_blockquoter(b, id)
 {
  sts_tagger(b, id, "<blockquote>", "</blockquote>");
 }

function sts_uler(b, id)
 {
  sts_tagger(b, id, "<ul><li>", "</ul>");
 }

function sts_oler(b, id)
 {
  sts_tagger(b, id, "<ol><li>", "</ol>");
 }

function sts_tagger(b, id, open, close)
 {
  if (!id) id = 'ta';

  ta = document.getElementById(id);

  if (!b.srcvalue)
   b.srcvalue = b.value;

  if (document.selection)
   {
    theSelection = document.selection.createRange().text;

    if (theSelection) 
     {
      ta.focus();
      document.selection.createRange().text = open + theSelection + close;
      theSelection = '';
      return;
     }
   }
  else
   {
    ta.value += open + close;
    return;
   }

  b.tagged = !b.tagged;
  ta.focus();

  if (b.tagged)
   {
    b.value = b.srcvalue + '*';

    if (document.selection)
     document.selection.createRange().text += open;
    else
     ta.value += open;
   }
  else
   {
    b.value = b.srcvalue;

    if (document.selection)
     document.selection.createRange().text += close;
    else
     ta.value += close;
   }
 }

function sts_styler(b, id, open, close)
 {
  ta = document.getElementById(id);

  if (document.selection)
   {
    theSelection = document.selection.createRange().text;

    if (theSelection) 
     {
      document.selection.createRange().text = open + theSelection + close;
      theSelection = '';
      ta.focus();
      return;
     }
   }
  else
   {
    ta.value += open + close;
    return;
   }

  ta.focus();
  document.selection.createRange().text += open + close;
 }

function sts_put(id, s)
 {
  id = document.getElementById(id);
  id.focus();

  if (document.selection)
   document.selection.createRange().text += s;
  else
   ta.value += s;
 }

function sts_put_opener(id, s)
 {
  if (window.opener)
   {
    id = window.opener.document.getElementById(id);

    if (id)
     {
      id.focus();

      if (window.opener.document.selection)
       window.opener.document.selection.createRange().text += s;
      else
       id.value += s;
     }
   }
 }

function sts_image(b, id)
 {
  if (!id) id = 'ta';

  var FoundErrors = '';
  var enterURL = prompt(text_enter_image, "http://");

  if (!enterURL)
   FoundErrors += "\n" + error_no_url;

  if (FoundErrors)
   alert("Ошибка!" + FoundErrors);
  else
   sts_put(id, '<img src="' + enterURL + '" border="0">');
 }

function sts_link(b, id)
 {
  if (!id) id = 'ta';

  var FoundErrors = '';
  var enterURL = prompt(text_enter_url, "http://");
  var enterTITLE = prompt(text_enter_url_name, "Описание страницы");

  if (!enterURL)
   FoundErrors += "\n" + error_no_url;

  if (!enterTITLE)
   FoundErrors += "\n" + error_no_title;
 
  if (FoundErrors)
   alert("Ошибка!" + FoundErrors);
  else
   sts_put(id, '<a href="' + enterURL + '">' + enterTITLE + '</a>');
 }

function sts_email(b, id)
 {
  if (!id) id = 'ta';

  var emailAddress = prompt(text_enter_email, "");

  if (!emailAddress) 
   alert(error_no_email);
  else
   sts_put(id, '<a href="mailto:' + emailAddress + '">' + emailAddress + '</a>');
 }

/* various preview handlers */

function PreviewShowNotes()
 {
  if (f = document.getElementById('EditorForm'))
   {
    f.preview.value = '1';
    f.target = 'efpreview';
    f.submit();
   }

  if (p = document.getElementById('PreviewPanel'))
   {
    p.style.display = 'block';
    f.target = '';
    f.preview.value = '0';
   }

  return 0;
 }

function PreviewComment()
 {
  if (f = document.getElementById('EditorForm'))
   {
    f.preview.value = '1';
    f.target = 'efpreview';
    f.submit();
   }

  if (p = document.getElementById('PreviewPanel'))
   {
    p.style.display = 'block';
    f.target = '';
    f.preview.value = '0';
   }

  return 0;
 }

function PreviewPersonalInfo()
 {
  if (f = document.getElementById('EditorForm'))
   {
    f.preview.value = '1';
    f.target = 'efpreview';
    f.submit();
   }

  if (p = document.getElementById('PreviewPanel'))
   {
    p.style.display = 'block';
    f.target = '';
    f.preview.value = '0';
   }

  return 0;
 }

function PreviewFeedInfo()
 {
  if (f = document.getElementById('EditorForm'))
   {
    f.preview.value = '1';
    f.target = 'efpreview';
    f.submit();
   }

  if (p = document.getElementById('PreviewPanel'))
   {
    p.style.display = 'block';
    f.target = '';
    f.preview.value = '0';
   }

  return 0;
 }

function PicturesManager()
 {
  width = 600;
  height = 400;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open('/cp/pictures_manager.html', 
                        '', 
                        'toolbar=no,' +
                        'scrollbars=yes,' + 
                        'status=no,' +
                        'height=' + height + ',' + 
                        'width=' + width + ',' + 
                        'top=' + top + ',' + 
                        'left=' + left);

  win.focus();
 }

function CommentsPicturesManager()
 {
  width = 600;
  height = 400;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open('/cp/pictures_manager.html', 
                        '', 
                        'toolbar=no,' +
                        'scrollbars=yes,' + 
                        'status=no,' +
                        'height=' + height + ',' + 
                        'width=' + width + ',' + 
                        'top=' + top + ',' + 
                        'left=' + left);

  win.focus();
 }

function ViewPicture(id, width, height)
 {
  width = width + 40;
  height = height + 40;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open('/cp/pictures_manager.html?view=' + id, 
                        '', 
                        'toolbar=no,' +
                        'scrollbars=auto,' + 
                        'status=no,' +
                        'height=' + height + ',' + 
                        'width=' + width + ',' + 
                        'top=' + top + ',' + 
                        'left=' + left);

  win.focus();
 }

/* taken from yandex.mail, the greatest mail I've ever seen
   (Bobuk, hi :) */

function dropFile(btn)
 {
  if(document.getElementById) 
   {
    tr = btn;
    while (tr.tagName != 'TR') tr = tr.parentNode;
    tr.parentNode.removeChild(tr);
    checkForLast();
   }
 }

function addFile(btn)
 {
  if(document.getElementById) 
   {
    tr = btn;
    while (tr.tagName != 'TR') tr = tr.parentNode;
    var idSuffix = Math.round(Math.random()*1000);
    var newTr = tr.parentNode.insertBefore(tr.cloneNode(true),tr.nextSibling);
    thisChilds = newTr.getElementsByTagName('td');
    for (var i = 0; i < thisChilds.length; i++)
     {
      if (thisChilds[i].className == 'header') thisChilds[i].innerHTML = '';
      if (thisChilds[i].className == 'files') thisChilds[i].innerHTML = '<input size="32" name="att[]" class="attfile" type="file">';
     }
    checkForLast();
   }
 }

function checkForLast()
 {
  btns = document.getElementsByName('drop');
  for (i = 0; i < btns.length; i++)
   {
    btns[i].disabled = (btns.length == 1) ? true : false;
   }
 }

/* stolen, yea */

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

/* RTFM */

function RTFM(link, data)
 {
  if (!(link = xGetElementById(link))) return 0;
  if (!(data = xGetElementById(data))) return 0;

  link.style.display = 'none';
  data.style.display = 'block';
 }

/* our megacommenting engine :) produced by LJ & Kolbaskin :) */

var prevkapusta = '';
var prevswitch = '';
var prev1 = 0;
var prev2 = 0;

function FocusCommenter()
 {
  if (ta = xGetElementById('ta'))
   ta.focus();
 }

function DelayedFocusCommenter()
 {
  setTimeout('FocusCommenter();', 250);
 }

function BringCommentForm(atarget, target, ownerid)
 {
  if (!(t = xGetElementById(target))) return;
  if (!(a = xGetElementById(atarget))) return;
  if (!(c = xGetElementById('CommentFormContainer'))) return;

  if ((prev1) && (prev2))
   SwapNodes(prev1, prev2);

  SwapNodes(c, t);

  prev1 = c;
  prev2 = t;

  if (prevkapusta)
   if (p = document.getElementById(prevkapusta))
    p.style.display = 'inline';

  a.style.display = 'none';
  c.style.display = 'block';

  prevkapusta = atarget;

  if (o = xGetElementById('coid'))
   o.value = ownerid;

  setTimeout('FocusCommenter();', 250);

  return;
 }

/* LiveJournal, respect :) */

function SwapNodes(orig, to_swap)
 {
  var orig_pn = xParent(orig, true);
  var next_sibling = orig.nextSibling;
  var to_swap_pn = xParent(to_swap, true);

  if (!to_swap_pn)
   return false;

  to_swap_pn.replaceChild(orig, to_swap);
  orig_pn.insertBefore(to_swap, next_sibling);

  return true;
 }

/* The PodHead */

var podsaid_timeout = 0;

function PodSaid()
 {
  HidePodSaid();

  new Ajax.Updater({success: 'podsaid2'}, 
                   rpc_baseurl + 'podhead.html', 
                   {method: 'get', 
                    parameters: '?digest=' + escape(rpc_digest) + '&' + Math.random(), 
                    onComplete: ShowPodSaid});
 }

function HidePodSaid()
 {
  if (!(p1 = $('podsaid1'))) return 0;
  if (!(p2 = $('podsaid2'))) return 0;

  p1.style.display = 'none';
  p2.style.display = 'none';

  if (podsaid_timeout) clearTimeout(podsaid_timeout);

  podsaid_timeout = setTimeout('PodSaid();', 30 * 1000);
 }

function ShowPodSaid(x)
 {
  if (!(p1 = $('podsaid1'))) return 0;
  if (!(p2 = $('podsaid2'))) return 0;

  if (p2.innerHTML.length > 0)
   {
    p1.style.display = 'block';
    p2.style.display = 'block';
   }
 }

function PodLive()
 {
  new Ajax.Updater({success: 'podlive'}, 
                   rpc_baseurl + 'podlive.html', 
                   {method: 'get', 
                    parameters: '?digest=' + escape(rpc_digest) + '&' + Math.random(), 
                    onComplete: RePodLive});
 }

function RePodLive()
 {
  setTimeout('PodLive();', 60 * 1000);
 }

function Startup()
 {
  PodSaid();
  RePodLive();
 }

function EditPodcast(podcastID)
 {
  var win = window.open('/edit_podcast.html?podcastID=' + podcastID,
                        '', 
                        'toolbar=no,' +
                        'scrollbars=yes,' +
                        'status=no,' +
                        'height=500,' +
                        'width=400');
  win.focus();
 }

function EditFeed(feedID)
 {
  var win = window.open('/edit_feed.html?feedID=' + feedID,
                        '', 
                        'toolbar=no,' +
                        'scrollbars=yes,' +
                        'status=no,' +
                        'height=600,' +
                        'width=500');
  win.focus();
 }

function EditUser(userID)
 {
  var win = window.open('/edit_user.html?userID=' + userID,
                        '', 
                        'toolbar=no,' +
                        'scrollbars=yes,' +
                        'status=no,' +
                        'height=500,' +
                        'width=400');
  win.focus();
 }

function GotSelector(s)
 {
  s = s.responseText;
  s = s.split("\x0D\x0A");

  if (s.length == 0) return;
  if (!(target = $(s[0]))) return;

  target.length = 0;

  for (k = 1; k < s.length - 1; k++)
   {
    y = s[k].split("\<\.\>");

    o = new Option();
    o.text = y[1];
    o.value = y[0];
    target.options[target.length] = o;
   }

  if (target.onchange) target.onchange();
 }

function Selector(src, dest, kind)
 {
  target = $(dest);

  o = new Option();  
  o.text = 'загрузка...';
  o.value = '0';

  target.length = 0;
  target.options[target.length] = o;

  new Ajax.Request(rpc_baseurl + 'selector.html',
                   {method: 'get',
                    parameters: 'src=' + escape(src) + 
                                '&dest=' + escape(dest) + 
                                '&kind=' + escape(kind) + 
                                '&value=' + escape($(src).value) + 
                                '&digest=' + escape(rpc_digest) + 
                                '&' + Math.random(),
                    onComplete: GotSelector});
 }

function VotePodcast(podcastID, feedID, vote)
 {
  params = 'podcastID=' + podcastID + '&' +
           'feedID=' + feedID + '&' +
           'vote=' + vote + '&' +
           'digest=' + rpc_digest;

  new Ajax.Updater('pv' + podcastID,
                   '/ajax/podcast_vote.html',
                   {asynchronous: true, 
                    onSuccess: dumb,
                    method: 'post',
                    parameters: params });

  return false;
 }

function SharePodcast(podcastID, feedID)
 {
  width = 600;
  height = 400;

  var left = Math.round((document.body.clientWidth - width) / 2);
  var top = Math.round((document.body.clientHeight - height) / 2);

  var win = window.open('/share_podcast.html?podcastID=' + podcastID + '&feedID=' + feedID, 
                        '', 
                        'toolbar=no,' +
                        'scrollbars=yes,' + 
                        'status=no,' +
                        'height=' + height + ',' + 
                        'width=' + width + ',' + 
                        'top=' + top + ',' + 
                        'left=' + left);

  win.focus();

  return false;
 }
