$(document).ready(function(){
    /**********************************************************
     * menu-products
     *
     **********************************************************/
/*
	$(".select-products").toggle(
	function(){
		$(".products-box-outer").show()
	},
	function(){
		$(".products-box-outer").hide()
	});

    $(".products-box li a").click(function(){
		var htmlStr = $(this).html();
		$(".select-products").text(htmlStr);
		$(".products-box-outer").hide();

	});
*/

    /**********************************************************
     * contact mail
     *
     **********************************************************/
	var lock = false;
	$("#submitContact").click(function(){
        if(!lock){
            lock = true;
            $.ajax({
                type: "POST",
                url: "/index/contact",
                data: $(".form_cont").serialize(),
                dataType: "json",
                error: function (o){
                    alert("Nie można wysłać zapytania");
                },
                success: function(msg){
                    if(msg.status == 'ok'){

                    	$('.contactMsg').text('Twoja wiadomość została wysłana. Dziękujemy za kontakt.');
						$('.form_cont').remove();
                    }
                    else {
                    	if(msg.errors.title == 'required') {
							$('.title-message').addClass('wrong');
						} else {
							$('.title-message').removeClass('wrong');
						}

                    	if(msg.errors.name == 'required') {
							$('.name').addClass('wrong');
						} else {
							$('.name').removeClass('wrong');
						}

                    	if(msg.errors.surname == 'required') {
							$('.surname').addClass('wrong');
						} else {
							$('.surname').removeClass('wrong');
						}

                        if(msg.errors.mail == 'required') {
							$('.mail').addClass('wrong');
						} else {
							$('.mail').removeClass('wrong');
						}

                    	if(msg.errors.content == 'required') {
							$('.content').addClass('wrong');
						} else {
							$('.content').removeClass('wrong');
						}
                    }
                    lock = false;
                }
            });
        }
        return false;
    });


	$("textarea.content").focus(function(){
		if($(this).text() == 'Tutaj wpisz treść wiadomości...') {
			$(this).text('');
		}
	});


	$("textarea.content").blur(function(){
		if($(this).text() == '') {
			$(this).text('Tutaj wpisz treść wiadomości...');
		}
	});


	$('.leftBtn').click(function(){

		var rel = $(this).attr('rel');

		if(parseInt(rel) > 1) {
			$('.rightBtn').show();
			$('#popup p').css('display', 'none');
			$('.sentence_' + rel).prev().css('display', 'block');
			$('.leftBtn').attr('rel', parseInt(rel) - 1);
			$('.rightBtn').attr('rel', parseInt(rel) - 1);
		}

		if(parseInt(rel) == 2) {
			$(this).hide();
		}

		return false;
	});


	$('.rightBtn').click(function(){

		var rel = $(this).attr('rel');

		if(parseInt(rel) + 1 <= $('#popup p').size()) {
			$(this).show();
			$('.leftBtn').show();
			$('#popup p').css('display', 'none');
			$('.sentence_' + rel).next().css('display', 'block');
			$('.leftBtn').attr('rel', parseInt(rel) + 1);
			$('.rightBtn').attr('rel', parseInt(rel) + 1);
		}

		if(parseInt(rel) == $('#popup p').size() - 1) {
			$(this).hide();
		}

		return false;
	});


	$('.closeBtn').click(function(){

		$('#popup').hide();

		return false;
	});


	$('.pnts-fct').click(function(){

		$('#popup').show();

		return false;
	});


	$('.videoPrev').click(function(){

		var prev_id = $(this).attr('rel');
		var current_id = parseInt(prev_id) + 1;

		$('#movie_' + current_id).hide();
		$('#movie_' + prev_id).show();

		$('.count').text(prev_id);

		return false;
	});

	$('.videoNext').click(function(){

		var next_id = $(this).attr('rel');
		var current_id = parseInt(next_id) - 1;

		$('#movie_' + current_id).hide();
		$('#movie_' + next_id).show();

		$('.count').text(next_id);

		return false;
	});

	var carouselPage = 1;
	var pagesCount = Math.round(parseInt($('#mycarouselBox').attr('rel')) - 2);

	$('.carouselPrev').click(function(){

		var last = $('#mycarousel li:last').clone();
		var last = $('#mycarousel li:last').remove();

		$('#mycarousel').prepend(last);

		carouselPage--;


		if(carouselPage == 1) {
			$('.carouselPrev').hide();
		}

		$('.carouselNext').show();

		return false;
	});

	$('.carouselNext').click(function(){

		if(carouselPage >= pagesCount) {
			$('.carouselNext').hide();
		}

		$('.carouselPrev').show();

		var first = $('#mycarousel li:first').clone();
		var first = $('#mycarousel li:first').remove();

		$('#mycarousel').append(first);

		carouselPage++;

		return false;
	});
});




