﻿/// <reference path="jquery-1.2.6-vsdoc.js"/>
/// <reference path="google.maps-vsdoc.js" />

function getHCardData() {

  var vcard = $('#venue-map-contanct-source');

  var data = {
    'fn': vcard.find('.fn').text(),
    'adr': {
      'street': vcard.find('.adr .street-address').text(),
      'postalCode': vcard.find('.adr .postal-code').text(),
      'locality': vcard.find('.adr .locality').text()
    },
    'tel': vcard.find('.tel').text(),
    'url': vcard.find('.contact .url').attr('href'),
    'email': vcard.find('.email').text(),
    'geo': {
      'lat': vcard.find('.geo .latitude').attr('title'),
      'lon': vcard.find('.geo .longitude').attr('title'),
      'latText': vcard.find('.geo .latitude').text(),
      'lonText': vcard.find('.geo .longitude').text()
    }
  };
  return data;
}

function getHCardHtml(data) {

  var html =
    '<div class="vcard">' +
      '<h3 class="fn org">' + data.fn + '</h3>' +
      '<p>' +
			  '<span class="street-address">' + data.adr.street + '</span><br />' +
        '<span class="postal-code">' + data.adr.postalCode + '</span>, <span class="locality">' + data.adr.locality + '</span>' +
		  '</p>' +
		  '<p>' +
		    ((data.tel) ? 'Tel: <span class="tel">' + data.tel + '</span><br />' : '') +
		    ((data.email) ? 'E-mail: <a class="email" href="mailto:' + data.email + '">' + data.email + '</a><br />' : '') +
		    ((data.url) ? 'Web: <a class="url" href="' + data.url + '">' + data.url + '</a>' : '') +
		  '</p>' +
      '<p class="geo">GPS: ' +
        '<abbr class="latitude" title="' + data.geo.lat + '">' + data.geo.latText + '</abbr>; ' +
        '<abbr class="longitude" title="' + data.geo.lon + '">' + data.geo.lonText + '</abbr>' +
      '</p>' +
    '</div>';
  return html;
}

function initToolbox(reviewContent, review) {
  var tools = $('.tools', review).hide();
  reviewContent.
    mouseover(function() {
      if (review.hasClass('hide-tools')) return;
      tools.show();
    }).
    mouseout(function() {
      tools.hide();
    });
}

function attachEditHandler(editButton, reviewContent, review) {
  editButton.click(function() {
    $.get($('a', editButton).attr('href'), null, function(data) {
      reviewContent.html(data);
      Trop.createRatings($('select', reviewContent));
      jQuery.getScript('/scripts/jquery.form.js', function() {
        $('form:not(.ajaxless)', reviewContent).ajaxForm({
          beforeSubmit: function(data, form) {
            var result = true;
            jQuery.each(data, function(i, pair) {
              if (pair.name == 'text' && !pair.value) {
                alert('Vyplňte text recenze.');
                $('#review-text', form).css('border-color', 'red').focus();
                result = false;
              }
            });
            return result;
          },
          success: function(data) {
            reviewContent.html(data);
            initMoreRatings(review);
            initToolbox(reviewContent, review);
            attachEditHandler($('.tools .ico-edit', reviewContent), reviewContent, review);
          }
        });
      });
    });
  });
}

function initMoreRatings(review) {
  $('.more-ratings', review).each(function(i, item) {
    var ratings = $(item);
    var moreButton = $('<a href="" />').
    text(localization.More).
    click(function() {
      ratings.show();
      moreButton.hide();
      review.addClass('hide-tools');
      return false;
    });
    var closeButton = $('<a href="" class="closeButton">x</a>').
    click(function() {
      ratings.hide();
      moreButton.show();
      review.removeClass('hide-tools');
      return false;
    }).
    attr('title', localization.Close);
    ratings.
    append(closeButton).
    hide().
    parent().append(moreButton);
  });
}

function preventCommentsHidingWhenLinked(review) {
  var linkedComment = window.location.hash;

  review.has(linkedComment).addClass('stayopen');
}

function hideComments(review, item) {
  if (review.hasClass('stayopen')) return;
  review.addClass('hidden-comments');
  $('.comment-button', item).click(function() {
    review.toggleClass('hidden-comments');
    return false;
  }).append(': ' + $('.hentry', item).length);
}

function setupMap() {
  if (position.x == 0 && position.y == 0) {
    return;
  }

  var data = getHCardData();
  var text = getHCardHtml(data);

  var map = new google.maps.Map2(document.getElementById('map'));
  var center = new google.maps.LatLng(position.x, position.y);
  map.setCenter(center, 15);
  var point = map.fromLatLngToDivPixel(center);
  point.x += 120; point.y -= 84;

  map.setCenter(map.fromDivPixelToLatLng(point), 15);
  map.addControl(new google.maps.SmallZoomControl3D);
  map.disableScrollWheelZoom();

  var marker = new twareg.trop.Marker(center, { label: 't' });
  var bubble = new twareg.trop.Bubble(marker, text);

  google.maps.Event.addDomListener(marker, 'click', function() { bubble.show(); });

  map.addOverlay(marker);
  map.addOverlay(bubble);
}

$(function() {

  setupMap();

  $('.ico1-add-social').click(function() {
    $('#social').modal();
    return false;
  });

  if ($.fn.ImageCarousel) {
    $('.carousel').ImageCarousel({
      display_header: false,
      carousel_width: '100%'
    });
  }

  if ($.fn.fancybox) {
    $('a[rel="lightbox"]').fancybox();
  }

  $('#reviews .discussion-content > ol > li').each(function(index, item) {
    var review = $(item);
    var reviewContent = $('.review', review);
    var editButton = $('.tools .ico-edit', review);

    $('.tools .ico-vote-up, .tools .ico-vote-down', review).click(function(e) {
      window.location = $('a', $(e.target)).attr('href');
    });

    initToolbox(reviewContent, review);
    attachEditHandler(editButton, reviewContent, review);
    initMoreRatings(review);
    preventCommentsHidingWhenLinked(review);
    hideComments(review, item);
  });

});

