/*
 * jQuery variantSelect Plugin for KeyPublisher
 * version: 1.0
 * @requires jQuery v1.3.2 or later
 *
 * @author  Bjørnar Helland
 *  2010-03-09 - Initial version
 */

/**
 * Converts passed JSON options into <select> elements.
 * 
 * @param String
 *            id of the second select box. If empty or not found, it's ignored
 * @param String
 *            option values
 * @param array
 *            options additional options (optional)
 */

 (function($jQ) {
    $jQ.fn.variantSelect = function(doubleid, values, options)
    {

        options = $jQ.extend({
            preselectFirst: null,
            preselectSecond: null,
            emptyOption: false,
            emptyKey: -1,
            emptyValue: 'Velg ...'
        },
        options || {});

        var $first = this;
        var $secondid = "#" + doubleid;
        var $second = $jQ($secondid);

        var setValue = function(value)
        {
            $second.val(value).change();
        };

        var removeValues = function()
        {
            $jQ($secondid + " option").remove();
        };
        
        if ($second.length > 0)
        {
            $jQ(this).change(function()
                {
                    removeValues();
                    $current = this.options[this.selectedIndex].value;
                    if ($current != '')
                    {
                        $jQ.each(values,
                            function(k, v)
                            {
                                var bestk;
                                if ($current == k)
                                {
                                    $jQ.each(v.option2,
                                        function(k, v2)
                                        {
                                            if (!bestk && (v.defaultvalue != null && v2.variantId == v.defaultvalue))
                                            {
                                                bestk = k;
                                            }
                                            if (options.preselectSecond != null && v2.variantId == options.preselectSecond)
                                            {
                                                bestk = k;
                                            }
                                        }
                                    );
                                    $jQ.each(v.option2,
                                        function(k, v2)
                                        {
                                            var o = $jQ("<option>").html(v2.name).attr('value', v2.variantId);
                                            if (k === bestk)
                                            {
                                                o.html(k).attr("selected", "selected");
                                            }
                                            o.appendTo($second);
                                        }
                                    );
                                }
                            }
                        );
                    }
                    else
                    {
                        setValue(options.emptyValue);
                    }
                }
            );
        }

        return this.each(function()
        {
            $first.children().remove();
            $second.children().remove();

            if (options.emptyOption)
            {
                var oe = $jQ("<option>").html(options.emptyValue).attr('value', options.emptyKey);
                oe.appendTo($first);
            }

            $jQ.each(values,
                function(k, v)
                {
                    if (!$second.length && v.variantId)
                    {
                        var tmpValue = v.variantId;
                    }
                    else
                    {
                    	var tmpValue = k;
                    }
                    var of = $jQ("<option>").html(v.name).attr('value', tmpValue);
                    var stock = v.stock.available;
                    if (options.preselectFirst != null && k == options.preselectFirst)
                    {
                        of.html(tmpValue).attr("selected", "selected");
                    }
                    
                    /* Håkon Bjerkenes addin for CHECKING STOCK */
                    if(stock > 0) {
                    	of.appendTo($first);
                    }
                }
            );

            if (options.preselectFirst == null && $second.length > 0)
            {
                $current = this.options[this.selectedIndex].value;
                if ($current != '')
                {
                    $jQ.each(values,
                        function(k, v)
                        {
                            var bestk;
                            if ($current == k)
                            {
                                $jQ.each(v.option2,
                                    function(k, v2)
                                    {
                                        if (!bestk && (v.defaultvalue != null && v2.variantId == v.defaultvalue))
                                        {
                                            bestk = k;
                                        }
                                        if (options.preselectSecond != null && v2.variantId == options.preselectSecond)
                                        {
                                            bestk = k;
                                        }
                                    }
                                );
                                $jQ.each(v.option2,
                                    function(k, v2)
                                    {
                                        var o = $jQ("<option>").html(v2.name).attr('value', v2.variantId);
                                        if (k === bestk)
                                        {
                                            o.html(k).attr("selected", "selected");
                                        }
                                        o.appendTo($second);
                                    }
                                );
                            }
                        }
                    );
                }
                else
                {
                    setValue(options.emptyValue);
                }
            }
            else
            {
                $first.change();
            }
        });
    };
})(jQuery);

