source=null;

$(document).ready(function() {	
	make_dialogs();
	$.get('/index.php/site/buy/compare_packages/' + new Date().getTime(),function(data){
		$('#compare_dialog').dialog('destroy');
		$('#compare_dialog').replaceWith(data);
		$('#compare_dialog').dialog({
			width:980,
			autoOpen: false,
			modal: true
		});
	});
    /*
    $('#info_dialog').dialog({
        width:980,
        autoOpen: false,
        modal: true,
        height: 300,
        buttons:
        {
            "Zamknij" : function() {
                $('#info_dialog').dialog('close');
            },
            "Zamknij i nie pokazuj więcej" : function() {
                $.get('/index.php/site/webuser/check_info/1/' + new Date().getTime(), function(data) {
                    $('#info_dialog').dialog('close');
                });
            }
        }
    });
    $.get('/index.php/site/webuser/check_info/' + new Date().getTime(), function(data) {
        if (data == 'show') {
            $('#info_dialog').dialog('open');
        }
    });
    $('a.go_info_contact').click(function(ev){
        $.get('/index.php/site/webuser/check_info/1/' + new Date().getTime(), function(data) {
            $('#info_dialog').dialog('close');
            window.location.href = $(ev.target).attr('goto');
        });
    });
    */
});

function make_dialogs(to_open) {
	$('#reg_error').html('');
	$('#reg_error').removeClass('ui-state-error');		
	$('#login_error').html('');
	if (to_open == 'message') {
		message_window(true);
	} else {
		message_window(false);
	}
	if (to_open == 'login') {
		login_window(true);
	} else {
		login_window(false);
	}	
	if (to_open == 'reg') {
		reg_window(true);
	} else {
		reg_window(false);
	}
    upgrade_window();
}

function fb_share(pid) {
	var place = $('#'+pid).parent();
	var user = place.find('h3').html();
	var content = place.next().find('span').html();;
	var title = place.next().find('h2').html();
	var link = $('#feed_data_url').html() + "#" + pid;
	FB.ui(
			   {
			     method: 'feed',
			     name: 'post użytkownika '+ user + ' na forum pzmot',
			     link: link,
			     caption: title,
			     picture: $('#feed_data_logo').html(),
			     description: content			     
			   },
			   function(response) {}
			 );
}

function fb_share_own(pid) {
    var place = $('#' + pid).parent();
    var user = place.find('h3').html();
    var content = place.next().find('span').html();
    ;
    var title = place.next().find('h2').html();
    var link = $('#feed_data_url').html() + "#" + pid;
    FB.ui(
        {
            method: 'feed',
            name: 'post użytkownika ' + user + ' na forum pzmot',
            link: link,
            from: 103063213076367,
            caption: title,
            picture: $('#feed_data_logo').html(),
            description: content
        },
        function(response) {
        }
    );
}


function fb_invite() {
	FB.ui({ method: 'apprequests', message: 'Zastanów się, czy nie potrzebujesz tego produktu' });
}

function fb_reg_needed() {
	make_dialogs('reg');
}

function fb_login() {		
	FB.login(function() {

	}, {scope:'email'});
}

function fb_logout() {
	FB.logout(function(response) {
		  
	});
}

function logout() {
	show_loader_mini_in($('#i_wait'));
	$.get('/index.php/site/webuser/logout',function(data) {
		reload_dialogs(callback_logout);
	});
}

function deauth() {
	$('#reg-dialog').dialog("close");
	//window.location.reload();
}

function attach_actions() {
	$('.login_but').button();
	$('.login_but, .login_link').click(function(){source=this; $( "#login-dialog" ).dialog( "open" );});
	$('.reg_but').button();
	$('.reg_but, .reg_link').click(function(){$( "#reg-dialog" ).dialog( "open" );});	
}

function reset_callbacks() {
	callback_login = function(){};
	callback_reg = function(){};
	callback_account = function(){};
	callback_logout = function(){};	
}

