
/******************************************************************************
 *
 *	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/
 *
 ******************************************************************************/

/**
* clone array/object without reference
*
*/
var clone = function(obj){
	var newObj = (obj instanceof Array) ? [] : {};
	for (i in obj) {
		if (obj[i] && typeof obj[i] == 'object') {
			newObj[i] = clone(obj[i]);
	    }
	    else {
			newObj[i] = obj[i]
		}
	}
	return newObj;
};

$(document).ready(function(){
	$('#start_import').click(function(){
		$('#import_start_nav').fadeOut(function(){
			$('#eil_statistic').fadeIn();
			importExport.start();
		});
	});
});

var eil_rowHandler = function(row){
	var eq = row ? ':eq('+row+')' : '';
	$('input[name^=rows]'+eq+'').each(function(){
		if ( $(this).is(':checked') )
		{
			$(this).closest('tr.body').removeClass('disabled no_hover').attr('title', '');
		}
		else
		{
			$(this).closest('tr.body').addClass('disabled no_hover').attr('title', eil_listing_wont_imported);
		}
	});
}

var eil_colHandler = function(){
	$('input[name^=cols]').each(function(){
		var index = $(this).closest('tr.col-checkbox').find('input').index(this);
		index = (index * 2) + 2;

		if ( $(this).is(':checked') )
		{
			$('table.import tr td:nth-child('+index+')').removeClass('disabled no_hover');
			$(this).closest('td').attr('title', '');
			$('table.import tr.header td:nth-child('+index+')').attr('title', '');
		}
		else
		{
			$(this).attr('checked', false);
			$('table.import tr td:nth-child('+index+')').addClass('disabled no_hover');
			$(this).closest('td').attr('title', eil_column_wont_imported);
			$('table.import tr.header td:nth-child('+index+')').attr('title', eil_column_wont_imported);
			$('table.import tr.header td:nth-child('+index+') select option').attr('selected', false);
		}
	});
}

var eil_typeHandler = function(key, element){
	if ( key )
	{
		$('select[name='+element+'] option:first').text(lang['loading']);
		xajax_fetchOptions(key, element, 1);
	}
	else
	{
		var option = '<option value="">'+eil_select_listing_type+'</option>';
		$('select[name='+element+']').html(option);
	}
}

var eil_status = function(value){
	value = value ? value : $('select[name=import_status]').val();
	
//	if ( value == 'approval' ) {
//		$('#plans_tr').hide();
//	}
//	else {
//		$('#plans_tr').show();
//	}
}

