﻿
var isShowChildrenClicked = false;
function $$(id) {
    return document.getElementById(id);
}

function scrollToListing(lineid) {

    line = $('#' + lineid);

    TwoPreviousLinesHeight = 0;

    test = getPreviousLineN(line, 1);


    if (test.length != 0) {
        TwoPreviousLinesHeight += test[0].offsetHeight;

        test = getPreviousLineN(test, 1);

        if (test.length != 0) {
            TwoPreviousLinesHeight += test[0].offsetHeight;
        }
    }


    var shift = line.parent().parent()[0].offsetTop - line[0].offsetTop;
    scrollToListingId = lineid;
    $$("SearchResultListingContainer").scrollTop = -TwoPreviousLinesHeight - shift;
    if ($$("SearchResultListingContainer").scrollTop <= 0) {
        //        loadOnTop();
//        $(".prevButton").click(function() {
//            showEndOfSearchResultMessage();
//        });
    }
    else {

        if ($$("SearchResultListingContainer").scrollTop > $$("SearchResultListingContainer").scrollHeight - $$("SearchResultListingContainer").clientHeight * 2) loadOnBottom();
    }
}


function SkipPageUp() {
    scrollToListing(getPreviousLineN($('#' + scrollToListingId), 21).attr("id"));
    $(".nextButton").show();
    $(".nextButton").unbind("click");
    $(".nextButton").click(function() {
        SkipPageDown();
        return false;
    });
    if ($$("SearchResultListingContainer").scrollTop <= 0) {
        //        loadOnTop();
        $(".prevButton").unbind("click");
        $(".prevButton").click(function() {
            bindNextButtonToShowMsg = false;
            showEndOfSearchResultMessage();
            return false;
        });
    }
}

function SkipPageDown() {
    scrollToListing(getNextLineN($('#' + scrollToListingId), 21).attr("id"));
    $(".prevButton").show();
    $(".prevButton").unbind("click");
    $(".prevButton").click(function() {
        bindNextButtonToShowMsg = false;
        SkipPageUp();
        return false;
    });    
}

function loadOnTop() {
    if (loadingOnTop || searchtype==1) return;
    loadingOnTop = true;
    $("#topLoading").css("display", "");
    //       window.open("xml/getlist.aspx?d=-&p="+firstListing+"&oi="+occupationOrIndustry+"&c="+countryId+"&s="+stateId,"xml");
    $.get("xml/getlist.aspx?d=-&p=" + firstListingId + "&oi=" + occupationOrIndustry + "&c=" + countryId + "&s=" + stateId, function(data) {
        listingsCount = data.firstChild.getElementsByTagName("row").length;

        if (listingsCount > 0) {
            for (var i = 0; i < listingsCount; i++) {
                listing = data.firstChild.getElementsByTagName("row")[i];
                listingid = listing.attributes.getNamedItem("t").nodeValue + listing.attributes.getNamedItem("id").nodeValue;
                listingname = listing.attributes.getNamedItem("n").nodeValue;
                listingoi = listing.attributes.getNamedItem("oi").nodeValue;
                listingcity = listing.attributes.getNamedItem("ci").nodeValue;
                listingcountry = listing.attributes.getNamedItem("c").nodeValue;
                listingstate = listing.attributes.getNamedItem("s").nodeValue;
                listingdesc = listing.attributes.getNamedItem("d").nodeValue;
                listingchildcount = listing.attributes.getNamedItem("cc").nodeValue;
                listingUrl = listing.attributes.getNamedItem("URL").nodeValue.replace(/[^a-zA-Z0-9\-\.]/, "");
                $("<div  id='" + listingid + "'><table  width='100%' border='0' style='cursor: pointer;' onclick=\"linkToLST('" + applicationPath + "/" + listingUrl + "')\"><tr><td width='11' valign=top>" + (listingchildcount != "0" ? "<img onclick=showChildren(this,'" + listingid + "') src='images/buttons/plus.gif' />" : "") + "</td><td width='200'><strong>" + listingname + "</strong><br /><span>" + listingdesc + "</span></td><td><span class='Occupation'>" + listingoi
                        + "</span></td><td align='right'><span class='CityState'>" + listingcity + ", " + listingstate + ",</span><strong> " + listingcountry + "</strong></td></tr></table></div>").insertBefore("#" + firstListingId);
            }
//            firstListing = data.firstChild.getElementsByTagName("row")[0].attributes.getNamedItem("n").nodeValue;
            firstListingId = data.firstChild.getElementsByTagName("row")[0].attributes.getNamedItem("t").nodeValue + data.firstChild.getElementsByTagName("row")[0].attributes.getNamedItem("id").nodeValue;

            loadingOnTop = false;
        }
        $("#topLoading").css("display", "none");
    });
}