function reload_dialogs(callback) {
	$( "#reg-dialog" ).dialog('destroy');	
	$.get('/index.php/site/webuser/load_data_dialog/'+  new Date().getTime(),function(data){
		$('#reg-dialog').replaceWith(data);					
		$.get('/index.php/site/webuser/show_logged/'+  new Date().getTime(),function(data){
			$('#webuser_wrapper').html(data);			
			attach_actions();			
			if (typeof(callback)=='function') {				
				callback();
			} else {
				make_dialogs();
			}
		});
	});											
}

function show_loader(container)
{
	$(container).html('<div id="ajax_loader"><img src="/img/ajax-loader.gif" alt="" border="" /></div>');
}

function show_loader_mini(container)
{
	$(container).replaceWith('<img src="/img/ajax-loader.gif" height=20 alt="" border="" style="display:inline;"/>');
}

function show_loader_mini_in(container)
{
	$(container).html('<img src="/img/ajax-loader.gif" height=20 alt="" border="" style="display:inline;"/>');
}

function perform_login(callback) {
	show_loader_mini_in($('#i_wait'));
	$.post(
			'/index.php/site/webuser/login',
			{
				'data[web_user][login]':$('#login_top').val(),
				'data[web_user][password]':$('#password_top').val()
			},
		function(data){			
			if (data =='ok') {					
				reload_dialogs(callback);								
			} else {
				if (data=='') data = 'Brak danych logowania';
				$('#message_error').html(data);
				$('#message-dialog').dialog('open');													
			}
	});		
}


function message_window(to_open) {
	$( "#message-dialog" ).dialog({
		autoOpen: to_open,
		modal: true,
		width:600,
		buttons: {
			"OK": function() {
				$('#wait').empty();
				$('#message_error').html('');				
				$( "#message-dialog" ).dialog('close');
			}			
		},
		close: function() {
			$('#i_wait').empty();
			$('#message_error').html('');								
		}		
	});	
}

function login_window(to_open) {
	$( "#login-dialog" ).dialog('destroy');
	$( "#login-dialog" ).dialog({
		autoOpen: to_open,
		modal: true,
		width: 600,
		buttons: {
			"Zaloguj": function() {
				show_loader_mini_in($('#login_error'));
				$.post(
					'/index.php/site/webuser/login',
					{
						'data[web_user][login]':$('#login').val(),
						'data[web_user][password]':$('#password').val()
					},
				function(data){					
					if (data =='ok' || data=='data') {
						$('#wait').empty();
						$('#webstatus').html(data);
						//$( "#login-dialog" ).dialog('close');
						reload_dialogs(callback_login);						
					} else {
						if (data=='') data = 'Brak danych logowania';
						$('#login_error').html(data);
						$('#login_error').addClass('ui-state-error');
						setTimeout(function() {
							$('#login_error').html('');
							$('#login_error').removeClass('ui-state-error');	
						}, 3000 );						
					}
				});
			},
			"Załóż konto" : function() {				
				$('#login_error').hide();
				$('#login_error').html('');
				//$( "#login-dialog" ).dialog('close');
				$( "#reg-dialog" ).dialog('open');
			},
			"Anuluj": function() {				
				$('#login_error').html('');				
				$( "#login-dialog" ).dialog('close');
			}		
		},
		close: function() {
			$('#login_error').html('');								
		}		
	});
}

function hide_buts() {
	var d = $('[aria-labelledby="ui-dialog-title-reg-dialog"]');
	d.find('.ui-dialog-buttonpane').hide();
}

function show_buts() {
	var d = $('[aria-labelledby="ui-dialog-title-reg-dialog"]');
	d.find('.ui-dialog-buttonpane').show();
}

function check_card() {
	var cn = $('#offreg_card').val();
	if (cn < 20000000 && cn >= 10000000) {
		$('#offreg_code_block').show();
		$('#offreg_valid_block').hide();
		$('#offreg_send').show();		
	} else if (cn <= 99999999 && cn >= 20000000){
		$('#offreg_code_block').hide();
		$('#offreg_valid_block').show();
		$('#offreg_send').show();
	} else {
		$('#offreg_code_block').hide();
		$('#offreg_valid_block').hide();
		$('#offreg_send').hide();
	}
}

