// I like privately scoped properties, so let's not use JSON.

// Please do not use invisible objects.

// IE 6+ supported

var DnrPCMod = new

function () {

    var dpm = this,

        doc = document,

        grid, ldrImg, dnrPc, dnrTxt, maxTries = 200,

        tick = 600,

        dnrPcGridId = "dnr-pc-grid",

        dnrPcLdrId = "dnr-pc-ldr",

        dnrPcId = "dnr-pc",

        dnrTxtId = "dnr-pc-sort-txt",

        jsonList, cType, alpha = {

            "a": 1,

            "b": 2,

            "c": 3,

            "d": 4,

            "e": 6,

            "f": 7,

            "g": 8,

            "h": 9,

            "i": 10,

            "j": 11,

            "k": 12,

            "l": 13,

            "m": 14,

            "n": 15,

            "o": 16,

            "p": 17,

            "q": 19,

            "r": 20,

            "s": 21,

            "t": 22,

            "u": 23,

            "v": 24,

            "w": 25,

            "x": 26,

            "y": 27,

            "z": 28

        },

		dnrPageLink = "http://europeanspermbankusa.com/why-us/oid-pc-link.html";



    // Public member to initialize the object (i.e. initialize, loading, etc.)

    dpm._init_ = function () {

        // Disregard the scope-chain performance here

        window.onunload = dispose;



        // Get handles on the grid DIV in which the actual list will be loaded,

        // and the loader image to hide when the grid content is ready.

        grid = doc.getElementById(dnrPcGridId);

        ldrImg = doc.getElementById(dnrPcLdrId);

        dnrPc = doc.getElementById(dnrPcId);

        dnrTxt = doc.getElementById(dnrTxtId);



        if (!(grid && ldrImg && dnrPc && dnrTxt)) {


            return;

        }



		dnrPc.onclick = function() {

			window.location.replace( dnrPageLink );

		};

		

        dnrTxt.onchange = sortChange;

		dnrTxt.onclick = sortClick;

        cType = 0;

    };



    function sortClick( e ) {

		var ev = ( e || window.event );

		

		if ( ev.stopPropagation ) {

			ev.stopPropagation();

		}

		ev.cancelBubble = true;

		ev.returnValue = false;

	}

	

    function sortChange(e) {

        var ev = (e || window.event),

            tar = (ev.target || ev.srcElement);

        sort(tar.value);

    }



    // Note: quick and dirty name sort by only the first character





    function sort(type) {

        var llen, i, data, sorted;



        type = parseInt(type, 10);



        if (jsonList && type && type !== cType) {

            cType = type;

            data = jsonList.data;

            llen = data.length;



            // Note: ICU & IUI & COMPANY_NAME have similar sort code, I will functionize it later on

            switch (type) {

            case 1:



                do {

                    sorted = 1;



                    for (i = 1; i < llen; ++i) {

                        if (parseFloat(data[i].ICI) > parseFloat(data[i - 1].ICI)) {

                            sorted = 0;

                            data.splice(i - 1, 0, data[i]);

                            data.splice(++i, 1);

                        }

                    }

                } while (!sorted);



                emptyGrid();

                jsonList.data = data;

                DnrPCMod.loadGrid(jsonList);

                break;



            case 2:



                do {

                    sorted = 1;



                    for (i = 1; i < llen; ++i) {

                        if (parseFloat(data[i].IUI) > parseFloat(data[i - 1].IUI)) {

                            sorted = 0;

                            data.splice(i - 1, 0, data[i]);

                            data.splice(++i, 1);

                        }

                    }

                } while (!sorted);



                emptyGrid();

                jsonList.data = data;

                DnrPCMod.loadGrid(jsonList);

                break;



            case 3:



                do {

                    sorted = 1;



                    for (i = 1; i < llen; ++i) {

                        if (highAlpha(data[i].COMPANY_NAME, data[i - 1].COMPANY_NAME)) {

                            sorted = 0;

                            data.splice(i - 1, 0, data[i]);

                            data.splice(++i, 1);

                        }

                    }

                } while (!sorted);



                emptyGrid();

                jsonList.data = data;

                DnrPCMod.loadGrid(jsonList);

                break;

            }

        } else {

            cType = 0;

        }

    }



    function highAlpha(str1, str2) {

        var ret = 0,

            a1, a2, alen, i;



        if (typeof str1 === "string" && typeof str2 === "string") {

            a1 = str1.toLowerCase().split("");

            a2 = str2.toLowerCase().split("");



            if (alpha[a1[0]] < alpha[a2[0]]) {

                ret = 1;

            }

        }



        return ret;

    }



    // Used to load the DNR list into the grid

    dpm.loadGrid = function (list) {

        var DIV = "DIV",

            nameClass, rowClass, oddClass, rowEl, nameEl, iciEl, iuiEl, i, llen, data, obj, odd;



        // Opera doesn't understand undefined as a string (i.e. "undefined") so just used the reserved constant

        if (list && list.data && list.data.length !== undefined) {

            nameClass = "dnr-pc-nme";

            rowClass = "dnr-pc-row";

            oddClass = "odd";

            data = list.data;

            llen = data.length;

            i = 0;

            odd = false;



            for (; i < llen; ++i) {

                obj = data[i];



                if (obj) {

                    // let's build the row rather use node meth's instead of innerHTML for compatability

                    rowEl = doc.createElement(DIV);

                    rowEl.className = (odd) ? rowClass + " " + oddClass : rowClass;

                    iuiEl = doc.createElement(DIV);
                    iciEl = doc.createElement(DIV);

                    iuiEl.appendChild(doc.createTextNode(obj.IUI));

                    rowEl.appendChild(iuiEl);
					
                    iciEl.appendChild(doc.createTextNode(obj.ICI));

                    rowEl.appendChild(iciEl);


                    nameEl = doc.createElement(DIV);

                    nameEl.appendChild(doc.createTextNode(obj.COMPANY_NAME));

                    nameEl.className = nameClass;

                    rowEl.appendChild(nameEl);

                    rowEl = grid.appendChild(rowEl);

                    odd = !odd;

                }

            }

        } else {

            grid.appendChild(doc.createTextNode("Unable to load list. Please refresh the page."));

        }



        rowEl.className += " last";



        // If the last row is an odd one, we want a blue end (i.e "blend" ) :P

        if (!odd) {

            dnrPc.className += " blend";

        }



        // Let's remove the loader image from the container and display the grid

        if (!jsonList) {

            ldrImg = ldrImg.parentNode.removeChild(ldrImg);

        }

        jsonList = list;

        grid.style.display = "block";

    };



    function emptyGrid() {

        while (grid.childNodes[0]) {

            grid.removeChild(grid.childNodes[0]);

        }

    }



    // Prevent IE memory leaks by disposing all references to DOM elements and etc.





    function dispose() {

        doc =

        ldrImg =

        dnrPc = 0;

    }

};
