/**
 * GE.Dealers.Map.Map Component
 *
 * @author Vitre
 * @alias tDealersMap
 */
(function() {

/**
 * GE.Dealers.Map.Dealer class
 *
 * @author Vitre
 * @alias DealersDealer
 */
var DealersDealer = window.DealersDealer = function(opts) {

	this.Index = 0;
	this.Name;
	this.Id;
	this.Picked;
	this.Image;
	this.Longitude;
	this.Latitude;
	this.Type;
	this.ShortDescription;
	this.DetailUrl;
	this.Map;
	this.Valid = false;

	this.init(opts);
}

DealersDealer.fn = DealersDealer.prototype = {

	init: function(opts) {
		if (opts.Index) {
			this.Index = opts.Index;
		}
		if (opts.Map) {
			this.Map = opts.Map;
		}
		if (opts.Dom) {
			this.initFromDom(opts);
		}
	},

	initFromDom: function(opts) {
		this.Id = jQuery(opts.Dom).attr("Id");
		this.Name = jQuery(opts.Dom).attr("Title");
//		this.Picked = jQuery(opts.Dom).attr("picked");
		this.Longitude = parseFloat(jQuery(opts.Dom).attr("Longitude"));
		this.Latitude = parseFloat(jQuery(opts.Dom).attr("Latitude"));
//		this.Type = this.Map.Dealers.getDealerTypeById(jQuery(opts.Dom).attr("typeid"));
//		alert(this.Type.Uid);
		this.ShortDescription = jQuery(opts.Dom).find("Description").text();
		this.DetailUrl = jQuery(opts.Dom).attr("Url");
		this.Image = { Defined: jQuery("ListImage", opts.Dom).attr("Exists"), Path: jQuery("ListImage", opts.Dom).attr("HttpPath") }
		this.Valid = (!isNaN(this.Longitude)) && (!isNaN(this.Latitude));
		return true;
	},

	toMapMarker: function(opts) {
		var mOpts = { title: this.Name, icon: null, coo: new GLatLng(this.Latitude, this.Longitude) };

    mOpts.icon = new GIcon();
	  mOpts.icon.iconSize = new GSize(32,32);
	  mOpts.icon.shadowSize = new GSize(56,32);
	  mOpts.icon.iconAnchor = new GPoint(16,32);
		mOpts.icon.infoWindowAnchor = new GPoint(16,0);

  	mOpts.icon.image = "http://maps.google.com/mapfiles/kml/pal3/icon18.png";
  	mOpts.icon.shadow = "http://maps.google.com/mapfiles/kml/pal3/icon18s.png";
		if (this.Type) {
	    if (this.Type.Uid == "APARTMENT") {
	    	mOpts.icon.image = "http://maps.google.com/mapfiles/kml/pal3/icon56.png";
	    	mOpts.icon.shadow = "http://maps.google.com/mapfiles/kml/pal3/icon56s.png";
	    } else if (this.Type.Uid == "HOTEL") {
	    	mOpts.icon.image = "http://maps.google.com/mapfiles/kml/pal3/icon21.png";
	    	mOpts.icon.shadow = "http://maps.google.com/mapfiles/kml/pal3/icon21s.png";
	    }
		}
    var result = new GMarker(mOpts.coo, mOpts);
		result.Dealer = this;
    GEvent.addListener(result, "click", eval("false||function() { tDealersMap.fn.markerOnClick(result, " + this.Index + "); }"));
		return result;
	}

}
})();

