﻿var map, directionsService, directionsDisplay;
var frontAds = new Array();

$(document).ready(function(){
  $('#h2-firma, h1, .breadcrumbs').supersleight();
  if($('#map-canvas').length) {
    var mapOptions = { zoom:      7,
                       center:    new google.maps.LatLng(55.659948,11.397983),
                       mapTypeId: google.maps.MapTypeId.ROADMAP
                     }
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
    for(i = 0; i < restaurants.length; i++) {
      setMarkers(map, restaurants[i]);
    }

    directionsService = new google.maps.DirectionsService();
    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("routedescription"));

    $('#directions-mode-selector').click(function(){
      $(this).toggleClass('walkthewalk');
    });
    $('#address-search-button').click(loadDirections);
    $('#map-address-search').keyup(function(event){
      if (event.keyCode == 13) {
        loadDirections();
      }
    });

  }

  $("a[href*='http://']:not([href*='"+location.hostname+"'])").click( function() {
    window.open(this.href);
    return false;
  });

  $("#subscribe-add").click(function() {
    $("#subscribe-action").val(1);
  });

  $("#subscribe-remove").click(function() {
    $("#subscribe-action").val(2);
  });

  $("#menu-tabs a").each(function() {
    var href = this.href + '?';
    href = href.substr(0, href.indexOf('?'));
    var lhref = location.href + '?';
    lhref = lhref.substr(0, lhref.indexOf('?'));
    if(href == lhref) {
      $(this).addClass('selected');
    }
  });

  // Do invalid CSS - Weee
  $('.store_board .store, #directions, .menu_specials td, .menu_drinks, .discount, .menu_sides').css( { '-moz-border-radius': '5px', '-webkit-border-radius': '5px' } );
  $('#routedescription').css( { 'overflow-x': 'hidden', 'overflow-y': 'auto' } );

  // Frontpage is loaded and images is supported
  if($('#front-ad').length && document.images) {
    var y;
    var fac = 0;

    // Find frontpage ads for current time spectrum
    var dobj = new Date();
    var day = dobj.getDay();
    var n = 1;
    if(dobj.getHours() >= 15) {
      n = 2;
    }
    for (c = 0; c < availableOffers.length ; c++)
    {
      if(availableOffers[c][((day*2)+n)]){
        $('#front-ad .initial').remove();
        $('<img src="images/front-ads/' + availableOffers[c][15] + '" alt="" />').appendTo('#front-ad .ad');
      }
    }
    $('#front-ad .ad').innerfade({ timeout: 6000 });
  }
});

var loadDirections = function() {
  $('#directions .nogo').hide();
  $('#routedescription').hide();
  $('#directions .searching').show();

  var tmode;
  var start = $('#map-address-search').val();

  if($('#directions-mode-selector').hasClass('walkthewalk')) {
    tmode = google.maps.DirectionsTravelMode.WALKING;
  } else {
    tmode = google.maps.DirectionsTravelMode.DRIVING;
  }

  var requests = new Array();
  var responses = new Array();
  // Find the shortest direction request
  var c = 0;
  var m;
  ld = function(response, status) {
    if(response) {
      responses[responses.length] = response;
    }
    if(c < restaurants.length) {
      m = requests.length;
      requests[m] = {
        origin:start, 
        destination:new google.maps.LatLng(restaurants[c][3], restaurants[c][4]),
        travelMode: tmode
      };
      c++;
      directionsService.route(requests[m], ld);
    } else {
      var distance = -1;
      var bestres = false;
      for(n = 0; n < responses.length; n++) {
        if(responses[n].status == google.maps.DirectionsStatus.OK) {
          tmpdist = responses[n].trips[0].routes[0].distance.value;
          if(tmpdist > 0 && (distance > tmpdist || distance == -1)) {
            distance = tmpdist;
            bestres = n;
          }
        }
      }
      $('#directions .searching').hide();
      if(bestres) {
        directionsService.route(requests[bestres], function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
            infowindow.close();
            directionsDisplay.setDirections(response);
            $('#routedescription').show();
          }
        });
      } else {
        $('#directions .nogo').show();
      }
    }
  }
  ld();
}

var delivery = [
  [ 900, 1474, 'København K'],
  [1500, 1799, 'København V'],
  [1800, 1999, 'Frederiksberg C'],
  [2000, 2000, 'Frederiksberg'],
  [2100, 2100, 'København Ø', 'Dog ikke nord for Svanemøllen St.'],
  [2200, 2200, 'København N', 'Dog ikke hele 2200'],
  [2300, 2300, 'København S', 'Dog ikke syd for Vejlands Allé'],
  [2450, 2450, 'København SV', 'Frederikskaj, Teglholmsgade, Scandiagade osv.', 1],
  [2500, 2500, 'Valby'],
  [2625, 2625, 'Vallensbæk'],
  [2635, 2635, 'Ishøj', 'Kun Ishøj by'],
  [2650, 2650, 'Hvidovre'],
  [2660, 2660, 'Brøndby Strand'],
  [2665, 2665, 'Vallensbæk Strand'],
  [4000, 4000, 'Roskilde', 'Roskilde by, inkl. Himmelev, Vindinge & Vor Frue']
];

