// ======================
// = storeLocator Class =
// ======================
var storeLocator = Class.create();

//A couple trim functions to help things out
function ltrim(s) {
	return s.replace( /^\s*/, "" )
}

function rtrim(s) {
	return s.replace( /\s*$/, "" );
}

function trim(s) {
	return rtrim(ltrim(s));
}

//Add element function
$E = function(tag,elm,para){
	var obj = document.createElement(tag)
	if(para.className)obj.className = para.className
	if(para.id)obj.id = para.id
	if(para.href)obj.href = para.href
	if(para.src)obj.src = para.src
	if(para.html)obj.innerHTML = para.html
	if(para.element)obj.appendChild = para.element
	if(para.onclick)obj.onclick = para.onclick
	if(para.onmouseover)obj.onmouseover = para.onmouseover	
	if(para.visible)obj.style.display = para.visible
	elm.appendChild(obj)
	return obj
}


Object.extend(storeLocator, {

	vars: {
		map: false,
		gdir: false,
		storeServiceUrl: "/templates/storeLocatorService.cfm",
		bounds: new GLatLngBounds(),
		baseIcon: new GIcon(),
		centerPoint: false,
		currentPath: window.location.pathname.split("/"),
		fromOJ: false
	},
	
	initialize: function() {
		storeLocator.vars.map = new GMap2(document.getElementById("map"));		
		storeLocator.vars.map.setCenter(new GLatLng(44.867553, -93.352972), 11);		
		storeLocator.vars.map.addControl(new GOverviewMapControl(new GSize(125,125)));	
		(window.location.hash == "#orange-julius") ? storeLocator.vars.fromOJ = true : storeLocator.vars.fromOJ = false;
		
		//setup custom controls
		obj = storeLocator.vars.map.getContainer();
		
		$E('div',obj,{className:'mapControl panUp',onclick:function(){
			storeLocator.vars.map.panDirection(0,+1);
		}});
		$E('div',obj,{className:'mapControl panDown',onclick:function(){
			storeLocator.vars.map.panDirection(0,-1);
		}});
		$E('div',obj,{className:'mapControl panLeft',onclick:function(){
			storeLocator.vars.map.panDirection(+1,0);
		}});
		$E('div',obj,{className:'mapControl panRight',onclick:function(){
			storeLocator.vars.map.panDirection(-1,0);
		}});
		$E('div',obj,{className:'mapControl zoomIn',onclick:function(){
			storeLocator.vars.map.zoomIn();
		}});
		$E('div',obj,{className:'mapControl zoomOut',onclick:function(){
			storeLocator.vars.map.zoomOut();
		}});		
		$E('div',obj,{id:'mapTypeNormal',className:'mapControl2 mapSelected',onclick:function(){
			//this.style.filter = null;
			this.className = 'mapControl2 mapSelected';
			this.style.backgroundImage = '';
			$('mapTypeHybrid').className = 'mapControl2 hybrid';
			$('mapTypeHybrid').style.backgroundImage = '';
			$('mapTypeSatellite').className = 'mapControl2 satellite';
			$('mapTypeSatellite').style.backgroundImage = '';
			storeLocator.vars.map.setMapType(G_NORMAL_MAP); //map type
		}});		
		$E('div',obj,{id:'mapTypeSatellite',className:'mapControl2 satellite',onclick:function(){
			//this.style.filter = null;
			this.className = 'mapControl2 satelliteSelected';	
			this.style.backgroundImage = '';
			$('mapTypeNormal').className = 'mapControl2 map';
			$('mapTypeNormal').style.backgroundImage = '';
			$('mapTypeHybrid').className = 'mapControl2 hybrid';
			$('mapTypeHybrid').style.backgroundImage = '';
			storeLocator.vars.map.setMapType(G_SATELLITE_MAP); //satellite type
		}});		
		$E('div',obj,{id:'mapTypeHybrid',className:'mapControl2 hybrid',onclick:function(){
			//this.style.filter = null;
			this.className = 'mapControl2 hybridSelected';
			this.style.backgroundImage = '';
			$('mapTypeNormal').className = 'mapControl2 map';
			$('mapTypeNormal').style.backgroundImage = '';
			$('mapTypeSatellite').className = 'mapControl2 satellite';
			$('mapTypeSatellite').style.backgroundImage = '';
			storeLocator.vars.map.setMapType(G_HYBRID_MAP); //hybrid type
		}});
		storeLocator.doSearch($('address'));
		$('orange_julius').checked = storeLocator.vars.fromOJ;
		
	},
	
	doSearch: function(address) {
		// begin Google analytics on each search
		// var axel = Math.random()+"";
		// var a = axel * 10000000000000;
		// document.write('<IFRAME SRC="http://fls.doubleclick.net/activityi;src=2209458;type=store026;cat=store653;ord='+ a + '?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>');
		// console.log('Floodlight');
		// end google analytics
		
		storeLocator.vars.map.clearOverlays();
		$('directions').innerHTML = "";
		storeLocator.vars.jsonStores = null;
		this.doGeocode(address.value);				
		urchinTracker(storeLocator.vars.currentPath[1] + '/store-locator/search/' + address.value);				
	},
	
	updateMap: function() {
		gmarkers=[];		
		$('results').innerHTML = '';

		if (typeof storeLocator.vars.jsonStores != "undefined" && storeLocator.vars.jsonStores != null) {
		    
			$('resultsTotalPage').innerHTML = Math.ceil(storeLocator.vars.jsonStores.data.length/5);
			$('resultsCurrentPage').innerHTML = "1";
					
			/* START VCARDS */
			objCardHolder = $('results');		
			for (var i = 0; i < storeLocator.vars.jsonStores.data.length; i++) {
				var data = storeLocator.vars.jsonStores.data[i];
				var point = new GLatLng(data['latitude'], data['longitude']);
				this.drawMarker( point, data );
				if (isNaN(data.dist)) {
					var card = new HCard(
						data.store_id,
						'<a href="/' + storeLocator.vars.currentPath[1] + '/store-details/' + data.store_id + '">' + data.address1 + '</a>',
						'',
						'',						
						data.address3,
						data.city,
						data.state_province_abbr,
						data.postal_code,
						'<br />' + data.phone_number
					);
				} else {
					var card = new HCard(
						data.store_id,
						'<a href="/' + storeLocator.vars.currentPath[1] + '/store-details/' + data.store_id + '">' + data.address1 + '</a>',
						'',
						'',						
						data.address3,
						data.city,
						data.state_province_abbr,
						data.postal_code,
						'<br />' + data.phone_number,
						'<br />' + Math.round(data.dist*100)/100 + ' mi from center of search'
					);
				}
				var para = document.createElement("p");
				var mapLink = document.createElement("span");

				mapLink.className="arrow_orng_flex";
				Element.observe(mapLink, 'click', function(_point,_data,e){
					Event.stop(e);
					this.showInfoWindow(_point,_data)
				}.bind(this,point,data));
				
				//check to see if it is english or french
				if (storeLocator.vars.currentPath[1].toLowerCase() == 'ca-fr') {
					mapLink.innerHTML = '<a href="#">Montrer sur la carte</a>';
				} else {
					mapLink.innerHTML = '<a href="#">SHOW ON MAP</a>';
				}
				
				var dirLink = document.createElement("span");
				dirLink.className="arrow_orng_flex";

				var directionsURL = 'http://maps.google.com/maps?q=+';
				directionsURL += point.lat()+","+point.lng()+ "(" + (encodeURIComponent(data.address3)+", "+data.city+", "+data.state_province_abbr+" "+data.postal_code).replace(/ +/g,"+") + ")";

				//check to see if it is english or french
				if (storeLocator.vars.currentPath[1].toLowerCase() == 'ca-fr') {
					dirLink.innerHTML = '<a href="'+directionsURL+'">Obtenir des indications</a>';
				} else {
					dirLink.innerHTML = '<a href="'+directionsURL+'">GET DIRECTIONS</a>';
				}
				para.appendChild(mapLink);
				para.appendChild(dirLink);
				
				if (i % 5 == 0) {
					var currentResults = 'resultPage' + i;
					if (i == 0) {
						$E('ul',objCardHolder,{id: 'resultPage' + i,visible:'block'});
					} else {
						$E('ul',objCardHolder,{id: 'resultPage' + i,visible:'none'});
					}				
				}
				objCurrentPage = $(currentResults);
				
				var result = $E('li',objCurrentPage,{className:'result', html:card.HTML});
				
				result.appendChild(para);
				result.flags = data;
				// if (data['errorhack']) storeLocator.vars.map.zoomOut();
			}
			
			$('resultsPrevious').style.visibility = 'hidden';

			if (storeLocator.vars.jsonStores.data.length < 6) {
				$('resultsNext').style.visibility = 'hidden';	
			} else {
				$('resultsNext').style.visibility = 'visible';
			}
			selectedResultPage = 0;			
	
			
			/* END VCARDS */
            storeLocator.vars.map.setZoom(storeLocator.vars.map.getBoundsZoomLevel(storeLocator.vars.bounds)-1);
			storeLocator.vars.map.setCenter(storeLocator.vars.bounds.getCenter());
			
			storeLocator.vars.bounds = new GLatLngBounds();
			
			if (storeLocator.vars.jsonStores.data.length == 0) {
				lightbox.prototype.activate('lightbox');	
			}

		} else {
			//throw error here
			lightbox.prototype.activate('lightbox');
		}
		
			
	},
	
	pageDirection: function(direction) {		
		
		this.showHidePageButtons(selectedResultPage);
		
		if (direction == 'next') {		
			for (var i = 0; i < Math.floor(storeLocator.vars.jsonStores.data.length/5); i++) {
				$('resultPage' + (i*5)).hide();				
			}
			selectedResultPage = selectedResultPage + 5;
			new Effect.Appear($('resultPage'+selectedResultPage));
		} else if (direction == 'previous') {
			for (var i = 0; i < Math.floor(storeLocator.vars.jsonStores.data.length/5); i++) {
				$('resultPage' + (i*5)).hide();
			}
			selectedResultPage = selectedResultPage - 5;
			new Effect.Appear($('resultPage'+selectedResultPage));
		}
		
		this.showHidePageButtons(selectedResultPage);
		
		$('resultsCurrentPage').innerHTML = ((selectedResultPage/5)+1);

	},
	
	showHidePageButtons: function(pageNumber) {
		if (pageNumber == 0) {
			$('resultsPrevious').style.visibility = 'hidden';
		} else {
			$('resultsPrevious').style.visibility = 'visible';
		}

		if (pageNumber == ((Math.ceil(storeLocator.vars.jsonStores.data.length/5)*5)-5)) {
			$('resultsNext').style.visibility = 'hidden';		
		} else {
			$('resultsNext').style.visibility = 'visible';
		}
	},
	
	drawMarker: function(point,data) {
		var icon = new GIcon(storeLocator.vars.baseIcon);
		storeLocator.vars.fromOJ ? icon.image = "/templates/img/ico_oj_sign.png" : icon.image = "/templates/img/ico_dq_sign.png";
		storeLocator.vars.fromOJ ? icon.iconSize = new GSize(80, 28) : icon.iconSize = new GSize(50, 40);
		icon.iconAnchor = new GPoint(15, 30);
		icon.infoWindowAnchor = new GPoint(25, 15);
		var marker = new GMarker(point, icon);		
		storeLocator.vars.bounds.extend(point);
		storeLocator.vars.map.addOverlay(marker);		
		GEvent.addListener(marker, "click", function() {
			storeLocator.showInfoWindow(point, data);
		});
		gmarkers.push(marker);
		//console.log(gmarkers);
	},
	
	clearCheckboxes: function(type) {
		//$('cones').checked = false;
		$('burgers').checked = false;
		$('chicken_strips').checked = false;
		//$('texas_food').checked = false;
		$('cakes').checked = false;
		$('hot_dogs').checked = false;
		$('breakfast').checked = false;
		$('drive_thru').checked = false;
		$('orange_julius').checked = false;
		$('gift_cards').checked = false;
		//$('karmelkorn').checked = false;
	},
	
	filterResults: function(type) {
		//$('all').checked = false;
		results = $$('#results li.result');
		
		for (var i=0; i<storeLocator.vars.jsonStores.data.length; i++) {
			var data = storeLocator.vars.jsonStores.data[i];
			
			for (var f0=0;f0<gmarkers.length;f0++) {
				if (data.longitude == gmarkers[f0].N.x && data.latitude == gmarkers[f0].N.y) {
					gmarkers[f0].show();
				}						
			}
			results.each(function(result){
				result.show();
			})
						
			//cones
			/*
			if ($('cones').checked) {
				if (!data.brazier_flag && !data.grill_n_chill_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
						}						
					}
				}
			}*/
			
			//burgers
			if ($('burgers').checked) {
				if (!data.brazier_flag && !data.grill_n_chill_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
							//$('burger').hide();
						}						
					}
				}
				results.each(function(result){
					if (!result.flags.brazier_flag && !result.flags.grill_n_chill_flag) {
						result.hide();
					}
				})
			}
			
				
			//chicken_strips
			if ($('chicken_strips').checked) {
				if (!data.brazier_flag && !data.ltd3_nochick_flag && !data.ltd3_chick_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
							//$('chicken-strip').hide();
						}						
					}
				}
				results.each(function(result){
					if (!result.flags.brazier_flag && !result.flags.ltd3_nochick_flag && !result.flags.ltd3_chick_flag) {
						result.hide();
					}
				})
			}			
			
			//texas_food
			/*
			if ($('texas_food').checked) {
				if (!node.tcf_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
						}						
					}
				}
			}*/
			
			//cakes
			if ($('cakes').checked) {
				if (!data.cakes_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
							//$('cake').hide();
						}						
					}
				}
				results.each(function(result){
					if (!result.flags.cakes_flag) {
						result.hide();
					}
				})
			} 
			
			
			//hot_dogs
			if ($('hot_dogs').checked) {
				if (!data.brazier_flag && !data.ltd2_nochick_flag && !data.ltd3_nochick_flag && !data.ltd2_chick_flag && !data.ltd3_chick_flag && !data.oj_dog_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
							//$('hot-dog').hide();
						}						
					}
				}
				results.each(function(result){
					if (!result.flags.brazier_flag && !result.flags.ltd2_nochick_flag && !result.flags.ltd3_nochick_flag && !result.flags.ltd2_chick_flag && !result.flags.ltd3_chick_flag && !result.flags.oj_dog_flag) {
						result.hide();
					}
				})
			}
			
			//breakfast
			if ($('breakfast').checked) {
				if (!data.breakfast_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
							//$('breakfast').hide();
						}						
					}
				}
				results.each(function(result){
					if (!result.flags.breakfast_flag) {
						result.hide();
					}
				})
			}
			
			//drive_thru
			if ($('drive_thru').checked) {
				if (!data.drive_thru_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
							//$('drive-thru').hide();
						}						
					}
				}
				results.each(function(result){
					if (!result.flags.drive_thru_flag) {
						result.hide();
					}
				})
			}
			
			//gift_cards
			if ($('gift_cards').checked) {
				if (!data.gift_card_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
							//$('gift-card').hide();
						}						
					}
				}
				results.each(function(result){
					if (!result.flags.gift_card_flag) {
						result.hide();
					}
				})
			}

			//karmelkorn
			/*
			if ($('karmelkorn').checked) {
				if (!data.kk_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
						}						
					}
				}
			}
			*/
			
			//orange_julius
			if ($('orange_julius').checked) {
				if (!data.oj_flag) {
					for (var f1=0;f1<gmarkers.length;f1++) {
						if (data.longitude == gmarkers[f1].N.x && data.latitude == gmarkers[f1].N.y) {
							gmarkers[f1].hide();
							//$('oj-store').hide();
						}						
					}
				}
				results.each(function(result){
					if (!result.flags.oj_flag) {
						result.hide();
					}
				})
			}
			// Check Orange Julius filter if user is coming from OJ site
			//(window.location.hash == "#orange-julius") ? $('orange_julius').checked = true : $('orange_julius').checked = false;
		}
	},
	
	showInfoWindow: function(point, data) {
		if (storeLocator.vars.infoWindow) {
			storeLocator.vars.map.removeOverlay(storeLocator.vars.infoWindow);
		}
		storeLocator.vars.infoWindow = new DQInfoWindow(point, data);
		storeLocator.vars.map.addOverlay(storeLocator.vars.infoWindow);		
		storeLocator.vars.map.panTo(point);
	},

	doGeocode: function(address) {
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					lightbox.prototype.activate('lightbox');
				} else {
					var icon = new GIcon(storeLocator.vars.baseIcon);
					icon.image = "/templates/img/map_ico_you_are_here.png";
					icon.iconSize = new GSize(22, 40);
					icon.iconAnchor = new GPoint(6, 20);
					storeLocator.vars.map.setCenter(point, 10);
					var marker = new GMarker(point, icon);
					//be sure to extend the bounds with the zipcode marker as well
					storeLocator.vars.bounds.extend(point);
					storeLocator.vars.map.addOverlay(marker);
					storeLocator.getStores(point);
					storeLocator.updateMap();
					storeLocator.filterResults();
				}
			});
            
	},
	
	getStores: function(point) {
	    var query;
	    storeLocator.vars.fromOJ ? query = ('x=' + point.x + '&y=' + point.y + '&radius=' + $('radius').value + '&oj_flag=Y' + '&search=' + $('address').value + '&hot_dogs=' + $('hot_dogs').checked + '&burgers=' + $('burgers').checked + '&gift_cards=' + $('gift_cards').checked + '&cakes=' + $('cakes').checked + '&breakfast=' + $('breakfast').checked + '&drive_thru=' + $('drive_thru').checked + '&chicken_strips=' + $('chicken_strips').checked + '&orange_julius=' + $('orange_julius').checked) : query = ('x=' + point.x + '&y=' + point.y + '&radius=' + $('radius').value + '&search=' + $('address').value + '&hot_dogs=' + $('hot_dogs').checked + '&burgers=' + $('burgers').checked + '&gift_cards=' + $('gift_cards').checked + '&cakes=' + $('cakes').checked + '&breakfast=' + $('breakfast').checked + '&drive_thru=' + $('drive_thru').checked + '&chicken_strips=' + $('chicken_strips').checked + '&orange_julius=' + $('orange_julius').checked);
		var pageloader = new Ajax.Request(storeLocator.vars.storeServiceUrl, {
    		method: 'post',
    		asynchronous: false,
    		postBody: query,
    		onSuccess: function(t) {
				if (t.responseText.match('Error')){
					alert('We\'re sorry, but the Store Locator is down for routine maintenance.');
					window.stop();
					window.location = "http://dairyqueen.com"
				} 
                storeLocator.vars.jsonStores = trim(t.responseText).evalJSON();
                // loop through results            
    		    for (var i=0; i<storeLocator.vars.jsonStores.data.length; i++) {
    		        data = storeLocator.vars.jsonStores.data[i];
    				for (var p in data) {						
    					if (data[p]=="Y") data[p] = true;
    					if (data[p]=="N") data[p] = false;
    				}
    				for (var q in data) {
    					if (q == 'dq_menu_type_no') {						
    						(isNaN(parseInt(data[q]))) ? data[q] = true : data[q] = false;
    					}
    				}    				
    				// check if outside preferred search radius
    				if(data['errorhack']) {
                        $$('.mid .inside h1')[0].update("Search Results <p>There were no results within the search distance you entered. We have expanded the search to show the DQ locations closest to you.</p>");
    				} else {
    				    $$('.mid .inside h1')[0].update("Search Results");
    				}
    			}
            }
			/*
			,onFailure: function(t) {
				alert('We\'re sorry, but the Store Locator is down for routine maintenance.');
				window.stop();
				window.location = "http://dairyqueen.com"
			}
			*/
		});
	},
	
	openCloseTripPlanner: function() {
		if ($('tp-detail').style.display == 'none') {
			$('tp-detail').show();
		} else {
			$('tp-detail').hide();
		}
	},
	
	onGDirectionsLoad: function() {
		var totalDistance = storeLocator.vars.gdir.getDistance();
		var coordsToSearch = new Array();
		var polyLine = storeLocator.vars.gdir.getPolyline();
		if (polyLine != null) {
			storeLocator.showProgress();
			var pageloader = new Ajax.Request(storeLocator.vars.storeServiceUrl, {
				method: 'post',
				asynchronous: true,
				postBody:'corridorSearch=true&polyLine=' + polyLine.L.toJSON(),
				onSuccess: function(t) 
					{
						storeLocator.vars.jsonStores = trim(t.responseText).evalJSON();
						for (var i=0; i<storeLocator.vars.jsonStores.data.length; i++) {
							var data = storeLocator.vars.jsonStores.data[i];
							for (var p in data) {
								if (data[p]=="Y")
									data[p] = true;
								if (data[p]=="N")
									data[p] = false;
							}
						}					
						storeLocator.updateMap();
						storeLocator.hideProgress();
						storeLocator.openCloseTripPlanner();					
					}
			});
		}
	},
	
	setDirections: function() {

		$('directions').innerHTML = ""; //clear any previous directions	
		
		storeLocator.vars.map.clearOverlays();
		
		storeLocator.vars.gdir = new GDirections(storeLocator.vars.map, $('directions'));

		GEvent.addListener(storeLocator.vars.gdir, "load", storeLocator.onGDirectionsLoad);
		GEvent.addListener(storeLocator.vars.gdir, "error", storeLocator.throwError);
		
		storeLocator.vars.gdir.load("from: " + $('fromAddress').value + " to: " + $('toAddress').value,{ "locale": 'en' });
		
		//Google analytics
		urchinTracker(storeLocator.vars.currentPath[1] + '/store-locator/trip-planner/from/' + escape($('fromAddress').value) + '/to/' + escape($('toAddress').value));
		
	},
	
	showProgress: function() {
		$('tp-instruct').hide();
		$('progress').show();
	},
	
	hideProgress: function() {
		$('tp-instruct').show();
		$('progress').hide();
	},
	
	throwError: function() {
		
		if (storeLocator.vars.gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
			lightbox.prototype.activate('lightbox');
		}
		if (storeLocator.vars.gdir.getStatus().code == G_GEO_SERVER_ERROR) {
			lightbox.prototype.activate('lightbox');
		}
		if (storeLocator.vars.gdir.getStatus().code == G_GEO_MISSING_QUERY) {
			lightbox.prototype.activate('lightbox');
		}
		if (storeLocator.vars.gdir.getStatus().code == G_GEO_BAD_KEY) {
			lightbox.prototype.activate('lightbox');
		}
		if (storeLocator.vars.gdir.getStatus().code == G_GEO_BAD_REQUEST) {
			lightbox.prototype.activate('lightbox');
		}
		//lightbox.prototype.activate('lightbox');
		//alert(storeLocator.vars.gdir.getStatus().code);

	}
	
})
		
