
/******************************************************************************
 *
 *	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 browseMapClass = function(){
	/**
	* configurations array
	**/
	this.config = new Array();
	
	/**
	* plugin phrases
	**/
	this.phrases = new Array();
	
	/**
	* markers
	**/
	this.markers = new Array();
	
	/**
	* markers mapping
	**/
	this.mapping = new Array();
	
	/**
	* local map search data
	**/
	this.localSearch = new Array();
	
	/**
	* reference to this object
	**/
	var self = this;
	
	/**
	* load map
	**/
	this.load = function(key){
		key = key ? '_'+key.replace(/_/gi, '') : '';
		/* hide map if markes doesn't exist */
		if ( this.markers.length <= 0 )
		{
			$('#bm_map'+key).closest('.no_design,.no-style,.content_block,.side_block').hide();
			return;
		}
		
		$('#bm_map'+key).parent().parent().show();
		
		/* generate map */
		$('#bm_map'+key).flMap({
			addresses: self.markers,
			phrases: {
				hide: self.phrases['hide'],
				show: self.phrases['show'],
				notFound: self.phrases['notFound']
			},
			scrollWheelZoom: false,
			zoom: 12,
			alphabetMarkers: self.config['use_letters'],
			ready: self.handler,
			localSearch: self.localSearch
		});
	}
	
	/**
	* map handler
	*
	* @param object base - map object, see flMap jquery plugin
	*
	**/
	this.handler = function(base){
		this.attachInfo = new Array();
		
		for ( var i = 0; i < base.markers.length; i++ )
		{
			eval("google.maps.event.addListener(base.markers["+i+"], 'click', function(){ \
				self.attachInfo(base, "+i+", self.markers["+i+"][3]); \
			});");
		}
		
		/* map icon click handler */
		$('span.bm_icon').unbind('click').click(function(){
			var accesskey = $(this).attr('accesskey');
			flynax.slideTo($('#bm_map').length > 0 ? '#bm_map' : '#bm_map_'+accesskey);
			var id = $(this).attr('id').split('_')[1];
			self.attachInfo(base, self.mapping[id], id);
		});
	}
	
	/**
	* map handler
	*
	* @param object base - map object, see flMap jquery plugin
	* @param int marker_id - marker ID to attache info to
	* @param int listing_id - listing ID
	*
	**/
	this.attachInfo = function(base, marker_id, listing_id){
		// close all windows
		if ( base.infoWindows.length > 0 )
		{
			for ( var i=0; i < base.infoWindows.length; i++ )
			{
				base.infoWindows[i].close();
			}
		}
		
		// load/open window
		if ( !base.infoWindows[marker_id].flLoaded )
		{
			$.get(rlConfig['seo_url'], {request: 'browseMap', id: listing_id}, function(response){
				base.infoWindows[marker_id].setContent(response);
				base.infoWindows[marker_id].flLoaded = true;
				base.infoWindows[marker_id].open(base.map, base.markers[marker_id]);
			});
		}
		base.infoWindows[marker_id].open(base.map, base.markers[marker_id]);
	}
	
	/**
	* recently added listing reload
	*
	* @param string key - requested type key
	*
	**/
	this.recentlyReload = function(key){
		this.load(key);
	}
}

var browseMap = new browseMapClass();