function createTooltip(element, guid)
{
	var leftPos = getLeftPos(element) + element.offsetWidth - 250;
	var topPos = getTopPos(element) + 34;
	
	if (!$(guid + '_tooltip')) {
	    div = document.createElement('div');
	    div.id = guid + '_tooltip';
	    div.className = 'tooltip';
	    
	    form = document.createElement('form');
	    div.appendChild(form);
	    
	    fieldset = document.createElement('fieldset');
	    fieldset.id = guid + '_tooltip_data';
	    
	    a = document.createElement('a');
	    a.href = '';
	    a.onclick = function() { $(guid + '_tooltip').parentNode.removeChild($(guid + '_tooltip')); return false; };
	    a.innerHTML = 'zamknij';
	    fieldset.appendChild(a);
	    
	    p = document.createElement('p');
	    p.id = guid + '_tooltip_recipientBlock';
	    
	    a = document.createElement('a');
	    a.href = '';
	    a.id = guid + '_tooltip_addRecipient';
	    a.className = 'add';
	    a.onclick = function() { return addRecipient(guid); };
	    a.innerHTML = 'dodaj odbiorcę';
	    p.appendChild(a);
	    
	    label = document.createElement('label');
	    label.innerHTML = 'Email odbiorcy';
	    p.appendChild(label);
	    
	    input = document.createElement('input');
	    input.id = guid + '_tooltip_recipientEmail1';
	    input.type = 'text';
	    input.className = 'full';
	    p.appendChild(input);
	    
	    span = document.createElement('span');
	    span.id = guid + '_tooltip_recipientEmailError';
	    span.className = 'error';
	    p.appendChild(span);
	    
	    fieldset.appendChild(p);
	    
	    p = document.createElement('p');
	    
	    label = document.createElement('label');
	    label.innerHTML = 'Twoje imię';
	    p.appendChild(label);
	    
	    input = document.createElement('input');
	    input.id = guid + '_tooltip_senderName';
	    input.type = 'text';
	    input.className = 'full';
	    p.appendChild(input);
	    
	    span = document.createElement('span');
	    span.id = guid + '_tooltip_senderNameError';
	    span.className = 'error';
	    p.appendChild(span);
	    
	    fieldset.appendChild(p);
	    
	    p = document.createElement('p');
	    
	    label = document.createElement('label');
	    label.innerHTML = 'Twój email';
	    p.appendChild(label);
	    
	    input = document.createElement('input');
	    input.id = guid + '_tooltip_senderEmail';
	    input.type = 'text';
	    input.className = 'full';
	    p.appendChild(input);
	    
	    span = document.createElement('span');
	    span.id = guid + '_tooltip_senderEmailError';
	    span.className = 'error';
	    p.appendChild(span);
	    
	    fieldset.appendChild(p);
	    
	    p = document.createElement('p');
	    
	    input = document.createElement('input');    
	    input.type = 'button';
	    input.className = 'blueButton';
	    input.value = 'wyślij';
	    input.onclick = function () { return sendLink(guid); };
	    p.appendChild(input);
	    
	    fieldset.appendChild(p);
	    
	    form.appendChild(fieldset);
	    
	    $('middleWrapper').appendChild(div);
	    
    	$(guid + '_tooltip').style.left = leftPos + 'px';
    	$(guid + '_tooltip').style.top = topPos + 'px';
    	$(guid + '_tooltip').style.display = 'block';
	}
	
	return false;
}


function getTopPos(element)
{		
	var returnValue = element.offsetTop;
	
	while ((element = element.offsetParent) != null) {
		if (element.tagName != 'html') {
			returnValue += element.offsetTop;
		}
	}
	
	return returnValue;
}


function getLeftPos(element)
{
	var returnValue = element.offsetLeft;
	
	while ((element = element.offsetParent) != null) {
		if (element.tagName != 'html') {
			returnValue += element.offsetLeft;
		}
	}
	
	return returnValue;
}

function addRecipient(guid)
{
    var recipients = $A($(guid + '_tooltip_recipientBlock').getElementsByTagName('input'));
    var nextId = 1;
    
    recipients.each(function(recipient) {
		id = parseInt(recipient.id.replace(guid + '_tooltip_recipientEmail', ''));
		if (nextId <= id)
		{
		    nextId = id + 1;
		}
	});
    
    input = document.createElement('input');
    input.id = guid + '_tooltip_recipientEmail' + nextId;
    input.className = 'full';
    input.type = 'text';
    $(guid + '_tooltip_recipientBlock').insertBefore(input, $(guid + '_tooltip_recipientEmailError'));
    
    if (nextId == 15) {
        $(guid + '_tooltip_recipientBlock').removeChild($(guid + '_tooltip_addRecipient'));
    }
    
    return false;
}