function showChildren(img, id, password) {
    isShowChildrenClicked = true;
    expand = (img.src.indexOf("minus") < 0);

    if (expand) // expand
    {
        //      window.open("xml/getchildren.aspx?id=" + id.substring(1) + '&p=' + password);
        $.post("xml/getchildren.aspx?id=" + id.substring(1) + '&p=' + password, function(data) {
            listingsCount = data.firstChild.getElementsByTagName("row").length;
            //      alert('got children data :'+ listingsCount);
            if (listingsCount > 0) {
                for (var i = 0; i < listingsCount; i++) {
                    listing = data.firstChild.getElementsByTagName("row")[i];
                    listingname = listing.attributes.getNamedItem("n").nodeValue;
                    listingothernames = listing.attributes.getNamedItem("otn").nodeValue;
                    if (password == '')
                        if (i == 0 && listingname == 'askpassword') {
                        img.src = "images/buttons/plus.gif";
                        var newpassword = prompt('Please enter password to access', '');
                        if (newpassword != '')
                            showChildren(img, id, newpassword);
                        break;
                    }
                    listingid = listing.attributes.getNamedItem("t").nodeValue + listing.attributes.getNamedItem("id").nodeValue;
                    listingoi = listing.attributes.getNamedItem("oi").nodeValue;
                    listingcity = listing.attributes.getNamedItem("ci").nodeValue;
                    listingcountry = listing.attributes.getNamedItem("c").nodeValue;
                    listingstate = listing.attributes.getNamedItem("s").nodeValue;
                    listingdesc = listing.attributes.getNamedItem("d").nodeValue;
                    listingchildcount = listing.attributes.getNamedItem("cc").nodeValue;
                    if (listing.attributes.getNamedItem("URL").nodeValue != '') {
                        listingUrl = listing.attributes.getNamedItem("URL").nodeValue.replace(/[^a-zA-Z0-9\-\.]/, "");
                        $("<div  style='margin-left:10px;'  id='" + listingid + "'><table  width='100%'  border='0' style='cursor: pointer;' onclick=\"linkToLST('" + applicationPath + "/" + listingUrl + "')\"><tr><td width='11' valign=top>" + (listingchildcount != "0" ? "<img onclick=showChildren(this,'" + listingid + "') src='images/buttons/plus.gif' />" : "") + "</td><td width='200'><strong>" + listingname + "</strong><br /><span>" + listingdesc + "</span></td><td><span class='Occupation'>" + listingoi
                        + "</span></td><td align='right'><span class='CityState'>" + listingcity + ", " + listingstate + ",</span><strong>" + listingcountry + "</strong></td></tr></table></div>").appendTo("#" + id);
                    }
                    else {
                        $("<div  style='margin-left:10px;'  id='" + listingid + "'><table  width='100%'  border='0' style='cursor: pointer;'\"><tr><td width='11' valign=top>" + (listingchildcount != "0" ? "<img  onclick=showChildren(this,'" + listingid + "') src='images/buttons/plus.gif' />" : "") + "</td><td width='200'><strong>" + listingname + "</strong><br /><span>" + listingdesc + "</span></td><td><span class='Occupation'>" + listingoi
                        + "</span></td><td align='right'><span class='CityState'>" + listingcity + ", " + listingstate + ",</span><strong>" + listingcountry + "</strong></td></tr></table></div>").appendTo("#" + id);
                    }

                }
                //                loadingOnBottom = false;
            }
        });
    }
    else                            // collapse
        $("#" + id).children("div").remove();

    img.src = (expand ? "images/buttons/minus.gif" : "images/buttons/plus.gif");

}

