/**
 * BOM Simulatorzeichen: üöä€ƒ█±áÌÜ○©a«♣◙☼±À╚Ä☻├☻Õ╚Ä§█
 * Header with CVS/Subversion Data of the file
 * 
 * $Id$
 * $LastChangedDate$
 * $Author$
 * $Revision$
 */
 
$(document).ready(function() {
	// Auto-hide all the selection areas on startup
	$('#selection_color').hide();
	$('#selection_material').hide();
	$('#selection_capacity').hide();
	$('#selection_price').hide();
	$('#selection_attributes').hide();
	$('#selection_genres').hide();
	
	$('#color-hide').hide();
	$('#material-hide').hide();
	$('#capacity-hide').hide();
	$('#price-hide').hide();
	$('#attributes-hide').hide();
	$('#genres-hide').hide();
	
	// border onmouseover in color selection (works with ie6)
	$('.selection_color_show img').mouseover(function() {
	    $(this).css('border','2px solid #000000');
    });
	$('.selection_color_show img').mouseout(function() {
	    $(this).css('border','2px solid #FFFFFF');
    });
	
	$('#searchfield').keyup(function() {
		retrieve_typeResults();
	})
					 .click(function() {
		retrieve_typeResults();
	})
					 .blur(function() {
		setTimeout("$('.autocomplete').remove()", 200);			 
	});
	
	// get chosen elements from form-submit in filter
	select_formData();
	
});


function hideThem(id) {
    $('#selection_'+id).hide();
    $('a#'+id+'-hide').hide();
    $('a#'+id+'-show').show();    
}

function showHide_selection(id, show_or_hide) {
    // hide them all
    switch(id) {
        case 'color':
            hideThem('material');
            hideThem('capacity');       
            hideThem('price'); 
            hideThem('attributes');
            hideThem('genres');
            break;   
        case 'material':
            hideThem('color');
            hideThem('capacity');       
            hideThem('price'); 
            hideThem('attributes');
            hideThem('genres');
            break;
        case 'capacity':
            hideThem('material');
            hideThem('color');       
            hideThem('price'); 
            hideThem('attributes');
            hideThem('genres');
            break;
        case 'price':
            hideThem('material');
            hideThem('capacity');       
            hideThem('color'); 
            hideThem('attributes');
            hideThem('genres');
            break;         
        case 'attributes':
            hideThem('material');
            hideThem('capacity');       
            hideThem('price'); 
            hideThem('color');
            hideThem('genres');
            break; 
        case 'genres':
            hideThem('material');
            hideThem('capacity');       
            hideThem('price'); 
            hideThem('color');
            hideThem('attributes');
            break;                                                 
    }
    
    // Show the Selection for id
    if(show_or_hide == 'show') {
        $('#selection_'+id).animate({
            height:'show'
        }, '800');
        $('#'+id+'-hide b').css('color','#000000');
        $('#'+id+'-show').hide();
        $('#'+id+'-hide').show();           
    }
    
    if(show_or_hide == 'hide') {
    // Hide the Selection for id
        $('#selection_'+id).animate({
            height:'hide'
        }, '800');
        $('#'+id+'-show').show();
        $('#'+id+'-hide').hide();
    }
}


function produktfinder_setItem(item, dest, id, name) {
    // check if item is already selected
    var exists = 0;
    var felder = $('input.items_'+ dest);
   
    for(var i = 0; i < felder.length; i++) {
        var color = $(felder[i]).val();
		console.log(color);
        if(color == id) {
            exists = 1;
        }
    }    
    if(exists == 0) {
        if(dest == 'material' || dest == 'genres') {
            $('#selected_'+ dest).append('<a onClick="$(this).next().remove(); $(this).remove();" style="cursor:pointer;" alt="" title="">'+ name +'</a><input type="hidden" class="items_'+ dest +'" name="'+ dest +'[]" value="'+ id +'" /> ');            
        } else {
            $('#selected_'+ dest).append('<img src="'+ item +'" onClick=\"$(this).next().remove(); $(this).remove();" class="'+ dest +'" alt="" title="" /><input type="hidden" class="items_'+ dest +'" name="'+ dest +'[]" value="'+ id +'" /> ');
            
            // border onMouseover
            $('#selected_'+ dest +' img.color').mouseover(function() {
                $(this).css('border','2px solid #C60203');
                $(this).attr({
                    'alt':'Entfernen', 
                    'title':'Farbauswahl entfernen'
                });
            });
            $('#selected_'+ dest +' img.color').mouseout(function() {
                $(this).css('border','2px solid #FFFFFF');
            });            
        }
        if(dest == 'attributes') {
             $.each($('.attributes'), function (i, n) {
                var splitStr = $(this).attr('src').split("/");
                var icon = splitStr[(splitStr.length-1)].substr(0, 2);
                $(this).attr('src', 'images/icons/product_properties/'+icon+'_small.gif');
             });                            
        }
        $('#selection_'+ dest).fadeOut('200');
	    $('#'+ dest +'-show').show();
	    $('#'+ dest +'-hide').hide();
    }
}


function produktfinder_setItem2(id, item, lang, lang2) {
    $('#selected_'+id).html('<a onClick="$(this).next().remove(); $(this).remove();" style="cursor:pointer;" alt="" title="">'+ lang +' '+ item +' '+ lang2 +'</a><input type="hidden" class="items_'+ id +'" name="'+ id +'" value="'+ item +'" />');
    $('#selection_'+id).fadeOut('200');
    $('#'+ id +'-show').show();
	$('#'+ id +'-hide').hide(); 
}