var importExportClass = 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.plans = new Array();
		
	this.import = function(index){
	    /* import request */
	    request = $.getJSON("../plugins/export_import/import.php", {index: index}, function(response){
			if ( index == 0 )
			{
				var runs = Math.ceil(response['count']/self.config['per_run']);
				item_width = Math.ceil(100/runs);
				percent_value = Math.ceil(100/runs);
			}
			
			index = response['from'];
			
			width += item_width;
			width = width > 100 ? 100 : width;
			percent = response['from'] > response['count'] ? 100 : percent + percent_value;
			
			$('#processing').css('width', width+'%');
			$('#loading_percent').html(percent+'%');
			
			if ( response['count'] > index )
			{
				var from = response['from'] + 1;
				var to = response['to'] + 1;
				to = response['count'] < to ? response['count'] : to;
				var import_current = from+'-'+to;
				$('#importing').html(import_current);
			
				self.import(index);
			}
			else
			{
				$('#eil_statistic').animate({opacity: 0});
				printMessage('notice', self.phrases['completed'].replace('{count}', response['count']));
				setTimeout(function(){
					if ( rlConfig['mod_rewrite'] ) {
						location.href=rlConfig['seo_url']+rlPageInfo['path']+'.html?reset';
					}
					else {
						location.href=rlConfig['seo_url']+'?page='+rlPageInfo['path']+'&reset';
					}
				}, 2000);
			}
		});
	}
	
	this.stop = function(){
		request.abort();
	}
	
	this.start = function(){
		self.import(0);
	}
	
	this.modeSwitcher = function(){
		$('a.eil_fullscreen').unbind('click').click(function(){
			var date = new Date();
			var button = '<a class="button eil_default" title="" href="javascript:void(0)"><span>'+ importExport.phrases['eil_default_view'] +'</span></a>';
			$('body > *:visible').addClass('tmp-hidden').hide();
			$('body').append('<div class="eil_fullscreen_area hide"><div class="eil_header"><table class="sTable"><tr><td class="lalign"><h1>'+ importExport.phrases['eil_import_table'] +'</h1></td><td class="ralign">'+ button +'</td></tr></table></div><div class="eil_body"></div><div id="footer" class="eil_footer"><table class="sTable"><tr><td class="lalign"><img alt="" src="'+ rlConfig['tpl_base'] +'img/logo.png" /></td><td class="ralign" valign="top"><span>&copy; '+date.getFullYear()+', '+importExport.phrases['powered_by']+' </span><a title="'+importExport.phrases['powered_by']+' '+importExport.phrases['copy_rights']+'" href="'+rlConfig['seo_url']+'">'+ importExport.phrases['copy_rights'] +'</a></td></tr></table></div></div>');
			$('body > div.eil_fullscreen_area > div.eil_body').append($('div.iel_table').parent());
			$('body > div.eil_fullscreen_area').fadeIn();
			
			$('a.eil_default').unbind('click').click(function(){
				$('body > div.eil_fullscreen_area').fadeOut(function(){
					$('#controller_area').prepend($('body > div.eil_fullscreen_area > div.eil_body').children(':first'));
					$('body > *.tmp-hidden').show().removeClass('tmp-hidden');
					$(this).remove();
				});
			});
		});
	};

	this.plansHandler = function(){
		var plans = clone(this.plans);
		
		/* calculate plans using */
		$('table.import td.row-plan select').each(function(){
			var id = parseInt($(this).val());
			
			if ( id ) {
				var plan = plans[id];
				
				/* limited plans */
				if ( (plan['Limit'] > 0) || (plan['Package'] == 'package' && !plan['Advanced_mode']) ) {
					plan['Listings_remains'] -= 1;
				}
				else if ( plan['Package'] == 'package' && plan['Advanced_mode'] ) {
					if ( $(this).next().find('input:checked').val() == 'standard' ) {
						plan['Standard_remains'] -= 1;
					}
					else if ( $(this).next().find('input:checked').val() == 'featured' ) {
						plan['Featured_remains'] -= 1;
					}
				}
			}
		});
		
		/* update plan details in dropdowns */
		$('table.import td.row-plan select').each(function(){
			var parent = this;
			
			$(this).find('option').each(function(){
				var id = parseInt($(this).val());
				
				if ( id ) {
					var plan = plans[id];
					
					if ( plan['Limit'] > 0 || (plan['Package'] == 'package' && !plan['Advanced_mode']) ) {
						var name = plan['name'] +' - '+ self.phrases[plan['Limit'] > 0 ? 'eil_free_listing' : 'eil_prepaid_package'];
						var limit_field = plan['Limit'] > 0 ? 'Limit' : 'Listing_number';
						
						if ( $(this).is(':selected') ) {
							$(this).html(name);
						}
						else {
							if ( plan[limit_field] > 0 ) {
								if ( plan['Listings_remains'] == 0 ) {
									$(this).html(name + ' ('+self.phrases['used_up']+')');
									$(this).attr('disabled', true);
								}
								else {
									$(this).html(name + ' ('+self.phrases['number_left'].replace('{number}', plan['Listings_remains'])+')');
									$(this).attr('disabled', false);
								}
							}
							else {
								$(this).html(name + ' ('+self.phrases['unlimited']+')');
							}
						}
					}
					else if ( plan['Package'] == 'package' ) {
						var name = plan['name'] +' - '+ self.phrases['eil_prepaid_package'];
						
						$(this).html(name);
						$(parent).next().hide();
						
						if ( plan['Advanced_mode'] ) {
							var area = $(parent).next();
							
							if ( $(this).is(':selected') ) {
								area.show();
								
								if ( $(area).find('input:not(:checked)').length == 2 ) {
									// standard info
									var standard_info = '';
									
									area.find('li:first input').attr('disabled', false);
									if ( plan['Standard_listings'] == 0 ) {
										standard_info = self.phrases['unlimited'];
									}
									else {
										if ( plan['Standard_remains'] > 0 ) {
											standard_info = self.phrases['number_left'].replace('{number}', plan['Standard_remains']);
										}
										else {
											standard_info = self.phrases['used_up'];
											area.find('li:first input').attr('disabled', true);
										}
									}
									area.find('li:first span').html('('+standard_info+')');
									
									// featured
									var featured_info = '';
									
									area.find('li:last input').attr('disabled', false);
									if ( plan['Featured_listings'] == 0 ) {
										featured_info = self.phrases['unlimited'];
									}
									else {
										if ( plan['Featured_remains'] > 0 ) {
											featured_info = self.phrases['number_left'].replace('{number}', plan['Featured_remains']);
										}
										else {
											featured_info = self.phrases['used_up'];
											area.find('li:last input').attr('disabled', true);
										}
									}
									area.find('li:last span').html('('+featured_info+')');
								}
								else if ( $(area).find('input:checked').length == 1 ) {
									area.find('span').html('');
								}
							}
							else {
								$(area).find('input').attr('checked', false);
							}
						}
					}
					else {
						$(this).html(plan['name'] +' - '+ self.phrases['eil_free_listing']);
					}
				}
			});
		});
	}
}

var importExport = new importExportClass();