﻿var ElementMaxHeight = function() {
  this.initialize.apply(this, arguments);
}

ElementMaxHeight.prototype = {
  initialize: function(className) {
    this.elements = document.getElementsByClassName(className || 'maxheight');    
    this.textElement = document.createElement('span');
    this.textElement.appendChild(document.createTextNode('A'));
    this.textElement.style.display = 'block';
    this.textElement.style.position = 'absolute';
    this.textElement.style.fontSize = '1em';
    this.textElement.style.top = '-1000px';
    this.textElement.style.left = '-1000px';
    document.body.appendChild(this.textElement);
    this.textElementHeight = document.getDimensions(this.textElement).height;
    var __object = this;
    var __checkFontSize = this.checkFontSize;
    this.checkFontSizeInterval = window.setInterval(function() {return __checkFontSize.apply(__object)}, 500);

    this.expand();

    // Refresh elements height onResize event
    var __expand = this.expand;
    if (window.addEventListener) {
      window.addEventListener('resize', function(event) {return __expand.apply(__object, [( event || window.event)])}, false);
    } else if (window.attachEvent) {
      window.attachEvent('onresize', function(event) {return __expand.apply(__object, [( event || window.event)])});
    }
  },

  expand: function() {
    this.reset();
  	for (var i = 0; i < this.elements.length; i++) {  	
      this.elements[i].style.height = document.getDimensions(this.elements[i].parentNode).height + 'px';
  	}
  },

  reset: function() {
    for (var i = 0; i < this.elements.length; i++) {    
      this.elements[i].style.height = 'auto';
    }
  },

  checkFontSize: function() {
  	var height = document.getDimensions(this.textElement).height;
  	if(this.textElementHeight != height) {
  		this.textElementHeight = height;
  		this.expand();
  	}
  }  
}


if (!!document.evaluate) {
  document._getElementsByXPath = function(expression, parentElement) {
    var results = [];
    var query = document.evaluate(expression, parentElement || document,
      null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    for (var i = 0, length = query.snapshotLength; i < length; i++)
      results.push(query.snapshotItem(i));
    return results;
  }
}

document.getElementsByClassName = function(className, parentElement) {
  if (!!document.evaluate) {
    var q = ".//*[contains(concat(' ', @class, ' '), ' " + className + " ')]";
    return document._getElementsByXPath(q, parentElement);
  } else {
    var children = (parentElement || document.body).getElementsByTagName('*');
    var elements = [], child;
    for (var i = 0, length = children.length; i < length; i++) {
      child = children[i];
      if (child.className.length != 0 &&
          (child.className == className ||
           child.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)")))) {      
        elements.push(child);
      }
    }
    return elements;
  }
}

document.getDimensions = function (element) {
  var display = element.style.display;
  if (display != 'none' && display != null) { // Safari bug
    return {width: element.offsetWidth, height: element.offsetHeight};
  }

  return {width: originalWidth, height: originalHeight};
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function GetDrivingDirections()
{
    var address = document.getElementById("address");
    
    if(address == null 
        || address.value.length < 1)
    {
        alert("Please enter an address");
        return;
    }
        
    var drivingDirectionsLink = "http://maps.yahoo.com/index.php?q1=" + address.value + "&q2=2505 Highway 2 Two Harbors, MN 55616";    
    
    var name = "directions";
	var features = "location=yes,menubar=yes,directories=yes,toolbar=yes,status=yes,resizable=yes,scrollbars=yes";

    //Set the window size (window automatically centered by this function)
    var height = 600;
    var width = 800;
	
    var screenHeight = window.screen.height;	
    var screenWidth = window.screen.width;
    var top = parseInt((screenHeight - height) / 2);
    var left = parseInt((screenWidth - width) / 2);
    features += ",top=" + top + "px";
    features += ",left=" + left + "px";
    features += ",width=" + width + "px";
    features +=",height=" + height + "px";
	
    var directionsWindow = window.open(drivingDirectionsLink, name, features);
    directionsWindow.focus();
}

var categoryInitialized = false;
$(document).ready(function () {
    var menuCategories = $("#restaurantItemMenu div");

    if (menuCategories && menuCategories.length > 0) {
        menuCategories.each(function (index) {
            $(this).attr("index", index);
        });
        menuCategories.click(changeMenuCategory);

        if (categoryInitialized == false) {
            var selectedCategory = getQuerystring("category");

            if (selectedCategory != "") {
                var categoryDiv = $("#" + selectedCategory);
                if (categoryDiv) {
                    categoryDiv.click();
                    categoryInitialized = true;
                }
            }
        }
    }
});

function changeMenuCategory() {
    $(".selectedRestaurantMenuItem").attr("class", "restaurantMenuItem");
    $(this).attr("class", "selectedRestaurantMenuItem");

    var menuCategoryItems = $(".restaurantMenuCategory");
    menuCategoryItems.slideUp('fast');

    var index = parseInt($(this).attr("index")) + 1;
    $(".restaurantMenuCategory:nth-child(" + index + ")").slideDown('slow');
}

function showCoupon(couponId)
{
    var name = "coupon";
	var features = "location=no,menubar=no,directories=no,toolbar=no,status=yes,resizable=no,scrollbars=no";

	//Set the window size (window automatically centered by this function)
	var height = 300;
	var width = 600;
	
	var screenHeight = window.screen.height;	
	var screenWidth = window.screen.width;
	var top = parseInt((screenHeight - height) / 2);
	var left = parseInt((screenWidth - width) / 2);
	features += ",top=" + top + "px";
	features += ",left=" + left + "px";
	features += ",width=" + width + "px";
	features +=",height=" + height + "px";
	
	var couponWindow = window.open("coupon-popup.aspx?id=" + couponId, name, features);
	
	if(couponWindow == null)
	{
	    alert("Please turn off your popup blocker and click the coupon again.");
	}
	else
	{
	    couponWindow.focus();
	}
}

function getQuerystring(key) {
    
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return "";
    else
        return qs[1];
}