function loadOnBottom() {
    if (loadingOnBottom) return;
    if (searchtype == 0) {
        $(".nextButton").unbind("click");
        $(".nextButton").click(function() {
            showEndOfSearchResultMessage();
            return false;
        });
        return;
    }
    //    alert('loading on bottom after ' + lastListing);
    loadingOnBottom = true;
    bindNextButtonToShowMsg = true;
    $("#bottomLoading").css("display", "");
    //       window.open("xml/getlist.aspx?d=+&p="+lastListing+"&oi="+occupationOrIndustry+"&c="+countryId+"&s="+stateId,"xml");

    loadingpattern = '';
    if (searchtype == 1)
        loadingpattern = pattern;
    else
        loadingpattern = lastListingId;

    $.get("xml/getlist.aspx?d=+&p=" + loadingpattern + "&oi=" + occupationOrIndustry + "&c=" + countryId + "&s=" + stateId + "&st=" + searchtype + "&lr=" + lastrowno, function(data) {
        listingsCount = data.firstChild.getElementsByTagName("row").length;

        if (listingsCount > 1) {
            for (var i = 1; i < listingsCount; i++) {
                listing = data.firstChild.getElementsByTagName("row")[i];
                listingid = listing.attributes.getNamedItem("t").nodeValue + listing.attributes.getNamedItem("id").nodeValue;
                listingname = listing.attributes.getNamedItem("n").nodeValue;
                listingoi = listing.attributes.getNamedItem("oi").nodeValue;
                listingcity = listing.attributes.getNamedItem("ci").nodeValue;
                listingcountry = listing.attributes.getNamedItem("c").nodeValue;
                listingstate = listing.attributes.getNamedItem("s").nodeValue;
                listingdesc = listing.attributes.getNamedItem("d").nodeValue;
                listingchildcount = listing.attributes.getNamedItem("cc").nodeValue;
                listingUrl = listing.attributes.getNamedItem("URL").nodeValue.replace(/[^a-zA-Z0-9\-\.]/, "");
                if (i == listingsCount - 1 && searchtype == 1)
                    lastrowno = listing.attributes.getNamedItem("rn").nodeValue;
                $("<div  id='" + listingid + "'><table width='100%' border='0' style='cursor: pointer;' onclick=\"linkToLST('" + applicationPath + "/" + listingUrl + "')\"><tr><td width='11' valign=top>" + (listingchildcount != "0" ? "<img onclick=showChildren(this,'" + listingid + "') src='images/buttons/plus.gif' />" : "") + "</td><td width='200'><strong>" + listingname + "</strong><br /><span>" + listingdesc + "</span></td><td><span class='Occupation'>" + listingoi
                        + "</span></td><td align='right'><span class='CityState'>" + listingcity + ", " + listingstate + ",</span><strong> " + listingcountry + "</strong></td></tr></table></div>").insertBefore("#bottomLoading");
                lastListingId = listingid;

            }

            loadingOnBottom = false;
        }
        else {
            if (bindNextButtonToShowMsg == true) {
                $(".nextButton").unbind("click");
                $(".nextButton").click(function() {
                    showEndOfSearchResultMessage();
                    return false;
                });

            }
            loadingOnBottom = false;
        }
        $("#bottomLoading").css("display", "none");
    });
}


function getPreviousLineN(line, index) {
    var idx = index;
    var tempLine = line;
    while (idx > 0) {
        previousLine = tempLine.prev();
//        while (previousLine != null && previousLine.nodeName != 'DIV')
//            previousLine = previousLine.previousSibling;

        if (previousLine.length == 0 || previousLine.attr("id") == "topLoading") return tempLine; else tempLine = previousLine;
        idx--;
    }
    return tempLine;
}

function getNextLineN(line, index) {
    var idx = index;
    var tempLine = line;
    while (idx > 0) {
        nextLine = tempLine.next();
//        while (nextLine != null && nextLine.nodeName != 'DIV')
//            nextLine = nextLine.nextSibling;

        if (nextLine.length == 0 || nextLine.attr("id") == "bottomLoading") return tempLine; else tempLine = nextLine;
        idx--;
    }
    return tempLine;
}


function selectLine(lineid) {
    $("#" + lineid).addClass("srSelect");
}


$(document).ready(function() {
    $("#SearchResultMainContainer").css("display", "");
    if (scrollToListingId != null && searchtype == 0) {
        scrollToListing(scrollToListingId);


        if (preSelectLine) {
            selectLine(scrollToListingId);

            $("#SearchResultInnerListingContainer").css("overflow", 'hidden');
        }
    }

    if (searchtype == 1) {
        $(".prevButton").click(function() {
        bindNextButtonToShowMsg
            showEndOfSearchResultMessage();
            return false;
        });
    }
    else {
        $(".prevButton").click(function() {
        bindNextButtonToShowMsg
            SkipPageUp();
            return false;
        });
    }

    if ($$("SearchResultListingContainer").scrollHeight <= $$("SearchResultListingContainer").clientHeight) {
        $(".nextButton").click(function() {
            showEndOfSearchResultMessage();
            return false;
        });

    }
    else {
        $(".nextButton").click(function() {
            SkipPageDown();
            return false;
        });
    }
    tb_pathToImage = "images/loading-thickbox.gif";

    // $('.stripeMe').clone().appendTo('#SearchResultListingContainer')
});

function linkToLST(url) {
    if (isShowChildrenClicked) {
        isShowChildrenClicked = false;
    }
    else {
        window.location.href = url;
    }
}


function logonPopupWithId(logonId) {
    var DialogUrl;
    DialogUrl = 'utilities/listing-logon.aspx?id=' + logonId;
    window.radopen(DialogUrl, 'MessageDialog');
    return false;

}

function showEndOfSearchResultMessage() {
    tb_show('', '#TB_inline?height=125&width=500&inlineId=endOfSearchResultMessage&modal=true', false);
}


