// base functions
//
// COPYRIGHT DAVID GRUDL 2004, ALL RIGHTS RESERVED
// DON'T USE ANY PART OF THIS CODE WITHOUT PERMISSION!
// http://www.dgx.cz






// AdSense relocator v1
//
// (c) David Grudl aka -dgx-

function adMove(id)
{
    var el = document.getElementById(id);
    var container = document.getElementById(id + '-container');

    if (el && container) {
        container.appendChild(el);
        el.style.display = 'block';
    }
}



////////////////////// POPUP WINDOWS

//
function queryMark(url) {
  return (url.indexOf('?') == -1) ? '?' : '&';
}


// otevře vyskakovací okénko
function popup(url, title) {
  url = url + queryMark(url) + 'popup';
  var wnd = window.open(url, title,'status=0,toolbar=0,location=0,scrollbars=1,width=350,height=300,resizable=1,left=150');
  var opened = (typeof(wnd) == "object");
  return (opened);
}

// otevře vyskakovací okénko s fixní velikostí a bez scrollbarů
function popupFixed(url, title) {
  url = url + queryMark(url) + 'popup&fixed';
  var wnd = window.open(url, title,'status=0,toolbar=0,location=0,scrollbars=0,width=350,height=300,resizable=0,left=150');
  var opened = (typeof(wnd) == "object");
  return (opened);
}


function popupImage(url) {
  return popup('/popup-image.php?f=' + escape(url), 'foto');
}






////////////////////// FORMS
// copyright (c) David Grudl, http://www.dgx.cz


function trim(s) {
  while (s.substr(0, 1) == ' ') s = s.substr(1);
  while (s.substr(s.length-1, 1) == ' ') s = s.substr(0, s.length-2);
  return s;
}


// ověřování formulářů
function validX(e, errormsg, re) {
  var ok;
  var value = trim(e.value);
  if (re) ok = (new RegExp(re)).test(value);
  else ok = value != '';

  if (!ok) {
    e.focus();
    if (errormsg) alert(errormsg);
  }

  return ok;
}


function validEmail(e, errormsg) {
  return validX(e, errormsg, '^[^@]+@[^.]+\\..+$');
}





////////////////////// MISC


// add bookmark / favorites
// example: <a href="#" title="Přidat stránku k oblíbeným odkazům" rel="sidebar" onclick="return AddFavorite(document.location.href,document.title,this);">Oblíbený</a>
function AddFavorite(uri, title, this_a)  {
  if (document.all && !window.opera)  {
    window.external.AddFavorite(uri,title);
    return false;
  }
  else if (window.opera) {
    this_a.title = title;
    return true;
  }
  else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) {
    if (window.confirm('Přidat oblíbenou stránku jako nový panel?'))  {
      window.sidebar.addPanel(title, uri, '');
      return false;
    }
  }
  window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k vašim oblíbeným odkazům.');
  return false;
}


function vote(el)
{
    ajax(el.href);
    //alert(el.firstChild.textContent);
    el.firstChild.nodeValue = 'Děkuji za váš ohlas.';
    return true;
}


function ajax(url)
{
    if (typeof window.ActiveXObject != 'undefined')
    {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        httpRequest = new XMLHttpRequest();
    }

    httpRequest.open("GET", url, true);
    httpRequest.setRequestHeader('Connection', 'Close');
    httpRequest.send(null);
}


function mailReverse(el)
{
  if (el.href.substr(0, 7) != 'mailto:') return;
  var parts=el.href.substr(7).split('@');
  el.href='mailto:' + parts[1] + '@' + parts[0];
  el.onclick = '';
}


function resizeComment(el)
{
    el.style.fontSize='100%';
    el.style.cursor='auto';
    el.onclick = el.title='';
}









//******** La Trine Advertisement

var adv_timeouts = new Array();

function getOffset(element)
{
    var x=0, y=0;
    while (element) {
//        x += element.offsetLeft - element.scrollLeft;
//        y += element.offsetTop - element.scrollTop;
        x += element.offsetLeft;
        y += element.offsetTop;
        element = element.offsetParent;
    }

    var result = new Object(); result.x = x; result.y = y;
    return result;
}


function adv_show(el) {
    var index = el.id.replace(/[^0-9]/g, '');
    var adv   = document.getElementById('adv_' + index);
    var label = document.getElementById('adv_label_' + index);
    if (!adv || !label) return;

    var ofs = getOffset(label);
    adv.style.left = ofs.x + 'px';
    adv.style.top = (ofs.y + 30) + 'px';
    adv.style.display = 'block';

    clearTimeout(adv_timeouts[index]);
}


function adv_do_hide(index) {
    var adv = document.getElementById('adv_' + index);
    adv.style.display = 'none';
}


function adv_hide(el) {
    var index = el.id.replace(/[^0-9]/g, '');
    adv_timeouts[index] = setTimeout('adv_do_hide('+index+');', 200);
}




function reply(id)
{
  var el = document.getElementById('nucleus_cf_body');
  if (el) {
     el.focus();
     if (el.value != '') el.value += "\r\n";
     el.value += '[' + (id) + '] ';
  }
}





$(function init()
{
    $('#add-to-favorite').show().click(function() { return AddFavorite(document.location.href,document.title,this) });

    $('#sb-stat').show();

    $('#sb-stat li span').wrap('<a href="#"></a>').parent().click( function(){ $(this).parent().find('ul').toggle(); return false } );

    $('a[href^=mailto]').each(function(){ mailReverse(this); });

    setTimeout('$("#wrapper").css("background-image", "none");', 6000);


    // TRACKLINKS (c) David Grudl
    $('a[href^=http://]').filter('a:not([href*="' + document.domain + '"])').click(function() {
        pageTracker._trackPageview(this.href.replace(/^http:\/\//i, '/outbound/'));
    });


    // copyright Drupal, GNU General Public License
    $('textarea.resizable:not(.textarea-processed)').each(function() {
        var textarea = $(this).addClass('textarea-processed'), staticOffset = null;

        // When wrapping the text area, work around an IE margin bug.  See:
        // http://jaspan.com/ie-inherited-margin-bug-form-elements-and-haslayout
        $(this).wrap('<div class="resizable-textarea"><span></span></div>')
          .parent().append($('<div class="grippie"></div>').mousedown(startDrag));

        var grippie = $('div.grippie', $(this).parent())[0];
        grippie.style.marginRight = (grippie.offsetWidth - $(this)[0].offsetWidth) +'px';

        function startDrag(e) {
          staticOffset = textarea.height() - e.pageY;
          textarea.css('opacity', 0.25);
          $(document).mousemove(performDrag).mouseup(endDrag);
          return false;
        }

        function performDrag(e) {
          textarea.height(Math.max(32, staticOffset + e.pageY) + 'px');
          return false;
        }

        function endDrag(e) {
          $(document).unbind("mousemove", performDrag).unbind("mouseup", endDrag);
          textarea.css('opacity', 1);
        }
    });
});
