/*var debug=true;*/
var debug=false;
/*lame function - shows alert if global var 'debug' is true*/
function dalert (str)
{
	if(debug){ alert(str) };
}
/*-------------------------------------------------------------*/
function load_footer_preview ()
{
	if (!simple_get('footer_preview_id')){
		/*dalert('nie ma elementu ');*/
		return;
	}
	dalert('odpalenie funkcjona');
	$.ajax({
		url:'/paper/footer_preview_ajax/',
		cache: false, 
		beforeSend: function(){ load_status_content('global_ajax_load_status_div_id','footer_preview_id')  },
		success: function(html){ 
			$('#footer_preview_id').html(html);
			/*dalert('Wynik ' + html);*/
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$('#footer_preview_id').html("error...");
			dalert('Wystąpił błąd w trakcie przesyłania danych');
		}
	})


}

function load_redaction_persons_preview ()
{
	/*dalert('odpalenie funkcjona');*/
	$.ajax({
		url:'/users/redaction_persons_preview_ajax/',
		cache: false, 
		beforeSend: function(){ load_status_content('global_ajax_load_status_div_id','redaction_persons_preview_id')  },
		success: function(html){ 
			$('#redaction_persons_preview_id').html(html);
			/*setting facebox*/
			set_facebox();
			/*dalert('Wynik ' + html);*/
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$('#redaction_persons_preview_id').html("error...");
			dalert('Wystąpił błąd w trakcie przesyłania danych');
		}
	})


}

function load_status_content (source_elem_id, target_elem_id)
{
	dalert('load ajax status');
	$('#'+target_elem_id).html($('#' + source_elem_id).html());
}

function check_login(url_check)
{
    /*toggle_checker()*/
    $('#checker').click(function() {
	var log_val = $('#id_login').val();
	$.ajax({
            type:'GET',
            url:url_check,
            datatype:'html',
            global: false,
            data:{login:log_val},
            beforeSend: function(request){
		$("#checker").hide();
                $('#check_ico').hide();
		$("#ajax_progress").show();
	    },
            success: function(data){
                modal_ajax_success()
                $("#ajax_progress").hide();
                $("#checker").show();
                /*$('#check_ico').text(data);*/
                load_django_js_message(data)
                $('#check_ico').show();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $("#ajax_progress").hide();
                $("#checker").show();
                $('#check_ico').text("Nieznany błąd..");
            }
            })
    });
}

function reload_captcha(url_reload)
{
    $("#captcha_reloader").click( function() {
	$.ajax({
            type:'GET',
            url:url_reload,
            datatype:'html',
            global: false,
            data:{},
            beforeSend: function(request){
		$("#captcha_reloader").hide();
		$('.captcha').fadeOut('fast');
		$("#ajax_progress1").show();
	    },
            success: function(data){
	    /*modal_ajax_success()*/
		$('.captcha').fadeOut('fast', function(){
			$(this).attr('src', '').attr('src', data).fadeIn('fast');
			/*$(this).attr('src', data).fadeIn();*/
			$("#ajax_progress1").hide();
			$("#captcha_reloader").show();
		})
/*=======*/
/*modal_ajax_success()*/
	/*$("#ajax_progress1").hide();*/
	/*$("#captcha_reloader").show();*/
	/*$('.captcha').attr('src', data);*/
	/*$('.captcha').fadeIn("fast");*/
	/*>>>>>>> other*/
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $("#ajax_progress1").hide();
                $("#captcha_reloader").show();
            }
        })
    });
}
/*add person to redaction views functions*/
function add_to_redaction_has_no_account(url){
	    $("input[name='has_account']").click(function() {
		   
		     $.ajax({
					type:'POST',
					url: url,
					datatype:'html',
                    global: false,
		    data: {},
                    beforeSend: modal_ajax_before_send,
                    success: function(data){
                        $('#facebox .content').html(data);  
                        modal_ajax_success()
                    },
                    error: modal_ajax_error 
					})
		});     
}

function add_to_redaction_has_account(url){
$("input[name='has_account']").click(function() {
	$.ajax({
		type:'POST',
		url: url,
		datatype:'html',
		global: false,
		data: {},
		success: function(data){
			$('#facebox .content').html(data);
			modal_ajax_success()
		},
		beforeSend: modal_ajax_before_send,
		error: modal_ajax_error 
	})
});

}

function toggle_checker(){
	$('#id_login').keyup(function(){
		if($('#id_login').val().length > 0){
			$("#checker").show();
			$('#check_ico').hide();
		}else{
			$("#checker").hide();
			$('#check_ico').hide();
		}
	})
}

function add_to_redaction_check_login(url){
	toggle_checker()
	$('#checker').click(function() {
		$("#checker").hide();
		var log_val = $('#id_login').val();
		$.ajax({
			type:'GET',
			url: url,
			datatype:'html',
			data:{login:log_val},
			global: false,
			success: function(data){
			/*$("#checker").show();*/
				$('#check_ico').show().text(data);
				modal_ajax_success()
			},
			beforeSend: modal_ajax_before_send,
				error: modal_ajax_error 
		});
	});
}
/*add person to redaction views functions*/


