attr = ""; //"Sortier-Index" (global);

//Vergleichsfunktion für sort aufsteigent:
function cmp2DArrAttr (a, b) {
	return (a)[attr] > (b)[attr];
}

//Vergleichsfunktion für sort absteigent:
function cmp2DArrAttr2 (a, b) {
	return (a)[attr] < (b)[attr];
}

function generate_dropdown(family_id,  options_id) {
	if(options_id != -1) {
		products_attribute = window['products_options_array_'+family_id];
		
		var attr = "sort_order";
		products_attribute.sort(cmp2DArrAttr);
		var current_options_id = options_id;
		var tmp_options_id = options_id;
		
		var select_html = "";
		while (current_options_id != -1) {
			for (i in products_attribute)
			{
				if (products_attribute[i]["options_id"] == current_options_id) {
					var options_html = "";
					for (j in products_attribute)
					{
						if (products_attribute[j]["main_options_id"] == products_attribute[i]["main_options_id"]) {
							if (products_attribute[j]["options_id"] == current_options_id) selected = " selected"; else selected = "";
							options_html += '<option value="' + products_attribute[j]["options_id"] + '"' + selected + '>' + products_attribute[j]["options_name"] + '</option>';
						}
					}
					
					for (k in products_attribute)
					{
						if (products_attribute[k]["options_id"] == products_attribute[i]["main_options_id"]) {
							select_html = '<b>'+products_attribute[k]["attribute_name"]+': </b><br /><select style="width: 100%;" id="options_selector_oID_'+k+'" onChange="javascript: generate_dropdown('+family_id+', this.options[this.selectedIndex].value);">'+options_html+'</select><br />' + select_html;
							tmp_options_id = products_attribute[k]["options_id"];
						}
					}
				}
			}
			if (tmp_options_id == current_options_id) {
				current_options_id = -1;
			} else { current_options_id = tmp_options_id; }
		}
		
		for (l in products_attribute)
		{
			if ((products_attribute[l]["options_id"] == options_id) && (products_attribute[l]["attribute_name"] != "")) {
				document.getElementById('options_delivery_'+family_id).innerHTML = "";
				document.getElementById('options_price_'+family_id).innerHTML = window['products_lowest_price_'+family_id];
				document.getElementById('options_image_'+family_id).innerHTML = window['products_main_image_'+family_id];
				document.getElementById('options_form_'+family_id).innerHTML = "";
				options_html = "";
				for (m in products_attribute)
				{
					if (products_attribute[m]["main_options_id"] == products_attribute[l]["options_id"]) {
						if (products_attribute[m]["options_id"] == current_options_id) selected = " selected"; else selected = "";
						options_html += '<option value="' + products_attribute[m]["options_id"] + '"' + selected + '>' + products_attribute[m]["options_name"] + '</option>';
					}
				}
				select_html += '<b>'+products_attribute[l]["attribute_name"]+': </b><br /><select id="options_selector_oID_'+l+'" onChange="javascript: generate_dropdown('+family_id+', this.options[this.selectedIndex].value);"><option value="-1">Bitte w&auml;hlen</option>'+options_html+'</select><br />';
			} else if ((products_attribute[l]["options_id"] == options_id) && (products_attribute[l]["products_id"] != "")) {
				document.getElementById('options_delivery_'+family_id).innerHTML = products_attribute[l]["products_delivery"];
				document.getElementById('options_price_'+family_id).innerHTML = products_attribute[l]["products_price"];
				document.getElementById('options_image_'+family_id).innerHTML = '<a href="javascript:popupWindow(\'popup_fimage.php?fID='+products_attribute[l]["products_id"]+'&image=0\')"><img src="images/'+products_attribute[l]["products_image"]+'" border="0" width="100" height="80"></a>';
				document.getElementById('options_form_'+family_id).innerHTML = '<input name="quantity" value="1" size="2" maxlength="2" type="text"><input name="cart_quantity" value="1" maxlength="6" size="4" type="hidden"><input name="products_id" value="'+products_attribute[l]["products_id"]+'" type="hidden"><br /><input src="templates/Original/images/buttons/german/button_buy_now.gif" alt="In den Warenkorb" title=" In den Warenkorb " align="absmiddle" border="0" type="image"><br /><font color="#888888">Art.Nr.:&nbsp;'+products_attribute[l]["products_id"]+'</font>';
			}
		}
		document.getElementById('options_dropdown_'+family_id).innerHTML = '<br>'+select_html;
	} else {
		document.getElementById('options_delivery_'+family_id).innerHTML = "";
		document.getElementById('options_price_'+family_id).innerHTML = window['products_lowest_price_'+family_id];
		document.getElementById('options_image_'+family_id).innerHTML = window['products_main_image_'+family_id];
		document.getElementById('options_form_'+family_id).innerHTML = "";
	}
}

