var CD4Align = Class.create()

CD4Align.prototype = {
	DefaultOptions : {
		selector	: '#doesnt_exist'
	},
	initialize : function (options) {
		this.options = Object.extend(
			Object.extend({},this.DefaultOptions), options || {}
		);

		// look for images 
		// for each match we're going to look at the width of the image and 
		// make that the width of the parent div
		
		$$(this.options.selector).each(function(img) {
			// get width
			var w = img.width	

			var p = img.parentNode
			
			//alert('setting width to ' + w + ' for ' + p)
			Element.setStyle(p,{width : w + 'px'})
		}.bind(this))
		
	}
}

/*	--------------
	layer controls
	--------------	*/

/*
	as much as i hate to do it, this is going to be pretty hardcoded.  
*/

var CD4LayerControls = Class.create()

CD4LayerControls.prototype = {
	DefaultOptions : {
		lgroups	: {
			main : ['extras','poi','publicsafety','parkslibraries','improvements'],
			nc : ['nc','ncbg'],
			rg : ['rg']
		},
		lcontrols : ['lc_main','lc_nc','lc_rg'],
		subgroups : {
			main : [
				['lc_poi','poi'],
				['lc_ps','publicsafety'],
				['lc_pl','parkslibraries'],
				['lc_improvements','improvements']
			],
			nc : [],
			rg : []
		},
		subs : ['main','nc','rg'],
		map : 'cfimap',
		poiinfo : 'poiinfo'
	},
	initialize : function (map,options) {
		this.options = Object.extend(
			Object.extend({},this.DefaultOptions), options || {}
		);
		
		this.map = map
		
		// save a copy of the POI help text
		this.poihelp = $( this.options.poiinfo ).innerHTML
		
		// attach listeners to the layer groups
		Event.observe('lc_main','click',this.clickMain.bind(this))
		Event.observe('lc_nc','click',this.clickNC.bind(this))
		Event.observe('lc_rg','click',this.clickRG.bind(this))
		
		// attach event listeners to the subs
	
		this.options.subs.each(function(sn){
			// now we have an array
			this.options.subgroups[sn].each(function(sc) {
				// sc is our subcontrol
				// sc[0] is the HTML id of our checkbox
				// sc[1] is the map layer
				//Log.write('binding to ' + sc[0])
				Event.observe(sc[0],'click',this.clickSub.bind(this,sc))
			}.bind(this))
		}.bind(this))
		
	},
	
	clickSub : function (sub) {
		//alert('got click on sub ' + sub[0])
		// get state to see if we're going on or off
		var s = Form.Element.getValue(sub[0])
		
		if (s) {
			this.map.showLayers([sub[1]])
		} else {
			this.map.hideLayers([sub[1]])			
		}
	},
	
	clickMain : function () {
		// make lc_main active
		this.activate('lc_main')
		
		// turn off NC & RG
		this.map.hideLayers( [ this.options.lgroups.nc, this.options.lgroups.rg ].flatten() )
		
		// slide in our sub controls and restore sub layer states
		this.activateSub('main')
		
		// turn on main
		this.map.showLayers(['extras'])		
	},
	
 	clickNC : function() {
		// make lc_nc active
		this.activate('lc_nc')
		this.activateSub('nc')
		
		// turn off main & RG
		this.map.hideLayers( [ this.options.lgroups.main, this.options.lgroups.rg ].flatten() )
		
		// turn on NC 
		this.map.showLayers( this.options.lgroups.nc )
	},
	
	clickRG : function() {
		// make lc_rg active
		this.activate('lc_rg')
		this.activateSub('rg')
		
		// turn off main & NC
		this.map.hideLayers( [ this.options.lgroups.nc, this.options.lgroups.main ].flatten() )
		
		// turn on RG
		this.map.showLayers( this.options.lgroups.rg )
	},
	
	activate : function(l) {
		this.options.lcontrols.each(function(id) {
			Element.removeClassName(id,'active')
		})
		
		Element.addClassName(l,'active')
		
		// also put the help back in the bubble
		$( this.options.poiinfo ).innerHTML = this.poihelp
	},
	
	activateSub : function(l) {		
		// disable any layer subgroup forms
		this.options.subs.each(function(id) {
			if ($('lc_sub'+id)) {
				$('lc_sub'+id).getElementsBySelector("input").each(function(f){
					Form.Element.disable(f)
				})
			}
		})
		
		// see if we're enabling anything
		if (l && $("lc_sub"+l)) {
			
			// now we need to restore layer states
			this.options.subgroups[l].each(function(sub) {
				var s = Form.Element.getValue(sub[0])

				if (s)
					this.map.showLayers([sub[1]])
			}.bind(this))
			
			// now re-enable this form
			$('lc_sub'+l).getElementsBySelector("input").each(function(f){
				Form.Element.enable(f)
			})
		}
	}
}

/*	--------------
	photo pop code
	--------------	*/
	
var CD4PhotoPop = Class.create()

CD4PhotoPop.prototype = {
	DefaultOptions : {
		selector	: '#setphotos img',
		loadEl : 'redbox_load'
	},
	initialize : function (options) {
		this.options = Object.extend(
			Object.extend({},this.DefaultOptions), options || {}
		);
		
		// run through each photo in the set and give it pop-up actions
		
		$$(this.options.selector).each(function(img) {
			Event.observe(img,'click',this.onImgClick.bind(this,img,img.parentNode.href))
			
			// replace the normal href
			img.parentNode.href = 'javascript:void(0)'
		}.bind(this))
		
	},
	
	onImgClick : function (img,href) {
		//Log.write('img is ' + href)
		
		new Ajax.Updater(
			this.options.loadEl,
			href,
			{
				asynchronous : true,
				evalScripts : true,
				onComplete : function(req) {
					RedBox.addHiddenContent(this.options.loadEl)
				}.bind(this),
				onLoading : function() {
					RedBox.loading()
				}
			}
		)
	}
}

1;