﻿


/*DOM Elements*/
var utility = function() {
    this.createBr = function() {
        return document.createElement("br");
    }

    this.createStrong = function(text) {
        var strong = document.createElement("strong");
        if (text.length > 0)
            strong.appendChild(document.createTextNode(text));
        return strong;
    }

    this.createDiv = function(className) {
        var div = document.createElement("div");
        if (className.length > 0)
            div.className = className;
        return div;
    }

    this.createUl = function(className) {
        var ul = document.createElement("ul");
        if (className.length > 0)
            ul.className = className;
        return ul;
    }

    this.createImg = function(source, alternateText, width, height) {
        var img = document.createElement("img");
        img.src = source;
        img.alt = alternateText;
        img.width = width;
        img.height = height;
        return img;
    }

    this.createAnchor = function(href, className, text) {
        var obj = document.createElement("a");
        if (text.length > 0)
            obj.appendChild(document.createTextNode(text));
        obj.href = href;
        if (className.length > 0)
            obj.className = className;

        return obj;
    }

    this.createTable = function(width, cellPadding, cellSpacing, className, align, rows, cells) {
        var obj = document.createElement("table");
        if (width.length > 0)
            obj.width = width;

        obj.cellPadding = cellPadding;

        obj.cellSpacing = cellSpacing;

        if (className.length > 0)
            obj.className = className;

        if (align.length > 0)
            obj.align = align;

        if (rows > 0) {
            var i = 1, j = 1;
            var tr, td;
            while (i <= rows) {
                tr = document.createElement("tr");
                if (cells > 0) {
                    while (j <= cells) {
                        td = document.createElement("td");
                        tr.appendChild(td);
                        j++;
                    }
                }
                obj.appendChild(tr);
                i++;
            }
        }

        return obj;
    }

    this.selectOption = function(elementId, value) {
            var obj = document.getElementById(elementId);
            for (var i = 0; i < obj.options.length; i++) {
                if (obj.options[i].value == value) {
                    obj.options[i].selected = true;
                    break;
                }
            }
        }

    this.formatString = function(text) {
        //check if there are two arguments in the arguments list
        if (arguments.length <= 1) {
            //if there are not 2 or more arguments there’s nothing to replace
            //just return the original text
            return text;
        }
        //decrement to move to the second argument in the array
        var tokenCount = arguments.length - 2;
        for (var token = 0; token <= tokenCount; token++) {
            //iterate through the tokens and replace their placeholders from the original text in order
            text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
        }
        return text;
    }
    this.trim = function(str) {
        return str.replace(/^\s+|\s+$/g, "");
    }

    this.bindProductListView = function(page, dataSource, divName) {

        if (dataSource != null && dataSource.length > 0) {
            var nuColumns = 1;
            var nuTotalColumns = 4;
            var ul;
            var divBoxListagem;
            var obj = dataSource[0];
            var index = 0;
            var pageCount = 0, recordCount = 0;
            pageCount = obj["pageCount"];
            recordCount = obj["tableCount"];

            ul = utility.createUl("products_list_listagem");
            for (index = 0; index < dataSource.length; index++) {
                ul.appendChild(this.getProduct(dataSource[index]));
                if (nuColumns == nuTotalColumns) {
                    divBoxListagem = utility.createDiv("box_listagem");
                    divBoxListagem.appendChild(ul);
                    $(divName).append(divBoxListagem);
                    nuColumns = 0;

                    ul = utility.createUl("products_list_listagem");
                }
                nuColumns++;
            }
            if (nuColumns > 1) {
                divBoxListagem = utility.createDiv("box_listagem");
                divBoxListagem.appendChild(ul);
                $(divName).append(divBoxListagem);
            }

            this.createProductListDraggable();

        }
        else {
            divBoxListagem = utility.createDiv("box_listagem");
            divBoxListagem.appendChild(document.createTextNode("Nenhum produto encontrado."));
            $(divName).append(divBoxListagem);
        }
    }

    this.getProduct = function(row) {
        var li = document.createElement("li");
        var divProdsListagem = utility.createDiv("prods_listagem");
        var div = utility.createDiv("conteudo_prods_listagem");
        var div2, h2, obj, obj2;
        var vlSalePrice;
        var vlListPrice = parseInt(row["vlListPrice"]);

        if (row["vlSalePrice"] == null)
            vlSalePrice = 0;
        else
            vlSalePrice = parseInt(row["vlSalePrice"]);

        //obj = utility.createAnchor(row["dsUrl"], "", "");
        //obj.appendChild(utility.createImg(row["dsImageList"], row["dsNameFormatted"], 189, 285));
        obj = utility.createImg(row["dsImageList"], row["dsNameFormatted"], 189, 285);
        obj.setAttribute("productImage", row["dsImageList"]);
        obj.setAttribute("idProduct", row["idProduct"]);
        obj.setAttribute("productName", row["dsName"]);
        obj.setAttribute("productUrl", row["dsUrl"]);

        div.appendChild(obj);
        divProdsListagem.appendChild(div);

        div = null;
        div = utility.createDiv("text_conteudo_prods_listagem");
        div2 = utility.createDiv("padding_bot_5");

        h2 = document.createElement("h2");
        obj = utility.createAnchor(row["dsUrl"], "", row["dsName"]);
        h2.appendChild(obj);
        div2.appendChild(h2);

        if (parseInt(row["fgAvailable"]) == 1 && vlListPrice > 0) {
            if (vlSalePrice > 0 && vlSalePrice < vlListPrice) {
                obj = document.createElement("span");
                obj.appendChild(document.createTextNode("De: " + row["vlListPriceFormatted"]));
                obj.appendChild(utility.createBr());
                div2.appendChild(obj)

                obj = document.createElement("span");
                obj2 = utility.createStrong(row["vlSalePriceFormatted"]);
                obj2.appendChild(utility.createBr());
                obj.appendChild(obj2);
                div2.appendChild(obj);
            }
            else {
                obj = document.createElement("span");
                obj2 = utility.createStrong("Por: " + row["vlListPriceFormatted"]);
                obj.appendChild(obj2);
                obj.appendChild(utility.createBr());
                div2.appendChild(obj);
            }


        }
        else {
            obj = document.createElement("span");
            obj2 = utility.createStrong("Produto indisponível");
            obj.appendChild(obj2);
            obj2.appendChild(utility.createBr());
            div2.appendChild(obj);
        }

        div.appendChild(div2);
        divProdsListagem.appendChild(div);
        li.appendChild(divProdsListagem);
        return li;
    }

    this.createProductListDraggable = function() {


        $("#dvHeaderCart,#dvCartItems").droppable({
            accept: "div[class='conteudo_prods_listagem'] > img",
            drop: function(event, ui) {
                var idProduct = ui.helper.attr("idProduct");
                $("#dialog").dialog("open");
                setTimeout(function() { ondvHeaderCartDrop(idProduct); }, 1000);

            }
        });

        $("div[class='conteudo_prods_listagem'] > img").click(function() {
            document.location = $(this).attr("productUrl");
        });

        $("div[class='conteudo_prods_listagem'] > img").mouseover(function() {
            $(this).css("cursor", "move");
        });

        $("div[class='conteudo_prods_listagem'] > img").mouseout(function() {
            $(this).css("cursor", "cursor");
        });

        $("div[class='conteudo_prods_listagem'] > img").draggable({
            cursor: 'move',
            //appendTo: "body",
            helper: function(event) {
                var cellProduct;
                var trProduct;
                var tableProduct, obj;
                var div = document.createElement("div");
                div.setAttribute("style", "width:300px;float:none;background-color:#FFFFFF;font-size: 12px;color: #000000;");
                div.className = "border_geral_bask_cinza";
                div.setAttribute("idProduct", $(this).attr("idProduct"));
                tableProduct = utility.createTable("95%", 0, 0, "", "center", 1, 2);
                tableProduct.border = "0";
                trProduct = tableProduct.firstChild;
                cellProduct = trProduct.childNodes.item(0);
                cellProduct.align = "center";
                cellProduct.width = "70";
                cellProduct.appendChild(utility.createImg($(this).attr("productImage"), "", 60, 87));

                cellProduct = trProduct.childNodes.item(1);
                cellProduct.align = "left";
                cellProduct.appendChild(document.createTextNode($(this).attr("productName")));

                div.appendChild(tableProduct);
                return div;
            }
        });
    }



    this.createPageClick = function(anchor) {
        var href = new String();
        document.location.href.replace(anchor.attr("href"));
        href = anchor.attr("href");
        href = href.replace("#", "");
        listViewPageIndexChanged(parseInt(href));
    }

    this.bindPages = function(div, currentPage, pageCount) {
        var startPage = 0;
        var endPage = 0;
        var totalPages = 0;
        var span = document.createElement("span");
        span.className = "pag_selecionada";
        div.empty();
        div.append(document.createTextNode("Pág.: "));

        if (currentPage > 1)
            div.append(this.createAnchor("#" + (parseInt(currentPage) - 1), "", "< "));


        if ((currentPage - 5) < 1)
            startPage = 1;
        else
            startPage = currentPage - 5;

        if ((startPage + 10) < pageCount)
            endPage = startPage + 10;
        else
            endPage = pageCount;

        totalPages = endPage - startPage - 1;

        if (totalPages < 10 && startPage > 1)
            startPage -= (10 - (endPage - startPage));

        if (startPage < 1)
            startPage = 1;

        for (var i = startPage; i <= endPage; i++) {
            if (i == currentPage) {
                span.appendChild(document.createTextNode(i + " "));
                div.append(span);
            }
            else {
                div.append(this.createAnchor("#" + i, "", i + " "));
            }
        }

        if (currentPage < pageCount) {
            div.append(this.createAnchor("#" + (parseInt(currentPage) + 1), "", ">"));
        }
        return div;
    }

    this.createMyOrdersListView = function(dataSource, page, tableName) {
        var index = 0;
        var content = "";
        var itemTemplate = "";
        var pageCount = 0, recordCount = 0;
        if (dataSource != null && dataSource.length > 0) {
            var tr, tr2;
            var td, cell, table, el, anchor;
            var obj = dataSource[0];
            pageCount = obj.pageCount;
            recordCount = obj.tableCount;

            tr = document.createElement("tr");
            td = document.createElement("td");
            td.align = "center";
            for (index = 0; index < dataSource.length; index++) {
                table = this.createTable("100%", 2, 0, "border_bask_sem_top", "", 1, 5);
                table.border = "0";
                tr2 = table.firstChild;

                cell = tr2.childNodes.item(0);
                cell.width = "121";
                cell.align = "center";
                cell.appendChild(document.createTextNode(dataSource[index].dtOrderFormatted));

                cell = tr2.childNodes.item(1);
                cell.width = "209";
                cell.align = "center";
                el = this.createStrong("");
                el.appendChild(this.createAnchor(this.formatString("{0}Site.aspx/PedidoConcluido/{1}", dataSource[index].defaultWebPath, dataSource.cdOrder), "arial_12_preto_basket", dataSource[index].cdOrder));
                cell.appendChild(el);

                cell = tr2.childNodes.item(2);
                cell.width = "199";
                cell.align = "center";
                cell.appendChild(document.createTextNode(dataSource[index].vlTotalOrderFormatted));

                cell = tr2.childNodes.item(3);
                cell.align = "center";
                cell.appendChild(document.createTextNode(dataSource[index].dsPaymentTypeName));

                cell = tr2.childNodes.item(4);
                cell.width = "90";
                cell.align = "center";
                cell.appendChild(this.createImg(dataSource[index].dsOrderStatusTypeIcon, "", 78, 28));

                td.appendChild(table);
            }
            tr.appendChild(td);
            $(tableName).append(tr);
        }
        else {
            $("#tbPedidosHeader").hide();
            $("#tbContent").hide();
            $(".paginacao").hide();
        }

    }

    this.bindNewsListView = function(page, dataSource, divName) {
        if (dataSource != null && dataSource.length > 0) {
            var obj = dataSource[0];
            var index = 0;
            var pageCount = 0, recordCount = 0;
            var divMain, divLeft, divRight, img, href, div, strong;
            pageCount = obj["pageCount"];
            recordCount = obj["tableCount"];

            for (index = 0; index < dataSource.length; index++) {

                divMain = this.createDiv("conteudo_noticias2");

                if (dataSource[index]["dsImageFormatted"] != "") {
                    divLeft = this.createDiv("");
                    divLeft.setAttribute("style", "float:left;width:74px;");
                    div = this.createDiv("img_noticias");
                    img = this.createImg(dataSource[index]["dsImageFormatted"], "", 68, 68);
                    div.appendChild(img);
                    divLeft.appendChild(div);
                    div = this.createDiv("conteudo_veja_mais");
                    strong = this.createStrong("");
                    strong.appendChild(this.createAnchor(dataSource[index]["dsUrlFormatted"], "", "veja mais"));
                    div.appendChild(strong);
                    divLeft.appendChild(div);
                    divMain.appendChild(divLeft);
                }
                divRight = this.createDiv("");
                divRight.setAttribute("style", "font-size: 12px;color: #707371;");
                div = this.createDiv("texto_noticias2 padding_bot_5");
                strong = this.createStrong("");
                strong.appendChild(this.createAnchor(dataSource[index]["dsUrlFormatted"], "", dataSource[index]["dsTitle"]));
                strong.appendChild(this.createBr());
                strong.appendChild(document.createTextNode(dataSource[index]["dtStartFormatted"]));
                strong.appendChild(this.createBr());
                div.appendChild(strong);
                div.appendChild(document.createTextNode(dataSource[index]["dsContent"]));
                divRight.appendChild(div);
                divMain.appendChild(divRight);

                $(divName).append(divMain);
                $(divName).append(this.createBr());
            }

        }
        else {
            div = utility.createDiv("box_listagem");
            div.appendChild(document.createTextNode("Nenhum evento encontrado."));
            $(divName).append(div);
        }
    }
}
