
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: LIB.JS
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

$(document).ready(function(){
	$('input[name=site_accounts]').click(function(){
		$('input.accounts').attr('checked', $(this).is(':checked'));
	});
	$('input.accounts').click(function(){
		$('input[name=site_accounts]').attr('checked', $('input.accounts:checked').length > 0 ? true : false);
	});
	
	$('input#confirm').click(function(){
		flynax.confirm(massmailer.phrases['send_confirm'], massmailer.save, null);
	});
	
	$('input#start_send').click(function(){
		flynax.confirm(massmailer.phrases['send_confirm'], massmailer.start, null);
	});
});

var massmailerClass = function(){
	var self = this;
	var item_width = width = percent = percent_value = 0;
	var window = false;
	var request;
	
	this.phrases = new Array();
	this.config = new Array();
	
	this.save = function(){
		$('input#confirm').val(lang['loading']);
		
		var account_types = new Array();
		$('input.accounts:checked').each(function(){
			account_types.push($(this).val());
		});
		
		xajax_massmailerSave(
			self.config['id'],
			$('input[name=from_mail]').val(),
			$('input[name=status]').val(),
			$('input[name=subject]').val(),
			CKEDITOR.instances['body'].getData(),
			$('input[name=newsletters_accounts]').is(':checked') ? 1 : 0,
			account_types.join(','),
			$('input[name=contact_us]').is(':checked') ? 1 : 0
		);
	}
		
	this.send = function(id, index){
		/* show window */
		if ( index == 0 )
		{
			if ( !window )
			{
				window = new Ext.Window({
					applyTo: 'statistic',
					layout: 'fit',
					width: 447,
					height: 140,
					closeAction: 'hide',
					plain: true
			    });
			    
			    window.addListener('hide', function(){
	            	self.stop();
	            });
			}
		    
			window.show();
		}
		
	    /* send request */
	    request = $.getJSON("../plugins/massmailer_newsletter/admin/send.php", {id: id, index: index}, function(response){
			if ( index == 0 )
			{
				item_width = Math.ceil(362/response['count']);
				percent_value = Math.ceil(100/response['count']);
			}
			
			index++;
			
			width += item_width;
			percent = response['count'] == index ? 100 : percent + percent_value;
			
			$('#total').html(response['count']);
			$('#sent').html(index);
			$('#processing').css('width', width+'px');
			$('#loading_percent').html(percent+'%');
			$('#sending').html(response['data']['Mail']);
			
			if ( response['count'] > index )
			{
				self.send(id, index);
			}
			else
			{
				printMessage('notice', self.phrases['completed'].replace('{count}', response['count']));
				setTimeout(function(){
					window.hide();
					self.clear();
				}, 4000);
			}
		});
	}
	
	this.stop = function(){
		request.abort();
	}
	
	this.start = function(){
		self.send(self.config['id'], 0);
	}
	
	this.clear = function(){
		item_width = width = percent = percent_value = 0;
		$('#total').html(0);
		$('#sent').html(0);
		$('#processing').css('width', '0px');
		$('#loading_percent').html('0%');
		$('#sending').html('-');
	}
}

var massmailer = new massmailerClass();