/**
 * @fileoverview
 * @author			Michael Ord <a href="mailto:michael.ord@think.eu">michael.ord@think.eu</a>
 * @version			0.1
 * @revision		2007-06-01 00:00:00
 * @class
 * @requires		YAHOO.util.Dom
 * @requires		YAHOO.util.Event
 *
 * @file			external-links.js
 * @description		DESCRIPTION HERE
 */

var $DOM			= YAHOO.util.Dom;
var $CREATE			= YAHOO.util.Dom.create;
var $EVENT			= YAHOO.util.Event;

$EVENT.onAvailable("iTabGroupServices", function()
{
	var el		= $DOM.get('iServiceListing');
	if(el)
	{
		$DOM.insertAfter(el, this);
	}
});

// an array to hold the requests for the services - so if a user checks a box before the request has completed, we can stack them up
var requestCalls	= new Array();
// this is should be pulled from the page
var dat_id			= null;
//
var googleMap		= null;
//
var servicesOrder	= new Array;

/**
 * Handle Ajax Success
 */
var handleSuccess = function(o)
{
	// remove the element from the request array
	var this_id		= o.argument.id

	// if there is some text to display
	if(o.responseText !== undefined)
	{
		var el		= $DOM.get('iServiceType-' + this_id)

		if(!el)
		{
			var div		= $DOM.get('iServiceListing');
			if(div)
			{
				var holder			= $CREATE('div', {className:'ajax-wrapper'});
				holder.innerHTML	= o.responseText;

				var last_element	= null;

				for(var i = 0; i < servicesOrder.length; i++)
				{
					// check to see if the element is available
					var el				= $DOM.get('iServiceType-' + servicesOrder[i]);

					if(el)
					{
						last_element	= el
					}

					// if this is the position we need to add the element to
					if(this_id == servicesOrder[i])
					{
						if(last_element)
						{
							// insert after last element
							$DOM.insertAfter(holder, last_element);
						}
						else
						{
							div.appendChild(holder);
						}
					}
				}
			}
		}
	}
}

/**
 * handle failure of the ajax request
 */
var handleFailure = function(o)
{
	if(o.responseText !== undefined)
	{
		alert("handleFailure");
	}
}

var markerList = new Array()

/**
 *
 */
function showServices(id)
{
	// if we cannot find the service in the javascript, there is no point continuing
	if(!mapdata.services[id]){
		return
	}

	var regEx		= /([0-9]+)/			// regular expression to parse the
	var matches		= id.match(regEx);		// pull the id from the id variable
	var service_id	= Number(matches[1]);	// the service id, is a number that corresponds to the li that the selected checkbox is wrapped in

	// try to find the corresponding element in the page
	var el			= $DOM.get('iServiceType-' + service_id);

	if(el)
	{
		$DOM.removeClass(el, 'jsHide')
	}
	else
	{
		// make an ajax request to load the information about that service
		var request = YAHOO.util.Connect.asyncRequest('GET', '/local-services/javascript/services.aspx?dat=' +dat_id + '&service-type=' + service_id, {success:handleSuccess, failure:handleFailure, argument:{id:service_id}});
	}

	var foo = function()
	{
		//alert(this._ntcdat_id)
		var el	= $DOM.get('iServiceItem-' + this._ntcdat_id)
		if(el)
		{
			this.openInfoWindowHtml('<div class="map-bubble">'+el.innerHTML+'</div>');
		}
	}

	// get the markers from the javascript
	var markers		= mapdata.services[id];
	// loop through the markers
	for(var i = 0; i < markers.length; i++)
	{
		// quick reference to the marker
		var marker	= markers[i];
		//
		if(!markerList[marker.id])
		{
			if(marker.longitude == 0 && marker.latitude == 0){
			}else{

				var icon		= new GIcon(G_DEFAULT_ICON);
				icon.image = "/_assets/css/images/icon/imgMarker"+service_id+".png";
				icon.iconSize = new GSize(34, 30);
				icon.iconAnchor = new GPoint(11,28);
				var markerOptions = { icon:icon };

				var point		= new GPoint (marker.longitude, marker.latitude );
				var m			= new GMarker ( point, markerOptions );
				m._ntcdat_id 	= marker.id;

				markerList[marker.id] = m

				GEvent.addListener(m, "click", foo);
			}
		}
		if(markerList[marker.id]){
			// add the marker to the overlay
			googleMap.addOverlay(markerList[marker.id]);
		}
	}
}

