/*
 * Ext JS Library 2.2.1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

var menu;
var autoMapType = "on";	



Ext.onReady(function(){
	//Ext.QuickTips.init();
 if(mapTypeVar=="all"){
    menu = new Ext.menu.Menu({
        id: 'mainMenu',
        items: [
            {
          		text: '<img src="images/Ship_icon_15.png">&nbsp;Survey Projects',
                checked: true,       // when checked has a boolean value, it is assumed to be a CheckItem
                id: 'surveyCheckID',
				checkHandler: toggleSurvey
            },		
			{
                text: '<img src="images/shovelIcon2_15.png">&nbsp;Restoration Projects',
                checked: true,       // when checked has a boolean value, it is assumed to be a CheckItem
                id: 'restorationCheckID',
				checkHandler: toggleRestoration
            },
            {
                text: 'Google Map Search',
                checked: false,       // when checked has a boolean value, it is assumed to be a CheckItem
                checkHandler: googleSearch
            },
            {
                text: 'Map Title',
                checked: true,
				id: 'titleCheckID',
                checkHandler: titleWindow
			},
            {
                text: 'Compact Controls',
                checked: false,
				id: 'controlSize',
                checkHandler: changeControlSize	
            }, '-', {
                text: 'Map Background',
                menu: {        // <-- submenu by nested config object
                    items: [
                        // stick any markup in a menu
                        '<b class="menu-title">Choose a Map Type</b>',
						{
                            text: 'Auto',
                            checked: true,
                            group: 'theme',
                            checkHandler: onItemCheck
                        }, {
                            text: 'Physical',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck
                        }, {
                            text: 'Satellite',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck
                        }, {
                            text: 'Hybrid',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck
                        }, {
                            text: 'Street',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck

/*
                        }, {
                            text: '3D',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck
*/

                        }
                    ]
                }
            } 
        ]
    }); //end menu definition
 }else{
    menu = new Ext.menu.Menu({
        id: 'mainMenu',
        items: [
            {
                text: 'Google Map Search',
                checked: false,       // when checked has a boolean value, it is assumed to be a CheckItem
                checkHandler: googleSearch
            },
            {
                text: 'Map Title',
                checked: true,
				id: 'titleCheckID',
                checkHandler: titleWindow
			},
            {
                text: 'Compact Controls',
                checked: false,
				id: 'controlSize',
                checkHandler: changeControlSize	
            }, '-', {
                text: 'Map Background',
                menu: {        // <-- submenu by nested config object
                    items: [
                        // stick any markup in a menu
                        '<b class="menu-title">Choose a Map Type</b>',
						{
                            text: 'Auto',
                            checked: true,
                            group: 'theme',
                            checkHandler: onItemCheck
                        }, {
                            text: 'Physical',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck
                        }, {
                            text: 'Satellite',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck
                        }, {
                            text: 'Hybrid',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck
                        }, {
                            text: 'Street',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck

/*
                        }, {
                            text: '3D',
                            checked: false,
                            group: 'theme',
                            checkHandler: onItemCheck
*/

                        }
                    ]
                }
            } 
        ]
    }); //end menu definition

}  
    
	    dataMenu = new Ext.Toolbar.MenuButton({
         text: 'Toggle Me',
        enableToggle: true,
        toggleHandler: aboutData,
        pressed: true

    	}); //end menu definition


    var tb = new Ext.Toolbar();
	
    tb.render('extToolbar');

    tb.add({
            text:'Map Options',
            iconCls: 'bmenu',  // <-- icon
			//tooltip: {text:'This is a an example QuickTip for a toolbar item', title:'Tip Title'},
            menu: menu  // assign menu by instance
        });//end tb.add

   if(mapTypeVar=="all"||mapTypeVar=="nos"){
    tb.add('-',{
				iconCls: 'info',
				id: 'info_button',
		        text: 'About the Data',
		        handler: aboutData
        	},'-',{
				iconCls: 'back',
				id: 'back_button',
		        text: 'Zoom back to full extent',
		        handler: backToFull
			}
	
				
		);//end tb.add
	}else{
    tb.add('-',{
				iconCls: 'info',
				id: 'info_button',
		        text: 'About the Data',
		        handler: aboutData
        	},'-',{
				iconCls: 'earth',
				id: 'earth_button',
		        text: 'View in Google Earth',
		        handler: loadKML
			},'-',{
				iconCls: 'back',
				id: 'back_button',
		        text: 'Zoom back to full extent',
		        handler: backToFull
			}
	
				
		);//end tb.add
	}


    // functions to display feedback
	 	function toggleSurvey(item, checked){
		//alert(checked)
       if(!checked){
			gMap.clearOverlays();
			if(menu.items.get('restorationCheckID').checked){
				//mapProjects();
				sendToCreateMarker(markerSize)
			}
		}else{
			//mapProjectsHydro();
			sendToCreateMarkerHydro(markerSize)

		}
    }
	    function toggleRestoration(item, checked){
		//alert(checked)
       if(!checked){
			gMap.clearOverlays();
			if(menu.items.get('surveyCheckID').checked){
				//mapProjectsHydro();
				sendToCreateMarkerHydro(markerSize)
			}
		}else{
			//mapProjects();
			sendToCreateMarker(markerSize)
			menu.items.get('restorationCheckID').checked
		}
    }
    function onButtonClick(btn){
        Ext.example.msg('Button Click','You clicked the "{0}" button.', btn.text);
    }

    function onItemClick(item){
        Ext.example.msg('Menu Click', 'You clicked the "{0}" menu item.', item.text);
    }

    function titleWindow(item, checked){
		//alert(checked)
       if(!checked){
			removeTitle();
		}else{
			addTitle();
		}
    }
	
	function onItemCheck(item, checked){
        //Ext.example.msg('Item Check', 'You {1} the "{0}" menu item.', item.text, checked ? 'checked' : 'unchecked');
		if(checked){
			if (item.text=="Auto"){
				autoMapType = "on";	
				//autoMapSwitch();
			}else{
				changeMap(item.text)
				autoMapType = "off";	
			}
		}
    }

    function onItemToggle(item, pressed){
        Ext.example.msg('Button Toggled', 'Button "{0}" was toggled to {1}.', item.text, pressed);
    }
	
	function changeControlSize (item,checked){
		gMap.removeControl(zoomControl)
		if (checked){
			var controlPosition = new GControlPosition(G_ANCHOR_TOP_RIGHT, new G.Size(25,googleZoomTop))
			zoomControl = new GSmallZoomControl3D;
			gMap.addControl(zoomControl,controlPosition)
		}else{
			var controlPosition = new GControlPosition(G_ANCHOR_TOP_RIGHT, new G.Size(googleZoomRight,googleZoomTop))
			zoomControl = new GLargeMapControl3D;
			gMap.addControl(zoomControl,controlPosition)

		}

	}
	
	var infoWin;
	var earthWin;
	var infoButton = Ext.get('info_button');
	var earthButton = Ext.get('earth_button');
	
	function aboutData(){
         if(!infoWin){
            infoWin = new Ext.Window({
                applyTo     : 'info-win',
                layout      : 'fit',
				title: 'About the Data',
                width       : 300,
                height      : 200,
				html: '<div class="infoWin"><img src="../EXT_JS/resources/images/default/window/icon-info.gif" class="infoIcon">Although each of the projects is represented by a single point, many involve activities that cover large areas at multiple project locations. Refer to the project descriptions and related information to better understand the full spatial extent of the project activities and their impacts.</div>',
                closeAction :'hide',
                plain       : true,
                buttons: [{
                    text     : 'Close',
                    handler  : function(){
                        infoWin.hide();
                    }
                }]
            });
        }
        infoWin.show(infoButton);
	}
	
	function loadKML(){
         if(!earthWin){
            earthWin = new Ext.Window({
                applyTo     : 'earth-win',
                layout      : 'fit',
				title: 'View in Google Earth',
                width       : 300,
                height      : 175,
				html: '<div class="infoWin"><img src="../EXT_JS/resources/images/default/window/icon-info.gif" class="infoIcon">This function requires that the Google Earth viewer is installed on your computer. If you do not have Google Earth installed it can be downloaded for free at: <center><a class="footerLink" target="blank_" href="http://www.nmfs.noaa.gov/disclaimer.htm"><img src="images/disclaimer2.gif" alt="The appearance of this icon denotes a link external to the Federal Government domain. The appearance of external links on this web site does not constitute endorsement by NOAA Fisheries of the web sites or the information, products or services contained therein. For other than government authorized activities, NOAA Fisheries does not exercise any editorial control over the information you may find at these locations." name="The appearance of this icon denotes a link external to the Federal Government domain. The appearance of external links on this web site does not constitute endorsement by NOAA Fisheries of the web sites or the information, products or services contained therein. For other than government authorized activities, NOAA Fisheries does not exercise any editorial control over the information you may find at these locations."></a>&nbsp;<a target="blank_" href="http://earth.google.com">http://earth.google.com</a></center></div>',
                closeAction :'hide',
                plain       : true,
				buttonAlign : 'center',
                buttons: [{
                    text     : 'Continue',
					iconCls: 'earth',
                    handler  : function(){
                        openInEarth();
                    }
                },{
                    text     : 'Cancel',
                    handler  : function(){
                     earthWin.hide();
                    }
                }]
            });
        }
		
		
        earthWin.show(earthButton);
	}
	
	function backToFull(){
		zoomToFull();

	}
	

	
	function openInEarth(){
		//alert("opem")
		//window.open('http://www.nmfs.noaa.gov/habitat/restoration/restorationatlas/NOAA_Recovery_Map.kml')
		earthWin.hide();
		window.open('http://www.nmfs.noaa.gov/habitat/restoration/restorationatlas/NOAA_Recovery_Map.kml')
		//window.open('http://www.nmfs.noaa.gov/habitat/restoration/restorationatlas/NOAA_Restoration_ARRA_Projects.kml')
	}

}); //end onReady