
function externalLinks() {  
    if (!document.getElementsByTagName) return;  
    var anchors = document.getElementsByTagName("a");  
    for (var i=0; i<anchors.length; i++) {  
        var anchor = anchors[i];  
        if (anchor.getAttribute("href") &&  anchor.getAttribute("rel") == "external")  
            anchor.target = "_blank";  
    }
}

window.onload = externalLinks;

Array.prototype.in_array = function( needle ) {
    for(var i = 0, l = this.length; i < l; i++) {
        if(this[i] == needle) { return true; }
    }
    return false;
}

function pExtInfo( extJson ) {

    //extJson['rid'];extJson['ra'];
    var iHtml = '';
    var Room  = new Array;
    if( extJson['minp'] > 0 ) {
        iHtml = "cena od <strong>"+extJson['minp']+"<\/strong>zł, ";
    }
    extJson['rid'].sort();
    // pokoje n-osobowe
    for( var i = 1; i < 5; i++ ) {
        if( extJson['rid'].in_array(i) ) {
            Room.push(i);
        }
    }
    if( Room.length > 0 ) {
        iHtml += "pokoje: <strong>"+Room.join(', ')+"<\/strong> osobowe, ";
    }
    if( extJson['rid'].in_array(6) ) {
        iHtml += "apartamenty, ";
    }
    if( extJson['rid'].in_array(5) ) {
        if( Room.length == 0 ) { iHtml += "pokoje "; }
        iHtml += "wieloosobowe, ";
    }
    document.write( iHtml );
}

var questionFormSentStatus = false;

function questionSendForm( objId ) {

    if( questionFormSentStatus ) return false;

    questionFormSentStatus = true;

    $('questionFormMessage').update( '<div class="loading">&nbsp;Wysyłanie ...</div>' );

    new Ajax.Request('/ajax/send_question/'+objId, {
        method: 'post',
        parameters: $('questionForm').serialize(true),
        onSuccess: function(transport) {

            var response = transport.responseText;

            if( response == 'ok' ) {
                $('questionFormMessage').update( '<div class="sent">Wiadomość została wysłana.</div>' );
                $('questionForm').reset();
                setTimeout( 'RefreshToken()', 500 );
            } else {
                $('questionFormMessage').update( '<div class="error">'+response+'</div>' );
            }
            questionFormSentStatus = false;
        },

        onFailure: function(transport) {
            $('questionFormMessage').update( '<div class="error">Wystąpił błąd podczas wysyłania zapytania. Spróbuj póżniej.</div>' );
        }
    });
    
    return true;
}


    function setDirections(fromAddress, toAddress, locale) {

      if( fromAddress.value.length < 3 ) {

          alert( 'Proszę podać nazwę miejscowości początku trasy.' );
          fromAddress.focus();
          return false;
      }

      if( toAddress.value.length < 3 ) {

          alert( 'Proszę podać nazwę miejscowości końca trasy.' );
          toAddress.focus();
          return false;
      }

      gdir.load("from: " + fromAddress.value + " to: " + toAddress.value, { "locale": locale.value } );
    }

    function handleErrors(){
        if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
            alert("Podana miejscowość nie została odnaleziona. Sprawdź poprawność pisowni.\nKod błędu: " + gdir.getStatus().code);
        else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
            alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
        else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
            alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
        else if (gdir.getStatus().code == G_GEO_BAD_KEY)
            alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
        else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
            alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
        else alert("An unknown error occurred.");
    }

    function onGDirectionsLoad(){

        var poly = gdir.getPolyline();
        if (poly.getVertexCount() > 100) {
            //alert("This route has too many vertices");
            return;
        }

        var baseUrl = "http://maps.google.com/staticmap?";

        var params = [];
        var markersArray = [];

        markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
        markersArray.push(poly.getVertex(poly.getVertexCount()-1).toUrlValue(5) + ",greenb");
        params.push("markers=" + markersArray.join("|"));

        var polyParams = "rgba:0x0000FF80,weight:5|";
        var polyLatLngs = [];

        for (var j = 0; j < poly.getVertexCount(); j++) {
            polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
        }

        params.push("path=" + polyParams + polyLatLngs.join("|"));
        params.push("size=300x300");
        params.push("key=ABQIAAAAxSPq9Ufxi00DC8ARiVO1QhTzuM5pb14pm9rSB8-kyQhlZKSCMhQa26tySLU3myAwLT_rY2qGSNLEfA");

        baseUrl += params.join("&");

        var extraParams = [];
        extraParams.push("center=" + map.getCenter().lat().toFixed(6) + "," + map.getCenter().lng().toFixed(6));
        extraParams.push("zoom=" + map.getZoom());
        addImg(baseUrl + "&" + extraParams.join("&"), "staticMapOverviewIMG");

        var extraParams = [];
        extraParams.push("center=" + poly.getVertex(0).toUrlValue(5));
        extraParams.push("zoom=" + 15);
        addImg(baseUrl + "&" + extraParams.join("&"), "staticMapStartIMG");

        var extraParams = [];
        extraParams.push("center=" + poly.getVertex(poly.getVertexCount()-1).toUrlValue(5));
        extraParams.push("zoom=" + 15);
        addImg(baseUrl + "&" + extraParams.join("&"), "staticMapEndIMG");
    }

    function addImg(url, id) {

        var img = document.createElement("img");
        img.src = url;
        document.getElementById(id).innerHTML = "";
        document.getElementById(id).appendChild(img);
    }