/*
    called after document is fully loaded
 */
$(document).ready(function() {
    $('#area-selector-menu-draggable').draggable({containment: 'document'});
});


function toggleSearchTip(divId, sendFocusToId, view) {
    if (!view) {
        document.getElementById(divId).style.display = 'none';
        document.getElementById(sendFocusToId).focus();
    } else {
        //if browser supports document.activeElement
        if (document.getElementById(sendFocusToId).value == '') {
            document.getElementById(divId).style.display = 'block';
        //if no activeElement (safari engine)
        } else if (document.getElementById(sendFocusToId).value == '') {
            document.getElementById(divId).style.display = 'block';
        }
    }
}

/**
 * Strip all forbidden for URL characters from string
 * @param toEncode
 */
function toUrl(toEncode) {
    //trimming
    toEncode = toEncode.replace(/^\s+|\s+$/g, '');
    //remove '/' and '+' and '%'
    var regexp = /[%+\/]/g;
    toEncode = toEncode.replace(regexp, '');
    toEncode = encodeURIComponent(toEncode);
    toEncode = toEncode.replace(/(%20)+/g, '+');

return toEncode;
}


/**
 * make redirect to URL regarding to search form field values.
 * Emulate form posting with creating SEO friendly URL.
 */
function gotoSearchPage(page){
    // prepare search word and geo_area to be included in the URL
    var searchWord = toUrl($('#search_word').val());
    var geoArea = toUrl($('#geo_area').val());
    if ( searchWord.length == 0) {
        searchWord = toUrl(defaultUrlCategory);
    }
    var urlString;
    if (geoArea.length == 0) {
        urlString = page+"/"+searchWord;
    } else {
        urlString = page+"/"+searchWord+"/"+geoArea;
    }
    self.location.href=urlString;
    return false;
}
