/**

*	 
* 
* 

function intiDealerLocator() {
	var appKey = null; // appKey would be obtained through google and is domain specific. the default value is for summitprojects.com domains only.
	var dealerLocator = new DealerLocator($('mapContainer'),$('resultsContainer'),$('locatorForm'),$('locatorSubmitBtn'),appKey,{
	onSubmitBegin:function(instance){
		//instance is the only parameter passed into this function onRequest of ajax request. 
		//instance is the request object
		console.log(instance);
	},
	onSubmitFail:function(instance){
		console.log(instance);
	},
	onSubmitComplete:function(){
		$('resultsContainer').addClass('show');
	},
	noResults: '<h1>THIS SHOULD DISPLAY IF NO DEALERS WERE RETURNED</h1>',
	defaultLocation: 'Los Angeles, CA, USA'
});
}Onload.add(intiDealerLocator);

* */

var Dealer = new Class({
	Implements: [Events, Options],
	options:{
		 onClickDealer: $empty
	},
	initialize: function(dealerInfo,options){
		this.setOptions(options);
		this.index = ($type(dealerInfo.index)) ? dealerInfo.index : null;
		this.marker = null;
		this.latlon = null;
		this.dealerLetter = null;
		this.info = {};
		this.info.name = ($type(dealerInfo.name)) ? dealerInfo.name : 'dealerName';
		this.info.address = ($type(dealerInfo.address)) ? dealerInfo.address : null;
		this.info.city = ($type(dealerInfo.city)) ? dealerInfo.city : null;
		this.info.stateProvince = ($type(dealerInfo.stateProvince)) ? dealerInfo.stateProvince : null;
		this.info.postalCode = ($type(dealerInfo.postalCode)) ? dealerInfo.postalCode : null;
        this.info.countryCode = ($type(dealerInfo.countryCode)) ? dealerInfo.countryCode : null;
		this.info.website = ($type(dealerInfo.website)) ? dealerInfo.website : null;
        this.info.email = ($type(dealerInfo.email)) ? dealerInfo.email : null;
		this.info.phone = ($type(dealerInfo.phone)) ? dealerInfo.phone : null;
		this.dealerEntry = null;
		if((this.info.address && this.info.city && this.info.stateProvince) || this.info.postalCode){
			this.createDealerEntry();
		}
	},
	createDealerEntry: function(){
		this.dealerEntry = new Element('div',{
			'class':'dl-entry',
			'events':{
				'click': function(e){
					this.fireEvent('onClickDealer',this);
				}.bind(this)
			}
		});
		this.dealerLetter = String.fromCharCode("A".charCodeAt(0) + this.index);
		this.letterContainer = new Element('div',{'class':'dl-letter'});
		this.letterContainer.set('text',this.dealerLetter)
		this.letterContainer.inject(this.dealerEntry, 'top');
		
		this.dealerContentContainer = new Element('div',{'class':'dl-content'});
		this.dealerContentContainer.inject(this.dealerEntry, 'top');
		 
		this.dealerSeperator = new Element('div',{
			'class':'dl-seperator'
		});
		var dealerDetails = new Element('p',{
			'class':'dl-details'
		});
        //console.log(this.info);
		dealerDetails.inject(this.dealerContentContainer);
		if(this.info.name){
			var dealerName = new Element('h3',{'class':'dl-name'});
			dealerName.set('text',this.info.name);
			dealerName.inject(this.dealerContentContainer,'top');
		}
		if(this.info.address){
			var dealerAddress = new Element('span',{'class':'dl-address'});
			dealerAddress.set('text',this.info.address);
			dealerAddress.inject(dealerDetails);
        }
        var dealerCity = new Element('span',{'class':'dl-cityStatePostal'});
        dealerCity.set('text',"");
        if(this.info.city) {
			dealerCity.appendText(" "+this.info.city);
		}
        if(this.info.stateProvince){
            dealerCity.appendText(", "+this.info.stateProvince);
        }
        if(this.info.postalCode){
            dealerCity.appendText(" "+this.info.postalCode+" ");
        }
        dealerCity.inject(dealerDetails);
        if(this.info.countryCode){
            var dealerCountry = new Element('span',{'class':'dl-country'});
            dealerCountry.set('text',this.info.countryCode);
            dealerCountry.inject(dealerCity);    
        }
        
        this.dealerDetails = dealerDetails.get('text');
        if(this.info.email){
             var email = new Element('a',{
                 'href':'mailto:'+this.info.email,
                 'class':'dl-email'
            });
            email.set('text','Email');
            email.inject(this.dealerContentContainer);
        }
        
        var link = new Element('a',{
            'href':'http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=&daddr='+dealerDetails.get('text')+'&ie=UTF8&z=14',
            'target':'_blank',
            'class':'dl-directions'
        });
        link.set('text','Get Directions');
        link.inject(this.dealerContentContainer);
        
		if(this.info.phone){
			var dealerPhone = new Element('span',{'class':'dl-phone'});
			dealerPhone.set('text',this.info.phone);
			dealerPhone.inject(dealerDetails);
		}
		if(this.info.website){
			var link = new Element('a',{
				'href':"http://"+this.info.website,
				'target':'_blank',
				'class':'dl-website'
			});
			link.set('text','View Website');
			link.inject(this.dealerContentContainer);
		}
		this.dealerSeperator.inject(this.dealerContentContainer,'bottom');
		
		this.dealerClear = new Element('div',{
			'class':'clear'
		});
		this.dealerClear.inject(this.dealerEntry,'bottom');
	}
});

