$(function() {
  // shoplist
  if ($('.shoplist').length) {
    var displayShoplist = function(ev) {
      var data = (ev && ev.target) ? $(ev.target.parentNode) : $('#shop01');
      $('#shop-name').text($('.name', data).text());
      $('#shop-address').text($('.address', data).text());
      $('#shop-tel').text('TEL. ' + $('.tel', data).text());

      var ll = $('.latlng', data).text().split(',');
      var latlng = new google.maps.LatLng(parseFloat(ll[0]), parseFloat(ll[1]));
      var myOptions = {
        zoom: 16,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map($('#shop-map').get(0), myOptions);
      var marker = new google.maps.Marker({
        position: latlng,
        title: $('.name', data).text()
      });
      marker.setMap(map);


      $('#shoplist-links li').removeClass('selected');
      data.addClass('selected');
      return false;
    }
    $('#shoplist-links .name').click(displayShoplist);
    displayShoplist();

    var scrollToArea = function() {
      var area = $(this).parent().attr('id').replace('link-', '');
      var scroll = $('#shoplist-links-scroll');
      var pos = $('#' + area).offset().top + scroll.scrollTop() - 293;
      scroll.animate({scrollTop: pos});
      return false;
    }
    $('#shoplist-menu a').click(scrollToArea);
  }

  // new items
  if ($('.newitems').length) {
    var displayNewItems = function(ev) {
      var data = (ev && ev.target) ? $(ev.target).parents('li').eq(0) : $('#newitems-links').find('li').first();

      $('#item-cat').text($('.cat', data).text());
      $('#item-date').text($('.date', data).text());
      var img = $('<img>').attr('src', 'assets/images/newitems/' + data.attr('id') + '.png')
      $('#item-image').empty().append(img);
      $('#item-price').text('P R I C E :   ' + $('.price', data).text());
      $('#item-size').text('S I Z E :   ' + $('.size', data).text());
      $('#item-color').text('C O L O R :   ' + $('.color', data).text());
      $('#item-desc2').text($('.desc', data).text());

      return false;
    }
    $('#newitems-links .image').click(displayNewItems);
    displayNewItems();
  }

  // coordinate
  if ($('.coordinate').length) {
    var displayCoordinate = function(ev) {
      var data = (ev && ev.target) ? $(ev.target).parents('li').eq(0) : $('#coordinate-links').find('li').first();

      $('#coordinate-date').text($('.date', data).text());
      var img = $('<img>').attr('src', 'assets/images/coordinate/' + data.attr('id') + '.png')
      $('#coordinate-image').empty().append(img);
      var list = $('.coordinate-item');
      $('.item-name', list).empty();
      $('.item-price', list).empty();
      $('.item-size', list).empty();
      $('.item-color', list).empty();

      $('#coordinate-desc').text($('.desc', data).text());
      $('.items li', data).each(function(index, e) {
        var idx = index + 1;
        var target = $('#coordinate-item' + idx);
        var source = $(this);
        $('.item-name', target).text($('.name', source).text());
        $('.item-price', target).text('PRICE: ' + $('.price', source).text());
        $('.item-size', target).text('SIZE: ' + $('.size', source).text());
        $('.item-color', target).text('COLOR: ' + $('.color', source).text());
      });

      return false;
    }
    $('#coordinate-links .image').click(displayCoordinate);
    displayCoordinate();
  }

  // campaign ad
  if ($('.ad').length) {
    var changeImage = function(name) {
      if ($.colorbox.element().attr('id') == name) return;
      $('#' + name).trigger('click');
    }
    $('.ad').colorbox({rel: 'ad', transition: 'elastic',
      onOpen: function() {
        $('body').addClass('adx');
        $('#cboxBottomCenter').append($('#ad-colorbox-controller').html());
        $('#cboxBottomCenter a').click(function() {
          var name = $(this).attr('class').replace('ad-colorbox-link', '').replace(/\s/, '');
          changeImage(name);
          return false;
        });
      },
      onLoad: function() {
        $('.ad-colorbox-link').removeClass('selected');
        var id = $.colorbox.element().attr('id');
        $('#cboxBottomCenter .' + id).addClass('selected');
        setTimeout(function() {
          $('.cboxPhoto').each(function() {
            this.style.cursor = 'default';
            this.onclick = function() { return false };
          });
        }, 350);
      },
      onClose: function() {
        $('body').removeClass('adx');
      }
    });
  }

  // movie
  if ($('.mv').length) {
    $('.mv').colorbox({rel: 'mv', transition: 'elastic',
      iframe: true, 
      width: 830, height: 685,
      onOpen: function() {
        $('body').addClass('mvx');
        $('<div></div>').attr('id', 'cboxXBottom').appendTo($('#cboxContent').append());
      },
      onClose: function() {
        $('body').removeClass('mvx');
      }
    })
  }

  // catalog
  if ($('.catalog-image').length) {
    var a = $('#catalog-a');
    var b = $('#catalog-b');
    var w, h;
    var pos = {x: 0, y: 0};
    var scale = 0.3;
    var flip = function() {
      pos.x = pos.y = 0;
      scale = 0.3;
      which = (b.css('display') == 'none') ? b : a;
      $('.catalog-image').css('display', 'none');
      which.css({'width': parseInt(w * scale) + 'px', 'left': pos.x, 'top': pos.y});
      which.show();
      return false;
    }
    var move = function(x, y) {
      if (x < - scale * w) return false;
      if (x > 1020) return false;
      which = (b.css('display') == 'none') ? a : b;
      which.animate({'left': x, 'top': y});
      pos.x = x, pos.y = y;
      return false;
    }
    var zoom = function(plus) {
      if (scale + plus <= 0.15) return false;
      if (scale + plus > 1) return false;
      which = (b.css('display') == 'none') ? a : b;
      var mx = (w * (scale + plus) - w * scale);
      var px = (pos.x - 510) / (w * scale);
      pos.x = pos.x + mx * px;
      var my = (h * (scale + plus) - h * scale);
      var py = (pos.y - 322) / (h * scale);
      pos.y = pos.y + my * py;
      scale += plus;
      which.animate({'width': parseInt(w * scale), 'left': pos.x, 'top': pos.y });
      return false;
    }
    $('#flip').click(flip);
    $('#zoom-in').click(function() {
      return zoom(0.1);
    });
    $('#zoom-out').click(function() {
      return zoom(-0.1);
    });
    $('#move-left').click(function() {
      return move(pos.x - 500, pos.y);
    });
    $('#move-right').click(function() {
      return move(pos.x + 500, pos.y);
    });
    $('#move-1').click(function() {
      return move(510 - (1 * (w * scale) / 8), 0);
    });
    $('#move-2').click(function() {
      return move(510 - (3 * (w * scale)) / 8, 0);
    });
    $('#move-3').click(function() {
      return move(510 - (5 * (w * scale)) / 8, 0);
    });
    $('#move-4').click(function() {
      return move(510 - (7 * (w * scale)) / 8, 0);
    });
    a.load(function() {
      w = parseInt(a.width());
      h = parseInt(a.height());
      $('.catalog-image').draggable({
        cursor: 'move',
        stop: function() {
          pos.y = parseInt($(this).css('top'));
          pos.x = parseInt($(this).css('left'));
        }
      });
      $('#loading').hide();
      flip();
    });
  }

});



