//contains common displays (address, distance, etc)
//common_dom.js also needs to be included

if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

function mq_ParamExists (varname) {
    var undef;
    return (varname !== undef);
}

//function used to display the outputting of a location in block format
function mq_display_address_location(container, loc, strURL) {
    var bSpace=false;

    if(!mq_ParamExists(loc))
        return;

    var div = createDiv (container, '', '');

    if (strURL) {
      if (mq_ParamExists(loc.name) && loc.name.length) {
        var a = createA (div, strURL, '');
        var em = a.appendChild (document.createElement ('b'));
            em.appendChild (document.createTextNode (loc.name));
        div.appendChild (document.createElement ('br'));
      } else {
        var a = createA (div, strURL, '');
        var em = a.appendChild (document.createElement ('b'));
            em.appendChild (document.createTextNode ('Tim Hortons'));
        div.appendChild (document.createElement ('br'));
      }
    }
    if (mq_ParamExists(loc.address) && loc.address.length) {
      div.appendChild (document.createTextNode (loc.address));
      div.appendChild (document.createElement ('br'));
    }
    if (mq_ParamExists(loc.city) && loc.city.length) {
      div.appendChild (document.createTextNode (loc.city + ','));
    }
    if (mq_ParamExists(loc.stateProvince) && loc.stateProvince.length) {
      div.appendChild (document.createTextNode (loc.stateProvince));
      bSpace=true;
    }
    if (mq_ParamExists(loc.postalCode) && loc.postalCode.length) {
      if (bSpace) { div.appendChild (document.createTextNode (' ')); }
      div.appendChild (document.createTextNode (loc.postalCode));
    }
    if (mq_ParamExists(loc.country) && loc.country.length) {
      if(bSpace) { div.appendChild (document.createTextNode (' ')); }
      div.appendChild (document.createTextNode (loc.country));
    }

    if (mq_ParamExists(loc.userFields) && mq_ParamExists(loc.userFields.user4) && loc.userFields.user4.length) {                
      div.appendChild (document.createElement ('br'));
      div.appendChild (document.createTextNode ('Tel: '+ loc.userFields.user4));
    }

    //example of how to output location parameters from the userfields
    //if (mq_ParamExists(loc.userFields) && mq_ParamExists(loc.userFields.user1) && loc.userFields.user1.length) {
    //if (mq_ParamExists(loc.userFields) && mq_ParamExists(loc.userFields.user1) && loc.userFields.user1.length) {
       //div.appendChild (document.createElement ('br'));
       //div.appendChild (document.createTextNode (loc.userFields.user1));
    //}
}

//this function is used to display the address in the single line format
function mq_display_address_location_single_line(container, loc, image, width, height, label) {
    var bSpace=false;

    if (arguments.length < 6)
        label = '';

    if (!mq_ParamExists(loc))
        return;

    var div = createDiv (container, '', '');

    if (image != "")
        createImg (div, "" + image, width, height, '', '', 'address icon');

    if (label != "")
        div.appendChild (document.createTextNode (label + ' '));

    if (mq_ParamExists(loc.address) && loc.address.length)
        div.appendChild (document.createTextNode (loc.address + " "));

    if (mq_ParamExists(loc.city) && loc.city.length)
        div.appendChild (document.createTextNode (loc.city + " "));

    if (mq_ParamExists(loc.stateProvince) && loc.stateProvince.length) {
        div.appendChild (document.createTextNode (loc.stateProvince));
        bSpace=true;
    }
    if (mq_ParamExists(loc.postalCode) && loc.postalCode.length) {
        if(bSpace)
        {
            div.appendChild (document.createTextNode (" "));
        }
        div.appendChild (document.createTextNode (loc.postalCode));
    }
    if (mq_ParamExists(loc.country) && loc.country.length) {
        if(bSpace) {
            div.appendChild (document.createTextNode (" "));
        }
        div.appendChild (document.createTextNode (loc.country));
    }
}

//function used to format a number (num) to x(dec) decimal places
function formatNumber(num,dec) {
   return Math.floor(num * Math.pow(10,dec))/Math.pow(10,dec);
}

//function displays the time in format x hours, x.xx minutes or x.xx minutes
function mq_display_time(totalTime) {
   var newTime;
   // more than a minute
   if(totalTime > 3600) {
      newTime = totalTime/3600;
      var result = (" " + Math.floor(newTime) + " hours,");
      newTime = (totalTime/60)%60;
      result += (" " + Math.floor(newTime) + " minutes");
      return result;
   }
   if(totalTime > 60) {
      newTime = totalTime/60;
      return (" " + Math.floor(newTime) + " minutes");
   } else {
      return("1 minute");
   }
}

//function used to display distance formated in a div with right alignment.
//outputs miles or kilometers with 2 digits of precision
function mq_display_distance(totalDistance) {
   return (" " + totalDistance.value + (totalDistance.units == "mi" ? " miles" : " kilometers"));
}

/*
 * functions to make non-dom compliant browsers behave
 */
function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    this.clientX = window.event.clientX;
    this.clientY = window.event.clientY;
    this.which = window.event.button;
    return this;
}

function add_listener (object, event, callback)
{
    if (object != null) {
        if (object.addEventListener) {  // w3c DOM
            object.addEventListener (event, callback, false);
        } else if (object.attachEvent) { // IE
            object.attachEvent ('on' + event, function () {callback (new W3CDOM_Event (object))});
        }
    }
}

