﻿/* HQCS common - Tool guide animator */

var ToolGuideAnimator = function() {
    // -------------------------
    // *** Private variables ***
    // -------------------------
    var _products = new Object();

    // -----------------------
    // *** Private methods ***
    // -----------------------
    var bindMenu = function(elem) {
        // Setup hover/click
        var clickParts = $(".tg_img_div, .tg_name, .tg_check, .tg_leftpart, .tg_body", elem);
        clickParts.hover(
			function() {
			    $(this).parents(".tg_resultitem_compact, .tg_resultitem_detailed").find(".tg_check:not(.tg_mark)").addClass("tg_mark_hover").show();
			},
			function() {
			    $(this).parents(".tg_resultitem_compact, .tg_resultitem_detailed").find(".tg_check").removeClass("tg_mark_hover");
			}
		);
        clickParts.click(
			function() {
			    $(this).parents(".tg_resultitem_compact, .tg_resultitem_detailed").find(".tg_check").removeClass("tg_mark_hover").toggleClass("tg_mark");
			}
		);

        // Setup menu
        clickParts.contextMenu("toolguideMenu", {
            eventType: "click",
            menuStyle: {
                width: "150px"
            },
            bindings: {
                "compare": function(t) {
                    var result = $("#tg_result");
                    var products = $(".tg_resultitem_compact:has('.tg_mark'), .tg_resultitem_detailed:has('.tg_mark')", result);
                    var nids = new Array();
                    $.each(products, function(i, prod) {
                        var idparts = prod.getAttribute("id").split("_");
                        nids.push(idparts[1]);
                    });
                    var href = ToolGuide.getCompareNode() + "?nid=" + nids;
                    location.href = href;
                },

                "go": function(t) {
                    var href = $(t).parents(".tg_resultitem_compact, .tg_resultitem_detailed").find("a").attr("href");
                    location.href = href;
                }
            }
        });
    };

    return {
        // -----------------------
        // *** Public methods ***
        // -----------------------

        hideAll: function() {
            $.each(_products, function(pid, product) {
                if (product.show) {
                    product.div.animate({ left: "-=100", top: "-=100", opacity: "hide" }, { duration: 1000 });
                    product.hidden = true;
                }
            });
        },

        onBeforeRequest: function() {
        },

        animateResult: function(result) {
            var outputdiv = $("#tg_result");

            var countLabel = $("div#tg_result_count");
            var text = ToolGuide.resultCountFormat.replace(/\%n%/, result.products.length);
            countLabel.text(text);

            // Assume all hidden
            $.each(_products, function(pid, product) {
                product.show = false;
            });

            if (result.products.length > 0)
                $("#tg_noresult").fadeOut("slow");
            else
                $("#tg_noresult").fadeIn("slow");

            var columns;
            var itemwidth;
            var itemheight;
            var templatediv;
            var techdatadiv;
            if (result.mode == "full") {
                templatediv = $("#tg_template_detailed");
                techdatadiv = $("div.tg_techdata", templatediv);
                columns = 2;
                itemwidth = 300;
                itemheight = 320;
            }
            else {
                templatediv = $("#tg_template_compact");
                columns = 4;
                itemwidth = 150;
                itemheight = 160;
            }
            
            // Set height on output div for footer placement    
            if (result.products.length > 0) {
                outputdiv.css("height", itemheight * (Math.ceil(result.products.length / columns)));
            }

            var currX;
            var currY;
            $.each(result.products, function(i, item) {
                var itemkey = [result.mode, item.nid].join("_");
                currX = itemwidth * (i % columns);
                currY = itemheight * Math.floor(i / columns);

                // Already in cache?
                var product;
                if (_products[itemkey]) {
                    product = _products[itemkey];
                    product.show = true;
                    product.hidden = false;
                }
                else {
                    var clone = templatediv.clone().attr("id", itemkey);
                    clone.find(".tg_name").text(item.name);

                    if (i % 2 == 1)
                        clone.addClass("tg_alt");

                    var imgurl = item.img;
                    if (imgurl != null && imgurl.length > 0)
                        clone.find(".tg_img").attr("src", imgurl).attr("title", item.name);
                    else
                        clone.find(".tg_img").remove();

                    clone.find("a").attr("href", ToolGuide.getDetailsNode() + "?pid=" + item.pid + "&nid=" + item.nid);

                    if (item.attrs) {
                        var tdlocation = $(".tg_techdata", clone);
                        $.each(item.attrs, function(j, techdata) {
                            var tddiv = techdatadiv.clone();
                            $(".tg_tdname", tddiv).text(techdata.name + ":");
                            $(".tg_tdval", tddiv).text(techdata.values);
                            if (j % 2 == 1)
                                tddiv.addClass("tg_alt");
                            tddiv.insertBefore(tdlocation);
                        });
                        tdlocation.remove();
                    }

                    clone.css("position", "absolute");
                    clone.css("width", itemwidth).css("height", itemheight); // Required for IE opacity to work
                    outputdiv.append(clone);

                    bindMenu(clone);

                    // Put in cache
                    product = { show: true, hidden: false, div: clone };
                    _products[itemkey] = product;
                }

                // Display product (must break in two parts because of jQuery fade bug)
                product.div.animate({ opacity: "show" }, { queue: false, duration: 1000 })
				.animate({ left: currX, top: currY }, { duration: 1000 });

            });

            // Hide non matching products
            $.each(_products, function(pid, product) {
                if (!product.show && !product.hidden) {
                    product.div.animate({ left: "-=100", top: "-=100", opacity: "hide" }, { duration: 1000 });
                    product.hidden = true;
                }
            });
        }

        // ToolGuideAnimator end
    }
} ();