function produktfinder_setItem_genre(id, item) {
    // check if item is already selected
    var exists = 0;
    var felder = $('input.items_'+ id);
           
    for(var i = 0; i < felder.length; i++) {
        var genre = $(felder[i]).val();
        if(genre == item) {
            exists = 1;
        }
    }
    if(exists == 0) {    
        $('#selected_'+id).append('<a onClick="$(this).next().remove(); $(this).remove();" style="cursor:pointer;" alt="" title="">'+ item +'</a><input type="hidden" class="items_'+ id +'" name="selected_'+ id +'" value="'+ item +'" /> ');
        $('#selection_'+id).fadeOut('200');
        $('#'+ id +'-show').show();
	    $('#'+ id +'-hide').hide(); 
    }	    
}


function retrieve_searchResults() {
    var search_string = $('#searchfield').val();
    var options = new Array('color', 'material', 'capacity', 'price', 'attributes','genres');
    var colorStr = "";
    var materialStr = "";
    var capacityStr = "";
    var priceStr = "";
    var attributesStr = "";
    var genresStr = "";
    
    for(var j = 0; j <= options.length; j++) {
        $.each($('input.items_'+ options[j]), function (i, n) {
            if(options[j] == 'color') {
                colorStr += '|'+ $(n).val();
            }
            if(options[j] == 'material') {
                materialStr += '|'+ $(n).val();
            }
            if(options[j] == 'capacity') {
                capacityStr = $(n).val();
            }
            if(options[j] == 'price') {
                priceStr = $(n).val();
            }                          
            if(options[j] == 'attributes') {
                attributesStr += '|'+ $(n).val();
            }
            if(options[j] == 'genres') {
                genresStr += '|'+ $(n).val();
            }                                      
        });
    }
    colorStr = colorStr.substr(1);
    materialStr = materialStr.substr(1);
    attributesStr = attributesStr.substr(1);
    genresStr = genresStr.substr(1);
    
    //if(search_string.length >= 3) {
    
        $('div#products_listing').html('<center><img src="admin/images/loading.gif" /></center>');
        
        $.get("produktfinder_results.php", { inputStr: search_string, colorStr: colorStr, materialStr: materialStr, capacityStr: capacityStr, priceStr: priceStr, attributesStr: attributesStr, genresStr:genresStr  },
            function(data){
                $('div#products_listing').html(data);
            }
        );
        
    //}        
}

function retrieve_typeResults() {
    var search_string = $('#searchfield').val();
    if(search_string.length <= 2) {
        $('div.autocomplete').remove('');
    }
    if(search_string.length > 2) {
        $.get("produktfinder_results.php", { action:"suggest", inputStr: search_string  },
            function(data) {
                $('div.autocomplete').remove();
               
                //var search_result = data.substr(1);
                var search_result = data.split('|');
                
                if(search_result[0] != "") {

                    $('div.search').append('<div class="autocomplete">');
                     
                    for(var i = 0; i < search_result.length; i++) {
                        $('div.autocomplete').append('<div class="result" onclick="$(\'#searchfield\').val($(this).text()); $(\'div.autocomplete\').remove();">'+ search_result[i] +'</div>');
                    }
                    
                    $('div.result').mouseover(function() {
	                    $(this).css({
	                        'background-color':'#DDDDDD',
	                        'cursor':'pointer'
                        });
                    });
                    
                    $('div.result').mouseout(function() {
                        $(this).css({
	                        'background-color':'#FFFFFF',
	                        'cursor':'default'            
                        });
                                
	                }); 
                                   
                }
            }
        );
    }    
}

function reset_selections() {
    $('span.selection').html('');
    $('input#searchfield').val('');
}

function select_formData() {
	// colors
	var hid_colors = $('input.hid_colors');
	if ($(hid_colors).length > 0) {
		hid_colors_val = $(hid_colors).val();
		var exp = hid_colors_val.split(',');
		console.log(exp);
		for(index in exp) {
			var color_id = exp[index];
			$('img.colorId_'+ color_id).click();
		}
	}
	
	// materials
	var hid_materials = $('input.hid_materials');
	if ($(hid_materials).length > 0) {
		hid_materials_val = $(hid_materials).val();
		var exp = hid_materials_val.split(',');
		for(index in exp) {
			var materials_id = exp[index];
			$('a.materialId_'+ materials_id).click();
		}
	}

	// volume
	var hid_volume = $('input.hid_volume');
	if ($(hid_volume).length > 0) {
		volumeId = $(hid_volume).val();
		$('a.volumeId_'+ volumeId).click();
	}
	
	// price
	var hid_price = $('input.hid_price');
	if ($(hid_price).length > 0) {
		priceId = $(hid_price).val();
		$('a.priceId_'+ priceId).click();
	}

	// properties
	var hid_properties = $('input.hid_properties');
	if ($(hid_properties).length > 0) {
		hid_properties_val = $(hid_properties).val();
		var exp = hid_properties_val.split(',');
		for(index in exp) {
			var properties_id = exp[index];
			if(properties_id < 10) {
				properties_id = "0"+ properties_id;
			}
			$('img.iconId_'+ properties_id).click();
		}
	}
	
	// genres
	var hid_genres = $('input.hid_genres');
	if ($(hid_genres).length > 0) {
		hid_genres_val = $(hid_genres).val();
		var exp = hid_genres_val.split(',');
		console.log(exp);
		for(index in exp) {
			var genre_id = exp[index];
			$('a.genreId_'+ genre_id).click();
		}
	}	
}