$(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);
				
				return true;
			}
			else return false;
		});
	}
	
	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() {
	if( $('.boxdescription').length > 0 && $('.boxcategories').length > 0 ) {
		$('.boxdescription').css('margin-top', ($('.boxcategories').outerHeight()+10)+'px');
		var move = ( $('.boxdescription').outerHeight()+10);
		if( $('.box_locations').length > 0 ) {
			$('.box_locations').css('margin-top', move+'px' );
		}
		else if( $('.box_tags').length > 0 ) {
			$('.box_tags').css('margin-top', move+'px' );
		}
	}
	return;
};

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;
};