// 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 = '';
	});

    setTimeout('$(".phpfashion-com #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);
        }
    });

	$("pre").each(function(){
		var contentwidth = $(this).contents().width();
		var blockwidth = $(this).width();
		var blockheight = $(this).height();
		if (contentwidth > blockwidth) {
			$(this).hover(function() {
				$(this).stop(true, true).animate({ width: Math.min(900, contentwidth + 10) + "px", height: blockheight + "px"}, 250);
			}, function() {
				$(this).stop(true, true).animate({ width: blockwidth, height: blockheight + "px" }, 250);
			});
		}
	});
});
