var wynik = false;
var verify_array = new Array();
var global_timer = null;

function hide_tips()
{
	$('.message_tip').each(function(){
			$(this).hide();
	});
}

function clear_form()
{
	$(':input').each(function(){
		if(($(this).attr('name') != 'recaptchasubmit') && ($(this).attr('name') != 'recaptcha_response_field') && ('recaptcha_challenge_field'))
			$(this).val('');
	});
}

function get_array_size(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

function unlock_submit($data_in)
{	
	var result = true;
	//if(get_array_size($data_in) == 3)
	{
		for(var i in $data_in)
		{
			if ($data_in[i] == 'false')
			{
				result = false;
				$(':submit').attr("disabled", "disabled");
			}
		}

		if (result)
		{
			$(':submit').removeAttr("disabled");
		}
	}
}

function verifyInputData_($data_in, $show_val)
{
	for (var i in $data_in)
	{
		switch(i)
		{
			case 'name':
			{
				if ($data_in[i].length < 5)
				{
					verify_array[i] = 'false';
					if(($('.message_tip.' + '_' + i).css('display') == 'none') && ($show_val == true))
					{
						$('.message_tip.' + '_' + i).slideDown('medium');
					}
				}
				else
				{
	
					$('.message_tip.' + '_' + i).css('display', 'none');
					verify_array[i] = 'true';
				}
				unlock_submit(verify_array);
			}
			break;		
			case 'email':
			{
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if (reg.test($data_in[i]) == false)
				{
					verify_array[i] = 'false';
					if(($('.message_tip.' + '_' + i).css('display') == 'none') && ($show_val == true))
					{
						$('.message_tip.' + '_' + i).slideDown('medium');
					}
				}
				else
				{

					$('.message_tip.' + '_' + i).css('display', 'none');
					verify_array[i] = 'true';
				}
				unlock_submit(verify_array);
			}
			break;
			case 'title':
			{
				if ($data_in[i].length < 5)
				{
					verify_array[i] = 'false';
					if(($('.message_tip.' + '_' + i).css('display') == 'none') && ($show_val == true))
					{
						$('.message_tip.' + '_' + i).slideDown('medium');
					}
				}
				else
				{

					$('.message_tip.' + '_' + i).css('display', 'none');
					verify_array[i] = 'true';
				}
				unlock_submit(verify_array);				
			}
			break;
			case 'text':
			{
				if ($data_in[i].length < 10)
				{
					verify_array[i] = 'false';
					if(($('.message_tip.' + '_' + i).css('display') == 'none') && ($show_val == true))
					{
						$('.message_tip.' + '_' + i).slideDown('medium');
					}
				}
				else
				{
					$('.message_tip.' + '_' + i).css('display', 'none');
					verify_array[i] = 'true';
				}
				unlock_submit(verify_array);
			}
			break;
			case 'nickname':
			{
				if ($data_in[i].length < 3)
				{
					verify_array[i] = 'false';
					if(($('.message_tip.' + '_' + i).css('display') == 'none') && ($show_val == true))
					{
						$('.message_tip.' + '_' + i).slideDown('medium');
					}
				}
				else
				{

					$('.message_tip.' + '_' + i).css('display', 'none');
					verify_array[i] = 'true';
				}
				unlock_submit(verify_array);				
			}
			break;
			case 'phone':
			{
				if ($data_in[i].length < 9)
				{
					verify_array[i] = 'false';
					if(($('.message_tip.' + '_' + i).css('display') == 'none') && ($show_val == true))
					{
						$('.message_tip.' + '_' + i).slideDown('medium');
					}
				}
				else
				{

					$('.message_tip.' + '_' + i).css('display', 'none');
					verify_array[i] = 'true';
				}
				unlock_submit(verify_array);				
			}
			break;
		}
	}
}

function hide_loading()
{
	var send_confirmation = $('#send_confirmation');
	if((send_confirmation.children().attr('class') != 'email_error') && (!send_confirmation.hasClass('send_subsite')))
	{
		//parent.$("#fancy_outer").height(100);
		//$("#fancy_outer").css({'width': '500px', 'height': '100px'});
		parent.$("#fancybox-content").css({'height': '100px'});
		$('#send_message_left').css("display", "none");
		$('#send_message_right').css("display", "none");
		$('.message_box.tip').css("display", "none");
		$('.double_line').css("display", "none");
	}
	setTimeout(function(){
		$('#message_loading_gif').css('display', 'none');
		$('#message_loading_caption').html($('#send_confirmation').html());
		setTimeout(function(){
			if($('#send_confirmation').children().attr('class') != 'email_error')
			{
				clear_form();
				$(':submit').attr("disabled", "disabled");
				//parent.$.fancybox.close(); 
			}
			$('#email_confirmation').fadeOut("slow")
			.queue(function(){
				$(':submit').show();
			});
		}, 3000);  		
	}, 3000);
}


//-------------------------------------------------------------------------------------------------------------

$(document).ready(function(){
	
	
	$('.message_tip.visible').each(function(){
		$.scrollTo($(this));
		return false;
	});
	
	$(':input[name="phone"]').bind('keypress', function(e) {
		return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
    });
	
	if(($('#message_loading_gif').length) && ($('#message_loading_gif').css('display') != 'none'))
	{
		$(':submit').hide();
		hide_loading();
	}
	
	var delay = (function(){
	var timer = 0;
	return function(callback, ms){
		clearTimeout (timer);
		timer = setTimeout(callback, ms);
	};
	})();
	
	var input_tab = new Array();
	$(':input').each(function(){
		if ($(this).prev().children().attr('class') == "red")
		{
			input_tab[$(this).attr('name')] = $(this).val();
		}
	});
	verifyInputData_(input_tab, false);

	$(':input')
	.keyup(function(){	
		if ($(this).prev().children().attr('class') == "red")
		{
		var input_tab = new Array();
		input_tab[$(this).attr('name')] = $(this).val();
		delay(function(){
			verifyInputData_(input_tab, true);
			
		}, 1000 );			
		}

	})
	.focusout(function(){
	{
		if ($(this).prev().children().attr('class') == "red")
		{	
			var input_tab = new Array();
			input_tab[$(this).attr('name')] = $(this).val();
			verifyInputData_(input_tab, true);	
		}
	}

	});
	
	$('div.button_bckgr').click(function(){
		if($(this).html() == '<span></span>dodaj własny komentarz')
			hide_tips();
			//clear_form();
	});
	
});
