/*
	(c) Copyright 2008 Doofah, Inc. All rights reserved.
*/


/*
	GLOBAL
*/
var gIsMSIE = ( navigator.userAgent.match( /MSIE/i ) ) ? 1 : 0;
var gIsSafari = ( navigator.userAgent.match( /KHTML/i ) ) ? 1 : 0;
var gIsFox = ( navigator.userAgent.match( /FIREFOX/i ) ) ? 1 : 0;

function Initialize()
{
	HrefBlank();
	IEPNG();
	AddCSS( 'WIN|FIREFOX', '/css/no_safari.css' );
	
	if( typeof window.RunApp == 'function' )
	{
		RunApp();
	}
}

function HrefBlank()
{
	var hrefs = document.getElementsByTagName('a');
	var domain = document.domain;
	
	for( var i = 0; i < hrefs.length; i++ )
	{
		if( !hrefs[i].href.match( domain ) )
		{
			hrefs[i].target = '_blank';
		}
	}
}

function IEPNG()
{
	if( gIsMSIE )
	{
		var imgs = document.getElementsByTagName('img');
		
		for( var i = 0; i < imgs.length; i++ )
		{
			if( imgs[i].className == 'IEPNG' )
			{
				var src = imgs[i].src;
				imgs[i].src = '/imgs/trans.gif';
				imgs[i].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '")';
				imgs[i].raw_src = src;
				imgs[i].style.border = 'none';
			}
		}
	}
}

// Add StyleSheet For aBrowser
function AddCSS( aBrowser, aCSSFile )
{
	var browser = ( navigator.userAgent.match( new RegExp( aBrowser, 'i' ) ) ) ? 1 : 0;
	var headers = document.getElementsByTagName( 'head' )[0];
	
	// Add css for aBrowser
	if( aBrowser == 'ALL' || browser )
	{
		var style = document.createElement( 'link' );
		style.setAttribute( 'rel', 'stylesheet' );
		style.setAttribute( 'type', 'text/css' );
		style.setAttribute( 'href', aCSSFile );
		
		headers.appendChild( style );
		
		return aBrowser;
	}
	
	return false;
}



function ShowProperty( aObj, aEndl )
{
	var str = '';
	aEndl = ( aEndl ) ? aEndl : "\n";
	str += '<b>PROPERTIES</b>' + aEndl;
	for( var p in aObj )
	{
		try{		str += '■ ' + p + ' = ' + aObj[p] + aEndl;	}
		catch(e){	str += '■ ' + p + ' = ERROR{' + e + '}' + aEndl;	}
	}
	
	return str;
}

function GoogleMap( aLabel, aMapId, aDirectionId ){
	this.label = aLabel;
	this.addr = '';
	this.enable = GBrowserIsCompatible();
	this.corder = new GClientGeocoder();
	this.mag = 16;
	this.map = null;
	this.direction = null;
	this.lat = null;
	this.SetElement( aMapId, aDirectionId );
}
GoogleMap.prototype.Error = function( aErr )
{
	var gdir = this.direction;
	
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	}
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR){
		alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	}
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY){
		alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
	}
	else if (gdir.getStatus().code == G_GEO_BAD_KEY){
		alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
	}
	else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){
		alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	}
	else{
		alert("An unknown error occurred.\n" + ShowProperty( gdir.getStatus() ) );
	}
}
GoogleMap.prototype.SetMagnification = function( aMag )
{
	this.mag = aMag;
}
GoogleMap.prototype.SetElement = function( aMapId, aDirectionId )
{
	if( this.enable )
	{
		var self = this;
		var mElm, dElm;
		
		if( aMapId )
		{
			mElm = document.getElementById( aMapId );
			if( this.map ){
				delete this.map;
				delete this.direction;
			}
			this.map = new GMap2( mElm );
			this.map.addControl( new GMapTypeControl() );
			this.map.addControl( new GLargeMapControl() );
			this.map.addControl( new GScaleControl() );
		}
		
		if( aDirectionId )
		{
			if( ( dElm = document.getElementById( aDirectionId ) ) )
			{
				this.direction = new GDirections( this.map, dElm );
				GEvent.addListener( this.direction, 'error', function(){
					self.Error( this );
				} );
			}
		}
	}
}
GoogleMap.prototype.Address2Point = function( aAddr, aCenter, aNotice )
{
	var self = this;
	this.addr = aAddr;
	if( this.enable && aAddr && aAddr.length )
	{
		this.corder.getLatLng( aAddr, function( aLatLng )
		{
			if( aLatLng )
			{
				self.SetMarker( aLatLng, aCenter );
			}
			else
			{
				if( aNotice ){ alert( aAddr + ': ' + aNotice ); }
			}
		} );
	}
}
GoogleMap.prototype.GetDirection = function( aStartPt, aEndPt, aLocale )
{
	if( this.enable && aStartPt && aEndPt )
	{
		this.direction.load( 'from: ' + aStartPt + ' to: ' + aEndPt, { 'locale': aLocale } );
	}
}
GoogleMap.prototype.SetMarker = function( aLatLng, aCenter )
{
	if( this.enable && aLatLng )
	{
		var marker = new GMarker(aLatLng);
		var info = '<p><b>' + this.label + '</b><br />' + this.addr.replace( 'Honolulu', "<br />\nHonolulu" ) + '</p>';
		
		GEvent.addListener( marker, "click", function(){
			marker.openInfoWindowHtml( info );
		});
		
		if( aCenter ){ this.SetCenter( aLatLng ); }
		this.map.addOverlay( marker );
		marker.openInfoWindowHtml( info );
	}
}
GoogleMap.prototype.SetCenter = function( aLatLng )
{
	if( this.enable && aLatLng )
	{
		this.map.setCenter( aLatLng, this.mag );
	}
}


function AddEvent( aElm, aEvt, aCapture, aCallback )
{
	if( aElm.addEventListener )
	{
		return aElm.addEventListener( aEvt, aCallback, aCapture );
	}
	else if( aElm.attachEvent )
	{
		return aElm.attachEvent( 'on'+aEvt, aCallback );
	}
}

AddEvent( window, 'load', true, function(){
	Initialize();
} );
