var IE = document.all ? true : false;

if(!IE) 
	document.captureEvents(Event.MOUSEMOVE);

var inputsStatus = new Array();
var areasStatus = new Array();

$(document).ready(function()
{
	$(document).pngFix();
	
	$('.aboutCanvas').corner('round 15px');
	$('.projectsHeaderBox').corner('round 15px');
	$('.welcomeHeaderBox').corner('round 15px');
	$('.descriptionCanvas').corner('round 15px');
	$('.serviceCanvas').corner('round 15px');
	$('.portfolioCanvas').corner('round 15px');
	$('.previewCanvas').corner('round 15px');
	$('.contactCanvas').corner('round 15px');
	$('.formCanvas').corner('round 15px');
	$('#messageText').corner('bottom');
	
	//przypisanie akcji pobrania wspolrzednych oraz wyswietlenia popup do linkow ktore je wywoluja
	$('.footerLink').bind('click', function(e)
	{
		var inputName = $(this).get(0).name;
		$('.popupBox').hide();
		var coords = getMousePosition(e);
//		alert(inputName+' x: ' + coords.x + ' y: ' + coords.y);
		var popupW = $('#'+inputName+'PopupBox').width();
		var popupH = $('#'+inputName+'PopupBox').height();
		var newW = coords.x - (popupW/2) + 60;
		var newH = coords.y - popupH - 7;
		
		$('#'+inputName+'PopupBox').css({'left': newW, 'top': newH}).show();
	});
	
	//TEXT INPUT
	//wypelnienie tablicy statusow pol tekstowych
	$('input').each(function (index, value)
	{
		inputsStatus[value.id] = 1;
	});
	//po pierwszym kliknieciu w pole tekstowe usuwamy wprowadzony napis i zmieniamy style
	$('input.preFont').bind('focus', function()
	{
		var inputId = $(this).get(0).id;
		
		if(inputsStatus[inputId] == 1)
		{
			$(this).val('');
			$(this).removeClass('preFont');
			inputsStatus[inputId] = 0;
		}
	});
	
	//TEXT AREA
	//wypelnienie tablicy statusow pol tekstowych
	$('textarea').each(function (index, value)
	{
		areasStatus[value.id] = 1;
	});
	//po pierwszym kliknieciu w pole tekstowe usuwamy wprowadzony napis i zmieniamy style
	$('textarea.preFont').bind('focus', function()
	{
		var inputId = $(this).get(0).id;
		
		if(areasStatus[inputId] == 1)
		{
			$(this).val('');
			$(this).removeClass('preFont');
			areasStatus[inputId] = 0;
		}
	});
});

function showDesc(idItem, image, link, tooltip)
{
	var effectTime = 600;
	
	//zamiana obrazkow ze strzlka zwin/rozwin
	$("[id^=showDescImage]").each(function (index, value)
	{
		var currentImageId = $(this).get(0).id;
		currentImageId = currentImageId.substring(currentImageId.length-1, currentImageId.length);
		var imgsrc = $('#showDescImage'+currentImageId).attr('src');
		
		if(currentImageId == idItem)
		{
			$('#showDescImage'+currentImageId).attr('src', imgsrc.replace('expand', 'collapse'));
		}
		else
		{
			$('#showDescImage'+currentImageId).attr('src', imgsrc.replace('collapse', 'expand'));
		}
	});
	
	//chowanie i pokazywanie ramki z opisem projektu
	$("[id^=portfolioDescBox]:visible").each(function (index, value)
	{
		var currentImageId = $(this).get(0).id;
		currentImageId = currentImageId.substring(currentImageId.length-1, currentImageId.length);
		
		//ramka opisu
		$('#portfolioDescBox'+currentImageId).animate(
		{ 
			height: "0px"
	    }, effectTime, function()
	    	{
	    		$('#portfolioDescBox'+currentImageId).hide();
	    		$('#portfolioDescBox'+idItem).show().animate(
	    		{ 
	    			height: "150px"
	    		}, effectTime+200);
	    	}
	    );
		
		//podglad strony projektu
		$('#previewImageBox').fadeOut(effectTime, function()
		{
			$('#previewImageBox').html
    		(
    			'<a href="'+link+'" title="'+tooltip+'">' +
    			' <img src="'+image+'" alt="'+tooltip+'">' +
    			'</a>'
    		);	    			
		});
		$('#previewImageBox').fadeIn(effectTime+200);
	});
}

