﻿var ns_t = document.getElementById('ns_target');

var W = 300;
var H = 250;

H = ns_size.substring(ns_size.indexOf("x") + 1);
W = ns_size.substring(0, ns_size.indexOf("x"));

/* BEGIN Css */
var style = document.createElement('style')
style.setAttribute("type", "text/css");
var ns_max_height = '';
if (ns_set_limitheight)
    ns_max_height = 'max-height:' + H + 'px;';

var sheet = '.sns_Block {border:1px solid #' + ns_color_border + ';background-color:#' + ns_color_bg + ';width:' + (W - 2) + 'px;' + ns_max_height + '}';
sheet += '.sns_Header {padding:5px;height:20px;line-height:20px; background-color:#' + ns_color_border + ';color:#' + ns_color_title + '}';
sheet += '.sns_InnerBlock {margin: 5px;width:' + (W - 12) + 'px;}';
sheet += '.sns_InnerBlockSearch {height:72px;}';
sheet += '.sns_InnerBlock input {0px;padding:0px;width:' + (W - 12 - 4) + 'px;}';
sheet += '.sns_InnerBlock input[type="button"] {0px;padding:0px;width:' + (W - 12) + 'px;}';
sheet += '.sns_Topic {margin-top: 10px;margin-bottom: 5px;}';
sheet += '.sns_Link {color:#' + ns_color_links + '}';
sheet += '.sns_Empty {margin-top: 10px;margin-bottom: 10px;text-align:center;font-weight:bold;}';
if (ns_set_limitheight) {
    sheet += '.sns_Results {overflow-y:auto;max-height:' + (H - 114) + 'px;}';
} else
    sheet += '.sns_Results {height:auto;}';

var isIE = style.styleSheet;
if (isIE)
    style.styleSheet.cssText = sheet;
else
    style.innerHTML = sheet;

ns_t.appendChild(style);
/* END Css */

function ns_Search() {
    if (ns_l == null || ns_l.value == '') {
        if (!ns_set_autosearch)
            alert('Please enter your city');
    }
    else if (ns_q == null || ns_q.value == '') {
        if (!ns_set_autosearch)
            alert('Please enter your keyword');
    }
    else {
        if (window.ns_country == undefined)
            ns_country = 'US';

        if (window.ns_filtergeo == undefined)
            ns_filtergeo = false;
        if (window.ns_determinegeo == undefined)
            ns_determinegeo = false;

        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = 'http://articles-api-v1.nsphereinc.com/Company.aspx?radius=10&q=' + escape(ns_q.value) + '&l=' + escape(ns_l.value) + '&country=' + ns_country + '&appkey=' + ns_appkey + '&appid=' + ns_appid + '&total=' + ns_max + '&output=json&callback=ns_Callback&stripHtml=true&stripgeo=' + ns_filtergeo.toString() + '&determineGeo=' + ns_determinegeo.toString();
        var x = document.getElementsByTagName('script')[0];
        x.parentNode.insertBefore(s, x);
    }

    return false;
}

function ns_Callback(ns_topics) {
    var statusHTML = [];
    var ns_tList = ns_topics.Companies.Company;

    var ns_target = '';
    if (ns_set_newwindow)
        ns_target = ' target="_blank"';

    if (ns_topics.Companies.numFound == 0 && !ns_set_autosearch) {
        statusHTML.push('<div class="sns_Topic"><div class="sns_Empty">No results found, please try again.</div></div>');

        ns_innerblocksearch.style.display = 'block'
    }
    else if (ns_topics.Companies.numFound == 0 && ns_set_autosearch && window.ns_set_alwayshidesearchbox != undefined && ns_set_alwayshidesearchbox != null && ns_set_alwayshidesearchbox) {
        ns_block.style.display = 'none';
    }
    else if (ns_topics.Companies.numFound == 1) {
        statusHTML.push(buildItem(ns_tList.CompanyName, ns_tList.Url, ns_tList.Description, ns_tList.Address1, ns_tList.Address2, ns_tList.City, ns_tList.State, ns_tList.PhoneNumber, ns_tList.Attributes, ns_target));

        if (ns_set_autohidesearchbox != null && ns_set_autohidesearchbox)
            ns_innerblocksearch.style.display = 'none'
        else
            ns_innerblocksearch.style.display = 'block'
    }
    else if (ns_topics.Companies.numFound > 1) {
        for (var i = 0; i < ns_tList.length; i++) {
            statusHTML.push(buildItem(ns_tList[i].CompanyName, ns_tList[i].Url, ns_tList[i].Description, ns_tList[i].Address1, ns_tList[i].Address2, ns_tList[i].City, ns_tList[i].State, ns_tList[i].PhoneNumber, ns_tList[i].Attributes, ns_target));
        }

        if (ns_set_autohidesearchbox != null && ns_set_autohidesearchbox)
            ns_innerblocksearch.style.display = 'none'
        else
            ns_innerblocksearch.style.display = 'block'
    }

    ns_results.innerHTML = statusHTML.join('');
}

