var proAZ;
var productIndex;
var productIndexShow;
var ppProductsCollums;

$(function(){

	$_search = {
		frm : $('#frmSearch'),
		txt : $('#search-phrase'),
		label : $('#search-phrase-label'),	
		isfilled: function() {
						var txt = this.txt;
						txt.val($.trim(txt.val()));
						return (txt.val().length == 0) ? false : true;
					},
		hits : null
	}
	
	$_search.label.live('click', function() {
		$(this).fadeOut(200);
		$_search.txt.focus();		
	});

	$_search.txt.live('focus', function() {
		$_search.txt.val('');
	});

	$_search.txt.live('focusout', function() {
		if(!$_search.isfilled()) {
			$_search.label.fadeIn(200);
		}
	});

	$_search.frm.live('submit', function() {
		
		if(!$_search.isfilled()) {
			return false;			
		}

       var emospro = {};
	   emospro.search = [[$_search.txt.val(), '1']];
	   window.emosPropertiesEvent(emospro);
	   
	});

    $_search.txt.autocomplete({
    	source: function(request, response) {
			$.ajax({
				url : "/suche/search_json.php",
				dataType: "json",
				data : request,
				success : function(data) {
					$_search.hits = data.length;
					response(data);
				}
			});
		},
		minLength: 2,
		select: function(event, ui) { 
			$(this).val(ui.item.value)
			$_search.frm.submit();
		}
	});


	$.each($('#nav #themen ul li a'), function(i,v) {

		var sHref = $(this).attr('href');
		sHref = sHref.substr(sHref.lastIndexOf("/"));
		var sLink = top.location.href.substr(top.location.href.lastIndexOf("/"));

		var nHref = sHref.indexOf("?");
		if (nHref !== -1) {
			sHref = sHref.substr(0,nHref);
		}
		var nLink = sLink.indexOf("?");
		if (nLink !== -1) {
			sLink = sLink.substr(0,nLink);			
		}

		if(sHref == sLink) {
			$(this).parent().addClass('selected');
		}
		
	});	
	
	
	
	// Erweitere Navigation für die Produkte von A-Z	
	
	proAZ = {
			
		navi_link : $('#themen ul li.last'),
		navi_expand : {
			selector : $('#navi_expand'),
			css	: {
				'width' : '760px',
				'height' : '445px',
				'visibility' : 'visible',
				'opacity' : '0.96'
			}
		},
		timer : null,
		delay : 600,


		menu_open : function() {
			var that = this;

			if ($(that.navi_expand.selector).is(":visible") == false) {
				$(that.navi_expand.selector).hide().css(that.navi_expand.css).fadeIn();
			}

			$(document).mousemove(function(e) {
				var offset = $(that.navi_expand.selector).offset();
							
				if (e.pageX < (offset.left - 184) || e.pageX > (offset.left + $(that.navi_expand.selector).width()) || e.pageY < offset.top || e.pageY > (offset.top + $(that.navi_expand.selector).height())) {
					that.menu_close();
				}
			});
			
			$(that.navi_expand.selector, "#navi_expand_close").live("click", function() {
				that.menu_close();
			});							
		},
		
		menu_close : function() {
			var that = this;
			$(that.navi_expand.selector).fadeOut();
		},

		init : function() {
			var that = this;
			
			$(that.navi_expand.selector).hide();
			
			// css um Last Item Position erweitern
			$.extend(that.navi_expand.css, that.navi_link.offset(), { 'margin-top' : '-388px', 'margin-left' : '184px' } );
			
			$(that.navi_expand.selector,'.produktliste').prepend('<a href="javascript:void(0);" id="navi_expand_close" title="Schlie&szlig;en">&nbsp;</a>');
			

			$(that.navi_link).live('click', function (event) {
				clearTimeout(that.timer);
				that.menu_open();
			});

			$(that.navi_link).live('mouseover', function () {
				that.timer = setTimeout('proAZ.menu_open()', that.delay);
			});
			
			$(that.navi_link).live('mouseout', function () {
				clearTimeout(that.timer);
			});
		}
				
	};
	proAZ.init();
	

	// Produkt-Index
	productIndexShow = function (opt, selector) {	
	
		productIndex = $(selector);
	
		if (opt == 'open') {
			productIndex.css({
				'left' : $(window).width() / 2 - productIndex.width() / 2,
				'margin-top' : -productIndex.height() - 20,
				opacity : 0.5
			});
			productIndex.show();
			productIndex.animate({ opacity: 0.95, 'margin-top': 0 }, 500);
		} else if (opt == 'close') {
			productIndex.fadeOut(300);
		}

		productIndex.live("mouseleave", function() {
			productIndexShow('close', selector);
		});

	}	
	

	// Produkte-Liste in Spalten aufteilen

	ppProductsCollums = function(columns, selector) {

		var ppProducts = $(selector);

		var colNext = Math.ceil($('li', ppProducts).size() / columns);
		var output = "";
		var suspend = false;
		
		$('li', ppProducts).each(function(index) {

			output += "<li>" + $(this).html() + "</li>\n";

			if (((index + 1) % colNext) == 0 || suspend) {
				if ($("a", this).size() || suspend) {	
					output += "</ul>\n<ul>\n";
					suspend = false;
				} else {
					suspend = true;
				}
			}

		});
		
		$("ul", ppProducts).parent(0).html("<ul>\n" + output + "</ul>\n");
	
	};
	
	
	var setProducts = function(data) {
		$.each(data, function(sLetter, sLink) {
			$("#product-az ul").append("<li><b>" + sLetter + "</b></li>");
			$.each(sLink, function(index, linkInfo) {
				$("#product-az ul").append("<li><a rel=\"nofollow\" href=\"/" + linkInfo[1] + "\">" + linkInfo[0] + "</a></li>");
			});
		});
		
		ppProductsCollums(5,'#product-az');
		
		$("#product-az .produktliste").clone().appendTo("#footer_main .produkte .content .middle");
		$("#product-az .produktliste").clone().appendTo("#navi_expand");		
	};	
	
	setProducts(aAllProducts);

});