function clearFormFields()
{
	//reset statusow pol tekstowych
	for(i in inputsStatus)
	{
		inputsStatus[i] = 1;
	}
	//reset statusow pol tekstowych typu area
	for(i in areasStatus)
	{
		areasStatus[i] = 1;
	}
	
	$('#name').addClass('preFont').val('Wpisz swoje imię i nazwisko');
	$('#email').addClass('preFont').val('Podaj swoj@email.me');
	$('#title').addClass('preFont').val('Wprowadź tytuł wiadomości');
	$('#message').addClass('preFont').val('Wprowadź treść wiadomości');
}

function checkFormFields(nameInput, emailInput, titleInput, messageInput)
{
	if(nameInput == null || $.trim(nameInput).length == 0 || inputsStatus['name'] == 1)
	{
		showMessage('Brak imienia i nazwiskia.', 'notice', true);
		return 1;
	}
	
	if(emailInput == null || $.trim(emailInput).length == 0 || inputsStatus['email'] == 1)
	{
		showMessage('Nie podano adresu email.', 'notice', true);
		return 2;			
	}
	
	if(emailInput.indexOf('@') < 0 || emailInput.indexOf('.') < 0 || inputsStatus['email'] == 1)
	{
		showMessage('Błędny format adresu email.', 'notice', true);
		return 2;
	}
	
	if(messageInput == null || $.trim(messageInput).length == 0 || areasStatus['message'] == 1)
	{
		showMessage('Pusta wiadomość.', 'notice', true);
		return 3;
	}
	
	if(titleInput == null || $.trim(titleInput).length == 0 || inputsStatus['title'] == 1)
	{
		if(!confirm('Wysłać wiadomość bez tematu ?'))
		{
			return 4;	
		}
	}

	return 0;	
}
 
function sendMail(url)
{
	var name = $("#name").val();
	var email = $("#email").val();
	var title = $("#title").val();
	var message = $("#message").val();

	var formFieldCorrect = checkFormFields(name, email, title, message);
	
	if(formFieldCorrect == 0)
	{
		showMessage('Wysyłanie wiadomości...', 'notice', false);
		
		$.post(url, {name_input: name, email_input: email, title_input: title, message_input: message}, function(data)
		{
			if(data == 'true') 
			{
				clearFormFields();
				
				hideMessage(200, function()
				{
					showMessage('Wiadomość została wysłana.', 'correct', true);
				});
				
			}
			else 
			{
				hideMessage(200, function()
				{
					showMessage('Błąd wysyłania wiadomości.', 'error', true);
				});
				
			}
	   	});
	}
}

function showMessage(message, type, isDisapear)
{
	$('#messageText').text(message).removeClass('correct error notice').addClass(type).animate({top: '-2px'}, 500);
	$('#messageText').corner('bottom');	
	
	if(isDisapear)
	{
		setTimeout(function ()
		{
			hideMessage();	
		}, 5000);
	}
}

function hideMessage(speed, callback)
{
	$('#messageText').animate({top: '-30px'}, (speed != null ? speed : 700), callback);
}

function getMousePosition(e)
{
	var posx = 0;
	var posy = 0;
	
	if(!e)
		var e = window.event;
		
	if(e.clientX || e.clientY) 	
	{
		posx = e.clientX;
		posy = e.clientY;
	}
	
	return {x: posx, y: posy};
}

function closePopup()
{
	$('.popupBox').hide();
}
