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

var isLocalStorageAvailable = function () {
    try {
        return 'localStorage' in window && window['localStorage'] !== null;
    } catch (e) {
        return false;
    }
}

var rvAddListing = function(listing) {
	var rv_listings = rvGetListings();
	var new_rv_listings = [];

	if ( rv_listings ) {
		for (var i = 0; i < rv_listings.length; i++) {
			if ( rv_listings[i][0] != listing[0] ) {
				new_rv_listings.splice(i, 0, rv_listings[i]);
			}
		};
	}

	new_rv_listings.splice(0, 0, listing);

	if ( new_rv_listings.length > rv_total_count ) {
		var difference = (new_rv_listings.length - rv_total_count);
		new_rv_listings.splice(rv_total_count, difference);
	}

	try {
		localStorage.setItem('rv_listings_' + storage_item_name, JSON.stringify(new_rv_listings));
	} catch (e) {
		if (e == QUOTA_EXCEEDED_ERR) {
			console.log('Error. Web storage is full');
		}
	}
}

var rvRemoveListing = function(listing_id) {
	var rv_listings = rvGetListings();
	var new_rv_listings = [];

	if ( rv_listings ) {
		for (var i = 0; i < rv_listings.length; i++) {
			if ( rv_listings[i][0] != listing_id ) {
				new_rv_listings.splice(i, 0, rv_listings[i]);
			}
		};
	}

	try {
		localStorage.setItem('rv_listings_' + storage_item_name, JSON.stringify(new_rv_listings));
	} catch (e) {
		if (e == QUOTA_EXCEEDED_ERR) {
			console.log('Error. Web storage is full');
		}
	}
}

var rvGetListings = function() {
	return JSON.parse(localStorage.getItem('rv_listings_' + storage_item_name));
}

var rvRemoveListings = function() {
	localStorage.removeItem('rv_listings_' + storage_item_name);
}

var addTriggerToIcons = function() {
	$('#listings .fieldset').hide();

	var rv_storage_ids = [];
	var rv_storage = rvGetListings();

	if ( rv_storage ) {
		for (var i = rv_storage.length - 1; i >= 0; i--) {
			rv_storage_ids.unshift(rv_storage[i][0]);
		};
	}

	rv_storage_ids = rv_storage_ids.join(',');

	$('.rv_remove span').each(function(){
		$(this).flModal({
			caption: notice,
			content: rv_del_listing_notice,
			prompt: 'xajax_removeRvListing(' + $(this).parent().attr('id').split('_')[1] + ', "' + rv_storage_ids + '")',
			width: 'auto',
			height: 'auto'
		});
	});

	$('.rv_del_listings').each(function(){
		$(this).flModal({
			caption: notice,
			content: rv_del_listings_notice,
			prompt: 'xajax_removeAllRvListings()',
			width: 'auto',
			height: 'auto'
		});
	});
}

var syncListings = function() {
	var rv_storage_ids = [];
	var rv_storage = rvGetListings();

	if ( rv_storage ) {
		for (var i = rv_storage.length - 1; i >= 0; i--) {
			rv_storage_ids.unshift(rv_storage[i][0]);
		};
	}

	rv_storage_ids = rv_storage_ids.join(',');

	xajax_syncRvListings( rv_storage_ids );
}

var loadRvListingsToBlock = function() {
	var rv_listings = rvGetListings();

	if ( rv_listings && rv_listings.length > 0 ) {
		var content = rlConfig['template_type'] != 'responsive_42' ? '<div id="rv_listings" class="hborder side_block">' : '<section id="rv_listings" class="side_block">';
		content += '<div class="item rv_first">' + rv_lang_listings + '</div>';
		content += '<div class="rv_items">';

		var max_count = rv_listings.length <= 8 ? rv_listings.length : 8;

		for (var i = 0; i < max_count; i++) {

			var image_path = '';
			if ( rv_listings[i][1] ) {
				image_path = rlConfig['files_url'] + rv_listings[i][1];
			} else {
				if ( rlConfig['template_type'] != 'responsive_42' ) {
					image_path = rlConfig['tpl_base'] + 'img/no-picture.jpg';
				} else {
					image_path = rlConfig['tpl_base'] + 'img/no-picture.png';
				}
			}

			var listing_path = rlConfig['seo_url'];
			listing_path += rlConfig['mod_rewrite'] ? rv_listings[i][2] + '/' + rv_listings[i][3] + '-' + rv_listings[i][0] + '.html' : '?page=' + rv_listings[i][2] + '&amp;id=' + rv_listings[i][0];

			content += '<div class="item"><a href="' + listing_path + '" target="_blank"><img ';
			content += rlConfig['template_type'] == 'responsive_42' ? 'class="hint"' : '';
			content += 'src="' + image_path + '" title="' + rv_listings[i][4] + '" alt="' + rv_listings[i][4] + '"></a></div>';
		};

		content += '</div>';
		content += '<div class="rv_last"><a href="' + rv_history_link + '" target="_self">' + rv_lang_history + '</a></div>';
		content += rlConfig['template_type'] != 'responsive_42' ? '</div>' : '</section>';

		rlConfig['template_type'] != 'responsive_42' ? $('div#content > table.content').after(content) : $('section#main_container > div.inside-container > section#content').after(content);

		var tmp_style = jQuery.extend({}, qtip_style);
		tmp_style.tip = 'bottomMiddle';

		$('.hint').each(function(){
			$(this).qtip({
				content: $(this).attr('title') ? $(this).attr('title') : $(this).prev('div.qtip_cont').html(),
				show: 'mouseover',
				hide: 'mouseout',
				position: {
					corner: {
						target: 'topMiddle',
						tooltip: 'bottomMiddle'
					}
				},
				style: tmp_style
			}).attr('title', '');
		});
	}
}