function buildItem(title, url, snippet, address1, address2, city, state, phone, attributes, target) {
    title = htmlDecode(title);
    snippet = htmlDecode(snippet);
    var output = '';

    if (url == '')
        output = '<div class="sns_Topic"><div class="sns_Title"><b>' + title + '</b></div>';
    else
        output = '<div class="sns_Topic"><div class="sns_Title"><a class="sns_Link" href="' + url + '"' + target + '>' + title + '</a></div>';

    if (phone != '')
        output = output + '<div class="sns_Snippet"><b>Phone:</b> ' + phone + '</div>';

    if (ns_country != null && (ns_country == 'US' || ns_country == 'CA' || ns_country == 'AU' || ns_country == 'MX'))
        output = output + '<div class="sns_Snippet"><b>Address:</b><br/>' + address1 + ' ' + address2 + '<br/>' + city + ', ' + state + '</div>';
    else
        output = output + '<div class="sns_Snippet"><b>Address:</b><br/>' + address1 + ' ' + address2 + '<br/>' + city + '</div>';

    if (attributes != null && attributes.Attribute != null && attributes.Attribute.length != null && attributes.Attribute.length > 0) {
        output = output + '<div class="sns_Snippet"><b>Details:</b>';

        for (var i = 0; i < attributes.Attribute.length; i++) {
            output = output + '<br/>' + htmlDecode(attributes.Attribute[i].Name);
        }

        output = output + '</div></div>';
    }
    
    if (snippet != '')
        output = output + '<div class="sns_Snippet"><b>Description</b><br/>' + snippet + '</div></div>';

    return output;
}

function htmlDecode(htmlString) {
    htmlString = unescape(htmlString);
    htmlString = htmlString.replace(/&lt;/g, '<');
    htmlString = htmlString.replace(/&gt;/g, '>');
    htmlString = htmlString.replace(/&ndash;/g, '-');
    htmlString = htmlString.replace(/&mdash;/g, '-');
    htmlString = htmlString.replace(/&amp;/g, '&');
    htmlString = htmlString.replace(/&rsquo;/g, '\'');
    return htmlString;
}

function geoPlugin(geoplugin) {
    ns_l.value = geoplugin.geoplugin_city;

    if (ns_set_autosearch)
        ns_Search();
}

if (ns_set_autoregion != null && ns_set_autoregion == true) {
    var sg = document.createElement('script');
    sg.type = 'text/javascript';
    sg.async = true;
    sg.src = 'http://www.geoplugin.net/json.gp';
    ns_t.appendChild(sg);
}

var ns_block = document.createElement('div');
ns_block.className = 'sns_Block'

var ns_title = document.createElement('div');

if (ns_set_title != null && ns_set_title != '')
    ns_title.innerHTML = ns_set_title;
else
    ns_title.innerHTML = 'Local Information';
ns_title.className = 'sns_Header'
ns_block.appendChild(ns_title);

var ns_innerblock = document.createElement('div');
ns_innerblock.className = 'sns_InnerBlock'
ns_block.appendChild(ns_innerblock);

var ns_innerblocksearch = document.createElement('div');
ns_innerblocksearch.className = 'sns_InnerBlockSearch'
ns_innerblocksearch.Id = 'InnerBlockSearch'

var ns_q = document.createElement('input');
ns_q.type = 'text';
if (isIE)
    ns_q.value = 'Keyword?';
else
    ns_q.placeholder = 'Keyword?';
ns_q.className = 'sns_Input'
if (ns_set_autotopic != null && ns_set_autotopic == true && document.title != null)
    ns_q.value = document.title;
if (ns_set_topic != '')
    ns_q.value = ns_set_topic;
ns_innerblocksearch.appendChild(ns_q);

var ns_l = document.createElement('input');
ns_l.type = 'text';
if (isIE)
    ns_l.value = 'City?';
else
    ns_l.placeholder = 'City?';
ns_l.className = 'sns_Input'
if (ns_set_regionlist != '')
    ns_l.value = ns_set_regionlist;
ns_innerblocksearch.appendChild(ns_l);

var ns_s = document.createElement('input');
ns_s.type = 'button';
ns_s.value = 'Go';
ns_s.className = 'sns_Input'
ns_s.onclick = ns_Search;
ns_innerblocksearch.appendChild(ns_s);

ns_innerblock.appendChild(ns_innerblocksearch);

var ns_results = document.createElement('div');
ns_results.className = 'sns_Results'
ns_innerblock.appendChild(ns_results);

ns_t.appendChild(ns_block);

if (window.ns_set_alwayshidesearchbox != undefined && ns_set_alwayshidesearchbox != null && ns_set_alwayshidesearchbox)
    ns_innerblocksearch.style.display = 'none';

if (ns_set_autosearch) {
    ns_Search();
}