function sendLink(guid)
{
    $(guid + '_tooltip_senderNameError').innerHTML = '';
    $(guid + '_tooltip_senderEmailError').innerHTML = '';
    $(guid + '_tooltip_recipientEmailError').innerHTML = '';
    
    var recipients = $A($(guid + '_tooltip_recipientBlock').getElementsByTagName('input'));
    var error = false;
    
    if (!$(guid + '_tooltip_senderName').value) 
    {
        $(guid + '_tooltip_senderNameError').innerHTML = 'Proszę podać swoje imię';
        error = true;
    }
    
    if (!$(guid + '_tooltip_senderEmail').value) {
        $(guid + '_tooltip_senderEmailError').innerHTML = 'Proszę podać swój email';
        error = true;
    } else if (!validEmail($(guid + '_tooltip_senderEmail').value)) {
        $(guid + '_tooltip_senderEmailError').innerHTML = 'Nieprawidłowy format emaila';
    }
    
    var isRecipient = false;
    var isCorrectRecipient = true;
    recipients.each(function(recipient) {
        if (recipient.value) {
            isRecipient = true;
            if (!validEmail(recipient.value)) {
                isCorrectRecipient = false;
            }
        }
	});
	
    if (!isRecipient) {
        $(guid + '_tooltip_recipientEmailError').innerHTML = 'Proszę podać adres odbiorcy';
        error = true;
    } else if (!isCorrectRecipient) {
        $(guid + '_tooltip_recipientEmailError').innerHTML = 'Nieprawidłowy format emaila';
        error = true;
    }
    
    if (!error) {
        var parameters = 'ajax=1&senderName=' + encodeURIComponent($(guid + '_tooltip_senderName').value) + '&senderEmail=' + encodeURIComponent($(guid + '_tooltip_senderEmail').value);
        
        recipients.each(function(recipient) {
            parameters += '&recipientEmail[]=' + encodeURIComponent(recipient.value);
    	});
    	
    	$(guid + '_tooltip_data').innerHTML = '';
        $(guid + '_tooltip_data').className = 'centerText';
        
        img = document.createElement('img');
        img.id = guid + '_tooltip_loader';
        img.src = siteUrl + 'img/loading-big.gif';
        img.alt = 'proszę czekać';
	
        $(guid + '_tooltip_data').appendChild(img);
        
    	new Ajax.Request(siteUrl + 'news/' + guid + '/wyslij_link',
      	{
    		asynchronous: true,
       		method: 'post',
       		parameters: parameters,
       		onComplete: function(obj) 
       		{
       		    if (obj.responseText == 1) {
       		        $(guid + '_tooltip_data').innerHTML = 'Wiadomość została wysłana.<br /><br />';
       		    } else {
       		        $(guid + '_tooltip_data').innerHTML = 'Wystąpił błąd. Proszę spróbować później.<br /><br />';
       		    }
       		    
       		    input = document.createElement('input');
       		    input.type = 'button';
       		    input.className = 'blueButton';
       		    input.value = 'ok';
       		    input.onclick = function () {
       		        $(guid + '_tooltip').parentNode.removeChild($(guid + '_tooltip'));
       		    }
       		    $(guid + '_tooltip_data').appendChild(input);
       		}
       	});
    }
    
    return false;
}


function vote(guid, vote)
{
    vote = vote == 'minus' ? 'minus' : 'plus';
    
    $('_votes_' + guid).style.display = 'none';
    
    img = document.createElement('img');
    img.id = guid + '_loader';
    img.src = siteUrl + 'img/loading.gif';
    img.alt = 'proszę czekać';
    $('_votes_' + guid).parentNode.appendChild(img);
    
    new Ajax.Request(siteUrl + 'news/' + guid + '/zaglosuj',
  	{
		asynchronous: true,
   		method: 'post',
   		parameters: 'ajax=1&vote=' + encodeURIComponent(vote),
   		onComplete: function(obj) 
   		{
   		    response = obj.responseText.split('|');
   		    $('_votes_' + guid).parentNode.removeChild($(guid + '_loader'));

   		    if (parseInt(response[0]) == 1) {
       		    $('_votes_' + guid).innerHTML = '';
       		    span = document.createElement('span');
       		    span.className = response[1] == 0 ? 'gray' : response[1] > 0 ? 'green' : 'red';
                span.innerHTML = response[1];
                $('_votes_' + guid).appendChild(span);
       		    $('_votes_' + guid).style.display = 'block';
   		    } else {
   		        span = document.createElement('span');
                span.innerHTML = parseInt(response[0]) == 0 ? response[1] : 'Błąd. Spróbuj później';
                $('_votes_' + guid).parentNode.appendChild(span);
                $('_votes_' + guid).parentNode.removeChild($('_votes_' + guid));
   		    }
   		}
   	});
   	
   	return false;
}


function validEmail(email) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	return filter.test(email) ? true : false;
}


function updateArchiveSelects()
{
    if ($('exactlyDate').checked) {
        $('day').disabled = false;
        $('month').disabled = false;
        $('year').disabled = false;
        
        $('fromDay').disabled = true;
        $('fromMonth').disabled = true;
        $('fromYear').disabled = true;
        $('toDay').disabled = true;
        $('toMonth').disabled = true;
        $('toYear').disabled = true;
    } else if ($('rangeDate').checked) {
        $('day').disabled = true;
        $('month').disabled = true;
        $('year').disabled = true;
        
        $('fromDay').disabled = false;
        $('fromMonth').disabled = false;
        $('fromYear').disabled = false;
        $('toDay').disabled = false;
        $('toMonth').disabled = false;
        $('toYear').disabled = false;
    }
}


function onStatusChange()
{
    $('rejectedReasonBlock').style.display = $('status').value == 'rejected' ? 'block' : 'none';
}


function onAdvertTypeChange()
{
    $('typeFileBlock').style.display = $('type').value == 'file' ? 'block' : 'none';
    $('typeCodeBlock').style.display = $('type').value == 'code' ? 'block' : 'none';
}


function showQuestionText(element)
{
    element.getElementsByTagName('span')[0].style.display = 'block';
}

function hideQuestionText(element)
{
    element.getElementsByTagName('span')[0].style.display = 'none';
}