function reset_pass() {
	$('#pass-dialog').dialog('destroy');
	$('#pass-dialog').dialog({
		autoOpen: false,
		modal: true,		
		width: 800,
		buttons: {
			"Wyślij": function(){
				show_loader_mini_in($('#pass_message'));				
				$.post('/index.php/site/webuser/reset_pass',
					{
					'data[pass_data]':$('#pass_email').val()							
					},
					function(data) {
						$('#pass_message').empty();
						$('#pass-dialog').dialog('close');
						$('#message_error').html(data);
						$('#message-dialog').dialog('open');
					}
				);
			}
		},
		close: function() {
			$('#pass_message').empty();
			$('#pass_email').val("");
		}
	});
	$('#pass-dialog').dialog('open');
}

function upgrade(pid) {
    $.get('/index.php/site/buys/upgrade_prepare/'
        + pid + '/'
        + new Date().getTime(), function(dane) {
            var json = $.evalJSON(dane);
            var data = json.tekst;
            var title = json.stary;
            $("#upgrade-dialog").dialog('option','title', 'Zamień swój '+title+" na");
            $("#upgrade-dialog").html(data);
            $("#up_buts").buttonset();
            $("#up_buts_cancel").button();
            $('#send_reg').button().click(function() {
                upgrade_finish(pid, $('#newpack').text(), true, false);
            });
            $('#upgrade_reg_part').button().click(function() {
                upgrade_finish(pid, $('#newpack').text(), true, true);
            });
            $('#upgrade_noreg').button().click(function() {
                upgrade_finish(pid, $('#newpack').text(), false, false);
            });
            $("#up_buts button").click(function() {
                $(this).siblings('button').hide();
                var pack = $(this).next('span').text();
                $('#newpack').text(pack);
                if ((pack == 61 || $('#oldpack').text() == '55') && $('#upgrade_reg_wrapper').length > 0) {
                    $('#upgrade_reg_wrapper').show();
                    if ($('#oldpack').text() == '55') {
                        if (pack == 61) {
                            $('#upgrade_reg_part').show();
                        } else {
                            $('#upgrade_noreg').show();
                        }
                    }
                } else {
                    upgrade_finish(pid, pack, false, false);
                }
            });
        $("#upgrade-dialog").dialog('open');
        }
    );
}

function upgrade_finish(pid, pack, check, part) {
    var spart = (part) ? '1' : '0';
    var newreg = "";
    if (check) {
        if ($("#upgrade_reg").val() == "") {
            newreg = " ";
        } else {
            newreg = $("#upgrade_reg").val();
        }
    }
    show_loader_mini_in('#upgrade_loader');
    $.post('/index.php/site/buys/upgrade/'
        + pid + '/'
        + pack + '/'
        + spart + '/'
        + new Date().getTime(), {"data[upgrade_reg]":newreg}, function(data) {
            $("#upgrade_loader").empty();
            if (data == 'registration') {
                $("#message_error").html("Zły format nr-u rejestr. (pusty, za krótki albo zawiera spacje");
                $("#message-dialog").dialog('open');
            } else {
                $("#upgrade-dialog").dialog('close');
                window.location.reload();
            }
        }
    );
}

function upgrade_window() {
    $("#upgrade-dialog").dialog({
        autoOpen: false,
        modal: true,
        width: 800,
        close: function() {
            $("#upgrade-dialog").empty();
        },
        buttons: {
            "Anuluj": function() {
                $("#upgrade-dialog").dialog('close');
            }
        }
    });
}