function DQInfoWindow(point, data) {
  this.point_ = point;
  this.data_ = data;
}
DQInfoWindow.prototype = new GOverlay();
Object.extend(DQInfoWindow.prototype, {
	initialize: function(map) {
		var div = document.createElement("div");
		div.id = "InfoWindow";
		
		// Close button
		var close = document.createElement("div");
		close.className = "close";
		$(close).observe('click', this.remove.bind(this));
		div.appendChild(close);
		
		// Address pane
		this.addressPane = $(document.createElement("div"));
		var node = this.data_;
		var card = new HCard(node.state_id,'<a href="/' + storeLocator.vars.currentPath[1] + '/store-details/' + node.store_id + '">' + node.address1 + '</a>','','',node.address3,node.city,node.state_province_abbr,node.postal_code,'<br />' + node.phone_number);
		this.addressPane.innerHTML = card.HTML;
		div.appendChild(this.addressPane);
		
		// Services pane
		this.featuresPane = $(document.createElement("div"));
		var features = [];
		if (!node.dq_menu_type_no) {
			features.push( '<img src="/templates/img/map_ico_blizzard.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"DQ Treats":"Desserts DQ") + '" />' );
		}
		if (node.breakfast_flag) {
			features.push( '<img src="/templates/img/map_ico_eggs.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"Breakfast":"D&eacute;jeuner") + '" />' );
		}
		if (node.cakes_flag) {
			features.push( '<img src="/templates/img/map_ico_cake.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"Cakes":"G&acirc;teaux") + '" />' );			
		}
		if (node.brazier_flag || node.ltd2_nochick_flag || node.ltd3_nochick_flag || node.ltd2_chick_flag || node.ltd3_chick_flag) {
			features.push( '<img src="/templates/img/map_ico_hotdog.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"Hot Dogs":"Hot-dogs") + '" />' );					
		}
		if (node.brazier_flag || node.ltd3_nochick_flag || node.ltd3_chick_flag) {
			features.push( '<img src="/templates/img/map_ico_chix.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"Chicken Strips or Sandwiches":"Lani&eacute;res de poulet ou sandwichs") + '" />' );					
		}
		if (node.brazier_flag || node.grill_n_chill_flag) {
			features.push( '<img src="/templates/img/map_ico_burger.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"Burgers":"Hamburgers") + '" />' );					
		}
		if (node.drive_thru_flag) {
			features.push( '<img src="/templates/img/map_ico_drivethru.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"Drive Thru":"Service au volant") + '" />' );					
		}
		if (node.gift_card_flag) {
			features.push( '<img src="/templates/img/map_ico_card.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"Gift Cards":"Cartes cadeaux") + '" />' );					
		}
		if (node.grill_n_chill_flag) {
			features.push( '<img src="/templates/img/map_ico_grillchill.png" alt="' + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"Grill &amp; Chill":"Grill &amp; Chill") + '" />' );
		}
		if (node.tcf_flag) {
			features.push( '<img src="/templates/img/map_ico_texas.png" alt="Texas Country Foods" />' );
		}
		if (node.oj_flag) {
			features.push( '<img src="/templates/img/map_ico_oj.png" alt="Orange Julius" />' );
		}
		if (node.kk_flag) {
			features.push( '<img src="/templates/img/map_ico_kk.png" alt="Karmelkorn" />' );
		}
		this.featuresPane.innerHTML = features.join(" ");
		div.appendChild(this.featuresPane)
		this.featuresPane.hide();
		$A(this.featuresPane.getElementsByTagName("img")).each(function(img) {
			$(img).observe('mouseover', function(){this.showTooltip(img)}.bind(this));
			$(img).observe('mouseout', this.hideTooltip.bind(this));
		}.bind(this));
		this.tooltip = $(document.createElement("div"));
		this.tooltip.id = "InfoWindowTooltip";
		this.featuresPane.appendChild(this.tooltip);
		this.tooltip.hide();
		
		// Pane buttons
		this.addressBtn = document.createElement("img");
		this.addressBtn.className = "addressButton";
		this.addressBtn.src = "/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_address_ro.png";
		$(this.addressBtn).observe('click', this.showAddress.bind(this));
		div.appendChild(this.addressBtn);
		this.featuresBtn = document.createElement("img");
		this.featuresBtn.className = "featuresButton";
		this.featuresBtn.src = "/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_service.png";
		$(this.featuresBtn).observe('click', this.showFeatures.bind(this));
		div.appendChild(this.featuresBtn);
		
		// Details buttons
		var detailsBtn = $(document.createElement("img"));
		detailsBtn.className = "detailsButton";
		detailsBtn.src = "/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_details.png";
		detailsBtn.observe('mouseover', function(){detailsBtn.src="/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_details_ro.png"});
		detailsBtn.observe('mouseout', function(){detailsBtn.src="/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_details.png"});
		detailsBtn.observe('click', function(){window.location='/' + storeLocator.vars.currentPath[1] + '/store-details/'+node.store_id+'/'});
		this.addressPane.appendChild(detailsBtn);

		var directionsURL = 'http://maps.google.com/maps?q=+';
		directionsURL += this.point_.lat()+","+this.point_.lng()+ "(" + (node.address3+" "+node.state_province_abbr+" "+node.postal_code).replace(/ +/g,"+") + ")";

		//original 
		//var directionsURL = 'http://maps.google.com/maps?f=q&q=Dairy+Queen+';
		//directionsURL += (node.address3+" "+node.state_province_abbr+" "+node.postal_code).replace(/ +/g,"+")+"&ll="+this.point_.lat()+","+this.point_.lng()+"&iwloc=A";
		
		var directionsBtn = $(document.createElement("img"));
		directionsBtn.className = "directionsButton";
		directionsBtn.src = "/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_directions.png";
		directionsBtn.observe('mouseover', function(){directionsBtn.src="/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_directions_ro.png"});
		directionsBtn.observe('mouseout', function(){directionsBtn.src="/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_directions.png"});
		directionsBtn.observe('click', function(){window.location=directionsURL});
		this.addressPane.appendChild(directionsBtn);
		
		map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	  this.map_ = map;
	  this.div_ = div;
		//this.shadow_ = shadow;
	},
	remove: function() {
		if (this.div_.parentNode) {
			this.div_.parentNode.removeChild(this.div_);
			storeLocator.vars.infoWindow = null;
		}
	},
	copy: function() {
	  return new DQInfoWindow(this.point_, this.data_);
	},
	redraw: function(force) {
		// Redrawing consists entirely of repositioning the window		
	  if (!force) return;

	  var divPoint = this.map_.fromLatLngToDivPixel(this.point_);
	  this.div_.style.left = divPoint.x + "px";
	  this.div_.style.top = divPoint.y + "px";
	},
	showTooltip: function(img) {
		this.tooltip.innerHTML = img.alt;
		var position = Position.positionedOffset(img);
		this.tooltip.style.top = (position[1]-12)+"px";
		this.tooltip.style.left = (position[0]+12)+"px";
		this.tooltip.show();
	},
	hideTooltip: function() {
		this.tooltip.hide();
	},
	showAddress: function() {
		this.addressBtn.src = "/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_address_ro.png";
		this.featuresBtn.src = "/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_service.png";
		this.addressPane.show();
		this.featuresPane.hide();
	},
	showFeatures: function() {
		this.addressBtn.src = "/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_address.png";
		this.featuresBtn.src = "/templates/img/" + (storeLocator.vars.currentPath[1].toLowerCase()!="ca-fr"?"":"fr/") + "map_btn_service_ro.png";
		this.addressPane.hide();
		this.featuresPane.show();
	}
});

Event.observe(window, 'load', storeLocator.initialize);

window.onunload = function(){
	GUnload()
};

