
// for debug
if (typeof(console) == "undefined") {
    window.console = {
        log: function(text) {
            //alert(text);
        }
    };
}

window.wapa = {
       
    infoTooltip: null,

    productTree: {
        handle: null,
        lastClickedNode: null,

        onExpand: function(node) {
            
            if (typeof(node.data.image) == "undefined") {
                
                //this is a category node
                wapa.productTree.selectLabel(node.children[0]);
            }
        },

        selectLabel: function(node) {            

            if (typeof(node.data.image) != "undefined") {

                // hide product tree instructions (if necessary)
                if ($("productInstructions").style.display != "none") {
                    $("productInstructions").style.display = "none";
                }

                // if there was a last clicked node, then remove focus
                if (this.lastClickedNode != null) {
                    YAHOO.util.Dom.removeClass(this.lastClickedNode, 'current');
                }

                // save current label as last clicked node
                this.lastClickedNode = node.labelElId
                YAHOO.util.Dom.addClass(this.lastClickedNode, 'current');

                // show product info box
                // setTimeout needed for correct switch/rendering of images in Explorer 6 (very weird bug)
                //var nodeData = node.data;
                setTimeout(function() {wapa.productTree.showProduct(node.data);},0);

            } else {
                // if the label has children, then expand them all
                if (typeof(node.children[0] != "undefined")) {
                    node.expandAll();
                }
            }
        },

        onClick: function(oArgs) {
            
            wapa.productTree.selectLabel(oArgs.node);
        },

        showProduct: function(productData) {
            var empty = "---";

            $("productInfoName").innerHTML = productData.name || empty;
            $("productInfoWeight").innerHTML = productData.weight || empty;
            $("productInfoSize").innerHTML = productData.size || empty;
            $("productInfoQuantity").innerHTML = productData.itemsPerPackage || empty;
            $("productInfoPrice").innerHTML = productData.pricePerPackage || empty;

            //document.getElementById('product_text').innerHTML = info;
            var image_url = 'images/products/' + productData.image;
            var image = $('product_image');
            image.style.visibility = 'visible';
            image.src = image_url + '_small.jpg';

            if ($("productinfo").style.display == "none") {
                $("productinfo").style.display = "block";
            }

            // update tooltip infotext
            wapa.createTooltip(productData.info);
        },
	
        init: function() {
            this.handle = new YAHOO.widget.TreeView("treeview");
            var root = this.handle.getRoot();

            for (var i=0; i < treeData.length; i++) {
                var nodeObj = {label:treeData[i].category};
                var node = new YAHOO.widget.MenuNode(nodeObj, root);
                node.setNodesProperty("enableHighlight", false);
                for (var j=0; j < treeData[i].products.length; j++) {
                    var node2 = new YAHOO.widget.TextNode(treeData[i].products[j], node);
                    node2.setNodesProperty("enableHighlight", false);
                }
            }

            // attach event handlers
            this.handle.subscribe("expandComplete", this.onExpand);
            this.handle.subscribe("clickEvent", this.onClick);

            //this.handle.setExpandAnim(YAHOO.widget.TVAnim.FADE_IN);
            //this.handle.setCollapseAnim(YAHOO.widget.TVAnim.FADE_OUT);

            this.handle.draw();
        }
    },
	
    /*
    * If the product element is hidden then it is
    * displayed and vice versa
    */
    showProductInfo: function(productId) {
        var element = $(productId);
        if (element.style.display == "none") {
            element.style.display = "table-cell";
        }
        else {
            element.style.display = "none";
        }
    },

    createTooltip: function(text) {

        if (this.infoTooltip == null) {
            this.infoTooltip = new YAHOO.widget.Tooltip("infoTooltip", {
                    context:"product_image",
                    container:"productinfo",
                    text:text,
                    showDelay:500,
                    autodismissdelay:3600000, // 1hour
                    width:"400px"} );
        } else {
            // just update text
            this.infoTooltip.cfg.setProperty("text", text);
        }       
    },

    onload: function() {
        // render product tree (for certain sections)
        if (	getQueryStringParameter("sektion") == "produkter" &&
                getQueryStringParameter("subsektion") == "bestall" &&
                (getQueryStringParameter("steg") == null || getQueryStringParameter("steg") == "1")	) {
            this.productTree.init();
        }
    }
};

 
window.onload = function()
{

    // initialize page
    wapa.onload();

    // apply rounded corners to header section
    var settings = {
      tl: { radius: 10 },
      tr: { radius: 10 },
      bl: false,//{ radius: 10 },
      br: false,//{ radius: 10 },
      antiAlias: true,
      autoPad: false,
      validTags: ["div"]
    };

    roundedCorners = new curvyCorners(settings, document.getElementById("container"));
    roundedCorners.applyCornersToAll();
}
