var PRODUCTS = function() {
    var _P = {
        init: function(params) {
            _P.params = params;
            _P.loadXml();
        },
        params: null,
        data: null,
        loadXml: function() {
            jQuery("ul", jQuery("div.category")).html('<li class="loader"></li>');
            jQuery("div.category .prev").css("visibility", "hidden");
            jQuery("div.category .next").css("visibility", "hidden");

            var xmlLink = "products_xml.aspx?" + _P.params.xmlPath
											.replace(/@/g, "=").replace(/\|/g, "&");
            jQuery.ajax({
                type: "POST",
                url: xmlLink,
                dataType: "xml",
                success: function(data) {
                    _P.data = data;
                    _P.max = (_P.params.items == undefined) ? 20 : _P.params.items;  // 4 rows * 5 items
                    _P.count = jQuery("product", data).length; //product > XML Node
                    _P.first = 0;

                    //Unbind Event For Clear Item Chage
                    jQuery("div.category .prev").unbind();
                    jQuery("div.category .next").unbind();

                    if (_P.count > 0) {
                        _P.preloadProducts();

                        //Call from WORLDTECHCART class jquery function for init event runtime
                        WORLDTECHCART.init();

                        //INIT thickbox function
                        tb_init('a.thickbox, area.thickbox, input.thickbox');

                        _P.browseProducts();
                    } else {
                        jQuery("ul", jQuery("div.category")).html('<li class="no_item">Sorry! not found data.</li>');
                    }
                }
            });
        },
        first: 0,
        max: 0,
        count: 0,
        preloadProducts: function() {
            jQuery("ul", "div.category").empty();
            jQuery("product", _P.data).each(function(i) {
                var title = jQuery.trim(jQuery("title", this).text());
                var bodytext = "";  //jQuery.trim( jQuery( "bodytext", this ).text() );
                var newsdate = jQuery.trim(jQuery("newsdate", this).text());
                var is_modal = jQuery.trim(jQuery("modal", this).text());
                var wishlish = jQuery.trim(jQuery("wishlish", this).text());
                var href = jQuery.trim(jQuery("href", this).text());
                if (bodytext != "" || newsdate != "") {
                    if (bodytext != "" && newsdate != "") {
                        bodytext = "<div>" + newsdate + "</div><div>" + bodytext + "</div>";
                    } else if (bodytext == "" && newsdate != "") {
                        bodytext = "<div>" + newsdate + "</div>";
                    }

                    if (bodytext != "") {
                        bodytext = "<div class='details'>" + bodytext + "</div>";
                    }
                }

                var linkImgmodal = "";
                var linkmodal = "title";
                if (is_modal == "1") {
                    linkImgmodal = " class='thickbox' ";
                    linkmodal = "title thickbox";
                }

                var btnWishlist = "";
                if (wishlish != "") {
                    btnWishlist += "<div class='wishlish_wrapper'>";
                    btnWishlist += "" + wishlish + "";
                    btnWishlist += "<a class='buy' title='Click here to request information about this product' href='cart.aspx'>Buy Now</a>";
                    btnWishlist += "</div>";
                }

                jQuery("ul", "div.category").append([
					"<li><div class='image'><a " + linkImgmodal + "title='", title, "' href='", href, "'><img alt='", title, "' src='", jQuery.trim(jQuery("image > src", this).text()), "' /></a></div><a title='", title, "' class='", linkmodal, "' href='", href, "'>", title, "</a>" + bodytext + btnWishlist + "</li>"].join(""));
            });

            jQuery("div.category .prev").click(function() {
                _P.browseProducts("prev");
                return false;
            });
            jQuery("div.category .next").click(function() {
                _P.browseProducts("next");
                return false;
            });
        },

        browseProducts: function(browse) {
            //console.log("============== browseProducts First ================")
            //console.log("_P.first=" + _P.first + " / _P.max=" + _P.max + " / range = " + range + " / start = " + start)

            if (browse == "prev") {
                if (_P.first == _P.count && (_P.count % _P.max > 0)) {
                    _P.first = _P.first - ((_P.count % _P.max) + _P.max);
                } else {
                    _P.first = _P.first - (_P.max * 2);
                }
            }
            var range = _P.first + _P.max;
            var start = 1;
            if (range > _P.max) {
                start = ((range - _P.max) + 1);
            }
            if (_P.first == 0) {
                jQuery("div.category .prev").css("visibility", "hidden");
            } else {
                jQuery("div.category .prev").css("visibility", "visible");
            }
            if (range < _P.count) {
                jQuery("div.category .next").css("visibility", "visible");
            } else if (range >= _P.count) {
                //range = _P.count;
                jQuery("div.category .next").css("visibility", "hidden");
            }
         
            jQuery("product", _P.data).each(function(i) {
                if (i >= _P.first && i < range) {
                    jQuery("div.category li:eq(" + i + ")").fadeIn("slow");
                    if (i == (range - 1)) {
                        jQuery("div.category li:eq(" + i + ")").addClass("last");
                    }
                } else {
                    jQuery("div.category li:eq(" + i + ")")
						.css("display", "none")
						.removeAttr("class");
                }
            });
            _P.first = range;

            //console.log("============== browseProducts Second ================")
            //console.log("_P.first=" + _P.first + " / _P.max=" + _P.max + " / range = " + range + " / start = " + start)

            /*
            jQuery( "div.category .showing" ).html([
            "Viewing <strong>",
            start,
            " - ",
            range,
            "</strong> of <strong>",
            _P.count,
            "</strong>" ].join( "" ));
            */
        }
    };
    return {
        init: function(params) {
            _P.init(params);
        }
    };
} ();

