var map;
var allRouteLatLng;
var obj_array;

 
$(document).ready(function() {
 $("#start_id").autocomplete( base_url+"index.php/bus/bus_stop_name_suggestions/",{minChars:2,scroll:false,cacheLength:5});   
	$("#dest_id").autocomplete(base_url+"index.php/bus/bus_stop_name_suggestions/",{minChars:2,scroll:false,cacheLength:5});
	$(".bus_no_search").autocomplete(base_url+"index.php/bus/bus_route_name_suggestions/",{minChars:1,scroll:false,cacheLength:5});
	
});
 

function validate(){ 	  
 
			  var start_name =  $("#start_id").val() ; 
			   
			  var dest_name = $("#dest_id").val() ; 
			  
			  if(start_name == 0 && dest_name == 0)
			  {
			     alert("Please fill to both form feild");
			     return false;
			  }
			  else if(start_name == 0)
			  {
			     alert("Please fill to start form feild");
			     return false;
			  }
			  else if(dest_name == 0)
			  {
			     alert("Please fill to dest form feild");
			     return false; 
			  }
			  else{   
		          var iChars = "!@#$%^*+=\\\;{}|\":<>?~_"; 
		          
				  for (var i = 0; i < start_name.length; i++) {
				  	if (iChars.indexOf(start_name.charAt(i)) != -1) {
				  	  alert ("Source point has special characters. \nThese are not allowed.");
				  	return false;
				  	}
				  } 
				  
				  for (var i = 0; i < dest_name.length; i++) {
				  	if (iChars.indexOf(dest_name.charAt(i)) != -1) {
				  	  alert ("Destination point has special characters. \nThese are not allowed.");
				  	return false;
				  	}
				  } 
			  } 
		        return true;
} 

function get_route_directions(ele){
    $('.route_names_container').hide();
    map.clearOverlays();
    var route_name = $('#route_no').val();
    
    if (route_name == '') {
        $('.route_direction').hide();
        return false;
    }
    var url = site_url + '/bus/get_route_direction';
    $('.loading_image').show();
    $('.route_direction').hide();
    
    $.post(url, 'route_name=' + route_name, function(resp){
        $('.loading_image').hide();
        $('.route_direction').show();
        var res_count = resp.length;
        //  console.log('length :' + res_count);
        var text;
        if (res_count == 0) {
            text = "<h3> No Trip Direction Found </h3>";
            $('.route_direction').html(text);
            return false;
        }
        
        text = 'Direction :';
		
        for (var i = 0; i < res_count; i++) {
            //  console.log('Route Direction :' + resp[i].route_direction);
            var direction = resp[i].route_direction;
            text += "&nbsp<input type='radio' id='direction' name='direction' value='" + direction + "' onclick=\"return get_bus_route_details('" + route_name + "','" + direction + "');\" >" + direction;
		}
        
        $('.route_direction').html(text);
		var firstEle = $('#direction:first').trigger('click'); 
    }, 'json');
    
}

