/**
 * JS Functions for Bob's Theme
 * REQUIRES JQUERY!
 * */

var bobs = {};
var direction_display = null;


var direction_marker = null;



bobs.get_center_point = function(state)
{
    if(state=="MA")
    {
        ar = new Array(42.36666166373274, -72.04833984375)
    }
    if(state=="NJ")
    {
        ar = new Array(40.3350998293524, -74.366455078125)
    }
    if(state=="NY")
    {
        ar = new Array(41.3243011691559, -73.828125)
    }
    if(state=="CT")
    {
        ar = new Array(41.86656098584028, -72.04833984375)
    }
    if(state=="NH")
    {
        ar = new Array(42.98710309515534, -71.25732421875)
    }
    if(state=="RI")
    {
        ar = new Array(41.648, -71.47)
    }
    
    return ar;
}


bobs.get_shadow = function()
{
    var shadow = new google.maps.MarkerImage('/wp-content/themes/bobs/images/map_icon_shad_bobs.png',
      // The shadow image is larger in the horizontal dimension
      // while the position and offset are the same as for the main image.
      new google.maps.Size(50, 35),
      new google.maps.Point(0,0),
      new google.maps.Point(0, 35));
    
    return shadow;
}

bobs.get_marker_image = function()
{
    var image = new google.maps.MarkerImage('/wp-content/themes/bobs/images/map_icon_bobs.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(30, 35),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(0, 35));
    
    return image;
}

/**
 * Adds an info window to map point
 * */
bobs.add_infowindow = function(infowindow, marker, map, html)
{
    google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent(html);
            infowindow.open(map,marker);
        });
}

/*
 * The map for individula directions
 * */
bobs.initialize_directions_map = function(id)
{
    
    var store_lat = $("#lat_address_"+id).text();
    var store_long = $("#long_address_"+id).text();
    var myLatlng = new google.maps.LatLng(store_lat, store_long);
    var image = this.get_marker_image();
    var shadow = this.get_shadow();
    
    var myOptions = {
      zoom: 10,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("directions-map"), myOptions);
    
    marker = new google.maps.Marker({
            map: map,
            icon: image,
            shadow: shadow,
            position: myLatlng
        });
    
    direction_marker = marker;
    directions_display = new google.maps.DirectionsRenderer();
    directions_display.setMap(map);
    directions_display.setPanel(document.getElementById("directions-panel"));
    
}

/*
 * gets directions
 **/
bobs.get_directions = function(from_address, to_address)
{
    var directions_service = new google.maps.DirectionsService();
    direction_marker.setMap(null);
    var request = {
        origin:from_address, 
        destination:to_address,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directions_service.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directions_display.setDirections(response);
      }
    });
}

/*
 * The state wide map for multiple locations
 */
bobs.initialize_state_map = function(state)
{
   var store_lat = null;
   var infowindow = null;
   var bubble_html = null;
   var store_long = null;
   var id = null;
   var store_lat_long = null;
   var geocoder;
   var map;
   var center_ar = this.get_center_point(state)
   var myLatlng = new google.maps.LatLng(center_ar[0], center_ar[1]);
   var shadow = this.get_shadow();
   var image = this.get_marker_image();

   
   
   
   geocoder = new google.maps.Geocoder();
    var myOptions = {
      zoom: 8,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("state_map"), myOptions);
    
    

    
    $(".address_holder").each(function(){
        text = $(this).text();
        id = this.id;
        bubble_html = $("#mailing_"+id).html();
        store_lat = $("#lat_"+id).text();
        store_long = $("#long_"+id).text();
        
        store_lat_long = new google.maps.LatLng(store_lat, store_long);
        
        infowindow = new google.maps.InfoWindow({
        content: bubble_html
        });
        
        marker = new google.maps.Marker({
            map: map,
            icon: image,
            shadow: shadow,
            position: store_lat_long
        });
        
        bobs.add_infowindow(infowindow, marker, map, bubble_html);
        
        
        
    });
  
}



$(function(){
   
   //reset the header form for check points
   $("input.check-points").click(function(){
        var val = $(this).val();
        
        if(val == "ID# or Email")
        {
            $(this).val("");
        }
   });
   
   //reset the sidebar email form
   $("input#sb_email").click(function(){
        var val = $(this).val();
        
        if(val == "Email Address")
        {
            $(this).val("");
        }
   });
   
   //fires the store locator
   $("#store-dropdown").change(function(){
       
        if($(this).val() != "")
        {
            $("#store-locator").submit();
        }
   });
   
   //sees if we should display a state-wide map
   if($("#state_map").length > 0)
   {
     var the_class = $("#state_map").attr("class");
     var state = the_class.split("_")[1];
     
     bobs.initialize_state_map(state);
   }
   
   //sees if we display the driving directions map
   if($("#directions-map").length > 0)
   {
    var the_class = $("#directions-map").attr("class");
    var store_id = the_class.split("_")[1];
    
    bobs.initialize_directions_map(store_id);
   }
   
   $("#area_code").keyup(function(){
    
    if($(this).val().length == 3)
    {
       
        $("#prefix").focus();
    }
    
   });
   
   $("#prefix").keyup(function(){
    if($(this).val().length == 3)
    {
        $("#extension").focus();
    }
    
   });

    $("#marea_code").keyup(function(){
    
    if($(this).val().length == 3)
    {
       
        $("#mprefix").focus();
    }
    
   });
   
   $("#mprefix").keyup(function(){
    if($(this).val().length == 3)
    {
        $("#mextension").focus();
    }
    
   });
   
   //opens the pop-up for rewards printout
   if($("#rewards-thanks").length > 0)
   {
        mywindow = window.open ("/_app/rewards/print_signup.php", "mywindow","location=1,status=1,scrollbars=1, width=630,height=650");
        mywindow.moveTo(0,0);
  }
   
   //listens for directions click and calls teh direction function
   $("#get-directions").click(function(){
    var address = $("#from_address").val();
    var city = $("#from_city").val();
    var state= $("#from_state").val();
    var zip = $("#from_zip").val();
    var to_address = $("#to_address").val();
    var to_city = $("#to_city").val();
    var to_state= $("#to_state").val();
    var to_zip = $("#to_zip").val();
    
    if(zip=="" && state=="" && city=="" && address=="")
    {
        alert("Some Starting Place is Required");
    }
    else
    {
        bobs.get_directions(address+" "+city+", "+state+" "+zip, to_address+" "+to_city+", "+to_state+" "+to_zip)
    }
    
    return false;
    
   });
   
   //prevent points submit if field is blank
   $("#header-point-form").submit(function(){
     var val = $("input.check-points").val();
    
    if(val.trim()=="" || val == "ID# or Email")
    {
        return false;
    }
   });
   
   // opens holiday flipbook
   //$("a.flipbook").fancybox();

});

function SetCheck(el)
{
  if(el)
  {
    if(el.checked)  el.setAttribute('checked','checked');
    else            el.removeAttribute("checked");
  }
}

function toggleRadio(el) {
var groupName = el.name;
var checkedButtonId = el.id;

var groupButtonNodes = document.getElementsByName(groupName);

for (var i = 0; i < groupButtonNodes.length; i++) {
if (groupButtonNodes[i].id == checkedButtonId)
groupButtonNodes[i].setAttribute('checked', 'checked');
else
groupButtonNodes[i].removeAttribute('checked');
}
} 

