/*
* code specifically for timetables.cfm
*/ 
var $jq = jQuery.noConflict();
$jq(document).ready(function(){
	
	var currSelectedArrow = $jq('#currSelectedArrow');
	
	$jq("#busMode #route, #busMode #stop").autocomplete({
		source: function(request, response) {
			if ( $jq(this).attr('id') === 'route' ) {
				$jq('#routeNumber').val('');
			} else {
				$jq('#stopNumber').val('');
			}
			
			$jq.ajax({
				url: MAXX['jpHost'] + '/iptis/ajax/' + (this.element.attr('id') === 'stop' ? 'stops-jsonp.asp' :  'locations-jsonp.asp?type=routesuburb'),
				dataType: "jsonp",
				data: request,
				success: function(data) {
					response(data);
				}
			});
		},
		select: function(event, ui) {
			if ( $jq(this).attr('id') === 'route' ) {
				$jq('#routeNumber').val(ui.item.id);
			} else {
				$jq('#stopNumber').val(ui.item.id);
			}
		}
	});
	
	/*var zIndexNumber = 1000;
	$jq('.timetable div').each(function() {
		$jq(this).css('z-index', zIndexNumber);
		zIndexNumber -= 2;
	});*/
	
	$jq('#busMode input').hasText();
	
	$jq('.journeyMenu .journeyOption').hover(function () {
		if ( $jq(this).hasClass('clickable') ) {
			$jq(this).addClass('hilight');
		}
	}, function () {
		$jq(this).removeClass('hilight');
	});
	
	$jq('.viewAll').click(function() {
		var visableDetails = $jq('.journeyDetails:visible');
		
		if ( visableDetails.length == 1 ) {
			var jDet = $jq('.journeyDetails');
			jDet.show(300);
			$jq(this).html('Hide All');
			return false;
		} else {
			var curr_option = $jq('.journeyMenu .journeyOption').filter('.disabled');
			curr_option.addClass('clickable');
			curr_option.find('ol *').css({'opacity': '1'});
			curr_option.click();
			return false;
		}
	});
	
	var animating = false;
	$jq('.journeyMenu .journeyOption').bind('click', function () {
		
		if( $jq(this).hasClass('clickable') && animating === false) {
			animating = true;
			var curr_option = $jq('.journeyResults .journeyOption');
			var curr_plan = curr_option.attr('plan');
			var curr_trip = curr_option.attr('trip');
			var new_trip = $jq(this).attr('trip');
			var new_content = $jq(this).find('ol').html();
			var new_journey = $jq(this).attr('journey');
			var queryString = curr_option.attr('queryString');
			
			curr_option.attr('trip', new_trip);
			
			var target = curr_option.find('ol');
			target.fadeOut('300', function () {
				target.html(new_content);
				target.fadeIn('300');
			});
			
			var curr_journeyDetail = $jq('.journeyDetails[trip=' + curr_trip + ']');
			var new_journeyDetail = $jq('.journeyDetails[trip=' + new_trip + ']');
			var curr_height = curr_journeyDetail.height();
			var new_height = new_journeyDetail.height();
			var panel_support = $jq('.support');
			
			
			$jq('.journeyDetails:visible').fadeOut(100, function () {
				panel_support.animate({'height': new_height}, 100, function () {
					new_journeyDetail.fadeIn(100, function () {
						return;
					});
				});
			});
			
            $jq('html, body').animate({scrollTop: '0px'}, 300);
			
			//$jq('.viewAll').html('Show All');
			
			currSelectedArrow.css({
				'top':$jq(this).position().top+'px'
			});
			
			$jq(this).siblings().addClass('clickable').removeClass('disabled').find('ol *');/*.animate({'opacity': '1.0'}, 300);
			$jq(this).find('ol *').animate({'opacity': '0.1'}, 300);*/
			$jq(this).removeClass('clickable').addClass('disabled').removeClass('hilight');
		}
		
		animating = false;
		return false;
	});
	/*allows the user to see why the first element is disabled*/
	$jq('.journeyMenu .journeyOption[trip=1]').click();
	
	var journeyDetails = $jq('.journeyDetails');
	
	$jq('.timetable .mover').ppdrag();
	
	$jq('#stop').focus(function() {
		$jq('#route').val("");
		return true;
	});
	
	$jq('#route').focus(function() {
		$jq('#stop').val("");
		return true;
	});
	
	$jq('#trainPDF').focus(function() {
		$jq('#trainHTML').val(""); // select the first element which is empty string
		return true;
	});
	
	$jq('#trainHTML').focus(function() {
		$jq('#trainPDF').val(""); // select the first element which is empty string
		return true;
	});
	
	$jq('#ferryPDF').focus(function() {
		$jq('#ferryHTML').val(""); // select the first element which is empty string
		return true;
	});
	
	$jq('#ferryHTML').focus(function() {
		$jq('#ferryPDF').val(""); // select the first element which is empty string
		return true;
	});
	
	$jq('.print').click(function () {
		var thisWindow = $jq(this).parents('.journeyDetails');
		var otherPanels = thisWindow.siblings().each(function (){
			$jq(this).hide();
		});
		window.print();
	});
	
	$jq('.printAll').click(function () {
		var jDet = $jq('.journeyDetails');
		var len = jDet.length;
		var i = 1;
		jDet.show(300, function () {
			if ( i === len ) {
				window.print();
			}
			i++;
		});
	});
	
	
	$jq('#busMode .mode').val('bus');
	$jq('#trainMode .mode').val('train');
	$jq('#ferryMode .mode').val('ferry');
	$jq('.mode').change(function() {
			
		var transportMode = $jq(this).val();
		$jq('.mode').val(transportMode);
		$jq('#busMode').hide();
		$jq('#trainMode').hide();
		$jq('#ferryMode').hide();
		$jq('#' + transportMode + 'Mode').show();
		
		 // clearing the field when a different mode is selected
		$jq('#route').val("");
		$jq('#ferryPDF').val("");
		$jq('#trainPDF').val("");
		
		return true;
	});
	
	var fms_map;
	var fms_dialog;
	
	fms_map = $jq('#map_canvas').fms({
		stopNumber: '7106',
		stopText: '7106',
		stopClickedCallBack: function (stop) {
			$jq('#stopNumber').val(stop['number']);
			$jq('#stop').val(stop['number']);
			fms_dialog.dialog( "close" );
			pageTracker._trackEvent('Find My Stop', 'Stop Selected', stop['number']);
		},
		mapSearchCallBack: function (searchString) {
			pageTracker._trackEvent('Find My Stop', 'Search', searchString);
		}
	});
	
	fms_dialog = $jq("#fms_container").dialog({
		title: 'Enter a location to find your bus stop, then simply click on the stop icon<img class="clickable help2" src="/images/helpIcon_24x24.png" alt"Help" style="position:absolute; top:1px; right:30px;"></img>',
		height: 420,
		width: 700,
		minHeight: 400,
		minWidth:700,
		modal: true,
		autoOpen: false,
		open: function(event, ui) {
			fms_map.trigger('updateStopNumber', $jq('#stopNumber').val());
			fms_map.trigger('updateStopText', $jq('#stop').val());
			fms_map.trigger('show');
		},
		close: function () {
			fms_map.trigger('close');
		},
		resize: function() {
			fms_map.trigger('resize');
		}
	});
	
	$jq(".findMyStopButton").click( function () {
		fms_dialog.dialog('open');
		return false;
	});
			
});
