// JavaScript Document
$(document).ready(function(){
	$(".gal").hover(
		function() {
			
				$('#info').remove();
				
				$(this).append('<div id="info" style="display:none"></div>')

				inerthtml = '';
				
				temp_ob = $(this).parent().children('.li_m').children('span');
				if ( temp_ob.length ) {
					inerthtml += '<div class="full_info_title">'+clearcomma(temp_ob.html()) + '</div>';
				}

				if ($(this).parent().children('.li_m').children('.gallery_views').length ) {
					inerthtml += '<div class="gallery_views">'+ $(this).parent().children('.li_m').children('.gallery_views').html() +'</div>';
					inerthtml += '<div class="stars">'+ $(this).parent().children('.li_m').children('.stars').html() + '</div><br clear="all">';
				}

				temp_ob = $(this).parent().children('.li_m').children('.file_size').children('span');
				if ( temp_ob.length && temp_ob.html() != '') {
					inerthtml += '<div class="full_info_file_size">File Size: '+clearcomma(temp_ob.html()) + ' Mb</div>';
				}
        
				temp_ob = $(this).parent().children('.li_m').children('.gallery_pornstar').children('span');
				if ( temp_ob.length && temp_ob.html() != '') {
					inerthtml += '<div class="full_info_pornstars">Pornstars: '+clearcomma(temp_ob.html()) + '</div>';
				}
				
				temp_ob = $(this).parent().children('.li_m').children('.channels').children('em');
				if ( temp_ob.html() == '-' ) {
					inerthtml += '<div class="full_info_tube"><a href="/source/exclusive-1.html" class="source" target="_top"><strong>Exclusive movie!</strong></a></div>';
				}
				else if ( temp_ob.html() != 'planetsuzy' ) {
					inerthtml += '<div class="full_info_tube">Tube: <a href="/source/'+clearcomma(temp_ob.html()) + '-1.html" target="_top" class="source">'+clearcomma(temp_ob.html()) + '</a></div>';
				}

				temp_ob = $(this).parent().children('.li_m').children('.channels').children('span');
				if ( temp_ob.html() != '-' ) {
					inerthtml += '<div class="full_info_channels">Channels: '+clearcomma(temp_ob.html()) + '</div>';
				}

				temp_ob = $(this).parent().children('.li_m').children('.channels').children('b');
				if ( temp_ob.html() != '-' ) {
					inerthtml += '<div class="full_info_hosting">hosted @ '+clearcomma(temp_ob.html()) + '</div>';
				}

				temp_ob = $(this).parent().children('.li_m').children('.channels').children('p');
				if ( temp_ob.html() != '-' ) {
					inerthtml += '<div class="full_info_tags">Tags: '+clearcomma(temp_ob.html()) + '</div';
				}

				temp_ob = $(this).parent().children('.li_m').children('.date');
				if ( temp_ob.length && temp_ob.html() != '') {
					inerthtml += '<div class="date">'+ clearcomma(temp_ob.html()) +'</div>';
				}
				else
				{
				inerthtml += '<div class="date_old">'+ clearcomma($(this).parent().children('.li_m').children('.date_old').html()) +'</div>';
				}

				$('#info').html(inerthtml);
				
				$('#info').hover(
					function(){
						$(this).addClass('hover');
					},function(){
						$(this).removeClass('hover');
					}
				);
				$('#info').slideDown('fast');
		},
		function() {
			
			$('#info').remove();
		}
	);

/*
liFormat и selectItem я оставил тут ясно для чего - чтобы не возникало
ошибки. Их имена используются в опциях autocomplete
*/
function liFormat (row, i, num) {
	var result = row[0];
	return result;
}
function selectItem(li) {
	if( li == null ) var sValue = 'А ничего не выбрано!';
	if( !!li.extra ) var sValue = li.extra[2];
	else var sValue = li.selectValue;
//	alert("Выбрана запись с ID: " + sValue);
}
	
$('#id_q').autocomplete("/autocomplete.php", {
	delay:10,
	minChars:2,
	matchSubset:0,
	autoFill:true,
	matchContains:1,
	cacheLength:1,
	extraParams:{
//		a:name
	}, 
	selectFirst:0,
	formatItem:liFormat,
	maxItemsToShow:20,
	onItemSelect:selectItem
});


});
function clearcomma(text) {
	text = text.replace(/\s+$/,"");
	text = text.replace("'",'"');
	if (text[text.length-1] == ',') {
		text = text.substring(0,text.length-2);
	}
	return text + ' ';
}

