/**
 * @author Domenico Lupinetti
 * @email ostico@gmail.com
 */
function s_el(el)
{
	var obj = $(el).find('.api_url').select();
}

set_api_compare = function() {
		$('#submenu_bg').addClass('sub_bg_small');
		$('#toggle_search').hide();
		$('.tablesorter').tablesorter();
};

function hide_search() {
	if ($('#submenu_bg').hasClass('sub_bg')
			&& !$('#submenu_bg').hasClass('sub_bg_small')) {
		$('#stat_navigation').fadeOut(200, function() {
			$('#submenu_bg').addClass('sub_bg_small');
			$('#toggle_search em').html("Show");
		});
	} else {
		$('#stat_navigation').fadeIn(200);
		$('#submenu_bg').removeClass('sub_bg_small');
		$('#toggle_search em').html("Hide");
	}
}

$(document).ready(function(e) {
	$('dl').click(function(handler) {

		$('ul.dd_bottom').each(function(){
			if( $(this).is(':visible') && !$(handler.target).parent().find('ul.dd_bottom').is(':visible') )
			{ 
				$(this).hide(300);
			}
		});
		
		//&& !$(handler.target).parent().find('ul.dd_bottom').is(':visible')
		if( $(this).is(':visible') )
		{ 
			$(this).find('ul.dd_bottom').toggle('fast', function()
					{ 
						if( $(handler.target).parent().find('ul.dd_bottom').is(":visible") ){
							var new_height = $(handler.target).parentsUntil('ul.layers').outerHeight(true) + 20;
							$('ul.layers').animate({height : [new_height + 'px', 'linear'] },'fast'); 
							$('brand_list').animate({ height : ['auto','linear'] },'fast'); 
						}
						else { 
							$('ul.layers').animate({ height : ['60px','linear'] },'fast');
							$('brand_list').animate({ height : ['auto','linear'] },'fast'); 
						}
					}
			);
		}
	});
});

function hide_items_list() {
	$('ul.dd_bottom').each(function(){
		if( $(this).is(':visible') )
		{ 
			$(this).hide(300, function() { 
				$('ul.layers').animate({ height : ['60px','linear'] },'fast'); 
				$('brand_list').animate({ height : ['auto','linear'] },'fast'); 
			});
		};
	});
};

function _wait( element, time, callback )
{
	var wait = setInterval(function() {
		var rnd = Math.random(0,999999999);
		var n = jQuery.queue( $(element), "fx_" + rnd );
		if( n.length == 0 && !$(element).is(':animated') )
		{
			clearInterval(wait);
			if( typeof callback == 'function' )
			{
				callback();
			}
		}
	}, time);
}

function sleep(naptime) {
	naptime = naptime * 1000;
	var sleeping = true;
	var now = new Date();
	var alarm;
	var startingMSeconds = now.getTime();
	while (sleeping) {
		alarm = new Date();
		alarmMSeconds = alarm.getTime();
		if (alarmMSeconds - startingMSeconds > naptime) {
			sleeping = false;
		}
	}
}

function selectItem() {
	var item = arguments[0];
	$.post("./ajax_res/manage_data.php", {
		item : item.id
	});

}

//****************/

function colorize_li(obj, _class) {
	var searched_classes = $(obj).attr("class");
	$('li').each(function(index) {
		if ($(this).attr("class") == searched_classes) {
			$(this).toggleClass(_class);
		}
	});
}

// ****************

function get_base()
{
	var x = location.href;
	var domain = x.split("http://", 2)[1].split("/")[0];
	var path = x.split("http://", 2)[1].split("/");
	path.shift();
	//var lenPath = path.length;
	if( x.indexOf(".php") || x.indexOf(".htm") || x.indexOf(".html") ){
		//lenPath--;
		path.pop();
	}
	return ("http://" + domain + "/" + path.join("/") );
}

function lookup(inputString, suggestions, autoSuggestionsList) {
	var b = get_base();	
	if (inputString.value.length == 0) {
		// Hide the suggestion box.
		$("#" + suggestions).hide();
	} else {
		$.post( b + "/ajax_res/_search.php", {
			queryString : "" + inputString.value + "",
			inputField : inputString.name,
			suggestionBox : suggestions
		}, function(data) {
			if (data.length > 0) {
				$("#" + suggestions).show();
				$("#" + autoSuggestionsList).html(data);
			}
		});
	}
} // lookup

function fill(thisValue, inputField, suggestionBox, ID_thisValue) {
	$('#' + inputField).attr( {
		value : thisValue
	});
	$("#" + suggestionBox + " > input[type='hidden']").attr( {
		value : ID_thisValue
	});
	$('#' + suggestionBox).hide();
}

function avoid(inputField, suggestionBox) {
	$('#' + inputField).attr( {
		value : ""
	});
	$("#" + suggestionBox + " > input[type='hidden']").attr( {
		value : ""
	});
	$('#' + suggestionBox).hide();
	$('#' + inputField).focus();
}

function res(obj)
{	
	obj.value = ' Cerca Un Prodotto'; 
};

function submit_choice(page) {
	try {	
		if (page.length > 0) {
			window.location.replace( page );
		}
	} catch (err) {}
}

