$(function(){
	RepositionBoxes();
	if( $('form.searcher').length > 0 ) {
		var defaultText =  $('form.searcher').find('input.input').val();
		$('form.searcher').find('input.input').focus(function(){
			if( $(this).val() == defaultText )  $(this).val('');
		}).blur(function(){
			if( $(this).val() == '' )  $(this).val(defaultText);
		});
		$('form.searcher').submit(function(){
			var query = $(this).find('input[name="string"]').val();
			if( query != 'szukana fraza' && query.length > 0 ) {
				//pageTracker._trackPageview('/wyjscie/wyszukiwarka/'+query);
				/*
				$.ajax({
					type: 'POST',
					url: 'ajax/use-searcher.php',
					data: 'string='+query
				});
				*/
				return true;
			}
			else return false;
		});
	}
	$('.commentform').submit(function(){
		$(this).find('.error, .info').remove();
		if( $(this).find('textarea').val().length == 0 ) {
			$(this).prepend( $('<div class=\'error\'>Nie podano treści komentarza</div>') );
			return false;
		}
		if( $(this).find('textarea').val().length < 15 ) {
			$(this).prepend( $('<div class=\'error\'>Komentarz musi zawierać co najmniej 15 znaków</div>') );
			return false;
		}
		return true;
	});
	if( $('form.commentform').find('textarea').length > 0 )
		$('form.commentform').find('textarea').textlimit( 'span#counter strong', 255 );
	
	if( $('.showcase').length > 0 ) ShowcaseLoad(0);
	
	if( $('.categories').find('.switcher').length > 0 ) {
		$('.categories').find('.switcher').find('a').toggle(function(){
			$('.categories').find('.novisible').css('display','list-item');
			$(this).text('zwiń');
			RepositionBoxes();
		},function(){
			$('.categories').find('.novisible').hide();
			$(this).text('rozwiń wszystkie');
			RepositionBoxes();
		});
	};
});

function RepositionBoxes() {
	var top = 190;
	if( $('.boxcategories').length > 0 ) {
		$('.boxcategories').css('position','absolute');
		$('.boxcategories').css('top',top+'px');
		top += $('.boxcategories').outerHeight(); 
	}
	if( $('.boxdescription').length > 0 ) {
		$('.boxdescription').css('position','absolute');
		$('.boxdescription').css('top',top+'px');
		top += $('.boxdescription').outerHeight();
	}
	if( $('.box_locations').length > 0 ) {
		$('.box_locations').css('position','absolute');
		$('.box_locations').css('top',top+'px');
		top += $('.box_locations').outerHeight();
	}
	if( $('.box_tags').length > 0 ) {
		$('.box_tags').css('position','absolute');
		$('.box_tags').css('top',top+'px');
		top += $('.box_tags').outerHeight();
	}
	if( $('.adsense_list_bottom').length > 0 ) {
		$('.adsense_list_bottom').css('margin-top',($('.bigboxpositioned').outerHeight()+10)+'px');
	}
	
	if( $('.list').length <= 1 &&  $('.bluebox_big').find('table').find('tr').length == 0 ) {
//		if( $('.blow_text').length > 0 ) 
//			top -= 1240;
//		else
		if( $('.plain').length > 0 )  
			top -= 470;
		else 
			top -= 590;
		$('.foot').css('margin-top',top+'px');
	}
	else if( $('.bluebox').length == 2 ) {
		top = 230;
		$('.foot').css('margin-top',top+'px');
	}
	
	if( $('.adsense_list').length == 0 ) {
		$('.bigboxpositioned').css('top','10px');
	}
	
	
};

function ShowcaseLoad(active) {
	var images = new Array();
	for( var i=1; i<=6; i++ ) {
		var img = new Image();
		img.src = 'showcase/gfx'+i+'.png';
	}
	ShowcaseGo(active);
	$('.showcase .arrows').find('.right').click(function(){
		active++;		
		ShowcaseGo(active);
	});
	$('.showcase .arrows').find('.left').click(function(){
		active--;
		ShowcaseGo(active);
	});
	
};

function ShowcaseGo(active) {
	if( active == 0 ) {
		$('.showcase .arrows').find('.left').fadeOut();
		$('.showcase .arrows').find('.right').fadeIn();
	}
	else if( active == 5 ) {
		$('.showcase .arrows').find('.left').fadeIn();
		$('.showcase .arrows').find('.right').fadeOut()
	}
	else {
		if( $('.showcase .arrows').find('.left').css('display') == 'none' )
			$('.showcase .arrows').find('.left').fadeIn();
		if( $('.showcase .arrows').find('.right').css('display') == 'none' )
			$('.showcase .arrows').find('.right').fadeIn();
	}
	
	// obrazek 
	$('.showcase').find('img#showcase1').fadeOut('fast',function(){
		$('map#showcase1').remove();
		$('img#showcase1').remove();
		$('.showcase .img').append( '<img src=\'showcase/gfx'+(active+1)+'.png\' alt=\'\' id=\'showcase1\' name=\'szolkejs\' usemap=\'#showcase1\' style=\'border:0;\' style=\'display:none;\' />' );
		$('#showcase1').fadeIn();
		var map = '';
		map = $.ajax({
			url: 'showcase/map'+(active+1)+'.html',
			async: false
		 }).responseText;
		$('body').append(map);
		
	});
	
	// opis
	$('.showcase .desc p').fadeOut('fast',function(){
		$(this).load('showcase/text'+(active+1)+'.html');
		$(this).fadeIn();
	});
	return;	
};

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
};

function VoteUp( idAuction, ip ) {
	$.ajax({ url: 'ajax/saveVote.php', data: 't=up&ip='+ip+'&idAuction='+idAuction, type: 'POST' });
	VoteSaved();
	return;
};

function VoteDown( idAuction, ip ) {
	$.ajax({ url: 'ajax/saveVote.php', data: 't=down&ip='+ip+'&idAuction='+idAuction, type: 'POST' });
	VoteSaved();
	return;
};

function VoteSaved() {
	$('.voting').find('div').slideUp('fast', function(){
		$(this).prev('p').text('Twój głos został zapisany');
	});
	return;
};