var current_system = '';
var save_mail = '';
var save_phone = '';

//return defaut style to popup window and reset content
function closePopupWindow()
{
	current_system = '';
	$('#opacity_window').css('display', 'none');
	$('#popup_window').css('display', 'none');
	$('body').css('overflow', 'visible');
	$('#system_selector').html('');
	$('#proceed_button').html('');
	$('#obtain_form').html('');
	$('#display_errors').html('');
	return false;
}

$(document).ready(function(){
	//Create class for connecting to store API
	online_rpc = new rpc.ServiceProxy('/stores/store_api.php', {
	asynchronous: true,   //default: true
	sanitize: true,       //default: true
	protocol: 'JSON-RPC', //default: JSON-RPC
	methods: ['getPopup', 'getForm']
	});

	$('a.sale_button').click(function(){
		//get button object
		getButton = $(this);
		button_name = getButton.attr('name').split('::');
		
		if(button_name[1] !== undefined)
		{
			_gaq.push(['_trackPageview','/button/buy-'+button_name[1]]);
		}
		else
		{
			_gaq.push(['_trackPageview','/button/buy-'+button_name[0]]);
		}
		
		online_rpc.getPopup({
								local_params: {'i' : 0},
								params: {'product_url' : getButton.attr('name')},
								onSuccess:function(ret, params)
								{
									if(ret['action'] == 'form')
									{
										$('#system_selector').html(ret['content']);
										$('#proceed_button').html(ret['button']);
										
										$('#opacity_window').width($(document).width());
										$('#opacity_window').height($(document).height());
										$('body').css('overflow', 'hidden');
									
										$('#opacity_window').css('display', 'block');
									
										$('#popup_window').css('margin-left', ($(document).width()-504)/2);
										$('#popup_window').css('margin-right', ($(document).width()-504)/2);
											
										$('#popup_window').css('display', 'block');
										
									}
									else
									{
										window.location = getButton.attr('href');
									}
								},
								onException:function(e)
								{
									window.location = getButton.attr('href');
								}
							});
		return false;
	});
	
	//Close popup by click on picture
	$('#close_popup').live('click', function(e){
		closePopupWindow()
	});
	
	//Send form on click buy button in popup. This action for forms where we may verify email
	$('#issue_pay').live('click', function(e){

		reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

		address = $('#user_email').val();
		phone = $('#user_phone').val();
		
		paySystemName = $(this).attr('name');

		get_errors = '';

		if(address == '')
		{
			get_errors = get_errors+'Для продолжения необходимо ввести e-mail!<br>';
		}
		else if(reg.test(address) == false)
		{
			get_errors = get_errors+'Не корректный e-mail!<br>';
		}
		
		if(paySystemName == 'mobile_pay' && phone == '')
		{
			get_errors = get_errors+'Необходимо ввести номер мобильного телефона!<br>';
		}
		
		if(get_errors == '')
		{
			if( $('#pay_event').length > 0 )
			{
				$('#pay_event').trigger('click');
			}
			$('#pay_form').submit();
			return true;
		}
		
		$('#display_errors').html(get_errors);
		return false;
	});
	
	$('.system_choice').live('click', function(e){
		if( $('input.system_choice:checked').val() !== current_system )
		{
			current_system = $('input.system_choice:checked').val();
			
			$('#display_errors').html('');
			$('#proceed_button').html('');
			$('#obtain_form').html('');			
			
			online_rpc.getForm({
									local_params: {'i' : 0},
									params: {
												'product_url' : $('input#need_program').val(),
												'pay_type' : $('input.system_choice:checked').val()
											},
									onSuccess:function(ret, params)
									{
										if(ret['action'] == 'form')
										{
											$('#obtain_form').html(ret['content']);
											$('#proceed_button').html(ret['button']);
											$('#user_email').val(save_mail);
											$('#user_phone').val(save_phone);
										}
										else
										{
											$('#display_errors').html('К сожалению этот тип платежа не доступен для данного продукта');
										}
									},
									onException:function(e)
									{
										$('#display_errors').html('К сожалению этот тип платежа не доступен для данного продукта');
									}
								});			
		}
	});
	
	//save user mail for use in other forms
	$('input#user_email').live('change', function(e){
		save_mail = $(this).val();
	});
	
	//save user phone for use in other forms
	$('input#user_phone').live('change', function(e){
		save_phone = $(this).val();
	});

});

//Movement for popup window on resizing browser
$(window).resize(function(){
	if($('#opacity_window').css('display') == 'block' || $('#popup_window').css('display') == 'block')
	{
		$('#opacity_window').width($(window).width());
		$('#opacity_window').height($(document).height());
		$('#popup_window').css('margin-left', ($(document).width()-504)/2);
		$('#popup_window').css('margin-right', ($(document).width()-504)/2);
	}
});

//Close popup by press Esc button
$(document).keydown(function(e){
	if(e.which == 27) closePopupWindow();
});

