var cleanLogin = function(){
	return true;
}

var _captcha_post;
function processCaptcha(key,url,uid){
	if (!uid){
		return false;
	}
	var _captcha_img = $("#captcha_img"+uid);
	var _captcha_key = $("#captcha_key"+uid);
	if (_captcha_img.length < 1 || _captcha_key.length < 1){
		return false;
	}
	if ($(_captcha_img).is("img")){
		$(_captcha_img).attr("src",url);
	}else{
		$(_captcha_img).replaceWith('<img id="captcha_img'+uid+'" src="'+url+'" alt="Введите эти цифры" widht="200px" height="60px" />');
	}
	$(_captcha_key).val(key);
	$("#captcha_value"+uid).val("");
}

function _make_gallery(){
	$("div.b-photo-informer").each(function(){
		var _block = this;
		$("table.pager a, td.listing a",this).each(function(){
			$(this).unbind('click').click(function(){
				var _href = $(this).attr('href');
				var _uid = _href.match(/uid=(\d*)/i)[1];
				var _page = _href.match(/page=(\d*)/i)[1];
				var _return = true;
				$(_block).height($(_block).height());
				$.ajax({
					url:"/inc/fotki/"+_uid+".html",
					data:{'uid':_uid,'page':_page},
					async:false,
					success:function(msg){
						var _html = $(msg).html();
						$(_block).html(_html);
						window.setTimeout(function(){
							$(_block).css({'height':'auto'});
							_make_gallery();
						},50);
						_return = false;
					}
				});
				return _return;
				
			});
		});
	});
}

function _make_feedback(){
	var imgIndicator = new Object();
	var imgCount = 6;
	var imgRepeat = 2;
	var counts = 0;
	var img_need = {};
	var _captcha_count = {};
	for (var j = 0; j < imgRepeat; j++ ){
	    for (var i = 1; i <= imgCount; i++ ){
	        imgIndicator['img' + (i + j * imgCount)] = new Image(16,16);
	        imgIndicator['img' + (i + j * imgCount)].src = 'http://passport.yandex.ru/i/next' + i + '.gif';
	    }
	}
	imgIndicator['img' + (imgRepeat * imgCount + 1)] = new Image(16,16);
	imgIndicator['img' + (imgRepeat * imgCount + 1)].src = 'http://passport.yandex.ru/i/next1.gif';
	var _rotate_timeout;
	function rotate(_uid){
		if (img_need[_uid] && document.images['img_reloading']){
			counts++;
			document.images['img_reloading'].src = imgIndicator['img' + ((counts % imgCount)+1)].src;
			window.clearTimeout(_rotate_timeout);
			_rotate_timeout = window.setTimeout(function(){rotate(_uid)},70);
		}
	}	

	$("div.b-feedback[id]").each(function(){
		var _uid = $(this).attr("id")||"";
		if (typeof($(this).attributes) == "function"){
			_uid = $(this).attributes("uid");
		}else{
			_uid = _uid.replace("e_","")||"";
		}
		if (_uid){
			$("#captcha_rotate"+_uid).unbind('click').click(function(){
				_getCaptcha(_uid);
				return false;
			});
			$("#feedback_form"+_uid).unbind('submit').submit(function(){
                var _this = this;
                $("input,textarea",_this).attr("disabled","disabled");
				$(_this).attr("action","/feedback/");
				$("#captcha_error"+_uid+",#captcha_success"+_uid+",#internal_error"+_uid+",#name_error"+_uid+",#text_error"+_uid+",#email_error"+_uid).hide();
				var params = {};
				$("input",_this).each(function(){
					var name = $(this).attr("name");
					var val = $(this).val();
					
					params[name]=val;
				});
				params["text"]=$("#b-feedback__input_comment").val()||"";
				params["action"]="send";
				params["id"]=_uid;
				
				var _errors = {
					'INVALID_USER_NAME':'name_error',
					'INVALID_USER_EMAIL':'email_error',
					'INVALID_MESSAGE':'text_error',
					'INVALID_CAPTCHA_VALUE':'captcha_error'
				};
				
				$.ajax({
					url:"/feedback/",
					data:params,
					type:"POST",
					dataType:"xml",
					success:function(msg){
						msg = $("response",msg);
						if (!$(msg).is("response")){
							$("#internal_error"+_uid).show();
						}else{
							if ($(msg).attr("status") == "SUCCESS" ){
								$("#captcha_success"+_uid).show();
							}else{
								$("errors",msg).each(function(){
									var _err = $(this).text();
									if (_errors[_err]){
										$("#"+_errors[_err]+_uid).show();
									}else{
										$("#internal_error"+_uid).show();
									}
								});
							}
						}
						
                        $("input,textarea",_this).removeAttr("disabled");
						_getCaptcha(_uid);
					},
					error:function(){
						$("#internal_error"+_uid).show();
                        $("input,textarea",_this).removeAttr("disabled");
						_getCaptcha(_uid);
					}
				});
				
				return false;
			});

            function _getCaptcha(_uid){
            		if (img_need[_uid]){
            			return false;
            		}
                    if (_captcha_post){
                        _captcha_post.abort();
                        if (typeof _captcha_count[_uid] == "number"){
	                    	_captcha_count[_uid]++;
	                    }else{
                   			_captcha_count[_uid]=0;
	                    }
                    }else{
                   		_captcha_count[_uid]=0;
                    }
                    img_need[_uid] = true;
                    rotate(_uid);
                    var rnd = Math.floor ( Math.random ( 777) * 777 );
                    if (_captcha_count[_uid] < 25){
	                    _captcha_post  = $.ajax({
	                            url:"/feedback/",
	                            type:"POST",
	                            data:{'action':'get_captcha','uid':_uid,'rnd':rnd},
	                            success:function(msg){
	                    				img_need[_uid] = false;
	                                    $("head").append('<script type="text/javascript">'+msg+'</script>');
	                            },
	                            error:function(){
	                                    window.setTimeout(function(){	
	                    						img_need[_uid] = false;
	                                            _getCaptcha(_uid);
	                                    },300);
	                            }
	                    });
                    }else{
                    	img_need[_uid] = false;
                    }
            }
            _getCaptcha(_uid);
		}
	});
}


$(document).ready(function(){

	_make_gallery();
		
	_make_feedback();
});