var RecaptchaOptions = {
	theme : 'clean', 
	custom_translations : {
	    instructions_visual : "Przepisz dwa słowa:",
	    instructions_audio : "Przepisz dwa słowa:",
	    play_again : "Odtwórz ponownie",
	    cant_hear_this : "Pobierz w formacie MP3",
	    visual_challenge : "Zobacz kod",
	    audio_challenge : "Odsłuchaj kod",
	    refresh_btn : "Odśwież",
	    help_btn : "Pomoc",
	    incorrect_try_again : "Niepoprawnie, spróbuj ponownie."
	}
};

$(function(){
	var i;
	
	
	$('form.ask-about-item-form').submit(function(){
		var ok = false;
		var $button = $(this);
		var orginalAnchor = $button.val();   
		$button.val('Przetwarzam...');
		$button.parents('form').addClass('disabled');
		$button.parents('form').find('input').attr('disabled','disabled');
		
		$('.askAboutItem').find('.inline-error').html('&nbsp;').removeClass('inline-error');
		var query = $('.askAboutItem').find('input[name="itemName"]').val();
		var email = $('.askAboutItem').find('input[name="userEmail"]').val();
		var captcha = $('.askAboutItem').find('input[name="recaptcha_response_field"]').val();
		var err = new Array();
		if( query.length < 3 ) {
			err.push( new Array( 'itemName', 'Nazwa przedmiotu musi zawierać co najmniej 3 znaki' ) );
		}
		if( email.length == 0 ) {
			err.push( new Array( 'userEmail', 'Nie podano adresu e-mail' ) );
		}
		else if( email.indexOf('@') == -1 ) {
			err.push( new Array( 'userEmail', 'Podano nieprawidłowy adres e-mail' ) );
		}
		
		if( captcha.length == 0 ) {
			err.push( new Array( 'recaptcha_response_field', 'Nie przepisano kodu zabezpieczającego' ) );
		}
		
		if( err.length == 0 ) {
			$.ajax({
				url: '/wp-content/themes/wp-theme-2/ajax/ask-about-item.ajax.php', 
				async: false, 
				type: 'POST', 
				data: {
					itemName : query, 
					userEmail : email, 
					recaptcha_response_field : captcha 
				}, 
				dataType: 'json',
				success: function( response ) {
					
					if( response.ok == 0 ) {
						$.each(response.errors, function(k, v){
							if( k == 'recaptcha_response_field' ) {
								small = $('.askAboutItem').find('[name="recaptcha_response_field"]').parents('div.field').find('small');
							}
							else {
								small = $('.askAboutItem').find('[name="'+k+'"]').next('small');
							}
							
							small.addClass('inline-error').text(v);
						});
						$button.val( orginalAnchor );
						$button.parents('form').removeClass('disabled');
						$button.parents('form').find('input').removeAttr('disabled');
					}
					else {
						ok = true;
					}
				}
			});	
		}
		else {
			var small = '';
			$(err).each(function(k, el){
				if( el[0] == 'recaptcha_response_field' ) {
					small = $('.askAboutItem').find('[name="recaptcha_response_field"]').parents('div.field').find('small');
				}
				else {
					small = $('.askAboutItem').find('[name="'+el[0]+'"]').next('small');
				}
				small.addClass('inline-error').text(el[1]);
			});
			$button.val( orginalAnchor );
			$button.parents('form').removeClass('disabled');
			$button.parents('form').find('input').removeAttr('disabled');
		} 
		if( ok == true ) {
			return true;
		}
		else {
			return false;
		}
	});
	
	if( $('#allebanner').length > 0 ) {
		$('#allebanner').flash({
			swf : '/wp-content/themes/wp-theme-2/flash/baner_allegro2.swf', 
			height : 250, 
			width : 300
		});
	}
	
	$('a').click(function(){
		var targeurl = $(this).attr('href');
		if( targeurl == '#show_email' )  {
			return false;
		}
		else if( targeurl.indexOf( '#zarejestruj-sie' ) != -1 ) {
			location.href = 'https://ssl.allegro.pl/Register.php?ap='+config.ap+'&aid='+config.aid+'&bid='+config.bid;
			return false;
		}
		else  if( targeurl.indexOf( '#' ) != -1) {
			
			var redirect_type = '', targeturi = '';
			var param = targeurl.substr( (targeurl.indexOf( '#' ) +1 ));
			var tokens = new Array();
			
			var foo = param.split('-');
			if( foo.length == 1 ) {
				redirect_type = 'item';
				tokens = param.split('');
				for( i=0; i<tokens.length; i++ ) {
					if( tokens[i] != '1' && tokens[i] != '2' && tokens[i] != '3' && tokens[i] != '4' && tokens[i] != '5' && 
						tokens[i] != '6' && tokens[i] != '7' && tokens[i] != '8' && tokens[i] != '9' && tokens[i] != '0' ) 
					{
						redirect_type = 'search';
						break;
					}
				}
			}
			else {
				param = foo.pop();
				redirect_type = 'seller';
			}
			
			if( redirect_type == 'item' ) {
				targeturi = '/show_item.php?item='+param+'&ap='+config.ap+'&aid='+config.aid+'&bid='+config.bid;
			}
			else if( redirect_type == 'seller' ) {
				targeturi = '/show_user.php?uid='+param+'&ap='+config.ap+'&aid='+config.aid+'&bid='+config.bid;
			}
			else {
				targeturi = '/listing.php/search?string='+param+'&ap='+config.ap+'&aid='+config.aid+'&bid='+config.bid;
			}
			$.post( 
				config.template_uri+'/libs/catch-allegro-out.php', 
				'loadtime='+config.visit_date+'&host='+config.host+'&clientip='+config.client_ip, 
				function(){
					location.href = 'http://allegro.pl'+targeturi;
					return false;
				}
			);
			return false;
		}
		else return true;
	});
	SaveVisit();
});

function SaveVisit()
{
	$.post( 
		config.template_uri+'/libs/save-visit.php', 
		'loadtime='+config.visit_date+'&host='+config.host+'&clientip='+config.client_ip
	);
};

function NoAccount()
{
	save_out( 'https://ssl.allegro.pl/new_user.php?ap='+config.ap+'&aid='+config.aid+'&bid='+config.bid );
};

function HaveAccount()
{
	NoAccount();
};

function save_out( target_url )
{
	var target_uri = '';
	if( target_url.substr(0,22) == 'https://ssl.allegro.pl' ) {
		target_uri = target_url.substr(22);
	}
	else if( target_url.substr(0,18) == 'https://allegro.pl' ) {
		target_uri = target_url.substr(18);
	}
	else if( target_url.substr(0,22) == 'https://wwww.allegro.pl' ) {
		target_uri = target_url.substr(22);
	}
	else if( target_url.substr(0,17) == 'http://allegro.pl' ) {
		target_uri = target_url.substr(17);
	}
	else if( target_url.substr(0,21) == 'http://wwww.allegro.pl' ) {
		target_uri = target_url.substr(21);
	}
	$.post( 
		config.template_uri+'/libs/catch-allegro-out.php', 
		'loadtime='+config.visit_date+'&targeturi='+target_uri.replace(/&/gi,'[amp]')+'&host='+config.host+'&clientip='+config.client_ip, 
		function(){
			location.href = 'http://allegro.pl'+target_uri;
			return false;
		}
	);
};
