var ncnPage = {
	init: function() {
		this.initWidthSet();
		this.initButtons();
		this.initImageRotator();
	},
	initWidthSet: function() {
		var dimensions = this.getClientCoords();
		if((dimensions.width > 970) && (!$('body').hasClass('home'))) {
			$('body').addClass('wide');
		}
	},
	getClientCoords: function() {
		var dimensions = {width: 0, height: 0};
		if (document.documentElement) {
			dimensions.width = document.documentElement.offsetWidth;
			dimensions.height = document.documentElement.offsetHeight;
		} else if (window.innerWidth && window.innerHeight) {
			dimensions.width = window.innerWidth;
			dimensions.height = window.innerHeight;
		}
		return dimensions;
	},	
	initButtons: function() {
		// automagic buttons
		$('span.btnWrap a:first-child').each(function() { // first child rule addresses problem where edit links were turned into buttons.
			var b = $(this);
			b.addClass('btn');
		});
		
		$('a.btn').each(function(){
			var b = $(this);
			var tt = b.html() || b.val();
			b.text('').css({cursor:'pointer'}).prepend('<i></i>').append($('<span></span>').text(tt).append('<i></i><span></span>'));
		});

		$('input.btn:submit,button[@type=button],button[@type=submit]').each(function(){
			var b = $(this);
			var tt = b.text() || b.val();
			$this = this;
			if (this.onclick) {
				b = $('<a></a>').insertAfter(this).addClass(this.className).attr('name',this.name).attr('id',this.id);
				b.get(0).onclick = this.onclick ;
			} else {
				b = $('<a></a>').insertAfter(this).addClass(this.className).attr('name',this.name).attr('id',this.id).click(function(){
					$(this).parents('form').eq(0).submit();
				});
			}
			if ( $this.click ) b.click = $this.click ;
			$(this).remove();
			b.text('').css({cursor:'pointer'}). prepend('<i></i>').append($('<span></span>').text(tt).append('<i></i><span></span>'));
		});
		$('input:reset,button[@type=reset]').each(function(){
			var b = $(this);
			var tt = b.text() || b.val();
			$this = this;
			b = $('<a></a>').insertAfter(this).addClass(this.className).attr('id',this.id).attr('name',this.name).click(function(){$(this).parents('form').eq(0).reset();});
			$(this).remove();
			b.text('').css({cursor:'pointer'}). prepend('<i></i>').append($('<span></span>').text(tt).append('<i></i><span></span>'));
		});		

	},
	initImageRotator: function() {
		// home page image rotator
		if ( $("#rotate").length > 0 ) {
			$("#rotate").innerfade({
					speed: 'slow',
					timeout: 4000,
					type: 'sequence',
					containerheight: '122px'
			});		
		}
	}	
};