function reg_window(to_open) {
	$('#offreg_valid').datepicker({dateFormat: "yy-mm-dd"});
	if ($('#reg_question').length > 0) {
		$('#offreg_send').button();
		$('#offreg_cancel').button();
		$('#offreg_card').blur(check_card);
		$('#offreg_cancel').click(function(){
			$('#existing_card').val('');
			$("#reg-dialog" ).dialog('close');
			$('#reg_exists').hide();
			$('#reg_question').show();
			$('#offreg_card').val('');
			$('#offreg_valid').val('');
			$('#offreg_postcode').val('');
			$('#offreg_code_block').hide();
			$('#offreg_valid_block').hide();
			$('#offreg_send').hide();			
		});
		$('#offreg_send').click(function(){
			$.post(
					'/index.php/site/webuser/off',
					{
						'data[offreg_data][card]':$('#offreg_card').val(),
						'data[offreg_data][valid]':$('#offreg_valid').val(),
						'data[offreg_data][code]':$('#offreg_code').val(),
						'data[offreg_data][postcode]':$('#offreg_postcode').val()							
					},
				function(data){		
					if (data=='error') {
						$('#existing_card').val('');
						$('#offreg_error').show();							
						setTimeout(function() {
							$('#offreg_error').hide();																							
						}, 3000 );	
					} else {
							var user = $.evalJSON(data);							
							$('#email').val(user.login);
							$('#name').val(user.name);
							$('#last_name').val(user.second_name);						
							$('#street').val(user.street);
							$('#city').val(user.city);
							$('#postcode').val(user.postcode);
							$('#phone').val(user.telephone);
							//$('#province').val(user.province);
							$('#province').val(user.province);
							$('#existing_card').val(user.card_number);
							$('#loc_choice').html("<input type='checkbox' style='display:none' id='loc'/>");
							$('#reg_exists').hide('fade',function(){							
								$('#reg_standard').show('fade');
								show_buts();
							});													
					}
				});					
		});
		$('#reg_question_yes_but').button();
		$('#reg_question_no_but').button();		
		$('#reg_question_yes_but').click(function(){
			$('#reg_question').hide('fade',function(){
				$('#reg_exists').show('fade');
				hide_buts();
			});
		});
		$('#reg_question_no_but').click(function(){
			$('#reg_question').hide('fade',function(){
				$('#reg_standard').show('fade');
				show_buts();
			});
		});
	}
	$('#loc').button();
	$('#loc').click(function(){
		$('#location_data').fadeToggle(500);
		$('.mytoggle').toggleClass('myhidden');
		var state = $('label[for="loc"]').attr('aria-pressed');
		if (state=='false') {
			$('#location').val(1);
		} else {
			$('#location').val(0);
		}
	});	
	//alert($('#reg-dialog').html());
	$( "#reg-dialog" ).dialog({
		autoOpen: to_open,
		modal: true,
		position:'top',
		width: 800,
		buttons: {
			"Zapisz": function() {
				show_loader_mini_in($('#reg_error'));
				$.post(
						'/index.php/site/webuser/login',
						{
							'data[registration_data][email]':$('#email').val(),
							'data[registration_data][password]':$('#r_password').val(),
							'data[registration_data][password2]':$('#password2').val(),
							'data[registration_data][name]':$('#name').val(),
							'data[registration_data][last_name]':$('#last_name').val(),
							'data[registration_data][nickname]':$('#nickname').val(),
							'data[registration_data][street]':$('#street').val(),
							'data[registration_data][city]':$('#city').val(),
							'data[registration_data][postcode]':$('#postcode').val(),
							'data[registration_data][phone]':$('#phone').val(),
							'data[registration_data][province]':$('#province').val(),
							'data[registration_data][location]':!($('#loc').attr('checked')),
							'data[registration_data][location2]':'false',
							'data[registration_data][existing_card]':$('#existing_card').val()
						},
					function(data){		
						$('#reg_error').empty();
						if (data =='ok') {							
							$( "#reg-dialog" ).dialog('close');
							if ($('#reg_question').length > 0) {
								$('#reg_exists').hide();
								$('#reg_standard').hide();
								$('#reg_question').show();
								hide_buts();
							}
							reload_dialogs(callback_reg);							
						} else {
							if (data=='') data = 'Brak danych rejestracji';
							$('#reg_error').addClass('ui-state-error');
							$('#reg_error').html(data);							
							setTimeout(function() {
								$('#reg_error').html('');
								$('#reg_error').removeClass('ui-state-error');																
							}, 3000 );						
						}
					});
			},
			"Anuluj": function() {
				$('#existing_card').val('');
				$( "#reg-dialog" ).dialog('close');			
				if ($('#reg_question').length > 0) {
					$('#reg_exists').hide();
					$('#reg_standard').hide();
					$('#reg_question').show();
					$('#offreg_card').val('');
					$('#offreg_valid').val('');
					$('#offreg_postcode').val('');
					$('#offreg_code_block').hide();
					$('#offreg_valid_block').hide();
					$('#offreg_send').hide();					
					hide_buts();
				}
			}			
		},
		close: function() {
			$('#existing_card').val('');
			if ($('#reg_question').length > 0) {
				$('#reg_exists').hide();
				$('#reg_standard').hide();
				$('#reg_question').show();
				$('#offreg_card').val('');
				$('#offreg_valid').val('');
				$('#offreg_postcode').val('');
				$('#offreg_code_block').hide();
				$('#offreg_valid_block').hide();
				$('#offreg_send').hide();				
				hide_buts();
			}	
		},
		open: function() {
			$( "#login-dialog" ).dialog('close');
		}
	});
	if ($('#reg_question').length > 0) {
		hide_buts();
	}
}

