var MAX_SELECTED_MANUFACTURERS = 3;
var manufacturerSelected = [];

function paging(i){
	handyfinder.execute('',i);
	self.location.href="#top";
	return false;
}

function sortResultsBy(value){

	$("#sort").val(value);
	handyfinder.execute();
	
}

/**
 * Optionen nach bestimmten Kriterien holen
 */
function getSelectboxOptionsByGroup(group){
				
	optionsArray = [];
				
	options = $(group);
	options.each(function(i,el){
										
		optionsArray.push({
			val:$(this).val(),
			text:$(this).text(),
			dis:$(this).attr('disabled')
		});
								
	})
				
	optionsArray.sort(function(a, b){
    				
    	if(a.val>b.val){
    		return 1;
    	}else if (a.val==b.val){
        	return 0;
    	}else{
        	return -1;
    	}
	});

	return optionsArray;
}

/**
 * Hersteller hinzufuegen
 */
function addManufacturer(manufacturer){
		
	var sb = $("#hersteller");
	var listelement = $('<li id="'+manufacturer+'">').append('<span title="Hersteller löschen"></span>'+manufacturer); 
	var manufacturerList = $("#selected-manufacturers");
		
	// Hersteller in die Liste aufnehmen
	manufacturerList.append(listelement);	
		
	// gewaehlte Hersteller speichern
	manufacturerSelected.push(manufacturer);
	$("#manufacturers").val(manufacturerSelected);
		
	// gewaehlten Hersteller aus Selectbox entfernen
	sb.removeOption(manufacturer);
		
	// Selectbox zuruecksetzen
	//sb.find("option:first").attr("selected","selected");
	sb.addOption("","- weitere Hersteller -");
		
	// Loeschbutton
	$("#selected-manufacturers span").bind('click',removeManufacturer).hover(function(){
			
		$(this).css({'background-image' : 'url(/img/icons/icn_close_OR.png)'});
		
	},function(){
		
		$(this).css({'background-image' : 'url(/img/icons/icn_close_GR.png)'});
			
	});
		
	// max. 3 Hersteller sind auswählbar
	if ($("#selected-manufacturers li").length == MAX_SELECTED_MANUFACTURERS){
		$(sb).hide();
	}
}

/**
 * Hersteller löschen
 */
function removeManufacturer(){
		
	// Hersteller, der geloescht werden soll
	var manufacturer = ($(this).parent().attr("id"));
		
	manufacturerSelected = $.grep(manufacturerSelected,function(value){
		return value != manufacturer;
	});
		
	$("#manufacturers").val(manufacturerSelected);
		
	// Eintrag loeschen
	$(this).parent('li').remove();
		
	var listitems = $("#selected-manufacturers li").length;
		
	// Selectbox nur anzeigen, wenn Anzahl der gewaehlten Hersteller die Maximalanzahl nicht übersteigt 
	if (listitems < MAX_SELECTED_MANUFACTURERS){
		$("#hersteller").show();
	}
		
	if (listitems == 0){
		$("#hersteller").addOption("","- alle Hersteller -");
	}
		
	// Hersteller der Selectbox hinzufuegen
	$("#hersteller").append('<option value="'+manufacturer+'">'+manufacturer+'</option>');
		
	// Hersteller in die Gruppe der Extra-Hersteller einfügen falls er dort vorkommt
	extra_manufacturers = $("#extra_manufacturers").val().split(',');
	if ($.inArray(manufacturer,extra_manufacturers) != -1){
		$("#hersteller").append('<option class="extra" value="'+manufacturer+'">'+manufacturer+'</option>');
	}
		
	// alle Optionen (sortiert) der jeweiligen Gruppe holen
	extra = getSelectboxOptionsByGroup("#hersteller option[class^=extra]");
	normal = getSelectboxOptionsByGroup("#hersteller option[class!=extra]:not(:first)");
		
	// Selectbox leeren								
	$("#hersteller").removeOption(/./);
		
	// Extra-Optionen hinzufügen
	for(i=0;i<extra.length;i++){
		option = $('<option class="extra" value="'+extra[i].val+'">'+extra[i].text+'</option>').attr('disabled',extra[i].dis);
		$("#hersteller").append(option);
	}
				
	// Normale Optionen hinzufügen
	for(i=0;i<normal.length;i++){
		$("#hersteller").append('<option value="'+normal[i].val+'">'+normal[i].text+'</option>');
	}			
	
	$("#current_feature").val('');
	
	handyfinder.execute();
}

var handyfinder = {

	execute: function(showLoader,page){
		var url = 'ajax/ajax_handyfinder.php';
	   	if (page){
			url += '?page='+page;
	   	}
	   	if (!showLoader){
			var loader = $("#loader").empty().addClass('loading');
	   	}
	   	
		$.ajax({
			
			type: 'POST',
			url: url,
			data: $('#handyfinder').serialize(),
			success: function(html){
				$('#handyfinder_results').html(html);
			},
			complete: function(){
				if (!showLoader){
					loader.removeClass('loading');
				}
			}
			
		
		});
		
	}
	
	
}