/**
 *
 */
function hideServices(id)
{
	// if we cannot find the service in the javascript, there is no point continuing
	if(!mapdata.services[id]){
		return
	}

	var regEx		= /([0-9]+)/			// regular expression to parse the
	var matches		= id.match(regEx);		// pull the id from the id variable
	var service_id	= Number(matches[1]);	// the service id, is a number that corresponds to the li that the selected checkbox is wrapped in

	// try to find the corresponding element in the page
	var el			= $DOM.get('iServiceType-' + service_id);

	if(el)
	{
		$DOM.addClass(el, 'jsHide')
	}

	// get the markers
	var markers		= mapdata.services[id];
	// loop through the markers
	for(var i = 0; i < markers.length; i++)
	{
		// quick reference to the marker
		var marker	= markers[i];
		// remove the marker from the map
		if(markerList[marker.id])
		{
			googleMap.removeOverlay(markerList[marker.id]);
		}
	}
}

/**
 *
 */
function PageLoad_Init_Map ()
{
	if(!mapdata){
		return false;
	}

	var el			= $DOM.get('iRegionalServices');

	if(el)
	{
		var cn		= el.className;
		var matches	= cn.match(/dat-([0-9]+)/);

		if(matches)
		{
			dat_id	= Number(matches[1])
		}
	}

	google_map 		= document.getElementById("iGoogleMap");

	if ( google_map && GBrowserIsCompatible ( ) )
	{
		googleMap 		= new GMap2(google_map);
		var _geocoder	= new GClientGeocoder();

		var _centre		= new GLatLng( mapdata.latitude, mapdata.longitude );

		googleMap.setCenter ( _centre, mapdata.zoom );
		googleMap.addControl(new GLargeMapControl());

		googleMap.addControl(new GScaleControl());
		googleMap.addControl(new GMapTypeControl());

		// Set up clean up routine
		if(googleMap) window.onunload = GUnload;
	}

	var checkboxes		= $DOM.getElementsBySelector("div#iServiceTypes ul li input");

	var onCheck = function(ev, obj)
	{
		var el			= this;

		if(obj)
		{
			if(obj.target) {
				el			= obj.target
			}
		}

		var li			= $DOM.getAncestorsByClass(el, null, 'li', 10, true);

		var id			= $DOM.generateId(li);
		// regular expression to parse the
		var regEx		= /([0-9]+)/
		// pull the id from the id variable
		var matches		= id.match(regEx);
		// the service id, is a number that corresponds to the li that the selected checkbox is wrapped in
		var service_id	= Number(matches[1]);

		if(el.checked)
		{
			showServices($DOM.generateId(li.id))

			$DOM.addClass(li, 'selected' )
			$DOM.addClass(li, 'service-' + service_id )
		} else {

			hideServices($DOM.generateId(li.id))

			$DOM.removeClass(li, 'selected')
			$DOM.removeClass(li, 'service-' + service_id )
		}
	}

	// loop through the checkboxes
	for(var i = 0; i < checkboxes.length; i++)
	{
		// get a reference to the checkbox
		var checkbox	= checkboxes[i];
		// get the parent list item
		var parent_li	= $DOM.getAncestorsByClass(checkbox, null, 'li', 10, true);
		// get the id od the list item
		var id			= $DOM.generateId(parent_li);
		// regular expression to parse the
		var regEx		= /([0-9]+)/
		// pull the id from the id variable
		var matches		= id.match(regEx);
		// the service id, is a number that corresponds to the li that the selected checkbox is wrapped in
		var service_id	= Number(matches[1]);
		// add the id to the services order
		servicesOrder.push(service_id);
		// if the check box is checked
		if(checkbox.checked)
		{
			onCheck(false,{target:checkbox})
		}
		$EVENT.addListener ( checkbox, "click", onCheck );
	}
};

$EVENT.onDOMReady ( PageLoad_Init_Map );