function after_logout() {
	if ($('#package_id').length>0 && $('#package_id').html() != '') {
		$('#mindate').html('+1');
		$('#maybuy').html('');		
		test();
		package_actions();
		check_purchase_status($('#person_id').html());
		make_dialogs();
	} else {
		window.location.reload();
	}
}

function after_login() {
		$.get('/site/webuser/get_id/' +  new Date().getTime(),function(data){
			check_purchase_status(data);
			if ($('#package_id').html() != '') {
				$('#person_id').html(data);
				$.get('/site/webuser/get_validity/' +  new Date().getTime(),function(data){					
					$('#mindate').html(data);
					$.get('/site/webuser/is_member/' +  new Date().getTime(),function(data){
						$('#maybuy').html(data);
						$.get('/site/webuser/purchase_note/' +  new Date().getTime(),function(data){
							prepare_confirm_window(data,purchase);
							test();
							package_actions();
							make_dialogs();
						});		
					});
				});
			}			
		});				
}

function package_actions() {
	var dd = $('#mindate').html();
	$( "#valid_since" ).datepicker('destroy');
	$( "#valid_since" ).datepicker({
		minDate: dd, onSelect:function(){
			if ($('#valid_until').length > 0) {
				var mydate = new Date($.datepicker.parseDate('yy-mm-dd',$('#valid_since').val()));
				mydate.setMilliseconds(0);
				mydate.setSeconds(0);
				mydate.setMinutes(0);
				mydate.setHours(0);				
				mydate.setTime(mydate.getTime() + 10*3600*24*1000);
				$( "#valid_until" ).datepicker('option','minDate',mydate);
				datediff();
			}
		}		
	});
	$( "#valid_since" ).datepicker( "option", "dateFormat", "yy-mm-dd");

	$( "#valid_until" ).datepicker('destroy');
	if ($('#valid_until').length > 0) {
		$( "#valid_until" ).datepicker({minDate: +11, onSelect:function(){
			var mydate = new Date($.datepicker.parseDate('yy-mm-dd',$('#valid_until').val()));
			mydate.setMilliseconds(0);
			mydate.setSeconds(0);
			mydate.setMinutes(0);
			mydate.setHours(0);
			mydate.setTime(mydate.getTime() - 10*3600*24*1000);
			$( "#valid_since" ).datepicker('option','maxDate',mydate);
			datediff();
		}});
	}
	$( "#valid_until" ).datepicker( "option", "dateFormat", "yy-mm-dd");
	
	function datediff() {
		if ($('#valid_until').val().length > 0 && $('#valid_since').val().length > 0) {
			d1 = new Date($.datepicker.parseDate('yy-mm-dd',$('#valid_since').val())).getTime();
			d2 = new Date($.datepicker.parseDate('yy-mm-dd',$('#valid_until').val())).getTime();
			var d = (d2-d1)/1000/3600/24;
			$( "#validity" ).html( Math.round(d).toFixed(0) );
			$('#valid_wrapper').show();
			var ap = $( "#validity" ).html()*$('#hidden_price').html();
			$('#actual_price').html(ap.toFixed(2));
			$('#1day').hide();
		} else {
			$( "#validity" ).html('');
			$('#actual_price').html($('#actual_price').html());
			$('#1day').show();
			$('#valid_wrapper').hide();
		}
	}
}

