/*++++++++++++++++++++++++++++++++++

Purpose:
Custom Javascript for the store locator vodafone.ie

Created by:
Colm McBarron (iQ Content)

Change Log:
[VOP_i02] - Production ready
++++++++++++++++++++++++++++++++++*/


/*
========================================
	Global variables
========================================
*/

var map;
var baseIcon = new GIcon();
var gmarkers = [];
var icon_folder_location = '/images/design/' 
var mapsxml = '/download?id=STORELOCATOR';
var bounds = null;


/*
========================================
	Initialise the page
========================================
*/

$(
	function () {
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("store-locator-map"));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(53.5, -8), 7);

			// Create a base icon for all of our markers that specifies the
			// shadow, icon dimensions, etc.
			
			baseIcon.shadow = icon_folder_location + "pin_shadow.png";
			baseIcon.iconSize = new GSize(32, 32);
			baseIcon.shadowSize = new GSize(59, 32);
			baseIcon.iconAnchor = new GPoint(16, 32);
			baseIcon.infoWindowAnchor = new GPoint(18, 28);
			baseIcon.infoShadowAnchor = new GPoint(18, 28);

			loadStores();
			$('#county-list').change(function(){
				showCounty($('#county-list').val());
			});
		}
	}
)

/*
========================================
	Load the stores onto the map
========================================
*/

function loadStores() {
	var bounds = map.getBounds();
	$.ajax({
		type: "GET",
		url: mapsxml,
		cache: false,
		dataType: "xml",
		success: function(xml) {
			
			$(xml).find('storeListing').each(function() {
				var storeArray = new Array();
				
				storeArray['store_name'] = $(this).find('storeName').text();
				storeArray['address_line1'] = $(this).find('addressLine1').text();
				storeArray['address_line2'] = $(this).find('addressLine2').text();
				storeArray['county'] = $(this).find('county').text();
				storeArray['telephone'] = $(this).find('telephone').text();
				storeArray['fax'] = $(this).find('fax').text();
				storeArray['latitude'] = $(this).find('lat').text();
				storeArray['longitude'] = $(this).find('long').text();
				storeArray['pin_type'] = $(this).find('pinType').text();
				map.addOverlay(createMarker(storeArray));
				
			});
		}
	});
}

/*
========================================
	Create markers with custom pins
========================================
*/

function createMarker(sArr) {
	var p = new GLatLng(sArr['latitude'],sArr['longitude']);
	var c_pin = new GIcon(baseIcon);
  	c_pin.image = icon_folder_location + "pin_" + sArr['pin_type'] + ".png";
	
	if (sArr['pin_type'] == 'vfretail') {
		c_pin.iconSize = new GSize(51, 52);
		c_pin.shadowSize = new GSize(59, 32);
		c_pin.iconAnchor = new GPoint(29, 52);
		c_pin.infoWindowAnchor = new GPoint(26, 53);
		c_pin.infoShadowAnchor = new GPoint(26, 53);
	}
	m_opts = { icon:c_pin };
	var m = new GMarker(p, m_opts);
	m.county = sArr['county'];
	m.lat = sArr['latitude'];
	m.lng = sArr['longitude'];
	GEvent.addListener(m, "click", function() {
		html = generateGMinfowin(sArr);
    	m.openInfoWindowHtml(html);
  	});
	gmarkers.push(m);
  	return m;
}

/*
========================================
	Generate the window when you click on a pin
========================================
*/

function generateGMinfowin(sArr) {
	var html = '<div class="store-locator-bubble">';
	if (sArr['pin_type'] != 'standard') {
		html += '<div class="image"><img src="' + icon_folder_location + 'logo_' + sArr['pin_type'] + '.png" /></div>'
		html += '<div class="info">'
	}
	html += "<strong>" + sArr['store_name'] + "</strong><br />";
	html += sArr['address_line1'] + "<br />";
	if (sArr['address_line2']) {
		html += sArr['address_line2'] + "<br />";
	}
	html += sArr['county'] + "<br />";
	if (sArr['telephone']) {
		html += "Phone: " +  sArr['telephone'] + "<br />";
	}
	if (sArr['fax']) {
	html += "Fax: " +  sArr['fax'];
	}
	if (sArr['pin_type'] != 'standard') {
		html += '</div>';
	}
	html += '</div>';
	return html;
}

/*
========================================
	Show pins only in one county
========================================
*/

function showCounty(c) {
	bounds = null;
	bounds = new GLatLngBounds();
        if (c != '-') {
			for (var i=0; i<gmarkers.length; i++) {
				if (gmarkers[i].county == c) {
            		gmarkers[i].show();
					point = new GLatLng(parseFloat(gmarkers[i].lat), parseFloat(gmarkers[i].lng));
					bounds.extend(point);
				} else {
					gmarkers[i].hide();
				}
        	}

		} else {
			for (var i=0; i<gmarkers.length; i++) {
				gmarkers[i].show();
				point = new GLatLng(parseFloat(gmarkers[i].lat), parseFloat(gmarkers[i].lng));
				bounds.extend(point);				
			}
		}
	map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
}