// - - - - - - - - - - JavaScript Document - - - - - - - - - - - - 
//
// Title : Js file	
// Author : SitePoint Enthusiast
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

//hierarchySelectList function

function hierarchySelectList(hierarchyChk,val){
$dd = $("select#cms_module_preisliste_preise_src");
if ($dd.length > 0) { // make sure we found the select we were looking for

// save the selected value
var selectedVal = $dd.val();

// get the options and loop through them
var $options = $('option', $dd);
var arrVals = [];
$options.each(function(){
//get the hierarchy
text = $(this).text();
textpos = (text.indexOf(' - ')-1);
hierarchy = (text.substring(0,textpos));
// push each option value and text into an array
arrVals.push({
val: $(this).val(),
text: $(this).text(),
hierarchy: hierarchy
});
});

// sort the array by the value (change val to text to sort by text instead)

newOptions = "";
// loop through the sorted array and set the text/values to the options
// work out what parent and what child items should be showing
for (var i = 0, l = arrVals.length; i < l; i++) {
//$($options[i]).val(arrVals[i].val).text(arrVals[i].text);

checkHier = arrVals[i].hierarchy.length;

if (arrVals[i].hierarchy.length < 2){
newOptions += '<option value="'+arrVals[i].val+'" style="color:#024b67;font-weight:900;">'+arrVals[i].text+'</option>';

} else if ((hierarchyChk.length == 2 || hierarchyChk.length == 4) && (arrVals[i].hierarchy.length == 3 && arrVals[i].hierarchy.substring(0,2) == hierarchyChk.substring(0,2))){
newOptions += '<option value="'+arrVals[i].val+'" style="color:#00adef;font-weight:900;">'+arrVals[i].text+'</option>';

} else if ((hierarchyChk.length == 4 || hierarchyChk.length == 6) && (arrVals[i].hierarchy.length == 5 && arrVals[i].hierarchy.substring(0,4) == hierarchyChk.substring(0,4))){
newOptions += '<option value="'+arrVals[i].val+'" style="color:#a2ddf3;font-weight:900;">'+arrVals[i].text+'</option>';

} else if (hierarchyChk.length == 6 && (arrVals[i].hierarchy.length == 3 && arrVals[i].hierarchy.substring(0,2) == hierarchyChk.substring(0,2))){
newOptions += '<option value="'+arrVals[i].val+'" style="color:#00adef;font-weight:900;">'+arrVals[i].text+'</option>';

} else {
//newOptions += '<option value="'+arrVals[i].val+'" style="display:none;visible:none;color:#ffffff;">'+arrVals[i].text+'</option>';
}

}
//empty drop down and add relavant options
$("select#cms_module_preisliste_preise").empty();
$("select#cms_module_preisliste_preise").append(newOptions);

// set the selected value back
$("select#cms_module_preisliste_preise").val(val);//(selectedVal);
}
}

$(document).ready(function() {

$("select[name*='cms_module_preisliste_preise']").attr('id','cms_module_preisliste_preise').clone(true).insertAfter("select[name*='cms_module_preisliste_preise']");
$("select[name*='cms_module_preisliste_preise']:last").attr({name:"cms_module_preisliste_preise_src", id:"cms_module_preisliste_preise_src"}).css("display","none");

//cloneSelect = $("select[name*='cms_module_preisliste_preise']").clone(true);
//cloneSelect.insertAfter("select[name*='cms_module_preisliste_preise']");
//$("select[name*='cms_module_preisliste_preise']:last").attr('name','cms_module_preisliste_preise_cpy').css("display","none");

hierarchySelectList(0);


$("select#cms_module_preisliste_preise").change(function () {
val = $(this).val(); // value of select
text = $("option[value*='"+val+"']").text();
textpos = (text.indexOf(' - '));
hierarchyChk = (text.substring(0,textpos));
hierarchySelectList(hierarchyChk,val);
});

});
