var map = null;
var increment = 0; // used to increment event locations
var sumLat = 0; // used to find the average LAT location
var sumLon = 0; // used to find the average LON location
var latlonArray = new Array();
var popup = null;
function initVars(){
  increment = 0; // used to increment event locations
  sumLat = 0; // used to find the average LAT location
  sumLon = 0; // used to find the average LON location
  latlonArray = new Array();
  
}
function resize_bg(){
  $("#bg").css("left","0");
  $("#bg-top").css("left","0");
  var doc_width = $(window).width();
  var doc_height = $(window).height();
  var image_width = $("#bg").width();
  var image_height = $("#bg").height();
  var image_ratio = image_width/image_height;      
  var new_width = doc_width;
  var new_height = Math.round(new_width/image_ratio);
  var bottle_width = $("#bottle").width();
  if(new_height<doc_height){
    new_height = doc_height;
    new_width = Math.round(new_height*image_ratio);
    var width_offset = Math.round((new_width-doc_width)/2);
    $("#bg").css("left","-"+width_offset+"px");
    $("#bg-top").css("left","-"+width_offset+"px");      
  }
  
  if(doc_width>(960+(bottle_width+15))){
    $("#bottle").css({"right":0});
  }else{
    $("#bottle").css({"right":((doc_width-960)-(bottle_width+15))});
  }


  $("#bg-top").width(new_width);
  $("#bg-top").height(new_height);
  $("#bg").width(new_width);
  $("#bg").height(new_height);
  
}

function wheelCallback(e){
     return true;
}


function startZoom(){}
function endZoom(){}
function startPan(){}
function endPan(){}

function loadMap(div){
  map = new VEMap(div);
  map.AttachEvent("onstartzoom", startZoom);
  map.AttachEvent("onendzoom",endZoom);
  map.AttachEvent("onstartpan", startPan);
  map.AttachEvent("onendpan",endPan);
  var zoom = 6;
  var latlong=new VELatLong(38.62774,-92.9918652);
  map.SetCredentials("Ai5wIZVoQNLmc3QzMP4ENfYso9L-3thrA9DP6UyZt6T7MoiRHjOvjxAKLQ9a_LFA");
  map.SetDashboardSize(VEDashboardSize.Tiny);
  map.LoadMap(latlong,zoom);
  map.AttachEvent("onmousewheel", wheelCallback);
  return map;
}

function loadPin(map,lat,lng,title,address,link,photo){
  
  var loc = new VELatLong(lat,lng);
  var pin = new VEShape(VEShapeType.Pushpin, loc);
  
  if(!title){

  }else{
    pin.SetTitle(title); 
  }
  if(!address){
    addr = ""
  }else{
    addr = "<p>"+address+"</p>";    
  }
  if(!link){
    link = ""
  }else{
    link = '<a href="'+link+'">Learn More</a>';
  }
  
  pin.SetDescription(addr + link);
  var icon = new VECustomIconSpecification();
  icon.Image = '/images/marker.png';
  icon.ImageOffset = new VEPixel(0, -17);
  icon.TextContent = ".";
  pin.SetCustomIcon(icon);
  map.AddShape(pin);
  return pin.GetId();
}

function clearMap(map){
  map.Clear();
  var zoom = 6;
  var latlong=new VELatLong(38.62774,-92.9918652);
  map.LoadMap(latlong,zoom);
  map.SetDashboardSize(VEDashboardSize.Tiny);
  map.AttachEvent("onmousewheel", wheelCallback);  
  return map;
  
}

function zoomMap(map,lat,lng,zoom){
  if(!zoom){
    zoom = 6
  }
  var loc = new VELatLong(lat,lng);
  map.SetZoomLevel(zoom)
  map.SetCenter(loc, zoom);
}

function showPopUp(pinID){
  var pin = map.GetShapeByID(pinID);
  if (pin != null){
    openBox(pin);
  }
}
function openBox(pin){
  map.ShowInfoBox(pin)
}


$(document).ready(function(){

  $('.panels').after('<ul class="menu">').cycle({
	  pagerAnchorBuilder: function(idx, slide) {
      return '<li><a href="#">'+$(slide).attr('title')+'</a></li>'; 
    },
    pager:  '.menu',
    timeout: 0,
    fx: 'scrollHorz',
    speed: 500,
    next:   '.next', 
    prev:   '.prev',
    cleartypeNoBg: true
  });  
  $("#bg").load(function(){
    resize_bg();
  });
  $(window).resize(function(){
    resize_bg();
  })
  
  resize_bg(); 


  $('input.ui-datepicker').datepicker();

  // open externally
  $("a[href^='http:']").attr('target','_blank'); 
});