/*set mockup family views functions*/
function set_mockup_family(url){
	$("input[name='set_mockup']").click(function() {
		$.ajax({
			type:'POST',
			url: url,
			datatype:'html',
			global: false,
			data: {},
			beforeSend: modal_ajax_before_send,
			success: function(data){
				$('#facebox .content').html(data);  
				modal_ajax_success()
			},
			error: modal_ajax_error 
		})
	});     
}

function my_mockup_family(url){
	$("input[name='set_mockup']").click(function() {
		$.ajax({
			type:'POST',
			url: url,
			datatype:'html',
			global: false,
			data: {},
			success: function(data){
				$('#facebox .content').html(data);
				modal_ajax_success()
			},
			beforeSend: modal_ajax_before_send,
			error: modal_ajax_error 
		})
	});

}
/*set mockup family views functions*/

/* dodawanie i usuwanie z ulubionych */
function add_favourite(url,id){
	$.ajax({
		url:url,
		cache: false,
        success: function(html){
            html="<img src=\"/static/img/ikona_dodajDoUlubionych_2.gif\" alt=\"\" />"
			$('#favourite_'+id).html(html);
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$('#favourite_'+id).html("error...");
        }
	})
}

function del_favourite(url,id){
	$.ajax({
		url:url,
		cache: false,
        success: function(html){
            /*html="<img src=\"/static/img/ikona_dodajDoUlubionych.gif\" alt=\"\" />"
			$('#favourite_'+id).html(html);*/
            $('#favourite_'+id).hide()
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			$('#favourite').html("error...");
        }
	})
}

/* dodawanie i usuwanie z ulubionych */
function clone_loader (loading)
{
	return loading.clone().removeAttr('id')
}

function global_before_send (element, loading)
{
	position = element.position()
	element.hide().after(loading.show().css({"width": element.width(), "height": element.height(), "position": "absolute" , "top": position.top , "left": position.left}))

}

function post_view_ajax_init (urls)
{
	$('#delete_post').click(function(){
			moderate_post(urls.delete_post_url, this ); ;
			return false;
			})
	$("#ban_user_link").click(function(){
			moderate_forum_user(urls.delete_user_url, this );
			return false;
			})
}

function moderate_post(url, link)
{
	element = $(link)
	loading = clone_loader($("#ajax_loading_img"))
	after = '<img alt="" src="/static/img/pl/buttonPostDeleted_14.gif"/>'

	$.ajax({
		url:url,
        /*cache: false,*/
        beforeSend: function(){
			global_before_send(element, loading)
        },
        success: function(data){
            loading.replaceWith(after)
		}
		/*error: function (XMLHttpRequest, textStatus, errorThrown) {*/
		/*alert('blad sie stal')*/
		/*}*/
	})
        
}

function ban_thread (url)
{
	$.ajax({
		url:url,
        /*cache: false,*/
        success: function(data){
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert('blad sie stal')
        }
	})

}


function moderate_forum_user(url, link)
{
	element = $(link)
	loading = clone_loader($("#ajax_loading_img"))

	$.ajax({
		url:url,
        /*cache: false,*/
        beforeSend: function(){
			global_before_send(element, loading)
        },
        success: function(data){
			$(link).remove()
			loading.remove()
		}
		/*error: function (XMLHttpRequest, textStatus, errorThrown) {*/
		/*alert('blad sie stal')*/
		/*}*/
	})

}

function moderate_delete_report(url, link)
{
	element = $(link)
	loading = clone_loader($("#ajax_loading_img"))

	$.ajax({
		url:url,
        /*cache: false,*/
        beforeSend: function(){
			global_before_send(element, loading)
        },
        success: function(data){
           $(link).remove()
		   loading.remove()
		}
		/*error: function (XMLHttpRequest, textStatus, errorThrown) {*/
		/**//*alert('blad sie stal')*/
		/*}*/
	})

}

function add_favourite_from_flash(url,id){
            $.ajax({
                url:url,
                cache: false,
                success: function(html){
                    if (html=='added to favourite issues'){
                        $("#flex_content_div_id").get(0).showMessage('dodano');
                    }
                    else{
                       $("#flex_content_div_id").get(0).showMessage('zaloguj się');
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                   $("#flex_content_div_id").get(0).showMessage('wystąpił błąd','błąd');
                }
            })
        }

        
function init_country_change (uri)
{
	$("select[name=country]").change(function (event) {
		$.ajax({
			type:'POST',
			url: uri,
			datatype:'html',
			data: {'country': this.value },
			success: function(data){
				$("#id_state").replaceWith(data)
			}
		})
	})
}

function init_qmam_lang_change (uri) 
{
	$("select[name=default_lang]").change(function (event) {
		$.ajax({
			type:'POST',
			url: uri,
			datatype:'html',
			data: {'default_lang': this.value },
			success: function(data){
				$('#id_category').replaceWith(data);
			}
		});
	});
}