var DealerLocator = new Class({
	Implements: [Events, Options],
	options:{/*
		 onClickDealer: $empty
		 * */
	 	noResults: '<h3>There are no results</h3>', //HTML STRING THAT WILL DISPLAY WHEN NO DEALERS ARE FOUND
	 	defaultLocation: 'Hood River, Oregon 97031', //DEFAULT LOACATION FOR THE MAP.
		onSubmitBegin: $empty,
		onSubmitComplete: $empty,
		onSubmitFail: $empty
	},
	initialize: function(mapContainer,resultsContainer,searchForm,submitBtn,appKey,options){
		this.setOptions(options);
		
		//MUST HAVE A MAP CONTAINER IF ITS NOT PASSED IN IT WILL LOOK FOR <id="mapContainer">
		this.mapContainer = ($type(mapContainer)) ? $(mapContainer) : $('mapContainer');
		
		//MUST HAVE A FORM TO SUBMIT FOR THE SEARCH OF DEALERS IF NOT PASSED IN IT WILL SEARCH FOR <id="searchForm"> 
		this.searchForm = ($type(searchForm)) ? $(searchForm) : $('searchForm'); 
		this.searchForm.addEvent('submit',this.submitSearchForm.bindWithEvent([this.searchForm,this]));
		
		//MUST HAVE A BUTTON TO SUBMIT THE SEARCH FORM. IF NO BUTTON PASSED IN IT WILL FIND THE FIRST SUBMIT BUTTON IN THE FORM AND ADD A CLICK EVENT.
		this.submitBtn = ($type(submitBtn)) ? $(submitBtn) : this.searchForm.getElement('input[type=submit]');
		this.submitBtn.addEvent('click',this.submitSearchForm.bindWithEvent([this.searchForm,this]));
		
		//SETUP SEARCH FORM EVENTS
		if(this.searchForm){
			this.searchForm.addEvent('onSubmitDealerSearch',this.submitSearchForm.bindWithEvent([this.searchForm,this]));
		}
		 
		this.dealers = [];
		this.resultsContainer = ($type(resultsContainer)) ? $(resultsContainer) : $('resultsContainer');
		this.gMap = null;

		/*
		 * DEFAULT APP KEY FOR SUMMIT PROJECTS.COM ===> ABQIAAAAP_06CPCgKhoMRFwxSuSWUhTwZcuYK5Te86v2recfC-wpz49WfBTyyNVtMMCYuHJxRtVr-rBUqLGTMw
		*/
		
		this.appKey = ($type(appKey)) ? appKey : 'ABQIAAAAP_06CPCgKhoMRFwxSuSWUhTwZcuYK5Te86v2recfC-wpz49WfBTyyNVtMMCYuHJxRtVr-rBUqLGTMw';
		/* Create Script tag */
		var googleLoaderScript = new Asset.javascript('http://www.google.com/jsapi?key='+this.appKey, {id: 'GoogleMapsLoaderScript'});
//		var markerManagerScript = new Asset.javascript('http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/src/markermanager.js', {id: 'MarkerManager'});
		
		/* Call Load Map with a DELAY */
		var loadMapBase = this.loadGoogleLoader;
		var loadMapDelay = loadMapBase.create({
			attempt: true,
			delay: 500,
			bind: this
		});
		loadMapDelay();
},
	loadGoogleLoader: function(){
		if(this.mapContainer){
			var initMap = this.initializeMap;
			intiMap = initMap.create({
				bind: this
			});
			google.load("maps", "2", {"callback" : intiMap});
		}
	},
	initializeMap : function(){
		if(!this.gMap){
			var mapContainer = this.mapContainer;
			this.geoEncoder = new GClientGeocoder();
			this.centerOnAddress(this.options.defaultLocation);
			this.gMap = new google.maps.Map2(mapContainer);
			this.gMap.addControl(new GLargeMapControl());
			this.gMap.addControl(new GMapTypeControl());
		}
	},
	centerOnAddress: function(address){
		this.geoEncoder.getLocations(address,function(response){
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
			this.gMap.setCenter(point,8);
		}.bind(this));
	},
	addDealerMarker: function(address,dealer){
		this.geoEncoder.getLocations(address,function(response){
                /* response.Status.code::::::
                    G_GEO_SUCCESS (200) 	 No errors occurred; the address was successfully parsed and its geocode has been returned. (Since 2.55)
                    G_GEO_BAD_REQUEST (400) 	A directions request could not be successfully parsed. (Since 2.81)
                    G_GEO_SERVER_ERROR (500) 	A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known. (Since 2.55)
                    G_GEO_MISSING_QUERY (601) 	The HTTP q parameter was either missing or had no value. For geocoding requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input. (Since 2.81)
                    G_GEO_MISSING_ADDRESS (601) 	Synonym for G_GEO_MISSING_QUERY. (Since 2.55)
                    G_GEO_UNKNOWN_ADDRESS (602) 	No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect. (Since 2.55)
                    G_GEO_UNAVAILABLE_ADDRESS (603) 	The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons. (Since 2.55)
                    G_GEO_UNKNOWN_DIRECTIONS (604) 	The GDirections object could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region. (Since 2.81)
                    G_GEO_BAD_KEY (610) 	The given key is either invalid or does not match the domain for which it was given. (Since 2.55)
                    G_GEO_TOO_MANY_QUERIES (620) 	The given key has gone over the requests limit in the 24 hour period. (Since 2.55)
                */
			if(response.Status.code == 200){
				place = response.Placemark[0];
				point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
				dealer.latlon = point;
				dealer.marker = this.createCustomMarker(point,dealer);
				if(dealer.index == 0){
					this.gMap.setCenter(dealer.latlon,10);
				}
				GEvent.addListener(dealer.marker, "click", function() {
					dealer.fireEvent('onClickDealer',dealer); // THIS LINE FIRES THE ONE EVENT THAT SHOULD BE RESPONSIBLE FOR HANDLING THE CLICK
				});
				this.gMap.addOverlay(dealer.marker)
			}
            else if(response.Status.code == 602){
                alert('Address: \n( '+response.name+')\n\nis unknown.');
            }
            else{
                
                alert('Error: ('+response.Status.code+') '+response.name+'\nThere has been an error.');
            }
		}.bind(this));
	},
	createCustomMarker: function(point,dealer){
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon();
		baseIcon.shadow = "/resources/images/map/shadow.png";
		baseIcon.iconSize = new GSize(27, 41);
		baseIcon.shadowSize = new GSize(34, 26);
		baseIcon.iconAnchor = new GPoint(9, 26);
		baseIcon.infoWindowAnchor = new GPoint(14, 0);
		baseIcon.infoShadowAnchor = new GPoint(27, 41);
		var letter = "";
		if(dealer.letter != "default"){
	  		// Create a lettered icon for this point using our icon class
	  		letter = String.fromCharCode("A".charCodeAt(0) + dealer.index);
		}else{
			letter = "default";
		}
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = "/resources/images/map/marker_"+letter+".png";
		// Set up our GMarkerOptions object
		markerOptions = { icon:letteredIcon };
		var marker = new GMarker(point, markerOptions);
		return marker;
	},
	submitSearchForm: function(e){
		var form = this[0];
		new Event(e).stop();
	 	form.set('send',{
	 		onRequest: this[1].locatorFormSent.bind(this[1]),
	 		onSuccess: this[1].locatorFormComplete.bind(this[1]),
	 		onFailure: this[1].locatorFormFailed.bind(this[1])
		});
		form.send();
	},
	locatorFormSent:function(instance){
		this.fireEvent('onSubmitBegin',instance);
	},
	locatorFormFailed: function(instance){
		this.fireEvent('onSubmitFail',instance);
	},
	locatorFormComplete: function(txt){
		this.dealers.empty();
		var dealerObject = JSON.decode(txt.clean(), true);
		var dealers = [];
		for(var i=0; i<dealerObject.recordcount; i++) {
			var dealer = {};
			dealer.index = i;
			dealer.name = dealerObject.data.DEALERNAME[i];
			dealer.address = dealerObject.data.ADDRESS[i];
			dealer.city = dealerObject.data.CITY[i];
			dealer.stateProvince = dealerObject.data.STATEPROVINCE[i];
            dealer.postalCode = dealerObject.data.POSTALCODE[i];
			dealer.countryCode = dealerObject.data.COUNTRYCODE[i];
			dealer.phone = dealerObject.data.PHONE[i];
			dealer.website = dealerObject.data.WEBSITE[i];
            dealer.email = dealerObject.data.EMAIL[i];
			dealer.latitude = dealerObject.data.LATITUDE[i];
			dealer.longitude = dealerObject.data.LONGITUDE[i];
			dealers.push(dealer);
		}
		dealers.each(function(el){
			this.dealers.push(new Dealer(el,{
				onClickDealer:function(dealerEntry){
					console.log(dealerEntry);
					this.gMap.panTo(dealerEntry.latlon);
					var mapInfo = dealerEntry.dealerEntry.clone();
					mapInfo.setStyle('width',250);
					mapInfo.id = "mapInfo";
					dealerEntry.marker.openInfoWindowHtml(mapInfo);
				}.bind(this)
			}));
		}.bind(this));
		
		this.showResults();
	},
	showResults: function(){
		this.fireEvent('onSubmitComplete');
		if(this.dealers.length > 0){
			this.resultsContainer.empty();
			this.gMap.clearOverlays();
			this.dealers.each(function(dealer,index,group){
				//CREATE MARKERS FOR ADDING TO THE MAP -- sore them in association with the the Dealer Entry
                
				this.addDealerMarker(dealer.dealerDetails,dealer);
                
				dealer.dealerEntry.inject(this.resultsContainer,'inside');
				
			}.bind(this));
			
			
		}
		else{
			this.resultsContainer.set('html',this.options.noResults);
		}
		
	}
});

