/*

	@Class:	InputClearAndReplace
	@Author:	Brandon Gray for O3 World 2008
	@Brief:	Loops through all elements with a class name of 'clear_replace' and clears the default text when focused.
			When focus is lost it checks to see if the field is blank (no new data entered), if value is blank the default text is placed back in.
	@Example: <input type="text" name="firstname" id="firstname" value="" class="clear_replace" />
			
*/

var InputClearAndReplace = new Class ({
	
	options: {
		
		InputElement: '.clear_replace'
		
	},
	
	initialize: function()  {
		
		var inputElementList = $$( this.options.InputElement );
		var originalValue = new Array();
		
		inputElementList.each( function( element, i ) {
			
			originalValue.push( inputElementList[ i ].value );
			
			element.onfocus = function() {
				
				if( this.value == originalValue[ i ] ) this.value = '';
				
			},
			
			element.onblur = function() {
				
				if( this.value == '' ) this.value = originalValue[ i ];
				
			}
			
		});
		
	}

});

// Google Maps functionality
function displayMap() {
	
	if ( GBrowserIsCompatible() ) {

		// create custom icon
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = "images/icon_google_map.png";
		icon.iconSize = new GSize(39, 31);
		icon.shadow = "images/icon_google_map_shadow.png";
		icon.shadowSize = new GSize(39, 31);
		icon.infoWindowAnchor = new GPoint(10, 10);
		icon.infoShadowAnchor = new GPoint(10, 10);
		
		// create the map
		var map = new GMap2( document.getElementById( 'locations_map' ) );
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		// center will be determined by bounds but must set a default here
		map.setCenter( new GLatLng( 0, 0 ), 0 );
		
		var bounds = new GLatLngBounds();
	
		var request = GXmlHttp.create();
		request.open( 'GET', 'data/map_data.xml', true );
		request.onreadystatechange = function() {
		
			if ( request.readyState == 4 ) {
				
				var xmlDoc = GXml.parse( request.responseText );
				// obtain the array of markers and loop through it
				var markers = xmlDoc.documentElement.getElementsByTagName( 'marker' );
				
				for (var i = 0; i < markers.length; i++) {
		
					// obtain the attribues of each marker
					var lat = parseFloat( markers[ i ].getAttribute( 'lat' ) );
					var lng = parseFloat( markers[ i ].getAttribute( 'lng' ) );
					var point = new GLatLng( lat, lng );
					var html = markers[ i ].getAttribute( 'html' );
					var label = markers[ i ].getAttribute( 'label' );
					// create the marker
					var marker = createMarker( point, label, html );
					map.addOverlay( marker );
					
					// each time a point is created, extend the bounds to include it
					bounds.extend( point );
					
				}
				
				// determine the zoom level from the bounds
				map.setZoom( map.getBoundsZoomLevel( bounds ) - 1 );
				// determine the center from the bounds
				map.setCenter( bounds.getCenter() );
				
			}
			
		}
		
		request.send( null );
		
		// create the marker and event listener to open info window
		function createMarker( point, name, html ) {
		
			var marker = new GMarker( point, icon );
			
			GEvent.addListener( marker, 'click', function() {
				
				marker.openInfoWindowHtml( html );
	
			});
			
			return marker;
	
		}
	
	} else {
		
		alert("Sorry, the Google Maps API is not compatible with this browser");
	
	}
	
}

function initEventTabs() {

	var menuAnchors = $( 'events_nav_tabs' ).getElements( 'a' );

	for ( var i = 0; i < menuAnchors.length; i++ ) {

		if ( menuAnchors[ i ].getAttribute( 'href' ) && menuAnchors[ i ].getAttribute( 'rel' ) == 'swap' ) {

			menuAnchors[ i ].addEvent( 'click', function() {
				
				// Remove the active state from all links
				for ( var j = 0; j < menuAnchors.length; j++ ) {
					
					menuAnchors[ j ].removeClass( 'active' );
					
				}
				
				swapEventContent( this.id );

			});

		}

	}

}

function swapEventContent( _id ) {
	
	// Set the active state on the selected link
	$( _id ).addClass( 'active' );

	var tabs = $$( '#events .tab_content' );
	
	for ( var i = 0; i < tabs.length; i++ ) {

		tabs[ i ].setStyle( 'display', 'none' );		

	}

	var newID = 'content_' + _id;

	var content = $( newID ).setStyle( 'display', 'block' );
	
}

function initGalleryTabs() {

	var menuAnchors = $( 'galleries_nav_tabs' ).getElements( 'a' );

	for ( var i = 0; i < menuAnchors.length; i++ ) {

		if ( menuAnchors[ i ].getAttribute( 'href' ) && menuAnchors[ i ].getAttribute( 'rel' ) == 'swap' ) {

			menuAnchors[ i ].addEvent( 'click', function() {
				
				// Remove the active state from all links
				for ( var j = 0; j < menuAnchors.length; j++ ) {
					
					menuAnchors[ j ].removeClass( 'active' );
					
				}
				
				swapGalleryContent( this.id );

			});

		}

	}

}

function swapGalleryContent( _id ) {
	
	// Set the active state on the selected link
	$( _id ).addClass( 'active' );

	var tabs = $$( '#gal_retail .tab_content' );
	
	for ( var i = 0; i < tabs.length; i++ ) {

		tabs[ i ].setStyle( 'display', 'none' );		

	}

	var newID = 'content_' + _id;

	var content = $( newID ).setStyle( 'display', 'block' );
	
}

window.addEvent( 'load', function() {
							  
	var InputList = new InputClearAndReplace();
	
	if ( $( 'locations_map' ) ) displayMap();
	if ( $( 'events_nav_tabs' ) ) initEventTabs();
	if ( $( 'galleries_nav_tabs' ) ) initGalleryTabs();
	
});
// Takes three parameters: this, 'focus' or 'blur', 'default field value'
function SwapFocus(obj, state, msg){
	if(state == 'focus'){
		if(obj.value == msg){
			obj.value = '';
		}
	} else if(state == 'blur') {
		if(obj.value == ''){
			obj.value = msg;
		}
	}
}

// newsletter signup
function validateSignup() {
	var email = $( 'newsletterEmail' ).value;
	if ( email == '' || email == 'Submit Email Address' || email == 'Thanks!' ){
		alert( 'Please enter a valid email address. (' + email + ')' );
		return false;
	} else if ( !( /^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/.test( email ) ) ){
		alert( '"' + email + '" is not a valid email address.' );
		return false;
	} else {
		return true;
	}
}

