/*!
 * Code v1 by @vip
 * Copyright 2014.
 *
 * Designed and built with all the love in the world by @vip
 */
        function AJAXy(){
                    $(window).trigger("resize");
                    loadAJAX("return");
                    loadAJAX("cookie");
                    loadAJAX("contact");
                    loadAJAX("l");
                }


        (function () {

            cookieAlert();
            var menus = new menuBars();
            var socialMenus = new socialMenu();
            /**/


            /*---------------   diffrent sizes ----------------------*/
            function Normal990() {

                $(".title").css("display", "block");

                //console.log("Normaler");
            }

            function Smaller990() {
                $(".title").css("display", "none");
            }
            
            function formNormal(perCent3, verticalPercent){
                $("form").css("margin-left", perCent3 + "%");
                $("form").css("margin-top", verticalPercent + "px !important"); 
                $(".postCard").css("max-width","70%");
                $(".leftColumn").css("width","40%");
            }
            
            function formMiddle(){
                $("form").css("margin-left", "0px");
                $("form").css("margin-top", "80px");
                $(".postCard").css("max-width","100%");
                $(".leftColumn").css("width","100%");
                
            }

            /*------------------------RESIZE FUNCTION------------------------*/
            function Resize() {

                var h = window.innerHeight,
                        w = window.innerWidth,
                        //console.log(h.toString());
                        formWidth = $("form").width(),
                        /* title left side */
                        titleWidth = $(".title").width(),
                        perCent = 100 * ((w - titleWidth) / 9) / w,
                        /*perCent2 = 100 * ((w - tilesWidth) / 2) / w,*/
                        perCent3 = 100 * ((w - formWidth) / 2) / w,
                        formHeight = $("form").height(),
                        verticalPercent = (h - formHeight) / 2;
                
                
                
                //console.log("tilesWidth: ", tilesWidth);
                /*
                 $(".title").css("margin-left", perCent + "%");
                 $(".tiles").css("margin-left", perCent2 + "%");
                 */
                /* end of title centerized
                $("title").text("Width: "+w);
                console.log("Szer: "+w, h);
                console.log(verticalPercent);

*/
                /*regarding title*/
                if (w < 990) {
                    //console.log("Smaller");
                    Smaller990();
                } else {
                    Normal990();
                }
                
                /*regarding forms*/
                if((w < 820) && (w > 700)){
                    $("form").css("margin-top", "50px");
                }
                else if (w < 700) {
                    //console.log("Smaller");
                    formMiddle();
                } else {
                    formNormal(perCent3, verticalPercent);
                }
                /**/
                if (w < 840) {
                    menus.smaller();
                    socialMenus.addToPage(socialMenus.socialToolbar(),1);
                } else {
                    menus.normal();
                    socialMenus.addToPage(socialMenus.showroom(),0);
                }
                
                $("img.lazy").lazy({
                    combined: true,
                    effect: "fadeIn",
                    effectTime: 1500
                });
            }

            $(window).bind("resize", function () {
                Resize();
            });
            /*-------------------------------------------------------END OF RESIZE FUNCTION-------------------------------------------------------------*/
            /*-----------------------  E-mail out animation  ------------------------*/

            function mailOut() {
                $(".postCard").removeClass("rotateInDownLeft");
                $(".postCard").addClass("lightSpeedOut");

                setTimeout(function () {
                    $("input[id='name']").val('imię, jesli można');
                    $("input[id='email']").val('email wystarczy...');
                    $(".postCard").addClass("rotateInDownLeft");
                    $(".postCard").removeClass("lightSpeedOut");

                }, 1000);
            }



            /*
             setInterval(function(){
             Resize();
             console.log("interval");
             }, 1000);
             */

            /*      --------------------------     -----------------     FUNKCJA PROCESS() po załądowaniu strony    -------------------------------- */
            function process() {
                
                $(".leftColumn").eq(0).css("width", "40%");
                $("input").addClass("goschi");
                $("textarea").addClass("goschi");
                $(".heading").addClass("justHand");
                
                loadAJAX("contact");
                /* style labeli*/
                /*$("form label").eq(1).css("top", "14.5%");
                 $("form label").eq(2).css("top", "28.5%");
                 
                 $("label").css("display", "block");
                 $("label").css("position", "absolute");
                 
                 $("label").css("width", "40%");
                 
                 $("input").eq(1).css("top", "38px");
                 */
                // hovers

                $('#submit').tooltip(true);


                // fonts
                $('#footer').addClass('orbitron');
                $('body').eq(0).addClass('coda');


                // dodanie animaci loga
                $("img[alt='MARKII']").addClass("animated bounceIn");
                $(".postCard").addClass("animated rotateInDownLeft");
                $(".postCard").css("display","block");

                $("footer").addClass("animated fadeInUp");
                $("nav").addClass("animated fadeInDown");

                /*regarding title*/
                if (window.innerWidth < 990) {
                    //console.log("Smaller");
                    Smaller990();
                } else {
                    Normal990();
                }


                setTimeout(function () {
                    correctSVG("svg", "gif");
                    //correctSVG($("[src$='svg']"),"png");
                }, 1000);

                //loadAJAX()
                //Animate();
            }

            //*******************************************//***************
            // test na wsparcie dla blending-modes
            if (document.createElement("detect").style.mixBlendMode === "") {
                console.log("supports blending modes");

            } else {
                console.log("does not supports blending modes");
            }
            //*******************************************//***************
            /******************* MOuseMOve Element Background *********************/
            $(window).on("mousemove", function (event) {
                //$(".top-row").css("backgroundPosition", (2000 - window.innerWidth) - event.pageX / 7 + 'px');

                $("body").css("backgroundPosition", "-" + event.pageX / 12 + 'px');
                //$(".infoBox > h2").css("backgroundPosition", "-" + event.pageX / 12 + 'px');
                //console.log($(".top-row").css("backgroundPosition"));
                //console.log(event.pageX);
                //console.log("left + "+event.pageY+'px ', "top + "+event.pageX+'px');

            });




            /********************** form validation **************************/


            /*
             var x = $("#name").val();
             
             if ((x === null || x === "" || x === "imię, jesli można")&&($("#msg").val() === null || $("#msg").val() === "" || $("#msg").val() === "Tylko nic zboczonego")) {
             alert("Nie wpisałeś poprawnie imienia i wiadomości. Popraw to!");
             return false;
             }
             else if ((x === null || x === "" || x === "imię, jesli można")) {
             alert("Nie wpisałeś poprawnie imienia. Popraw to!");
             return false;
             }
             else if (($("#msg").val() === null || $("#msg").val() === "" || $("#msg").val() === "Tylko nic zboczonego")) {
             alert("Nie wpisałeś poprawnie wiadomości. Popraw to!");
             return false;
             }
             else {
             return true;
             }
             */
            
            function fieldExist(element, warnMsg){
                var field = $(element).val();
                
                if(field){
                    return true;
                }
                else{
                    alert(warnMsg);
                    element.focus();
                    return false;
                }
            }
            
            
            function emailValidate(element, warnMsg){
                var mailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
                
                if($(element).val().match(mailExp)){
                    
                    return true;
                }
                else{
                    alert(warnMsg);
                    element.focus();
                    return false;
                }
            };
            function lettersNumbersValidate(element, warnMsg){
                
                var letEXP =/^[a-zA-Z ]*$/;
                var elVal = $(element).val().toString();
                
                console.log(elVal);
                
                if(elVal.match(letEXP)){
                    return true;
                }
                else{
                    alert(warnMsg);
                    element.focus();
                    return false;
                }
            };
            
            function numbersValidate(element, warnMsg){
                
                var numEXP =/^[0-9]+$/;
                var elVal = $(element).val();
                
                if(elVal.match(numEXP)){
                    return true;
                }
                else{
                    alert(warnMsg);
                    element.focus();
                    return false;
                }
            };
            
            
            function lenghtValidate(element, min,max){
                
                var elementValue = $(element).val();
                
                var elementLenght = elementValue.toString().length;
                
                console.log("elementLenght: ", elementLenght);
                if((elementLenght >= min) && (elementLenght <= max)){
                    return true;
                }
                else{
                    alert("Musisz podać od "+min+", do "+max+" znaków.");
                    element.focus();
                    return false;
                }
            };
            
            
            function heavyGearFormValidator(){
                var name = $("form #name");
                var email = $("form #email");
                var numer = $("form #number");
                var msg = $("form #msg");
                
                if(lettersNumbersValidate(name,"Jest coś nie tak z twoim imieniem."))
                        {
                            if(emailValidate(email,"Nie podano poprawnego adresu..."))
                            {
                                if(numbersValidate(numer,"Nie poprawny numer telefonu."))
                                {
                                    if(lenghtValidate(numer,7,11))
                                    {
                                        if(fieldExist(msg,"Napisz cos do mnie."))
                                        {
                                            
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                 else{
                     return false;
                 }
                };

            $("form button#submit").click(function(){
                heavyGearFormValidator();
            });
            
            // triggery dla fukncji po ząłądowaniu AJAX
            $(window).load(function () {
                
                process();
                $(window).trigger("resize");
            });

        }());
