var GBrowserIsCompatible = [];
var MalFixtures = {
	
	has_init	: false,
	
	init : function() {
		if(MalFixtures.has_init) return;
		
		MalFixtures.has_init = true;
		
		window.scroll = new Fx.Scroll($('fixtures'));
		window.scroll.set(0,0);
		window.current_scroll = 0;
		
		top.malsport_collapseAll = function() {
			$$("#fixtures ul li.active").each(function(el2) {
				el2.tween('background-color', "#0D3F2C", "#9EB2AA");
				el2.className = "";
				el2.morph({
					'height' : '91px'
				});
			});
		}
		
		$$('#fixtures ul li').each(function(el) {
			if(el.className != "footer")
			{
				el.addEvent("click", function() {
					if(el.className == 'active')
						return true;
					malsport_collapseAll();
					
					el.className = "active";
					el.tween('background-color', "#9EB2AA", "#0D3F2C");
					el.morph({
						'height' : '182'
					});
					top._scrollto = this;
					setTimeout(function() {
						scroll.toElement(top._scrollto);
						window.current_scroll = scroll.to[1];
						top._scrollto = null;
					}, 550);
					
					// Close any open markers
					if(typeof this.previousSibling=='undefined') {
						top._scrollto = this;
					} else {
					try {
					if(typeof this.previousSibling.tagName=='undefined') {
						top._scrollto = this.previousSibling.previousSibling;
					} else 
					if(typeof(this.previousSibling)!='undefined'){
						top._scrollto = this.previousSibling;
					} else {
						top._scrollto = this;
					}} catch(e) {
						top._scrollto = this;
					}
					}
					
					/* try {
						top.markers[el.id].openInfoWindowHtml(el._body);
					} catch(e) {
						
						new Request.JSON({
							url: '/typo3conf/ext/ev_sports_fixtures/pi1/fixtures.php?event=' + el.id,
							onComplete: function(events) {
								events.each(function(event) {
									
									try {
			 							//alert(el.id);
										window.showAddress(event.location + ', uk');
										//alert(event.location);
										var lookup = event.ajdlocation;
										if (lookup.length < 5) {
											var lookup  = event.location + ', UK';
											}
										//alert(lookup);
										window.geocoder.getLatLng( lookup ,
											function(point) {
												try {
													if (!point) {
														//alert('no point found for address: ' + lookup);
													} else {
														window.thepoint = point;
														var marker = new GMarker(
															point,
															{draggable: false}
														);
													
														$('event_' + event.id)._body = event['body'];

														// REMOVED BALLOON
														
														
														marker._dbg_point = point;
														marker._location = event.location;

														// Removed ballon
														//GEvent.addListener(marker, "click", evnt);

														// TODO: open balloon when clicking on RHS.
														top.markers['event_' + event.id] = marker;

														mgr.addMarker(marker, 0, 15);
														
														top.openmarker = marker;
														setTimeout(function() {
															window.map.setCenter(top.openmarker.getLatLng());
															
															//top.openmarker.openInfoWindowHtml('testing..');
															
															//window.map.setCenter(top.openmarker.getLatLng());
															
															//$('event_' + event.id).fireEvent('click');
														}, 500);
														
													}
												} catch(e) {
													alert(e.message);
												}
											}
										);
									} catch(e) {
										alert(e.message);
									}
								});
							}
						}).get({'type' : 'json'});
					} */
				});
			}
		});
		
		/*top.markers = {};
		
		if (GBrowserIsCompatible()) {
			window.map = '';
			window.geoXml = '';
			window.map = new GMap2(document.getElementById("map"));
			window.map.setCenter(new GLatLng(52.093008, -2.338715), 13);
			window.geocoder = new GClientGeocoder();

			window.map.addControl(new GLargeMapControl());
			
			window.point;
			
			  window.showAddress = function (address) {
				  return window.geocoder.getLatLng(
					address,
					function(point) {
					  if (!point) {
						window.point = null;
					  } else {
						window.point = point;
					  }
					}
				  );
				}
			
			window.mgr = new MarkerManager(map, { trackMarkers: true });
			
			
			new Request.JSON({
				url: '/typo3conf/ext/ev_sports_fixtures/pi1/fixtures.php',
			//	url: 'http://malvernstage.3evoffice.co.uk/fileadmin/rss/fixtures.php',
				onComplete: function(events) {
					events.each(function(event) {
						
						try {
 							//alert(event.location);
							//window.showAddress(event.location + ', uk');
							
							
						} catch(e) {
							alert(e.message);
						}
					});
				}
			}).get({'type' : 'json'});
			
			
			setTimeout(function() {
				window.mgr.refresh();
			}, 750);
			
		} */
		
		$('up').addEvent('click', function(e) {
			e = new Event(e).stop();
			top.malsport_collapseAll();
			//scroll.start(0, window.current_scroll - 92);
			scroll.start(0, window.current_scroll - (92*5));
			window.current_scroll = scroll.to[1];
			var eL = $$('#fixtures ul');
			eL.setStyle('background', '#9EB2AA none repeat scroll 0 0');
		});
		
		
		
		$('down').addEvent('click', function(e) {
			e = new Event(e).stop();
			top.malsport_collapseAll();
			//scroll.start(0, window.current_scroll + 92);
			scroll.start(0, window.current_scroll + (92*5));
			window.current_scroll = scroll.to[1];
			 var eL = $$('#fixtures ul');
                        eL.setStyle('background', '#9EB2AA none repeat scroll 0 0');
		});
		
		var elList = $$('#fixtures ul li');
		top._currentItem = elList[0];
		
	}
};


window.addEvent("domready", function(){
	try {
		MalFixtures.init();
	} catch(e) {
		alert(e.message);
	}
});


setTimeout(function() {
	try {
		MalFixtures.init();
	} catch(e) {
		// try again
		MalFixtures.has_init = false;
		setTimeout(function() {
		try {
			MalFixtures.init();
		} catch(e) {
			//
		}}, 4000);
	}
}, 4000);
