// JavaScript Document
$(document).ready(function(){	
//slider
	$("#slider").easySlider({
		auto: true, 
		continuous: true
	});		
//table
	$(".stripeMe tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
	$(".stripeMe tr:even").addClass("alt");
				
	//JQyery Option Tree
	$.fn.optionTree = function(tree, options) {

    options = $.extend({
        choose: 'Välj...',
        preselect: {},
        select_class: 'tree',
        leaf_class: '',
        empty_value: ''
    }, options || {});
    var cleanName = function (name) {
        return name.replace(/_*$/, '');
    };
    var removeNested = function (name) {
        $("select[name^='"+ name + "']").remove();
    };
    var setValue = function(name, value) {
        $("input[name='" + cleanName(name) + "']").val(value).change();
    }
    return this.each(function() {
        var name = $(this).attr('name') + "_";

        // remove all dynamic options of lower levels
        removeNested(name);

        if (typeof tree == "object") { // many options exists for current nesting level
            // create select element with all the options
            // and bind onchange event to recursively call this function
            var $select = $("&nbsp;HEJ&nbsp;&nbsp;<select>").attr('name',name)
            .change(function() {
                if (this.options[this.selectedIndex].value != '') {
                        $(this).optionTree(tree[this.options[this.selectedIndex].value], options);
                } else {
                       removeNested(name + '_');
                       setValue(name, options.empty_value);
                }
            });

            if ($(this).is('input'))
                $select.insertBefore(this);
            else
                $select.insertAfter(this);

            if (options.select_class)
                $select.addClass(options.select_class);

            $("<option>").html(options.choose).val('').appendTo($select);
            $.each(tree, function(k, v) {
                var o = $("<option>").html(k)
                    .attr('value', k);
                var clean = cleanName(name);
                    if (options.leaf_class && typeof v != 'object') // this option is a leaf node
                        o.addClass(options.leaf_class);

                    o.appendTo($select);
                    if (options.preselect && options.preselect[clean] && options.preselect[clean] == v) {
                        o.get(0).selected = true;
                        $select.change();
                    }
            });

        } else { // single option is selected by the user (function called via onchange event())
            setValue(name, tree);
			$("#price").html(tree + ' kr/månad');   // populate div with price
        }
    });
}
});	

