var newTip;
$(window).load(function() {
    if( $('#slider').length > 0){
        $('#slider').nivoSlider();
    }
});
//realizacje przewijanie
function scrollPageVertical(){
    var elementClicked = $(this).attr("href");
    var destination = $(elementClicked).offset().top;
    $("html:not(:animated),body:not(:animated)").animate({
        scrollTop: destination-20
        }, 1000 );
    return false;
}//realizacje przewijanie end
var isH = false;//wizytowka

$(document).ready(
    function(){
        // bottom slide
        $('a.menuSlide').click(function(){
            if( $(this).next().is(':hidden') ) {
                //$('.acc_trigger').removeClass('active').next().slideUp();
                $(this).toggleClass('active').next().slideDown();
            }else{
                $(this).removeClass('active').next().slideUp();
                $(this).toggleClass('active');
            }
            return false;
        }); 
        // main page slide
        $('span.slide').click(function(){
            if( $(this).next().is(':hidden') ) {
                //$('.acc_trigger').removeClass('active').next().slideUp();
                $(this).toggleClass('active').next().slideDown();
            }else{
                $(this).removeClass('active').next().slideUp();
                $(this).toggleClass('active');
            }
            return false;
        }); 


 //wizytowka
        $(".send-card").mouseover(
            function(){
                $(".card-holder").show();
                $(".card-holder").animate({
                    opacity:1,
                    height:230
                }, 200);
                isH = true;
            }
            )
        $("#card-close").click(
            function(){
                $(".card-holder").animate({
                    opacity:0, 
                    height:2
                }, 200, function(){
                    $(".card-holder").hide()
                    });
                return false;
            }
            )//wizytowka end
        //skalowanie area
        if( $('textarea.resizable:not(.processed)').length > 0)
            $('textarea.resizable:not(.processed)').TextAreaResizer();
        //Tooltips
        $(".tip_trigger").hover(
            function(){
                if(newTip)newTip.remove();
                tip = $(this).find('.tip');
                newTip = tip.clone();
                newTip.id="currentTip";
                $("body").append(newTip);
                newTip.show();
            },
            function() {
                newTip.hide();
            }).mousemove(function(e) {
            var tipWidth = newTip.width();
            var tipHeight = newTip.height();
            var mousex = e.pageX - tip.width()/4;// - $(".holder-content").css(""):// - tip.width()-20;
            if(e.pageY > 150){
                var mousey = e.pageY - newTip.height()-12;
            }
            else{
                var mousey = e.pageY + 20;
            }
            newTip.css({
                top: mousey, 
                left: mousex
            });
        });//Tooltips end
       
       
       /* ---------------- TIP About -------------------- */
       //$(".programs .item .tip")
        $(".programs .item").hover( 
            function(){
                $(this).find(".tip").stop();
                $(this).find(".tip").css("display", "block");
                $(this).find(".tip").animate({
                    opacity:  1,
                    bottom:  82
                })
            return false;
            },
                
            function(){
                $(this).find(".tip").stop();
                $(this).find(".tip").animate({
                    opacity: 0,
                    bottom:  60
                }, function(){$(this).css("display", "none");}
                )
                    return false;
            }
        );
        /*$('#clickme').click(function() {
            $('#book').animate({
                opacity: 0.25,
                left: '+=50',
                height: 'toggle'
                }, 5000, function() {
    // Animation complete.
  });
});*/

        /*
         *$(".programs .item").hover( 
            function(){
                $(this).find(".tip").stop();
                $(this).find(".tip").fadeTo(1000, 1);
                
                
            }, 
            function(){
                $(this).find(".tip").stop();
                $(this).find(".tip").fadeTo(1000, 0);
            }
        );
            */
       
        /* ---------------- / TIP About -------------------- */
       
       
        //Wizytowka
        $("a#card-send-btn").click(submitForm);
        $("#askForm").validate(
            {
                rules:{
                    clientAdres:{
                        required:true,
                        email:true
                    }
                },
                messages:{
                    clientAdres:{
                        required: "Musisz poda\u0107 adres email",
                        email:"Musisz poda\u0107 adres email"
                    }
                },
                errorPlacement:function(error,element){
                    $("#askFormMessage").show();
                },
                submitHandler:function(form){
                //form.submit();
                }
            }
            ); //Wizytowka end
        //Formularz
        
        $("a#message").click(submitFormContact);
        $("#contactForm").validate({
            rules:{
                client_mail:{
                    required:true,
                    email:true
                },
                client_message:{
                    required:true,
                    minlength: 3
                }
            },
            validClass:'formValid',
            errorClass:'formError',
            
                highlight: function(element, errorClass, validClass) {
                    $(element).addClass(errorClass).removeClass(validClass);
                },
                unhighlight: function(element, errorClass, validClass) {
                    $(element).removeClass(errorClass).addClass(validClass);
                },
                
                messages:{
                client_mail:{
                    required: "Musisz poda\u0107 adres email",
                    email:"Musisz poda\u0107 adres email"
                }
            },
            errorPlacement:function(error,element){
                $("#contactFormMessage").show();
            },
            submitHandler:function(form){
            //form.submit();
            }
        }
        );//Formularz end
        $("#relative-selector").click(scrollPageVertical );
        //FANCYBOX
        if($("a.big").length>0)
            $("a.big").fancybox({
                'transitionIn' : 'elastic',
                'transitionOut' : 'elastic'
            });
        /* $("a#inline").fancybox({
'hideOnContentClick': true
});*/
        //FANCY end
        /*********************suwak****************************/
        if($("#suwak").length>0)
            $("#suwak").slider({
                min:0, max:128, step:2, animate:true,
                slide:function(e, ui){
                    if(ui.value==128){
                        $("#unblocked").show();
                        $("#blocked").hide();
                        $("#message").addClass("message-hover");
                    }else{
                        $("#blocked").show();
                        $("#unblocked").hide();
                        $("#message").removeClass("message-hover");
                    }
                    $("#blockade").attr("value", ui.value);
                }
            });
    /********************* suwak end *********************/
    
    
    
    
    
    
    if($("#gMap").length>0){
        var latlng  = new google.maps.LatLng(51.742884,19.377507);
        var latlng2 = new google.maps.LatLng(51.746429,19.439778);
        
        /*var ikona1 = new GIcon();
            ikona1.image = "http://maps.google.com/mapfiles/kml/pal2/icon38.png";
            ikona1.shadow = "http://maps.google.com/mapfiles/kml/pal2/icon38s.png";
            ikona1.iconSize = new GSize(32, 32);
            ikona1.infoWindowAnchor = new GPoint(16,16);
            ikona1.iconAnchor = new GPoint(16,16);
            ikona1.shadowSize = new GSize(59, 32);
            
            dodajMarker(53.4203,14.7011,{title: 'Restauracja #1', icon: ikona1});
            
            */
        var myOptions = {
            zoom: 12,
            center: latlng2,
            mapTypeId: google.maps.MapTypeId.ROADMAP//HYBRID
            };
        var map = new google.maps.Map(document.getElementById("gMap"), myOptions);
        var contentString = 
            '<div id="content">'+
                '<div class="map-logo">'+
                    '<p><strong>WebGunner</strong></p>'+
                    '<p>ul. Żużlowa 10,</p>'+
                    '<p>94-126 Łódź</p>'+
                    '<p>www.webGunner.pl</p>'+
                '</div>'+
            '</div>';
        
        var infowindow = new google.maps.InfoWindow({
            content: contentString
            });
        var markerIcon = new google.maps.MarkerImage('gfx/contact/marker.png');
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title:"WebGunner",
            icon:markerIcon
            });

        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map,marker);
        });
    }
    
    
    
    
    
    
    
    
    
    });/*ready end*/
