/*
 * jGMap jQuery Plug-in - "Outside" functions
 * http://fazchanj.com/jGMap
 * jQuery Google Maps Wrapper
 *
 * Copyright (c) 2010 Samuel Rouse
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 * Requires jQuery 1.4+, tested with 1.4.2
 *
 * Date: 2010-12-29
 * Revision: 0

 * This contains the standard outside functions for the mapper
 
  
 */

function titleCase(str, glue){
/*
 * @param String str The text to be converted to titleCase.
 * @param Array glue the words to leave in lowercase. 
 */
glue = (glue)?glue:new Array(
        'of',
        'for',
        'and'
);
if (str){
return str.replace(/(\w)(\w*)/g, function(_, i, r){
    var j = i.toUpperCase() + (r != null ? r : "");
    return (glue.indexOf(j.toLowerCase())<0)?j:j.toLowerCase();
});
} else {
	return "";
}
}

var ptGetIcon = function (pinid) {
    // Three different pin colors
    var curImg = "";

    if (pinid == null) {
        switch ($.fn.jGMap.curPoint % 3) {
            case 0:
                curImg = '/PHDModules/maps/images/red-pushpin.png';
                break;
            case 1:
                curImg = '/PHDModules/maps/images/blue-pushpin.png';
                break;
            case 2:
                curImg = '/PHDModules/maps/images/grn-pushpin.png';
                break;
        }

        // Adjust for offset of pin point.
        return new google.maps.MarkerImage(curImg, new google.maps.Size(32, 32), new google.maps.Point(0, 0), new google.maps.Point(9, 32));
    } else {
        // Get the pin info from the array $.fn.jGMap.pins
        for (var curPin in $.fn.jGMap.pins) {
            p = $.fn.jGMap.pins[curPin];
            if (p.pin_id == pinid) {
                // Found the correct pin
                return new google.maps.MarkerImage($.fn.jGMap.options.pinBasePath + p.pin_image, new google.maps.Size(p.pin_width, p.pin_height), new google.maps.Point(0, 0), new google.maps.Point(p.pin_offset_x, p.pin_offset_y));

            }
        }


    }
};
var ptClick = function(map,pin,idx){
	
	var myIW = new google.maps.InfoWindow();
	
	// Create the content you want
	var myContent = $.fn.jGMap.points[idx].chrcomment != null ? $.fn.jGMap.points[idx].chrcomment : $.fn.jGMap.points[idx].intcount != null ? $.fn.jGMap.points[idx].intcount : " ";
	if (myContent == " ") {
		myContent = titleCase($.fn.jGMap.points[idx].chrcity) + ", " + ( $.fn.jGMap.points[idx].chrstate.length > 2 ? titleCase($.fn.jGMap.points[idx].chrstate) : $.fn.jGMap.points[idx].chrstate.toUpperCase());
		
	}
	myIW.setContent(myContent);
	
	// Pop the InfoWindow.
	myIW.open(map,pin);	
	
	google.maps.event.addListenerOnce(myIW,'closeclick',function(){
		// Add the standard listener to the pin.
		google.maps.event.addListenerOnce(pin,'click',function(){
			ptClick(map,pin,idx);
		});
	});
};

$(function(){
    $('#map_canvas').jGMap({
        ptDrop: false,
        ptDropDelay: 0,
		pointURL:'/PHDModules/maps/resources/ajax-phd.asp',
		pointURLData: { 'section': 'GetPoints', 'mapid': mapid },
		pinURL: '/Manage/map/resources/ajax-phd.asp',
		pinURLData: { 'section': 'GetPins' },
		pinBasePath: '/PHDModules/maps/images/',
		mapOpt: {},
		mapOptURL:'/PHDModules/maps/resources/ajax-phd.asp',
		mapOptURLData: { 'section' : 'GetMapData', 'mapid' : mapid },
		mapData: {
		    ptDrop: false,
            ptDropDelay: 0,
            ptGetIcon: function () { return ptGetIcon($.fn.jGMap.points[$.fn.jGMap.curPoint].intpinid); }, 
			ptDblClick : false,
			ptDblClickOnce : false,
			ptClick : false,
			ptClickOnce : true
		}
	});
});