function generate_simulator(family_id,  options_id) {
	if(options_id != -1) {
		products_attribute = window['products_options_array_'+family_id];
		
		var attr = "sort_order";
		products_attribute.sort(cmp2DArrAttr);
		var current_options_id = options_id;
		var tmp_options_id = options_id;
		
		var select_html = "";
		while (current_options_id != -1) {
			for (i in products_attribute)
			{
				if (products_attribute[i]["options_id"] == current_options_id) {
					var options_html = "";
					for (j in products_attribute)
					{
						if (products_attribute[j]["main_options_id"] == products_attribute[i]["main_options_id"]) {
							if (products_attribute[j]["options_id"] == current_options_id) selected = " selected"; else selected = "";
							options_html += '<option value="' + products_attribute[j]["options_id"] + '"' + selected + '>' + products_attribute[j]["options_name"] + '</option>';
						}
					}
					
					for (k in products_attribute)
					{
						if (products_attribute[k]["options_id"] == products_attribute[i]["main_options_id"]) {
							select_html = '<b>'+products_attribute[k]["attribute_name"]+': </b><br /><select id="options_selector_oID_'+k+'" onChange="javascript: generate_simulator('+family_id+', this.options[this.selectedIndex].value);"><option value="-1">Bitte w&auml;hlen</option>'+options_html+'</select><br />' + select_html;
							tmp_options_id = products_attribute[k]["options_id"];
						}
					}
				}
			}
			if (tmp_options_id == current_options_id) {
				current_options_id = -1;
			} else { current_options_id = tmp_options_id; }
		}
		
		for (l in products_attribute)
		{
			if ((products_attribute[l]["options_id"] == options_id) && (products_attribute[l]["attribute_name"] != "")) {
				options_html = "";
				for (m in products_attribute)
				{
					if (products_attribute[m]["main_options_id"] == products_attribute[l]["options_id"]) {
						if (products_attribute[m]["options_id"] == current_options_id) selected = " selected"; else selected = "";
						options_html += '<option value="' + products_attribute[m]["options_id"] + '"' + selected + '>' + products_attribute[m]["options_name"] + '</option>';
					}
				}
				select_html += '<b>'+products_attribute[l]["attribute_name"]+': </b><br /><select id="options_selector_oID_'+l+'" onChange="javascript: generate_simulator('+family_id+', this.options[this.selectedIndex].value);"><option value="-1">Bitte w&auml;hlen</option>'+options_html+'</select><br />';
			}
		}
		document.getElementById('options_dropdown_'+family_id).innerHTML = select_html;
	}
}


function unserialize(input)
    {
        var result = unserialize_(input);
        return result[0];
    }


    /**
    * Function which performs the actual unserializing
    *
    * @param string input Input to parse
    */
function unserialize_(input)
{
        var length = 0;
        
        switch (input.charAt(0)) {
            /**
            * Array
            */
            case 'a':
                length = unserialize_GetLength(input);
                input  = input.substr(String(length).length + 4);

                var arr   = new Array();
                var key   = null;
                var value = null;

                for (var i=0; i<length; ++i) {
                    key   = unserialize_(input);
                    input = key[1];

                    value = unserialize_(input);
                    input = value[1];

                    arr[key[0]] = value[0];
                }

                input = input.substr(1);
                return [arr, input];
                break;
            
            /**
            * Objects
            */
            case 'O':
                length = unserialize_GetLength(input);
                var classname = String(input.substr(String(length).length + 4, length));
                
                input  = input.substr(String(length).length + 6 + length);
                var numProperties = Number(input.substring(0, input.indexOf(':')))
                input = input.substr(String(numProperties).length + 2);

                var obj      = new Object();
                var property = null;
                var value    = null;

                for (var i=0; i<numProperties; ++i) {
                    key   = unserialize_(input);
                    input = key[1];
                    
                    // Handle private/protected
                    key[0] = key[0].replace(new RegExp('^\x00' + classname + '\x00'), '');
                    key[0] = key[0].replace(new RegExp('^\x00\\*\x00'), '');

                    value = unserialize_(input);
                    input = value[1];

                    obj[key[0]] = value[0];
                }

                input = input.substr(1);
                return [obj, input];
                break;

            /**
            * Strings
            */
            case 's':
                length = unserialize_GetLength(input);
                return [String(input.substr(String(length).length + 4, length)), input.substr(String(length).length + 6 + length)];
                break;

            /**
            * Integers and doubles
            */
            case 'i':
            case 'd':
                var num = Number(input.substring(2, input.indexOf(';')));
                return [num, input.substr(String(num).length + 3)];
                break;
            
            /**
            * Booleans
            */
            case 'b':
                var bool = (input.substr(2, 1) == 1);
                return [bool, input.substr(4)];
                break;
            
            /**
            * Null
            */
            case 'N':
                return [null, input.substr(2)];
                break;

            /**
            * Unsupported
            */
            case 'o':
            case 'r':
            case 'C':
            case 'R':
            case 'U':
                //alert('Error: Unsupported PHP data type found!');

            /**
            * Error
            */
            default:
                return [null, null];
                break;
        }
    }
    

    /**
    * Returns length of strings/arrays etc
    *
    * @param string input Input to parse
    */
    function unserialize_GetLength(input)
    {
        input = input.substring(2);
        var length = Number(input.substr(0, input.indexOf(':')));
        return length;
    }