function get_bus_route_details(bus_route_no, direction){
    map.clearOverlays();
    $('.route_names_container').hide();
    var boundary_box = new GLatLngBounds();
    $('.loading_image').show();
    var url = site_url + '/bus/get_single_bus_route_details';
    var post_data = "bus_route_no=" + bus_route_no + "&route_direction=" + direction;
	//alert(bus_route_no);
	//document.write(bus_route_no);
    allRouteLatLng = Array();
    obj_array = Array();
    $.post(url, post_data, function(resp){
		 map.clearOverlays();
        $('.loading_image').hide();
        
        var res_count = resp.length;
        
        if (res_count == 0) {
            alert('No result found');
            return false;
        }
       // alert(bus_route_no);
		var test="bus_time/" +bus_route_no;
		//alert(test);
		//var list_names1 ="<b>Bus Schedule</b> :<a href=bus_time/"+ bus_route_no +" class=\"thickbox\"> " + bus_route_no + "</a>";
		var list_names1 ='<b>Bus Schedule</b> :<a href="#" onClick=window.open("' + test + '","myWindow","status=1,height=400,width=650,resizable=0,scrollbars=1")>' + bus_route_no + '</a>';
        var list_names = "<ul>";
		var last_count = res_count;
		last_count--;
		var temp_count = 1;
        for (var i = 0; i < res_count; i++) {
            var temp_obj = new Object();
            var lat = resp[i].lat;
            var lon = resp[i].lon;
            var stop_name = resp[i].stop_name;
            var point = new GLatLng(lat, lon);
			var marker;
			
			if(i == 0){
				marker = createpoimarker(base_url + 'images/srcpnt.png', point, stop_name); 
			}else if(i == last_count){
				marker = createpoimarker(base_url + 'images/destpnt.png', point, stop_name);
			}else{
				marker = createpoimarker(base_url + 'images/bmtc_icon.png', point, stop_name);
			}
             
            list_names += "<li> "+temp_count+" ) <a href='' onclick=\"return my_click(" + i + ");\">" + stop_name + " </a></li>";
            map.addOverlay(marker);
            boundary_box.extend(point);
            allRouteLatLng.push(point);
            temp_obj.lat = lat;
            temp_obj.lon = lon;
            temp_obj.name = stop_name;
            temp_obj.marker = marker;
            obj_array.push(temp_obj);
			temp_count++;
        }
        list_names += "</ul>";
        $('.route_names_container').show();
        map.setCenter(boundary_box.getCenter());
        map.setZoom(map.getBoundsZoomLevel(boundary_box));
       var polyline = new GPolyline(allRouteLatLng, '#ff0000', 2, 1, 0.4);
        map.addOverlay(polyline);
        $('.route_names').html(list_names);
		 $('.route_names1').html(list_names1);
    }, 'json');
    
}

/*
function createpoimarker_bmtc(markerimage, point, stop_name){
    var bicon = new GIcon();
    bicon.image = markerimage;
    bicon.iconSize = new GSize(32, 18);
    bicon.iconAnchor = new GPoint(9, 20);
    bicon.infoWindowAnchor = new GPoint(5, 1);
    var temp = new GMarker(point, {
        icon: bicon,
        title: stop_name
    });
    GEvent.addListener(temp, 'click', function(){ 
        temp.openInfoWindow(stop_name);
    });
    return temp;
}
*/

function createpoimarker(markerimage, point, stop_name){
    var bicon = new GIcon();
    bicon.image = markerimage;
    bicon.iconSize = new GSize(12, 20);
    bicon.iconAnchor = new GPoint(9, 20);
    bicon.infoWindowAnchor = new GPoint(5, 1);
    var temp = new GMarker(point, {
        icon: bicon,
        title: stop_name
    });
    GEvent.addListener(temp, 'click', function(){ 
        temp.openInfoWindow(stop_name);
    });
    return temp;
}

function my_click(id){ 
    var temp_obj = obj_array[id];
    var marker = temp_obj.marker;
    marker.openInfoWindow(temp_obj.name); 
    return false;
}

function hightlight(id){

    if (id > 0) {
        $('#disp_direct_route').hide();
        $('#hightlight_container').html($('#displaycontent' + id).html());
        $('#hightlight_container').show();
        $('#showall_link').show();
    }
    else {
        $('#hightlight_container').hide();
        $('#disp_direct_route').show();
        $('#showall_link').hide();
        $('#alighting_points option:first').attr('selected', true);
    }
    load_thickbox();
}
 

 
   
function hightlight(id){

    if (id > 0) {
        $('#disp_direct_route').hide();
        $('#hightlight_container').html($('#displaycontent' + id).html());
        $('#hightlight_container').show();
        $('#showall_link').show();
    }
    else {
        $('#hightlight_container').hide();
        $('#disp_direct_route').show();
        $('#showall_link').hide();
        $('#alighting_points option:first').attr('selected', true);
    }
    load_thickbox();
}
 
function validateRouteField(ele_form){
	var bus_no = $.trim($('.bus_no_search').val());
	if(bus_no == ''){
		alert('Please Enter the Bus Number ');
		return false;
	}else{
		$(ele_form).submit();
	}
}


