$(document).ready(function() {

	
	/*----------------------------------------
	// Funktion öffnet ein gesondertes Fenster (Layer) mit der Handyauswahl
	----------------------------------------*/
	$('.open_handyauswahl').fancybox();
	$('.open_handyauswahl').livequery(function() {
		$(this).fancybox({
			'autoDimensions'		: false,
			'width'					: 750,
			'height'				: 550,
			'hideOnContentClick'	: false,
			'hideOnOverlayClick' 	: false,
			'centerOnScroll'		: false
		});
		return false;
	});
	
	
	
	/*----------------------------------------
	// Funktion filter per Texteingabefeld mögliche Filteroptionen anhand der Eingabe
	----------------------------------------*/
	
	$('#reduce_selections').livequery(function() {
		$(this).keyup(function () {
			var filter = $(this).val(), count = 0;
			var t = this;
			var ausklapp = new Array();
			var h_count = 0;
			if (this.value != this.lastValue) {
				if (this.timer) {
					clearTimeout(this.timer);
				}
				$("#prealoader").html('<img src="http://iimpic.de/all/preload16.gif"/>');
				this.timer = setTimeout(function () {
				    $('#ha_selections:first li').each(function () {
				        if ($(this).text().search(new RegExp(filter, "i")) < 0) {
				        	$(this).addClass('hide');
				        } else {
				        	if ($(this).attr('rel') == 'h') {
				        		ausklapp[h_count] = $(this).attr('id');
				        		h_count++;
				        	}
				        	$(this).removeClass('hide');
				        	count++;
				        }
				    });
				    $("#prealoader").html('');
				  
				    var h_anzahl = ausklapp.length;
				    if (h_anzahl < 4) {
				    	for(i=0; i<h_anzahl; i++){
				    		$('#'+ausklapp[i]).find('.ausklappen').prev().replaceWith("<span>- </span>");
				    		$('#'+ausklapp[i]).find('.ausklappen').next().show();
				    	}
				    } else {
				    	$('span.ausklappen').next().hide();
				    	$('span.ausklappen').prev().replaceWith("<span>+ </span>");
				    }
				    
				}, 400);
				this.lastValue = this.value;
			}
		});
	});

	
	$(function() {
	   	/* Alle Span mit der Klasse .ausklappen verstecken*/
	    $('span.ausklappen').next().hide();
		/* <span>+ </span> vor dem Ausgewälten Element einfügen*/
		$("span.ausklappen").before("<span>+ </span>");
		/* Den Mauszeiger ändern*/
		$("span.ausklappen").css("cursor", "pointer");
		
		$(".ha_produkte_form").each(function(){
			$('#p_'+$(this).attr('id').replace('p_form_', '')).hide();
		});
		
		$("span.ausklappen").click(function() {
			/*Ebene nach unten, oder oben gleiten lassen */
			$(this).next().slideToggle("fast");
			/*  Wenn das vorheirge elment + enthält, dann durch - ersetzen und umgekehrt*/
			if ($(this).prev(this).text() == "+ " )
				$(this).prev(this).replaceWith("<span>- </span>");
			else if ($(this).prev(this).text() == "- " )
				$(this).prev(this).replaceWith("<span>+ </span>");
		});
		
	});	
	
	if(typeof($(document).cluetip) == "function") {
		//Handybox
		$('.ha_info').cluetip({
			cluetipClass: 'handyauswahl',
			dropShadow: true, 
	    	dropShadowSteps: 6,	
	    	width: 300,
			attribute: 'href',
			activation: 'click',
			cluezIndex: 105,
			arrows: true,
			positionBy: 'mouse',
			closePosition: 'title',
			closeText: '<img src="http://iimpic.de/ih/profifinder_delete_grey.gif" alt="schliessen" title="schliessen"/>',
			showTitle: true,
		  	sticky: true,
		  	ajaxCache: true,
	 		fx: {             
	            open: 'show',
	            openSpeed:  ''
	    	}  	
	  	});
	}

	
	$('.ha_add_produkt').livequery(function() {
		$(this).click(function () {
			$('#cluetip').hide();			
			var anzahl_produkte = $('.ha_produkte_form').size();
			var id = $(this).attr('rel');
			if (anzahl_produkte == 4) {
				alert('Sie können max. 3 Handys dem Vergleich hinzufügen!');
			} else {
				$.ajax({
					type: "GET",
					url: "/lib/ajax/handyauswahl.php",
					data: "id="+id,
					success: function(msg) {
						$('#ha_auswahl_content').append(msg);
						$('#p_'+id).hide();
			  		}
				});	
			}
			
			
		});
	});
	
	$('.ha_del_produkt').livequery(function() {
		$(this).click(function () {
			$('#p_form_'+$(this).attr('rel')).remove();
			$('#p_'+$(this).attr('rel')).show();
		});
	});	
	
});

function ha_submit_check() {
	if ($('.ha_produkte_form').size() < 1) {
		alert('Sie haben noch nichts angewählt! Bitte wählen Sie zuerst ein Handy aus.'); 
		return false;
	} else {
		$('.btn_forward').val('Bitte warten');
		$('.btn_forward').attr("disabled",true);
		return true;
	}
}