/**
 * Data for the markers consisting of a name, a LatLng and a zIndex for
 * the order in which these markers should display on top of each
 * other.
 */
var restaurants = [
  ['Classensgade', 'Classensgade 26', '2100 København', 55.696691, 12.582267, 7, 0, '11.00', '23.00', 110168840],
  ['Amagerbrogade', 'Amagerbrogade 28', '2300 København', 55.665102, 12.599715, 6, 0, '11.00', '23.00', 110168843],
  ['Falkonér Allé', 'Falkonér Allé 47', '2000 Frederiksberg', 55.684266, 12.537408, 8, 0, '11.00', '23.00', 110168837],
  ['Hvidovre', 'Hvidovrevej 261B', '2650 Hvidovre', 55.639057, 12.47929, 9, 0, '11.00', '22.00', 100667840],
  ['Vallensbæk', 'Strandesplanaden 122', '2625 Vallensbæk Strand', 55.622704, 12.390598, 10, 0, '11.00', '22.00', 100705709],
  ['Roskilde', 'Københavnsvej 20', '4000 Roskilde', 55.641005, 12.098589, 5, 0, '11.00', '22.00', 100329529],
  ['MONARCH Karlslunde', 'Rasteplads 2', '2690 Karlslunde', 55.56159, 12.229886, 3, 1, '12.00', '22.00', 100157717],
  ['MONARCH Nyborg', 'Hjejlevej 107', '5800 Nyborg', 55.297275, 10.841167, 2, 1, '11.00', '20.00', 80672092],
  ['MONARCH Kildebjerg Nord', 'Motorvejen', '5492 Vissenbjerg', 55.394809, 10.188789, 1, 1, '11.00', '22.00', 80157710]
];

//  ['Nørreport', 'Nørre Voldgade 25', '1358 København K', 55.684125, 12.572747, 4, 0, '11.00', '23.00', 110831559],

var availableOffers = [
//  ['titel', son1, son2, man1, man2, tir1, tir2, ons1, ons2, tor1, tor2, fre1, fre2, lor1, lor2, image]
  ['LunchOffer', 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 'frokost-tilbud.jpg'],
  ['CrazyMonThu', 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 'crazy-monday-tuesday.jpg'],
  ['OneOneFree', 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, '2for1.jpg'],
  ['WeekendOffer', 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 'fredag-loerdag-soendag.jpg'],
  ['LavaCake', 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 'chocolate-lava-cakes.jpg'],
  ['555Deal', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, '555-deal.jpg']
];

var infowindow;
function setMarkers(map, restaurant) {
  var gmapLogo = new google.maps.MarkerImage('images/gmap-logo.png',
    new google.maps.Size(20, 20),
    new google.maps.Point(0,0),
    new google.maps.Point(10, 10));
  var gmapLogoMonarch = new google.maps.MarkerImage('images/gmap-logo-monarch.png',
    new google.maps.Size(33, 20),
    new google.maps.Point(0,0),
    new google.maps.Point(16, 10));

  var title = restaurant[1] + ', ' + restaurant[2];
  if(restaurant[6] == 1) {
    title = 'MONARCH - ' + title;
  }
  var markerLatLng = new google.maps.LatLng(restaurant[3], restaurant[4]);
  var markerOptions = { position: markerLatLng, 
                        map:      map,
                        icon:     gmapLogo,
                        title:    title,
                        zIndex:   restaurant[5]
                     };
  if(restaurant[6] == 1) {
    markerOptions.icon = gmapLogoMonarch;
  }
  var marker = new google.maps.Marker(markerOptions);

  infowindow = new google.maps.InfoWindow(); 
  google.maps.event.addListener(marker, 'click', function() { 
    var content = '<div class="gmapInfoWindow">' +
        '<img src="images/gmap-infowindow-logo.png" alt="Domino&apos;s Pizza" class="gmapInfoWindowLogo" />';
    if(restaurant[6] == 1) {
      content = content + '<img src="images/gmap-infowindow-monarch.png" alt="MONARCH" class="gmapInfoWindowLogoMonarch" />';
      content = content + '<div class="monarch-notice"><b>Bemærk:</b> MONARCH fører et <a href="http://monarch.dk/monarch/?page_id=4">andet udvalg</a></div>';
    } else {
      content = content + '<h4>' + restaurant[0] + '</h4>';
    }
    content = content + '<h5>' + restaurant[1] + '<br />' + restaurant[2] + '</h5>';
    if(restaurant[7] && restaurant[8]) {
      content = content + '<div class="hours">Åbningstider: <strong>' + restaurant[7] + ' - ' + restaurant[8] + '</strong></div>';
    }
    content = content + '<div class="reportlink"><a href="http://www.findsmiley.dk/da-DK/Searching/DetailsView.htm?virk=' + restaurant[9] + '" target="_blank" class="reportlink"><img src="images/button-inspection-report.png" alt="Se kontrol rapport" /></a></div>';
    content = content + '</div>';

    infowindow.setContent(content);
    infowindow.open(map, marker); 
    $('.gmapInfoWindow').parent().css('overflow', 'visible');
  }); 
}