/*===========================================================================
	Behavior (Global)
	
	This JavaScript executes dynamic behaviors such as:
	* Preloading images
	* Flash Replacements (i.e. sIFR, UFO, SWFObject, etc.)
	* Decorate the DOM with presentational elements
	* Collapsing/expanding sections
	* Form validation
	* Popup windows
===========================================================================*/

Cufon.replace("div#menu h2 em");
Cufon.replace("div#menu a.back-to-top em");

var map;
var Gpoint;
var marker;

function initializeMap() {
 if (GBrowserIsCompatible()) {
      // Display the map, with some controls and set the initial location 
      var map = new GMap2(document.getElementById("map"));
      map.setUIToDefault();
      map.setCenter(new GLatLng(32.8974,-96.8024), 16);

      point = new GLatLng(32.89595722983238,-96.80335342884064);
      marker = new GMarker(point);

      html = '<h3>Zinsky&rsquo;s Deli</h3><br />Directions: <a href="javascript:tohere()">To here</a> - <a href="javascript:fromhere()">From here</a>';
      marker.bindInfoWindowHtml(html);

      map.addOverlay(marker);
    }
}

// functions that open the directions forms
function tohere() {
    to_html = '<h3>Zinsky&rsquo;s Deli</h3><br />Directions: <b>To here</b> - <a href="javascript:fromhere()">From here</a><br /><br />' +
       '<form action="http://maps.google.com/maps" method="get" target="_blank">' +
       '<label for="saddr">Start address:<br />' +
       '<input type="text" maxlength="40" name="saddr" id="saddr" value="" /></label>' +
       '<input value="Get Directions" type="submit" />' +
       '<input type="hidden" name="daddr" value="' + Gpoint.lat() + ',' + Gpoint.lng() + 
              // "(" + name + ")" + 
       '"/>';
  marker.openInfoWindowHtml(to_html);
}

function fromhere() {
  from_html = '<h3>Zinsky&rsquo;s Deli</h3><br />Directions: <a href="javascript:tohere()">To here</a> - <b>From here</b><br /><br />' +
     '<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
     '<label for="daddr">End address:<br />' +
     '<input type="text" maxlength="40" name="daddr" id="daddr" value="" /></label>' +
     '<input value="Get Directions" type="submit" />' +
     '<input type="hidden" name="saddr" value="' + Gpoint.lat() + ',' + Gpoint.lng() +
            // "(" + name + ")" + 
     '"/>';
  
  marker.openInfoWindowHtml(from_html);
}

$(document).ready(function() {
  initializeMap();
  // GEvent.trigger(marker, "click");

  if ($.browser.safari() && $.browser.win()) {
    $("div#page ul#content li#whats-going-on a.close").css("margin-top", "27px");
    $("div#page ul#content li#sandwich-for-a-cause a.close").css("margin-top", "50px");
  }

  if ($.browser.msie() && $.browser.version.number() <= 7) {
    $("div#page ul#content li#about-zinskys img#jim-and-liz + p").css("margin-top", "0");
    $("div#page ul#content li#whats-going-on a.close").css("margin-top", "23px");
    overlay = 0;
  } else {
    overlay = 40;
  }

  if ($.browser.msie() && $.browser.version.number() == 8) {
    $("div#page ul#content li#about-zinskys a.close").css("margin-top", "31px");
    $("div#page ul#content li#whats-going-on a.close").css("margin-top", "23px");
    $("div#page ul#content li#sandwich-for-a-cause a.close").css("margin-top", "22px");
  }

  $("div#catering-menu").jqm({
    modal: false,
    ajax: 'catering.html div#menu',
    trigger:'li#catering-menu a.header.modal',
    closeClass:'close-modal',
    overlay:overlay,
    onLoad: function(hash) {
      Cufon.refresh();
      var height = hash.w.height();
      $("div#page").height(height+300);
    },
    onHide: function(hash) {
      if ($.browser.msie() && $.browser.version.number() <= 6) {
        hash.w.hide();
        $("div#page").height("100%");
      } else {
        hash.w.hide();
        if (hash.o.size() > 0) {
          hash.o.remove();
        }
        $("div#page").height("auto");
      }
    }
  });

  $("div#all-menus").jqm({
    modal: false,
    ajax: 'menu.html div#menu',
    trigger:'li#menu a.header.modal',
    closeClass:'close-modal',
    overlay:overlay,
    onLoad: function(hash) {
      Cufon.refresh();
      var height = hash.w.height();
      $("div#page").height(height+300);
    },
    onHide: function(hash) {
      if ($.browser.msie() && $.browser.version.number() <= 6) {
        hash.w.hide();
        $("div#page").height("100%");
      } else {
        hash.w.hide();
        if (hash.o.size() > 0) {
          hash.o.remove();
        }
        $("div#page").height("auto");
      }
    }
  });

  var previousContent;
  var open = false;

  $("li div.content").css("display", "none");

  $("a.header:not(.modal)").live("click", function(e) {
    e.preventDefault();
    var current = $(this);
    var currentContent = $(this).siblings();

    if (!previousContent) {
      currentContent.addClass("current").slideDown("2000", function() {
        previousContent = currentContent;
        open = true;
      });
    }

    if (previousContent && open) {
      if (previousContent.parents("li").attr("id") != currentContent.parents("li").attr("id")) {
        previousContent.removeClass("current").slideUp("2000", function() {
          currentContent.addClass("current").slideDown("2000", function() {
            previousContent = currentContent;
            open = true;
          });
        });
      } else {
        currentContent.removeClass("current").slideUp("2000", function() {
          previousContent = false;
          open = false;
        });
      }
    }
  });

  $("div.content a.close").live("click", function(e) {
    e.preventDefault();

    $(this).parents("div.content").removeClass("current").slideUp("1000", function() {
      previousContent = false;
      open = false;
    });
  });

  $("ul#menu-navigation li a").live("click", function(e) {
    e.preventDefault();
    var target = transposeHash($(this).attr("href"));

    $("ul#menu-navigation li a.current").removeClass("current");
    $(this).addClass("current");

    $("div.menu.current").css("display", "none").removeClass("current");
    $(target).css("display", "block").addClass("current");

    var new_height = parseInt($(this).parents("div.modal").height(), 10);
    $("div#page").height(new_height+300);
  });
  
  function transposeHash(hash) {
    hash = hash.replace(/^((http|https):\/\/)?[^#].[^\/]+/, "");
    hash = hash.replace(/^\//, "");
    hash = hash.replace(/\/$/, "");
    return hash;
  }

  $("a[rel='external']").click(function(e) {
    // Adapted from: http://www.456bereastreet.com/archive/200610/opening_new_windows_with_javascript_version_12/
    var oWin = window.open(this.getAttribute("href"), "_blank");
    if (oWin) {
      if (oWin.focus) oWin.focus();
      return false;
    }

    oWin = null;
    return true;
  });

	$("#zinskys-club-form").validate();
});