function showLoading(msg)
{
    $("#loading-overlay").show();
    $("#loading-msg").text(msg);
    $("#loading").show();   
}
function hideLoading()
{
    $("#loading").hide();
    $("#loading-overlay").hide();
} 
function showRequest(formData, jqForm, options) { 
    var queryString = $.param(formData); 
    showLoading(); 
    return true; 
} 
function FindResponse(responseText, statusText)  { 
    $("#outpostList").setTemplateElement("mapTMP");
    $("#outpostList").setParam('x',1);
    $("#outpostList").processTemplate(responseText);
    loadGMap(responseText.HomeLocation,responseText.Locations);
    hideLoading();
}
function LoginResponse(responseText, statusText)  { 
    if(responseText.errors.length>0) 
    {
        $("#output1").setTemplateElement("errorTMP");
        $("#output1").processTemplate(responseText);
        hideLoading();
    }
    else 
    {
        //alert("successful login");
        $("#output1").html("");
        $('#loginBox').slideUp('slow');
        $('#logindisplay').html('Welcome, <b>'+responseText.username+'</b>. <a href="/account/signout/">Sign Out.</a>');
        isAuthenticated = true;
        hideLoading();   
    }
}
function loadGMap2(name,address, latitude,longitude){ 
    if(typeof(name) != "undefined")
    {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(latitude, longitude), 10);

        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);

        // Load all the markers from the JSON ProximityLocations variable
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        var point = new GLatLng(latitude,longitude);
        map.addOverlay(createMarker2(point, name, address, address, 1, baseIcon));
      }
    }
}

function loadGMap(homeSpatialInfo, ProximityLocations){ 
    if(typeof(homeSpatialInfo) != "undefined" && typeof(ProximityLocations) != "undefined")
    {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(homeSpatialInfo.latitude, homeSpatialInfo.longitude), 10);
        GEvent.addListener(map,"infowindowclose",function() {
            $(".hilite").removeClass("hilite");
            });

        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);

        // Load all the markers from the JSON ProximityLocations variable
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        for (var i = 0; i < ProximityLocations.length; i++) {
          var point = new GLatLng(ProximityLocations[i].Latitude,ProximityLocations[i].Longitude);
          map.addOverlay(createMarker(point, ProximityLocations[i].OutpostName, homeSpatialInfo.name, ProximityLocations[i].Address+", "+ProximityLocations[i].City+", "+ProximityLocations[i].State+" "+ProximityLocations[i].ZipCode, i+1, baseIcon, homeSpatialInfo));
        }
      }
    }
}
// Creates a marker whose info window displays the letter corresponding// to the given index.
function createMarker(point, name, address, urladdress, locationNumber, baseIcon) {
  var icon = new GIcon(baseIcon);  
  icon.image = "/images/markers/marker" + locationNumber + ".png";
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("<strong>Outpost "+ name + '</strong><br>' + urladdress + '<br>' + '<a target="_blank" href="http://maps.google.com/maps?f=q&hl=en&q=from:'+address+'+to:'+urladdress+'">Directions</a>');
    $("#loc"+locationNumber).addClass("hilite");
  });
  return marker;
}
// Creates a marker whose info window displays the letter corresponding// to the given index.
function createMarker2(point, name, address, urladdress, locationNumber, baseIcon) {
  var icon = new GIcon(baseIcon);  
  icon.image = "/images/markers/marker" + locationNumber + ".png";
  var marker = new GMarker(point, icon);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("<strong>"+ name + '</strong><br>' + urladdress + '<br>' + 'Directions: <a target="_blank" href="http://maps.google.com/maps?li=d&hl=en&f=d&iwstate1=dir:to&daddr='+urladdress+'">To Here</a> | <a target="_blank" href="http://maps.google.com/maps?li=d&hl=en&f=d&saddr='+urladdress+'">From Here</a>');
  });
  return marker;
} 

function CommentResponse(responseText, statusText)  { 
    if(responseText.errors.length>0) 
    {
        $("#commentErrors").setTemplateElement("errorTMP");
        $("#commentErrors").processTemplate(responseText);
        hideLoading();
    }
    else 
    {
	    $("#commentTMP").setTemplateElement("TMP");
	    $("#commentTMP").processTemplate(responseText.comment);
	    $("#comments").append($("#commentTMP").html());  
	    $("#commentErrors").html("");
	    hideLoading();      
    }
}
function swap(){this.className="msieFix";}
function swapBack(){this.className="trigger";}
function toggle(){(this.parentNode.className=="trigger")?this.parentNode.className="msieFix":this.parentNode
.className="trigger";return false;}
function reveal(){
this.parentNode.parentNode.parentNode.className="msieFix";
}
function cleanUp(){
	var zA;
	var LI = document.getElementsByTagName("li");
	var zLI= LI.length;
		for(var k=0;k<zLI;k++){
		if(LI[k]!=this.parentNode){
		LI[k].className="trigger";
		}
	}	
}
function TJK_keyBoardDropDown(){// v1.2.1 Copyright (c) 2006 TJKDesign - Thierry Koblentz
	var LI = document.getElementById('TJK_dropDownMenu').getElementsByTagName("li");
	var zLI= LI.length;
	if (document.getElementById){	
		for(var k=0;k<zLI;k++){
			if(LI[k].id){
				LI[k].className="trigger";
				//LI[k].firstChild.onclick=toggle;
				LI[k].firstChild.title="show/hide the sub-menu";				
				LI[k].firstChild.onfocus=cleanUp;
			}
			if(LI[k].className=="trigger"){
				LI[k].onmouseover=swap;
				LI[k].onmouseout=swapBack;
			}
			if(!LI[k].id){
				LI[k].firstChild.onfocus=reveal;
			}
		}
	}
}
var options = { 
	//target:        '#output1',   // target element(s) to be updated with server response 
	beforeSubmit:  showRequest,  // pre-submit callback 
	success:       LoginResponse,  // post-submit callback 
	dataType:       'json'
}; 
function tagFileTypes()
{
var fileTypes = {
  doc: 'doc.png',
  docx: 'doc.png',
  xls: 'xls.png',
  xlsx: 'xls.png',
  pdf: 'pdf.png',
  ppt: 'ppt.png',
  pptx: 'ppt.png',
  zip: 'zip.png',
  gif: 'gif.png'
};
 
// this is like $.each() above, except
// it iterates over the matched elements
$('a.filetype').each(function() {
 
  // get a jQuery object for each anchor found
  var $a = $(this);
 
  // get the href attribute
  var href = $a.attr('href');
 
  // get the extension from the href
  var hrefArray = href.split('.');
  var extension = hrefArray[hrefArray.length - 1];
 
  var image = fileTypes[extension];
 
  if (image) {
    $a.parent().css({
      background: '#F4F3E9 url("/images/filetypes/' + image + '") no-repeat center left'
    });
  }
 
});
}
$(document).ready(function(){	
	TJK_keyBoardDropDown();
	$('a.lightbox').lightbox();
	tagFileTypes();
	//$('#loginForm').ajaxForm(options);
	//if(!googleDebug) $.gaTracker(googleAnalyticsKey);	
});