/*
 * konfiguracija
 * */
var hyper_datoteka = "/wp-content/themes/newsport/"; //putanja do mjesta gdje smjesten hyper_upit direktorij
/*
 * konfiguracija end
 * */

jQuery.fn.hyper_center = function () { /* centriraj prozor */
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}

function hyperValidateEmail(email){ /* provjera ispravnosti e-mail adrese */
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

    if(!emailReg.test(email)){
        return false;
    }
    else{
        return true;
    }
}

$(document).ready(function(){
    var hyper_id = null; /* url onoga sto saljemo */
    var hyper_id_naslov = null; /*samo zadnji dio (ime slike) */

    $(".hyper_hoverBox").click( /* otvori prozor za upit */
        function(){
            $("#hyper_odgovor").fadeOut(0);
            $("#hyper_upitForma").fadeIn();
            $("#hyper_upitForma").hyper_center();
            var hyper_id_full = $(this).attr('id');
            hyper_id = hyper_id_full.substring(6);
            var htemp = hyper_id.split("/").pop();
            hyper_id_naslov = htemp.substring(0, htemp.length-4);
            $("#hyper_spanid").html(hyper_id_naslov);
        }
    );

    $("#hyper_upitFormaZatvori").click( /* zatvori prozor za upit */
        function(){
            $("#hyper_upitForma").fadeOut();
        }
    );
    $(document).keydown(function(event){ /* zatvori prozor za upit */
        if((event.which==27) && ($("#hyper_upitForma").css("display")!="none")){
            $("#hyper_upitForma").fadeOut();
        }
    });

    $("#hyper_upitFormaPosalji").click( /* posalji upit */
        function(){
            var hyper_ime = $('#hyper_ime').val().trim();
            var hyper_email = $('#hyper_email').val().trim();
            var hyper_upit = $('#hyper_upit').val().trim();
            var hyper_telefon = $('#hyper_telefon').val().trim();

            if( (hyper_ime!="") && (hyper_email!="") && (hyper_upit!="")){
                if(hyperValidateEmail(hyper_email)){
                    if (window.XMLHttpRequest){// IE7+, Firefox, Chrome, Opera, Safari
                        xmlhttp=new XMLHttpRequest();
                    }
                    else{// IE6, IE5
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }

                    hyper_datoteka = hyper_datoteka + "hyper_upit/hyper_sendUpit.php";

                    xmlhttp.open("POST",hyper_datoteka,true);

                    var hyper_params = "hyper_url=" + hyper_id + "&hyper_naslov=" + hyper_id_naslov + "&hyper_ime=" + hyper_ime + "&hyper_email=" + hyper_email + "&hyper_upit=" + hyper_upit + "&hyper_telefon=" + hyper_telefon;

                    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    xmlhttp.setRequestHeader("Content-length", hyper_params.length);
                    xmlhttp.setRequestHeader("Connection", "close");

                    xmlhttp.send(hyper_params);

                    xmlhttp.onreadystatechange=function(){
                        if (xmlhttp.readyState==4 && xmlhttp.status==200){
                            $("#hyper_upitForma").fadeOut();
                            $("#hyper_odgovor").fadeIn();
                            $("#hyper_odgovor").hyper_center();
                            $("#hyper_odgovor").html(xmlhttp.responseText);
                            $("#hyper_odgovor").fadeOut(3000);
                        }
                    }
                }
                else{
                    alert("Neispravna e-mail adresa.");
                }
            }
            else{
                alert("Potrebno je popuniti sva obavezna polja.");
            }
        }
    );
});