function submitForm(){
    if(!isH) return false;
    if(!$("#askForm").valid() ){
        return false;
    }
    isH = false;
    $("#send-card-wait").show();
    $("#askForm").hide();
    $.ajax({
        type : "POST",
        cache : false,
        url : "forms/send-card.php",
        data : $("#askForm").serializeArray(),
        success: function(data){
            if(data=="ok"){
                $("#send-card-wait").hide();
                $("#send-card-done").show();
            /*$("a#completeForm").click();*/
            }
            else{
                $("#send-card-wait").hide();
                $("#send-card-done").hide();
                $("#send-card-error").show();
            /*$("a#errorForm").click();*/
            }
            return false;
        },
        error: function(){
            $("a#errorForm").click();
            return false;
        }
    });
    return false;
}
function submitFormContact(){
    if( $("#blockade").attr("value")<128 ) return false;
    if(!$("#contactForm").valid() ){
        return false;
    }
    //isH = false;
    $("#send-message-wait").show();
    // $("#contactForm").hide();
    $("#contactFormMessage").hide();
    $.ajax({
        type : "POST",
        cache : false,
        url : "forms"+"/"+"send-message.php",
        data : $("#contactForm").serializeArray(),
        
        success: function(data){
            if(data=="ok"){
                $("#send-message-wait").hide();
                $("#send-message-done").show();
            /*$("a#completeForm").click();*/
            }
            else{
                $("#send-message-wait").hide();
                $("#send-message-done").hide();
                $("#send-message-error").show();
            /*$("a#errorForm").click();*/
            }
            return false;
        },
        error: function(){
            //$("a#errorForm").click();
            return false;
        }
    });
    return false;
}
//realizacje menu
$(window).scroll(function(){
    if($(document).scrollTop() > 120){
        $("#pf-menu").css("position","fixed");
        $("#pf-menu").css("top",30);
    /*$("#pf-menu").css("left",0);*/
    }
    else{
        $("#pf-menu").css("position","absolute");
        $("#pf-menu").css("top","0px");
    }
});//realizacje menu end 
