$(document).ready( function() {
  ei = {  //expand image
    
    dataSet : {
                Titles    : [],
                Prices    : [],
                Ids       : [],
                ClickUrls : []
              },

    queryDataSet : [],

    //funcybox のタイトルHTMLを出力(コールバック)
    titleFormat : function ( title, currentArray, currentIndex, currentOpts ) {
      var html = "", i = currentIndex;
      html += '<div id="expandImageTitle">';
      html += '<p style="font-weight: bold;">' + ei.dataSet.Titles[i] + '</p>';
      html += '<p style="font-weight: bold; color: rgb(144,34,0);">' + ei.dataSet.Prices[i] + '円</p><br />';
      html += '<div class="exImageLinks"><a id = "' + ei.dataSet.Ids[i] + '"class="linkToResearch_expand">類似画像検索</a>';
      html += '<a class="linkToDetail_expand" href="' + ei.dataSet.ClickUrls[i]  + '" target="_blank">詳細へ</a></div>';
      html += '</div>';
      return html;
    },

    //拡大した後のコールバック関数
    afterExpand : function () {
      //バインドする
      $(".linkToResearch_expand").bind( "click", function (event) {
                                                   var div, img, imgData, data;

                                                   //まず消す
                                                   $.fancybox.close();

                                                   //setQueryをする
                                                   var targetid = event.target.id;
                                                   var targetParent = $("a#" + targetid).parent();
                                                   var targetimg = targetParent.children( "img" )[0];
                                                   var src = targetimg.getAttribute( "src" ),
                                                       tag = targetimg.getAttribute( "name" ),
                                                       imgUrl = targetParent.children( "a.linkToExpand" )[0].getAttribute( "href" ),
                                                       clickUrl = targetParent.children( "a.linkToDetail" )[0].getAttribute( "href" );
                                                   var data, Title, Price;

                                                   var tandp = ei.getTitleAndPriceById( this.id );
                                                   if ( tandp ) {
                                                     Title = tandp.Title;
                                                     Price = tandp.Price;
                                                   }
                                                
                                                   data = {
                                                            id : this.id,
                                                            thub : src,
                                                            tag : tag ,
                                                            imgUrl : imgUrl,
                                                            clickUrl : clickUrl,
                                                            Title : Title,
                                                            Price : Price
                                                          };
                                                   //左上にクエリ画像表示
                                                   is.setQueryImage( data );

                                                   //カテゴリ選択
                                                   ct.selectCategoryFromTag( tag );
                                                   
                                                   is.doSearch( this.id, "imageClick" );
                                                
                                                   //検索履歴に画像表示
                                                   hs.setImageList( data );
                                                 });
    },

    bindExpand  : function () {
      $("a.linkToExpand").fancybox({
        'type'          : 'image',
        'titlePosition' : 'inside',
        'titleFormat'   : ei.titleFormat,
        'modal'         : false,
        'onComplete'    : ei.afterExpand
      });
    },

    setDataSet : function ( data ) {
      this.dataSet.Titles = [];
      this.dataSet.Prices = [];
      this.dataSet.Ids = [];
      this.dataSet.ClickUrls = [];

      var i, id;
      for ( var i = 0; i < data.length; i++ ) {
        id = data[i].id;
        this.dataSet.Titles[i]    = data[i].title;
        this.dataSet.Prices[i]    = data[i].price;
        this.dataSet.Ids[i]       = data[i].id;
        this.dataSet.ClickUrls[i] = data[i].clickUrl;
      }
    },

    titleFormatQuery : function () {
      var html = "";
      html += '<div id="expandImageTitle">';
      html += '<p style="font-weight: bold;">' + ei.queryDataSet.Title + '</p>';
      html += '<p style="font-weight: bold; color: rgb(144,34,0);">' + ei.queryDataSet.Price + '円</p><br />';
      html += '<div><a class="linkToDetail_expandQuery" href="' + ei.queryDataSet.ClickUrl  + '" target="_blank">詳細へ</a></div>';
      html += '</div>';
      return html;
    },

    afterExpandQuery : function () {
      //クエリーを拡大しても類似検索はないので中身なし
    },

    bindExpandQuery  : function () {
      $("a.linkToExpandQuery").fancybox({
        'type'          : 'image',
        'titlePosition' : 'inside',
        'titleFormat'   : ei.titleFormatQuery,
        'modal'         : false,
        'onComplete'    : ei.afterExpandQuery
      });
    },

    setDataSetQuery : function ( data ) {
      ei.queryDataSet = { Title : data.Title, Price : data.Price, ClickUrl : data.clickUrl, Id : data.id};
    },

    getTitleAndPriceById : function ( id ) {
      for ( var i = 0; i < 36; i++ ) {
        if ( ei.dataSet.Ids[i] ) {
          if ( ei.dataSet.Ids[i] == id ) {
            return { Title : ei.dataSet.Titles[i], Price : ei.dataSet.Prices[i] };
          }
        } else {
          return false;
        }
      }
      return false;
    }
  };
});

