/*
Fix Array.prototype.reduce
MDC the source
https://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/Array/reduce
*/
if (!Array.prototype.reduce)
{
  Array.prototype.reduce = function(fun /*, initial*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    // 初期値がない場合と空配列の場合は値を返さない
    if (len == 0 && arguments.length == 1)
      throw new TypeError();

    var i = 0;
    if (arguments.length >= 2)
    {
      var rv = arguments[1];
    }
    else
    {
      do
      {
        if (i in this)
        {
          rv = this[i++];
          break;
        }

        // 配列が値を含まない場合、初期値を返さない
        if (++i >= len)
          throw new TypeError();
      }
      while (true);
    }

    for (; i < len; i++)
    {
      if (i in this)
        rv = fun.call(null, rv, this[i], i, this);
    }

    return rv;
  };
}

/*
Fix Array.prototype.forEach
MDC the source
https://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/Array/reduce
*/
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

var Site = function() {
  if (/MSIE/.test(navigator.userAgent) && parseInt(navigator.userAgent.match(/MSIE (\d+)/)[1], 10) < 8) {
    this.classes = document.getElementsByTagName('body')[0].getAttribute('className').split(' ');
  }
  else {
    this.classes = document.getElementsByTagName('body')[0].getAttribute('class').split(' ');
  }
  this.actions = [];
};

Site.prototype = {
  doAction: function() {
    var self = this;
    this.actions.forEach(function(action) {
      if (self.classes) {
        if (self.classes.reduce(function(x, y) {
          return (x === true || x === action.cls || y === action.cls) ? true : false;
        })) action.fn(self);
      }
      if (action.cls === 'common') action.fn(self);
    });
  },
  addAction: function(cls, fn) {
    this.actions.push({ cls: cls, fn: fn });
  }
};

var kinoe = new Site();

kinoe.addAction('common', function() {
  $('#globalnav > ul > li:first-child').mouseover(function() {
    $('#dropdownnav > ul').slideDown();
    $('#dropdownnav > ul > li').mouseout(function(e) {
      $('#dropdownnav > ul').hover(
        function(e) {
          e.stopPropagation();
        },
        function(e) {
          $('#dropdownnav > ul').slideUp();
        }
      );
    });
  });
});

kinoe.addAction('home', function() {
  $(window).load(function() {
    if ($.fn.nivoSlider) {
      $('#slider').nivoSlider({
        directionNav: false,
        controlNav: false
      });
    }
  });
  $.getJSON(
    'https://ajax.googleapis.com/ajax/services/feed/load?q=http%3A%2F%2Fkinoetaiei.blog49.fc2.com%2F%3Fxml&v=1.0&num=5&callback=?',
    function (json) {
      $.each(json.responseData.feed.entries, function(i, entry) {
        var parsedDate = new Date(entry.publishedDate);
        var date_string = parsedDate.getFullYear() + '年' + (parsedDate.getMonth() + 1) + '月' + parsedDate.getDate() + '日';
        $('#blogfeed').append('<li><a href="' + entry.link + '">' + date_string + ' ' + entry.title + '</a></li>');
      });
    }
  );
});

kinoe.addAction('archive', function() {
  $('#houses .house .point').click(function() {
  var img_url = $(this).find('.symbol > p.photo > a').attr('href');
    location.href = img_url;
  });
});

kinoe.addAction('single-house', function() {
  if ($.fancybox) $('a.fancybox').fancybox();
  var latlng, map_option, marker_option;
  if (!$('#lat').val() && !$('#lng').val()) {
    return;
  }
  latlng = new google.maps.LatLng($('#lat').val(), $('#lng').val());
  map_option = {
    zoom: 14,
    center: latlng,
    navigationControl: true,
    navigationControlOptions: {
      style: google.maps.NavigationControlStyle.SMALL
    },
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById('mapCanvas'), map_option);
  marker_option = {
    map: map,
    position: map_option.center
  };
  var marker = new google.maps.Marker(marker_option);
});

kinoe.addAction('single-house', function() {
  $('#contents .photos .etc img').each(function() {
    $(this).width(220);
  });
});

$(function() {
  /*if (!$.support.style) $('body').attr('id', 'ie7');*/
  kinoe.doAction();
});

