/**
 * @author qmmr
 * http://qmmr.pl
 */

$(function() {
	/* homepage slideshow plugin */
	$('#featured').cycle({
		fx: 'uncover',
		direction: 'down',
		random: 1,
		pause: 1,
		speed: 1000,
		timeout: 7500
	});

	/* homepage mail button animation */
	$("#mail-box").hover(
		function(){
			$(this).stop().animate({'backgroundColor':'#1f1f1f'}, 500);
		},
		function(){
			$(this).stop().animate({'backgroundColor':'#4f4f4f'}, 500);
		}
	);

	/* kontakt page social icons animation */
	$('.slidebtn').hover(
		function () {
			var $this = $(this),
                $slidelem = $this.prev(),
                thisWidth = $this.attr("rel");
                
			$slidelem.stop().animate({'width':thisWidth, 'background-color':'#3C3C3C'}, 500, "easeOutExpo");
			$slidelem.find('span').stop(true,true).fadeIn();
			//$this.addClass('button_c');
		},
		function () {
			var $this = $(this),
                $slidelem = $this.prev();
                
			$slidelem.stop().animate({'width':'32px', 'background-color':'#FFFFFF'},500, "easeOutSine");
			$slidelem.find('span').stop(true,true).fadeOut();
			//$this.removeClass('button_c');
		}
	); // end of slidebtn.hover

	/* kontakt page form validation */
	var kf = $("#kontakt_form");
    if(kf.length === 1){
        kf.validate({
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                },
                msg: {
                    required: true,
                    rangelength: [20, 255]
                }
            },
            messages: {
                name: "Jak mam się do Ciebie zwracać?",
                email: {
                   required: "Podaj email, bym mógł Ci odpisać!",
                   email: "To nie jest poprawny adres email"
                },
                msg: {
                    required: "Co chcesz mi przekazać?",
                    rangelength: "Min. ilość znaków 20, max. 255"
                }
            }
        });

        kf.submit(function() {
            if(kf.valid()) {
                var user_data = "name="+$("#name").val()+"&email="+$("#email").val()+"&url="+$("#url").val()+"&msg="+$("#msg").val(),
                    msg = "<h5 class=\"center hidden\">Dziękuję za kontakt, Twoja wiadomość została wysłana.<br>Możesz spodziewać się odpowiedzi w przeciągu następnych 24h.</h5><span class='email_big absolute hidden'></span>",
                    mail = $(".email_big").css({"top":"-280px", "left":"40%"});
                $("#kontakt_body").html(msg).find("h5").hide();
                $.ajax({
                    type: "POST",
                    data: user_data,
                    // url: "http://localhost/qmmr/includes/kontakt.php",
                    url: "http://qmmr.pl/includes/kontakt.php",
                    dataType: "json",
                    success: function(data) {
                        mail.animate({top: +50}, 1000, "easeOutBounce", function(){$("h5").fadeIn(500);});
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) { alert ("błąd podczas wysyłania email'a: "+textStatus); }
                }); // end of ajax
            }	// end of kf.valid()
            return false;
        }); // end of kf.submit
    }

}); // end of $(function)
