// COPYRIGHT DAVID GRUDL 2004, ALL RIGHTS RESERVED

// ověřování formulářů
function validX(e, errormsg, re) {
	var value = $.trim($(e).val());
	if (re ? !(new RegExp(re)).test(value) : (value == '')) {
		e.focus();
		alert(errormsg);
		return false;
	}
	return true;
}


function validEmail(e, errormsg) {
	return validX(e, errormsg, '^[^@]+@[^.]+\\..+$');
}



function vote(el) {
    $.get(el.href);
    $(el).replaceWith('Děkuji za váš hlas.');
    var karma = $('#karma');
    karma.text(parseInt(karma.text(), 10) + 1);
    return true;
}

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) });

    $('#stat').show();

    $('#stat li span').wrap('<a href="#"></a>').parent().click( function(){ $(this).parent().find('ul').toggle(); return false } );

    $('a[href^=mailto]').each(function(){
		if (this.href.substr(0, 7) != 'mailto:') return;
		var parts=this.href.substr(7).split('@');
		this.href='mailto:' + parts[1] + '@' + parts[0];
		this.onclick = '';
	});

    // 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);
        }
    });
});






//******** http://www.latrine.cz/strihak

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);
}
