/* 
Copyright © 2010 to Present, Local Matters, Inc.
All rights reserved.
 */

var listing = {};
listing.id = null;
listing.name = null;

var postcode = {};
postcode.example = "";

var errors = {};
errors.postalCode = 'Je dient een geldige postcode in te vullen.';

var content = {};
content.title = 'Onze lokatie';
content.route = 0;
content.commercial = 0;
content.panorama = 0;
content.foto = 0;
content.hotel = 0;

var section = 'bg';  // default section: bedrijvengids

init = function() {
    initContent();
    initRoute();
};

initContent = function() {
    listing.id = request('id');
    listing.name = request('name');
    if (request('s') !== null) { section = request('s'); }
    (request('title') === null) ? content.title = 'Over dit bedrijf' : content.title = unescape(request('title'));
    document.getElementById('title').innerHTML = content.title;
    document.getElementById('boxName').innerHTML = unescape(listing.name);

    var c = request('c');
    if (c !== null) {
        c = c.split('_');
        if (c.length === 5) {
            content.route = parseInt(c[0], 10);
            content.commercial = parseInt(c[1], 10);
            content.panorama = parseInt(c[2], 10);
            content.foto = parseInt(c[3], 10);
            content.hotel = parseInt(c[4], 10);
        }
    }

    if (content.route) { document.getElementById('route').style.display = 'block'; }

    if (content.commercial || content.panorama || content.foto || content.hotel) {
        document.getElementById('links').style.display = 'block';
        if (content.commercial) { document.getElementById('commercial').style.display = 'block'; }
        if (content.panorama) { document.getElementById('panorama').style.display = 'block'; }
        if (content.foto) { document.getElementById('foto').style.display = 'block'; }
        if (content.hotel) { document.getElementById('hotel').style.display = 'block'; }
    }
};

initRoute = function() {
    document.tgiroute.postcode.value = document.tgiroute.postcode.example = postcode.example;
};

fldFocus = function(f) {
    f.className = "text active";
    if (f.value===f.example) { f.value = ''; }
};

fldBlur = function(f) {
    if (f.value==='') {
        f.className = "text";
        f.value = f.example;
    }
};

showError = function(msg) {
    /*
    var er = document.getElementById('error');
    er.innerHTML = msg;
    er.style.display = 'block';
    */
    alert(msg);
    return false;
};

hideError = function() {
    document.getElementById('error').style.display = 'none';
};

validateTGIRoute = function(frm) {
    var error = false;
    var postalCode = frm.postcode.value;
    if (!((postalCode.search(/^\d{4}[\s]?[a-zA-Z]{2}$/)) != -1)) { error = true; }
    if (postalCode === frm.postcode.example) { error = true; }
    if (error) {
        frm.postcode.focus();
        return showError(errors.postalCode);
    }
    return true;
};

getQueryVariable = request = function(name) {
    var value = null;
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] === name) { value = pair[1]; }
    }
    return value;
};

createExtraCss = function() {
    var cssFile = request('css');
    if (cssFile !== null && cssFile !== 'default') { document.write('<link href="css/3rdparty/connectionbox/'+cssFile+'.css" rel="stylesheet" type="text/css" media="all" />'); }
};

function getUrl( path ) {
    var fullPath = LMI.Data.baseFullUrl;

    //fix the url for source, so that we don't have repeated slashes
    if( ! LMI.Data.baseFullUrl.match(/\/$/) && ! path.match(/^\//) ) {
        fullPath += '/' + path;
    } else if( LMI.Data.baseFullUrl.match(/\/$/) && path.match(/^\//) ) {
        fullPath += path.substring( 1 );
    } else {
        fullPath += path;
    }
    return fullPath;
}

showRoute = function(lnk) {
    var frm = document.tgiroute;
    if (validateTGIRoute(frm)) {
        var zipcode = frm.postcode.value;
        // TODO may need pretty url
        lnk.href = getUrl( '/finddirections.ds?location=postalCode='+zipcode+'&locationType=manual&location='+listing.id+'&locationType=listing' );
        return true;
    } else {
        return false;
    }
};

showMap = function(lnk) {
    var encodedUrlName = encodeURIComponent(listing.name);
    lnk.href = getUrl( '/'+section+'-l-f/'+listing.id+'-'+encodedUrlName+'/kaart/' );
    return true;
};

showVideo = function(lnk) {
    var encodedUrlName = encodeURIComponent(listing.name);
    lnk.href = getUrl( '/'+section+'-l-f/'+listing.id+'-'+encodedUrlName+'/fotovideo/' );
    return true;
};

showPanorama = function(lnk) {
    var encodedUrlName = encodeURIComponent(listing.name);
    lnk.href = getUrl( '/'+section+'-l-f/'+listing.id+'-'+encodedUrlName+'/fotovideo/' );
    return true;
};

showPhoto = function(lnk) {
    var encodedUrlName = encodeURIComponent(listing.name);
    lnk.href = getUrl( '/'+section+'-l-f/'+listing.id+'-'+encodedUrlName+'/fotovideo/' );
    return true;
};

showBooking = function(lnk) {
    var encodedUrlName = encodeURIComponent(listing.name);
    lnk.href = getUrl( '/'+section+'-l-f/'+listing.id+'-'+encodedUrlName+'/hotel/' );
    return true;
};

showDetail = function(lnk) {
    var encodedUrlName = encodeURIComponent(listing.name);
    lnk.href = getUrl( '/'+section+'-l/'+listing.id+'-'+encodedUrlName+'/vermelding/' );
    return true;
};

window.onload = function() {
    init();
};

if(typeof(LMI) === 'undefined'){
    LMI = {};
}