(function() {
/**
 * GE.Dealers.Map.Dealers.Map Component
 *
 * @author Vitre
 * @alias tDealersMap
 */
var tDealersMap = window.tDealersMap = function(opts) {

	if (opts) {
		this.InstanceName = opts.InstanceName;
	}
	this.Container;
	this.ContainerJq;
	this.MapContainerId;
	this.MapContainerJq;
	this.DataProvider;
	this.Map;
	this.DefaultTarget = { Coo: new GLatLng(50.7675, 15.0549) }; // Liberec Center: 50�46'2.92"N; 15�3'17.76"E
	this.Target;
	this.DefaultZoom = 2;
	this.Zoom = this.DefaultZoom;
	this.Markers = new Array();
	this.MarkersDom;
	this.MarkersCount = 0;
	this.Dealers = new Array();
	this.DealersCount = 0;
	this.DealerPopUpTpl;
	this.Controls = { Large: true, Type: true, Scale: true, Overview: true };
	this.List = { Publish: false };
	this.ListPanelJq;
	this.OpenedDealerId;
	this.CenteredDealerId;
	this.OpenedLocationId;

	this.init(opts);
}

tDealersMap.fn = tDealersMap.prototype = {

	init: function(opts) {
		if (this.Container) {
			this.ContainerJq = jQuery("#" + this.Container);
		}
		return true;
	},

	setDefaultTarget: function(lat, lng) {
		this.DefaultTarget = { Coo: new GLatLng(lat, lng) };
	},

	open: function(opts) {
		this.init();
		if (opts.LocationId) {
			this.OpenedLocationId = opts.LocationId;
		}
		this.showProgressBar();
		if (this.List.Publish) { this.createListGui(); }
		this.createGui();
		this.center();
		this.DealerPopUpTpl = jQuery.createTemplateURL(GeApp.ModulesHttpRoot + "/Dealers.Map/tpl/DealerPopUp.tpl.php");
		this.hideProgressBar();
		return true;
	},

	createGui: function() {
		google.load("maps", "2");

		this.ContainerJq.append("<div id=\"DealersMapCon\"></div>");
		this.MapContainerId = "DealersMapCon";
		this.MapContainerJq = jQuery("#" + this.MapContainerId);
		this.Map = new google.maps.Map2(document.getElementById(this.MapContainerId));

		if (this.Controls.Large) {
	  	this.Map.addControl(new GLargeMapControl());
	  }
	  if (this.Controls.Type) {
	  	this.Map.addControl(new GMapTypeControl());
	  }
	  if (this.Controls.Scale) {
			this.Map.addControl(new GScaleControl());
		}
		if (this.Controls.Overview) {
			this.Map.addControl(new GOverviewMapControl());
		}
		this.Map.enableContinuousZoom();

		jQuery(window.document).bind("unload", GUnload);
		return true;
	},

	Load: function(opts) {
	},

	_markersLoader: function(data, status) {
		this.MarkersDom = data;
		this.initMarkers();
		this.createMarkers();
		if (this.List.Publish) {
			this.bulidGuiList();
		}
		if (this.OpenedDealerId) {
			this.openDealer(this.OpenedDealerId);
		}
		if (this.CenteredDealerId) {
			this.centerDealer(this.CenteredDealerId);
		}
		return (status == "success");
	},

	getDealerIndexById: function(id) {
		var result = false;
		for (var i = 0; i < this.Dealers.length; i++) {
			if (this.Dealers[i].Id == id) {
				result = i;
				break;
			}
		}
		return result;
	},

	openDealer: function(id) {
		var index = this.getDealerIndexById(id);
		this.markerOnClick(DealersMap.Markers.Items[index], index);
		return true;
	},

	centerDealer: function(id) {
		var index = this.getDealerIndexById(id);
		this.Map.setCenter(this.Markers.Items[index].getLatLng());
		return true;
	},

	initMarkers: function() {
		var items = jQuery("data > item", this.MarkersDom);
		this.Markers = new GMarkerManager(this.Map);
		this.Markers.Items = new Array();
		var mI = -1;
		for (var i = 0; i < items.length; i++) {
			var item = items.get(i);
			var Dealer = new DealersDealer({ Dom: item, Map: this, Index: this.DealersCount });
			if (Dealer.Valid) {
				mI++;
				var Marker = Dealer.toMapMarker();
				this.Dealers[this.DealersCount] = Dealer;
	//			alert(this.Dealers[this.DealersCount].Name);
				this.DealersCount++;
		  	this.Markers.Items[mI] = Marker;
		  	this.Markers.addMarker(Marker, 0);
		  	this.MarkersCount++;
		  }
		}
		/*
		for (var i = 0; i < this.Dealers.length; i++) {
			alert(this.Dealers[i].Name);
		}
		*/
		return true;
	},

	createMarkers: function() {
		this.Markers.refresh();
		this.hideProgressBar();
		return true;
	},

	loadMarkers: function() {
		this.showProgressBar();
		var url = this.DataProvider.Url;
		if (this.OpenedLocationId) {
			url += "&LocationId=" + this.OpenedLocationId;
		}
		jQuery.get(url, {}, eval("false||function(data, status) { var Sender = " + this.InstanceName + "; Sender._markersLoader(data, status); }"));
		return true;
	},

	initDefaultTarget: function() {
		this.Target = this.DefaultTarget;
		return true;
	},

	center: function() {
		if (!this.Target) {
			this.initDefaultTarget();
		}
		this.Map.setCenter(this.Target.Coo, this.Zoom);
	},

	showProgressBar: function() {
		if (this.Map) {
			this.Map.disableScrollWheelZoom();
		}
		this.ContainerJq.block({ message: "Loading..." });
	},

	hideProgressBar: function() {
		this.Map.enableScrollWheelZoom();
		this.ContainerJq.unblock();
	},

	markerOnClick: function(marker, Index) {
		var tplData = { Dealer: {
			Id: marker.Dealer.Map.Dealers[Index].Id,
			Name: marker.Dealer.Map.Dealers[Index].Name,
			ShortDescription: marker.Dealer.Map.Dealers[Index].ShortDescription,
			Type: marker.Dealer.Map.Dealers[Index].Type,
			DetailUrl: marker.Dealer.Map.Dealers[Index].DetailUrl,
			Latitude: marker.Dealer.Map.Dealers[Index].Latitude,
			Longitude: marker.Dealer.Map.Dealers[Index].Longitude,
			Image: marker.Dealer.Map.Dealers[Index].Image
		} };
		var html = marker.Dealer.Map.DealerPopUpTpl.get(tplData, false, false);
		marker.WindowHtml = html;
    marker.openInfoWindowHtml(html);
	},

	createListGui: function() {
		this.ListPanelJq = this.ContainerJq.append("<div class=\"MarkersPanel\"><div class=\"In\"></div></div>").find(".MarkersPanel");
		this.ContainerJq.addClass("DealersMap_WithList");
	},

	bulidGuiList: function() {
		var ulJq = this.ListPanelJq.find("div.In").append("<ul class=\"ulMarkers\"></ul>").find("ul");
		for (var i = 0; i < this.Markers.Items.length; i++) {
			ulJq.append("<li><a id=\"aMarkerList_" + i + "\" href=\"#none\">" + this.Markers.Items[i].Dealer.Name + "</a></li>");
			jQuery("#aMarkerList_" + i).click(eval("false||function() { DealersMap.markerOnClick(DealersMap.Markers.Items[" + i + "], " + i + "); }"));
		}
		return true;
	},

	loadLocationsMarkers: function(opts) {
		this.LocationPopUpTpl = jQuery.createTemplateURL(GeApp.ModulesHttpRoot + "/Dealers.Map/tpl/LocationPopUp.tpl.php")
		for (var k in opts.Locations) {
			var icon = new GIcon();

		  icon.iconSize = new GSize(32,32);
		  icon.shadowSize = new GSize(56,32);
		  icon.iconAnchor = new GPoint(16,32);
			icon.infoWindowAnchor = new GPoint(16,0);

	  	icon.image = "http://maps.google.com/mapfiles/kml/pal3/icon20.png";
	  	icon.shadow = "http://maps.google.com/mapfiles/kml/pal3/icon20s.png";
			if (opts.Locations[k].Type == "RACEGROUND") {
		  	icon.image = "http://maps.google.com/mapfiles/kml/pal3/icon20.png";
		  	icon.shadow = "http://maps.google.com/mapfiles/kml/pal3/icon20s.png";
			} else if (opts.Locations[k].Type == "PRESSCENTER") {
		  	icon.image = "http://maps.google.com/mapfiles/kml/pal3/icon35.png";
		  	icon.shadow = "http://maps.google.com/mapfiles/kml/pal3/icon35s.png";
			}

			var marker = new GMarker(new GLatLng(opts.Locations[k].Latitude, opts.Locations[k].Longitude), icon);
			var location = opts.Locations[k];
			marker.bindInfoWindow(this.LocationPopUpTpl.get(location, false, false));
			this.Map.addOverlay(marker);
		}
	}

}

})();