function test() {	
	if ($('#package_id').html()!='') {	
		if ($('#webstatus').html() == 'ok') {
			if ($('#maybuy').html()=='yes') {
				$('#kupuj').show();
				$('#membership_required').hide();
			} else {
				$('#kupuj').hide();
				$('#membership_required').show();				
			}
			$('#data_required').hide();
			$('#login_required').hide();
		} else if ($('#webstatus').html() == 'data'){
			$('#kupuj').hide();
			$('#data_required').show();
			$('#login_required').hide();
			$('#membership_required').hide();
		} else {
			$('#kupuj').hide();
			$('#data_required').hide();
			$('#login_required').show();
			$('#membership_required').hide();
		}		
	}		
}

function check_purchase_status(pid) {
	$.get('/index.php/site/webuser/had_package/'+ new Date().getTime(),function(data){		
		if (data=='yes') {
			$('#white_menu').html('<a href="/index.php/site/buys/purchases_list/'+pid+'.html" style="color:white">Moje assistance</a>');
		} else {
			$('#white_menu').html('<a href="/index.php/site/buy.html" style="color:white">Kup assistance</a>');
		}				
	});	
}

function show_package(data) {
    var url = '/site/buy/get_packages_html/';
    if ($('#eurorabat').length > 0) url = '/site/buy/get_packages_html_special/';
	$('div#packages_wrapper').html(data);
	$('.block').corner();
	$('#kupuj').button();
	$('#showall').button();
	$('#owu').button();
	$('#owu').click(function(){
		window.open('/files/File/home-assistance.pdf','Home_Assistance');		
		window.open('/files/WU2011A.pdf','Road_Assistance');
	});
	if ($('#connected_to')) {
		$('#connected_to').change(function(){
			$('#vehicle_reg_wrapper').toggle();
            $('#vehicle_extra_reg_wrapper').toggle();
		});
	}											
	$('#showall').click(function(){
		show_loader_mini($(this));
		$.get(url +  new Date().getTime(),function(data){
			$('div#packages_wrapper').children().hide('fade',1000,function(){
				show_all(data);
			});														
		});
	});	
	$.get('/site/webuser/purchase_note/' +  new Date().getTime(),function(data){
		prepare_confirm_window(data,purchase);
		$('#kupuj').click(confirm_window);
	});			
}

function show_all(data) {
    var url = '/site/buy/get_package_html/';
    if ($('#eurorabat').length>0) url = '/site/buy/get_package_html_special/';
	$('div#packages_wrapper').html(data);
	$('.packages_block, .packages_block_extra').click( function() {	
		var clicked = $(this);
		show_loader(clicked);
		var id = clicked.attr('id').substr(1);
		$.get(
			url +  id +'/' + new Date().getTime(),
			function(data){	
				var ile = clicked.siblings().length;
				clicked.siblings().each(function(idx,el) {
					if (idx == ile - 1) {
						$(el).animate({opacity:0}, 1000, function(){
							$('#package_id').html(id);
							show_package(data);
							$('div#packages_wrapper').children().show('fade',1000);
						});
					} else {
						$(el).animate({opacity:0}, 1000);
					}
				});
			});
	});
	
	$('.block').corner();
	$('div#packages_wrapper').children().fadeToggle(2000);	
}

