$(function()
{
	$('.input-cautare input').each(function(){
		if ( $(this).val() != '' ) $(this).parent().find('label').hide();
	});
	$('#lat-st-cauta .input-cautare label').click(function(){
		$(this).fadeOut('fast').next().focus();
	}).focus(function(){
		$(this).fadeOut('fast').next().focus();
	}).select(function(){
		$(this).fadeOut('fast').next().focus();
	});
	
	if ( $('.lat .meniu .t').length ) $('.lat .meniu .t:not(#mag_detail_wrap .t):not(#mag_wrap .t)').click(function(e){
		e.preventDefault();
		$(this).removeClass('sel').parent().parent().toggleClass('sel');
	});

	// se ocupa de meniul cu taburi din stanga
	if ($('.lat .meniu-file').length)
		$('.lat .meniu-file').each(function()
		{
			var i = $(this).find('li:(.sel)').index(); // găsim primul tab care nu e inactiv 
			
			$(this).tabs( $(this).next().find('.lat-c'), { 
				current	:	'sel',
				initialIndex: i
			});
		});

	initLeftCategMenu();

	$('.filtru .meniu.toggle').parents('.filtru').find('>.titlu-filtru .t').click(function(e){
		e.preventDefault();
		$(this).parents('.filtru').toggleClass('filtru-sel');
	});
	
	if ($('#f-comanda-rapida').length)
		$('#f-comanda-rapida').find('.rand-plus').click(
			function()
			{
				var t = $(this);
				var ul = t.parent().prev();
				var li = ul.find('li');
				
				var cateRanduri = li.size();
				
				var nr = parseInt(cateRanduri + 1);
				var randNou = '<li><label for="product_' + nr + '"><small>' + nr + '. </small> <input type="text" id="product_' + nr + '" name="product_' + nr + '" class="f fo-product" /></label> <label for="qty_'+ nr + '" class="q"><small>Cantitate:</small> <input id="qty_' + nr + '" name="qty_' + nr + '" type="text" class="q-input" maxlength="2" value="1" /><a class="q-control q-sus" title=""></a><a class="q-control q-jos" title=""></a></label></li>'; 
				ul.append( randNou ).find('li:last-child').css('backgroundColor','#fafad2').animate({backgroundColor: '#ffffff'}, 1500);
				
				var options = {
					script:"ajax/ajax_products.php?json=true&plugin=products&limit=8&",
					varname: "input",
					json: true,						// Returned response type
					shownoresults: true,				// If disable, display nothing if no results
					noresults: "Nici un rezultat",			// String displayed when no results
					maxresults: 8,					// Max num results displayed
					cache:false,					// To enable cache
					minchars:2,						// Start AJAX request with at leat 2 chars
					timeout:1000,					// AutoHide in XX ms
					callback: function (obj) {} 			// Callback after click or selection
				};
				
				if ( typeof(bsn) != 'undefined' ) eval("var as_json" + nr + " = new bsn.AutoSuggest('product_" + nr + "', options);");
				if ( typeof(Watermark) != 'undefined' ) $("#product_" + nr).Watermark("Cod produs / cuvant cheie:");
				$("#product_" + nr).focus();
			});

	$('#cos-c-promotii>.ct').each(function(){
		var t = $(this);
		var col = t.find('>ul.promotie-col');
		var max = 0;
		col.each(function(){
			var h = $(this).height();
			if ( h > max )
				max = h;
		});
		col.height(max);
	});
});


function initLeftCategMenu()
{
	if ($('.lat .complex .cat, .lat .meniu .t').length)
	{
		$('.lat .complex .cat, .lat .meniu .t').click(function(e){
			
			var t = $(this);
			var c = t.parents('li');
			var et = $(e.target);
			var cat = et.text();
			
			if (!et.is('span'))
			{
				var rel = t.attr('rel');
				
				if (rel == 'produse' || rel == 'reduceri')
				{
					if (!leftCategs[cat])
					{
						$('#subcategs-left-' + rel + '-' + cat).load('ajax/ajax_left_categories.php?type=' + rel + '&categorie=' + cat, function() { reinitMenuTriggers('#subcategs-left-' + rel + '-' + cat + ' .cat'); });
						leftCategs[cat] = true;
					}
				}
				
				clasaDeschis = !$(e.target).is('.t') ? 'cat-deschisa' : 'sel';
				c.toggleClass(clasaDeschis);
				var t = t.parent().next();
				if ( t.is('.toggle') )
					t.jScrollPane({
						scrollbarWidth:7,
						arrowSize:6,
						scrollbarMargin:0
					});
				e.preventDefault();
			}
		});
	}

	$('.lat .meniu > li').each(function(){
		var t = $(this);
		var h = t.height();
		if (t.is('#lat-st-categorii li')) {
			h = t.find('a.t').height();
			if (h > 57) 
				t.addClass('mare');
		}
		else {
			if ( t.is('#lat-st-cont li') )
				if ( t.find('span').height() > 15 )	t.addClass('mare')
			else if (h > 30) t.addClass('mare')
		}
	});
}



function reinitMenuTriggers(selector)
{
	if ( $(selector).length)
		$(selector).click(function(e)
		{
			var t = $(this);
			var c = t.parents('li');
			var et = $(e.target);
			if (!et.is('span'))
			{
				clasaDeschis = !$(e.target).is('.t') ? 'cat-deschisa' : 'sel';
				c.toggleClass(clasaDeschis);
				var t = t.parent().next();
				if (t.is('.toggle'))
					t.jScrollPane({
						scrollbarWidth:7,
						arrowSize:6,
						scrollbarMargin:0
					});
				e.preventDefault();
			}
		});
}

