﻿var myjson = null;

/* ----- Get Home Page News - Call WebService ------ */

function getNewsForHome() {
    /* Call WCF via jQuery */
    jQuery.ajax({
        url: baseURL() + "EPVJsWS.svc/GetNews",
        cache: false,
        data: myjson,
        type: "GET",
        processData: true,
        contentType: "application/json",
        success: function (data, textStatus) {
            getNewsForHomeView(data, textStatus);
        }, error: function (XMLHttpRequest, textStatus, errorThrown) {
            getNewsForHomeErr();
        }
    });
}

function getNewsForHomeErr() {
    jAlert('Error encountered.  Please try again.');
}

var innrSrc = '';
var trimPthHomeNewsVwObj = null;

function getNewsForHomeView(rtnData, rtnStatus) {
    if (trimPthHomeNewsVwObj == null) {
        /* Parse Template Object */
        trimPthHomeNewsVwObj = TrimPath.parseDOMTemplate("newsList_jst");
    }

    /* Populate parsed template object */
    innrSrc = trimPthHomeNewsVwObj.process(rtnData.d);
    $("#dvHomeNewsList").html(innrSrc);
    /* Hide loading */
    $("#dvHomeNewsLoading").hide();

    if (rtnData.d.newsColl != null && rtnData.d.newsColl.length > 0) {
        $("#dvHomeNewsList").fadeIn("slow");
        $("#dvHomeNewsNone").hide();
    } else {
        $("#dvHomeNewsList").hide();
        $("#dvHomeNewsNone").show();
    }
    resizeMainCntr();
}

/* ----- jQuery Autocomplete for Org Name - Call WebService ------ */

$(function () {
    $("#txtOrgNameSearch").autocomplete({
        source: function (request, response) {
            myjson = { "orgName": request.term };
            $.ajax({
                url: baseURL() + "EPVJsWS.svc/GetOrgNameForAC",
                cache: false,
                data: myjson,
                type: "GET",
                processData: true,
                contentType: "application/json",
                dataFilter: function (data) { return data; },
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            value: item
                        }
                    }))
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    jAlert('Sorry, we hit a problem. Please try again.');
                }
            });
        },
        minLength: 2,
        select: function (event, ui) { acSel(event, ui); }
    });
});

/* On jQuery Autocomplete click */

function acSel(e, u) {
    $("#txtOrgNameSearch").val(u.item.value);
    /* Redirect to Search page with Org name appneded to URL */
    redirect("Search/" + u.item.value.replace(/ /g,"-"));
}

/* Click for Org Type Search */
function orgTypeSrc(clkObj) {
    var hldTxt = $(clkObj).children().text().trim().replace(/ /g, "-").replace("/", "_");
    redirect("Search/Type/" + hldTxt);
}