var ncnForm = {
	init: function() {
		this.initDependentSelectors();
	},
	initDependentSelectors: function() {
		$('input.dependentSelector').each(function() {
			var hEl = jQuery(this);
			var childId = hEl.attr('title');
			var parentId = hEl.attr('value');
			var parent = $('#' + parentId);
			var child = $('#' + childId);

			child.before('<div style="display:none;" id="optGroupBucket-' + parentId + '"></div>');
			var bucket = $('#optGroupBucket-' + parentId);
	
			// move options into buckets
			$('optgroup',child).each(function() {
				var grp = $(this);
				bucket.append(grp);
			});
			
			child.attr("disabled", true);
			ncnForm.updateDependentSelectors(parentId,childId,parent,child);
			parent.bind("change", function(e){
				ncnForm.updateDependentSelectors(parentId,childId,parent,child);
			});
		});
	},
	updateDependentSelectors: function(parentId,childId,parent,child) {
		var foundGroup = false;
		var bucket = $('#optGroupBucket-' + parentId);
		$('optgroup',child).each(function() {
			var grp = $(this);
			bucket.append(grp);
		});		
		$('optgroup',bucket).each(function() {
			var grp = $(this);
			if(grp.attr('id') == childId + '-' + parent.val()) {
				foundGroup = true;
				child.attr("disabled", false);
				child.append(grp);
			}
		});
		if(foundGroup == false) child.attr("disabled", true);
	}
};
// TODO: Add class="selected" to checkboxes in the filters list that get or load selected.  Add "first" and/or "last" to each one if necessary to add the border to the top and bottom respectively, as well.
var tidesProjDir = {
	init: function() { 
		this.projMap.initMap();
	},
	// the map object
	projMap : {
		assetDir: 'http://www.nonprofitcenters.org/fileadmin/templates/images/map_assets/img/',
		pins: new Array(),
		initMap: function() {
			if ($('#mapDiv').length > 0 && GBrowserIsCompatible()) {
				// create map object

			    this.map = new GMap2(document.getElementById('mapDiv'));

				var center = new GLatLng(37.0902, -95.7129);

				this.map.setCenter(center, 4);
				this.map.addControl(new GLargeMapControl());	
				this.map.addControl(new GMapTypeControl());

				// create our pin icons
				this.bluePin = new GIcon(G_DEFAULT_ICON);
				this.greenPin = new GIcon(G_DEFAULT_ICON);
				this.redPin = new GIcon(G_DEFAULT_ICON);
				var iconSize = new GSize(37,35);
				var shadowImg = this.assetDir + 'map_pin_shadow.png';

				this.redPin.image = this.assetDir + 'map_pin_red.png';
				this.greenPin.image = this.assetDir + 'map_pin_green.png';
				this.bluePin.image = this.assetDir + 'map_pin_blue.png';

				this.redPin.shadow = shadowImg;
				this.greenPin.shadow = shadowImg;
				this.bluePin.shadow = shadowImg;

				this.redPin.iconSize = iconSize;
				this.greenPin.iconSize = iconSize;
				this.bluePin.iconSize = iconSize;

				this.displayPins();				
			}		
		},
		displayPins: function() {
			$(this.pins).each(function() {
				var pin = this;
				var color = pin['color'];
				var lat = pin['lat'];
				var lng = pin['lng'];
				var address = pin['txt'];
				var toLink = 'http://maps.google.com/maps?f=d&hl=en&geocode=&daddr=' + lat + ',' + lng;
				var fromLink = 'http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=' + lat + ',' + lng;

				if ($("#mapDiv").length > 0 && GBrowserIsCompatible()) {
					if(color == 'red') {
						markerOptions = { icon:tidesProjDir.projMap.redPin};
					}
					if(color == 'blue') {
						markerOptions = { icon:tidesProjDir.projMap.bluePin};
					}
					if(color == 'green') {
						markerOptions = { icon:tidesProjDir.projMap.greenPin};			
					}
					var point = new GLatLng(lat,lng);
					var marker = new GMarker(point,markerOptions);
					
					var str = '<br />' + address + '<div style="margin-bottom: 8px; width: 200px;">Get directions: <a target="blank" href="#' + toLink + '">To here</a> <a target="blank" href="' + fromLink + '">From here</a></div><br />';

					GEvent.addListener(marker, "click", function() {						
						marker.openInfoWindowHtml(str);
					});
					
					tidesProjDir.projMap.map.addOverlay(marker);
				}				
			});
		},
		addPin: function(color,lat,lng,text) {
			var hash = {'color':color,'lat':lat,'lng':lng};
			hash['txt'] = text;
			this.pins.push(hash);
		}				
	}
};

var fontSizeToggler = {
	element: false,
	init: function() {
		this.element = $('body');
		$('img#fontSizeToggle').css('display','inline');
		$('img#fontSizeToggle').css('cursor','pointer');
	
		if($.cookie('fontSizeToggle') == 'sizeLarge') {
			this.element.attr('id','sizeLarge');
		} else {
			this.element.attr('id','sizeNormal');
		}

		$('img#fontSizeToggle').click(function() {
			fontSizeToggler.toggleFontSize();
		});
	},
	toggleFontSize: function() {
		if(this.element.attr('id') == 'sizeNormal') {
			this.element.attr('id','sizeLarge');
			$.cookie('fontSizeToggle','sizeLarge',{path: '/'});
		} else {
			this.element.attr('id','sizeNormal');
			$.cookie('fontSizeToggle','',{path: '/'});
		}
	}
};

$(document).ready(function() {
	tidesProjDir.init();
	ncnPage.init();
	ncnForm.init();
//	fontSizeToggler.init();
});