function purchase() {
	var ret = {};
	ret.package_id = $('#package_id').html();
    ret.eurorabat = $('#eurorabat').length;
	ret.person_id = $('#person_id').html(),
	ret.valid_since = $('#valid_since').val();
	ret.connected_to = ($('#connected_to').length > 0) ? $('#connected_to').val() : null;
    if ($('#vehicle_reg').length > 0 && $('#vehicle_reg').val().length > 0) {
        ret.vehicle_reg = $('#vehicle_reg').val();
    } else if ($('#vehicle_extra_reg').length > 0 && $('#vehicle_extra_reg').val().length > 0) {
        ret.vehicle_reg = $('#vehicle_extra_reg').val();
    } else {
        ret.vehicle_reg = null;
    }
	ret.validity = ($('#validity').length > 0) ? $('#validity').html() : null;	
	$.post('/index.php/site/buy/purchase/' +  new Date().getTime(),{data:{dane:ret}},function(data){
		if (data == 'ok') {
			FB.getLoginStatus(function(response) {					
				  if (response.session) {
					  FB.ui({
						     method: 'feed',
						     name: "pakiety członkowskie pzmot",
						     link: $('#feed_data_url').html(),
						     caption: $('#feed_data_user').html() + " kupił(a) "+$('#feed_data_purchase').html()+" za "+ $('#actual_price').html() + " zł w PZMOT",
						     message: "",
						     picture: $('#feed_data_logo').html(),
						     description: 'pzmot zapewnia ochronę assistance wszystkim swoim członkom: pomoc drogową i pomoc techniczną w domu lub mieszkaniu'
					  },
					  function(response) {
						  $('div#packages_wrapper').hide('fade',1000,function(){
								window.location.href='/index.php/site/buys/purchases_list/'+$('#person_id').html();
						  });	
					  });
				  } else {
					  $('div#packages_wrapper').hide('fade',1000,function(){
							window.location.href='/index.php/site/buys/purchases_list/'+$('#person_id').html();
					});	
				  }
			});
			
		} else {
			$('#message_error').html(data);
			$('#message-dialog').dialog('open');
		}
	});
}

function forum_login(nologin) {
		if ($(source).is('.add_new_post') || nologin) {
			if ($('#myposttitle').val()=="" || $('#mytxtarea').val()=="") {
				$('#add_post').submit();
			} else {
			FB.getLoginStatus(function(response) {					
				  if (response.authResponse) {
					  $('login-dialog').dialog('close');
					  FB.ui({
						     method: 'feed',
						     name: $('#myposttitle').val(),
						     link: $('#feed_data_url').html(),
						     caption: 'post użytkownika ' + $('#feed_data_user').html() + ' na forum pzmot',
						     message: $('#mytxtarea').val(),
						     picture: $('#feed_data_logo').html(),
						     description: 'pzmot zaprasza na forum - błyskawiczna rejestracja dla użytkowników Facebooka'
					  },
					  function(response) {
						  $('#add_post').submit();
					  }
					);				    
				  } else {
					  $('#add_post').submit();
				  }
				});
			}
		} else {
			window.location.reload();
		}
}

function forum_logout() {
	if ($('#return_url').length > 0) {
		window.location.href = $('#return_url').attr('href');
	} else {
		window.location.reload();
	}
}

function prepare_confirm_window(tekst,yes) {
	$('#dialog_confirm').dialog('destroy');
	$('#confirm_text').html(tekst);
	$("#dialog_confirm" ).dialog({
		resizable: false,
		autoOpen: false,
		width:600,
		modal: true,
		buttons: {
			"Potwierdź": function() {
				$( this ).dialog( "close" );
				yes();
			},
			"Anuluj": function() {
				$( this ).dialog( "close" );
			}
		}
	});	
}

function confirm_window() {	
	$('#dialog_confirm').dialog('open');
}

