var images = new Array();
images.push('static/images/sort-arrow-down.jpg');
images.push('static/images/sort-arrow-up.jpg');
for( var i=0; i<images.length; i++ ) {
	var img = new Image();
	img.src = images[i];
}
delete(img);

var suggests_options = {
	script:'ajax/get-suggests.php?json=true&limit=6&',
	varname:'input',
	json:false, 
	shownoresults:false,
	minchars: 3, 
	maxresults:16//,
//	callback: function (obj) { $('#xml_info').html('you have selected: '+obj.id + ' ' + obj.value + ' (' + obj.info + ')'); }
};


$(function(){
	$('div.thumb > div').tooltip2();
	$('.top form input[name="query"]').autoComplete(suggests_options).keyup(function(){
		$('.top form').find('.tip').css({
			fontWeight: 'normal', 
			color: '#5b9ac2'
		});
	});
	$('.top form').submit(function(){
		if( $('.top form input[name="query"]').val().length < 3 ) {
			tip_bolder();
			return false;
		}
		else {
			var tokens = $('.top form input[name="query"]').val().split('.');
			if( tokens.length > 1 ) {
				if( tokens[(tokens.length-1)].length == 2 || tokens[(tokens.length-1)].length == 3 ) {
					tip_bolder();
					return false;
				}
			}
			var tokens = $('.top form input[name="query"]').val().split(',');
			if( tokens.length > 1 ) {
				if( tokens[(tokens.length-1)].length == 2 || tokens[(tokens.length-1)].length == 3 ) {
					tip_bolder();
					return false;
				}
			}
			if( $('.top form input[name="query"]').val().substr(0,4) == 'http' ) {
				tip_bolder();
				return false;
			}
		}
		return true;
	});
	
	if( $('.search-result').length > 0 ) {
		$('.search-result').find('.out').click(function(){
			var $link = $(this);
			if( $(this).parent().parent().parent().parent().parent('tr').length > 0 )
				var parent = $(this).parent().parent().parent().parent().parent('tr');
			else if( $(this).parent().parent().parent('tr').length > 0 )
				var parent = $(this).parent().parent().parent('tr');
			else if( $(this).parent().parent().parent().parent('tr').length > 0 )
				var parent = $(this).parent().parent().parent().parent('tr');
			var idCategory = parent.find('.path').find('a:last').attr('class').substr(4);
			var fquery = $('#searched_fquery').text();
			var query = $('#searched_query').text();
			$.ajax({
				url: 'http://aukcjeallegro.pl/ajax/auction-clicked.php', 
				type: 'post', 
				data: 'idCategory='+idCategory+'&fquery='+fquery+'&query='+query, 
				success: function(){
					top.location.href = $link.attr('href');
					return true;
				}
			});
			return false;
			
		});
	}
	
	$('.sortby_matched a').mouseover(function(){
		$('.sortby_matched a.txt').css('text-decoration','underline');
		if( $('.sortby_matched .arrows').find('.nodir').length > 0 ) 
			$('.sortby_matched .arrows').find('img').attr('src','static/images/sort-arrow-down.jpg');
		else if( $('.sortby_matched .arrows').find('.up').length > 0 ) 
			$('.sortby_matched .arrows').find('img').attr('src','static/images/sort-arrow-down.jpg');
		else if( $('.sortby_matched .arrows').find('.down').length > 0 ) 
			$('.sortby_matched .arrows').find('img').attr('src','static/images/sort-arrow-up.jpg');
	}).mouseout(function(){
		$('.sortby_matched a.txt').css('text-decoration','none');
		if( $('.sortby_matched .arrows').find('.nodir').length > 0 ) 
			$('.sortby_matched .arrows').find('img').attr('src','static/images/sort-arrow.jpg');
		else if( $('.sortby_matched .arrows').find('.up').length > 0 ) 
			$('.sortby_matched .arrows').find('img').attr('src','static/images/sort-arrow-up.jpg');
		else if( $('.sortby_matched .arrows').find('.down').length > 0 ) 
			$('.sortby_matched .arrows').find('img').attr('src','static/images/sort-arrow-down.jpg');
	});
	
	$('.sortby_price a').mouseover(function(){
		$('.sortby_price a.txt').css('text-decoration','underline');
		if( $('.sortby_price .arrows').find('.nodir').length > 0 ) 
			$('.sortby_price .arrows').find('img').attr('src','static/images/sort-arrow-down.jpg');
		else if( $('.sortby_price .arrows').find('.up').length > 0 ) 
			$('.sortby_price .arrows').find('img').attr('src','static/images/sort-arrow-down.jpg');
		else if( $('.sortby_price .arrows').find('.down') ) 
			$('.sortby_price .arrows').find('img').attr('src','static/images/sort-arrow-up.jpg');
	}).mouseout(function(){
		$('.sortby_price a.txt').css('text-decoration','none');
		if( $('.sortby_price .arrows').find('.nodir').length > 0 ) 
			$('.sortby_price .arrows').find('img').attr('src','static/images/sort-arrow.jpg');
		else if( $('.sortby_price .arrows').find('.up').length > 0 ) 
			$('.sortby_price .arrows').find('img').attr('src','static/images/sort-arrow-up.jpg');
		else if( $('.sortby_price .arrows').find('.down').length > 0 ) 
			$('.sortby_price .arrows').find('img').attr('src','static/images/sort-arrow-down.jpg');
	});
	
	$('.sortby_matched a').click(function(){
		$('form.sort').find('input[name="switchsort"]').val('matched');
		$('form.sort').submit();
	});
	$('.sortby_price a').click(function(){
		$('form.sort').find('input[name="switchsort"]').val('price');
		$('form.sort').submit();
	});
	
	if( $('.last-searched-form').length > 0 ) {
		$('.last-searched-form').each(function(){
			$(this).find('a').click(function(){
				$(this).parent('form').submit();
			});
		});
	}
	
	if( $('.search-result').length > 0 ) {
		$('.search-result').find('tr:not(.promo)').mouseover(function(){
			$(this).find('td').css({
				'background-color':'#f5f5f5', 
				'background-image': 'none'
			});
		}).mouseout(function(){
			$(this).find('td').css({
				'background-color':'#fff', 
				'background-image': 'url(static/images/search-result-td-bg.jpg)'
			});
		});
	};
	
	if( $('.search-result').length > 0 ) {
		$('.top form').find('input[name="query"]').focus(function(){
			$(this).val('');
		});
	}
	
	if($('form.changeMatch').length > 0 ) {
		$('form.changeMatch').find('a.submit').click(function(){
			$(this).parent('form').submit();
		});
	}
	if( $('input[name="enable_preview"]').length > 0 ) {
        var options = { path: '/', expires: 30 };
        if( $.cookie('aukcjeallegro_enable_preview') == 1 ) 
        	$('input[name="enable_preview"]').attr('checked','checked');
		$('input[name="enable_preview"]').change(function(){
			if( $(this).attr('checked') == true ) 
				$.cookie('aukcjeallegro_enable_preview', 1, options);
			else $.cookie('aukcjeallegro_enable_preview', null, options);
		});
	}
});

function tip_bolder() {
	if( $('.top form').find('.tip span').css('color') == 'rgb(255, 255, 255)' ) 
		$('.top form').find('.tip span').css('font-weight','bold');
	else
		$('.top form').find('.tip span').css('color','#ffffff');
}