$(document).ready(function(){
	
	if ($("#extra_manufacturers").val() == ''){
	
		// alle Extra-Hersteller aus der Selectbox holen
		options_extra_manufacturers = getSelectboxOptionsByGroup("#hersteller option[class^=extra]");
		array_extra_manufacturers = [];
		for(z=0;z<options_extra_manufacturers.length;z++){
			if (options_extra_manufacturers[z].dis == false){
				array_extra_manufacturers.push(options_extra_manufacturers[z].val);
			}
		}
	
		// ... und abspeichern
		$("#extra_manufacturers").val(array_extra_manufacturers);
	
	}
	
	// Ajax
	handyfinder.execute(true);

	// Tooltip
	$("#handyfinder label, #handyfinder img, #selected-manufacturers").tooltip({
	
		delay: 300,
		track: true,
		showURL: false,
		left: 5,
		top: 10
		
	}).mouseover(function(){
	
		$("#tooltip").append("<span></span>");
	
	}).mouseout(function(){
	
		$("#tooltip span").remove();
	
	});
	
		
	/**
	 * Benutzung Back-Button - vorher gewaehlte Farbe setzen
	 */
	if ($("#colors").val() != ''){
		var selected_colors = $("#colors").val().split(",");
		$.each(selected_colors,function(i,color){
			$("#"+color).parent().addClass("enabled");
		})
	}
	
	/**
	 * Hersteller setzen
	 */
	if ($("#manufacturers").val() != ''){
	
		$.each($("#manufacturers").val().split(','),function(i,manufacturer){
			addManufacturer(manufacturer);
		})
	
	}
	
	/**
	 * Handytyp setzen
	 */
	if ($("#handytypes").val() != ''){
		
		var selected_types = $("#handytypes").val().split(",");
		
		$.each(selected_types,function(i,type){
			var img = $("#"+type);	
			img.attr({"src": img.attr("src").replace("GR","OR")});
			img.parent().addClass("selected");
		})
	}
		
	/** 
	 * Checkboxen
	 */
	$(":checkbox").click(function(){
		
		var this_id = $(this).attr("id");
		var value = $("#"+this_id).attr("checked") ? this_id : '';
		$("#current_feature").val(value);
		handyfinder.execute();
	
	})
	
	/**
	 * Drop-Down
	 */
	$("select").change(function(){
		
		var this_id = $(this).attr("id");
		var this_value = $(this).val();
		
		if (this_id == "hersteller"){
			
			addManufacturer(this_value);
			
		}
				
		var value = this_id == "kamera" ? "kamera_"+this_value : this_value;
			
		$("#current_feature").val(value);
		handyfinder.execute();
		
	})	
	
	/**
	 * Handytypen
	 */
	 var handytypes = [];
	$("#handytype-selector img").click(function(){
		
		var this_src = $(this).attr("src");
		var this_id = $(this).attr("id");
		
		// Grafik aktiviert
		if (this_src.indexOf("OR") > 0){
			
			// wenn Markierung gesetzt
			if ($(this).parent().hasClass("selected")){
				
				// Grafik austauschen
				$(this).attr({"src": this_src.replace("OR","GR")});
				
				// Markierung loeschen
				$(this).parent().removeClass("selected");
								
				// und Handytyp entfernen
				handytypes = $.grep(handytypes,function(value){
					return value != this_id;
				});
				
				$("#current_feature").val('');
				
			}else{
				// Markierung setzen
				$(this).parent().addClass("selected")
				
				// Handytyp hinzufügen
				handytypes.push(this_id)
				$("#current_feature").val(this_id);
			
			}
			
		}else{
			
			// Markierung löschen
			//if ($(this).parent().hasClass("selected")){
				//$(this).parent().removeClass("selected")
			//}else{
				
				$(this).parent().addClass("selected")
				
				// Grafik austauschen
				$(this).attr({"src": this_src.replace("GR","OR")});
				
				// Handytyp hinzufügen
				handytypes.push(this_id)
				$("#current_feature").val(this_id);
				//alert("test");
			//}
			//alert('test');
		}
		
		// Handytyp speichern
		$("#handytypes").val(handytypes);
		
		handyfinder.execute();
		
	}).mouseover(function(){
		var this_src = $(this).attr("src");
		$(this).attr({"src": this_src.replace("GR","OR")});
	
	}).mouseout(function(){
		var this_src = $(this).attr("src");
		if (!$(this).parent().hasClass("selected")){
			$(this).attr({"src": this_src.replace("OR","GR")});
		}
	})
		
	/**
	 * Farbwaehler
	 */
	
	// Array zur Speicherung der Farben
	var colors = []; 
	
	$(":button").click(function(){
		
		$(this).blur();
		
		var button = $(this).parent();
		var this_id = $(this).attr("id");
				
		if (button.hasClass("enabled")){
			
			// einzelne Farbe deaktivieren...
			button.removeClass("enabled");
			
			// und Farbe entfernen
			colors = $.grep(colors,function(value){
				return value != this_id;
			});
			
			$("#current_feature").val('');
			
		}else{
			
			// einzelne Farbe aktivieren...
			button.addClass("enabled");
			
			// und Farbe hinzufügen
			colors.push(this_id);
			$("#current_feature").val(this_id);
		}
		
		// Farben speichern
		$("#colors").val(colors);
				
		handyfinder.execute();
		
	})
});