// wheel viewクラスの定義
// 現在はwheelは使っていない
var wv = function () {};

// 定数
wv.prototype.mode        = "";    // 検索モード similar / color
wv.prototype.category_id = ""; // 検索カテゴリ
wv.prototype.price = {
    from  :  "",
    to    :  ""
};
wv.prototype.loadingTimer = "";
wv.prototype.loadingFrame = 1;

wv.prototype.dispPos   = [
    {top:0,left:0},
    {top:0,left:125},
    {top:0,left:250},
    {top:0,left:375},
    {top:0,left:500},
    {top:0,left:625},
    {top:125,left:0},
    {top:125,left:125},
    {top:125,left:250},
    {top:125,left:375},
    {top:125,left:500},
    {top:125,left:625},
    {top:250,left:0},
    {top:250,left:125},
    {top:250,left:250},
    {top:250,left:375},
    {top:250,left:500},
    {top:250,left:625},
    {top:375,left:0},
    {top:375,left:125},
    {top:375,left:250},
    {top:375,left:375},
    {top:375,left:500},
    {top:375,left:625},
    {top:500,left:0},
    {top:500,left:125},
    {top:500,left:250},
    {top:500,left:375},
    {top:500,left:500},
    {top:500,left:625},
    {top:625,left:0},
    {top:625,left:125},
    {top:625,left:250},
    {top:625,left:375},
    {top:625,left:500},
    {top:625,left:625}
  ];
wv.prototype.yshoppingURL = "http://store.shopping.yahoo.co.jp";
wv.prototype.redirectURL = "http://rd.yahoo.co.jp/shp/fashionnavi/evt=85979/*";

// method定義
wv.prototype.doLoading = function () {
  loadingFrame = wv.prototype.loadingFrame;
  if ( !$('#dispLoadingWrap').is(":visible") ) {
    clearInterval(wv.prototype.loadingTimer);
    return;
  }
  $('#dispLoading').css('top', (loadingFrame * -40) + 'px');
  wv.prototype.loadingFrame = (loadingFrame + 1) % 12;
}

/*
* 商品画象表示領域を作成
*/
wv.prototype.createDispSpace = function () {
  var div, p, div_loading, div_loading_wrap;
  $("#dispSpace").remove();
  div = document.createElement("div");
  div.setAttribute("id", "dispSpace");
  div_loading_wrap = document.createElement("div");
  div_loading_wrap.setAttribute( "id", "dispLoadingWrap" );
  div_loading = document.createElement("div");
  div_loading.setAttribute( "id", "dispLoading" );
  //div_loading.innerHTML = "Now Loading.....";
  $(div_loading_wrap).append( div_loading );
  $(div).append( div_loading_wrap );
  $("#dispSpaceWrap").append(div);
};

/*
* ノードからテキストを取得
*/
wv.prototype.getTextContents = function (node) {
  if (typeof node.textContent != "undefined") {
    // Firefox
    return node.textContent;
  } else {
    // IE
    return node.text;
  }
};

/*
* Y!ショッピングへのリンクの場合リダイレクトURLを付加する
*/
wv.prototype.addRedirectURL = function (url) {

  if ( url.indexOf("store.shopping.yahoo.co.jp") != -1 ) {
    url = this.redirectURL + url;
  }

  return url;
}

wv.prototype.createDom = function ( data, error ) {
  var elms = [], i, left, top, cl = "productImg", p, pInner, img, imgInner, linkToDetail, linkToResearch, linkToExpand, priceBg, priceTxt;
  
  $("#dispLoadingWrap").hide();

  if ( error ) {
    //サーバー混雑
    // 多重レスポンスを回避
    $("#dispSpace").remove("p");
    $(".serverError").remove();
    p = document.createElement( "p" );
    p.innerHTML = "現在、混雑しておりますので時間を置いてからご利用ください。 ";
    $(p).addClass("serverError");
    $("#dispSpace").append( p );

  }  else if ( data.length == 0 )  {
    //ゼロマッチ
    // 多重レスポンスを回避
    $("#dispSpace").remove("p");
    $(".zeromatch").remove();
    p = document.createElement( "p" );
    p.innerHTML = "条件に一致する情報は見つかりませんでした。";
    $(p).addClass("zeromatch");
    $("#dispSpace").append( p );

  } else {
    $(".serverError").remove();
    $(".zeromatch").remove();

    for ( i = 0; i < 36; i++ ) {
      
      top  = is.dispPos[i].top;
      left = is.dispPos[i].left;
      p = document.createElement( "p" );
      $(p).css( {top : top + "px", left : left + "px"} );
      $(p).addClass( cl );
 
      if ( data[i] ) {
 
      img = document.createElement( "img" );
      img.setAttribute( "src", data[i].thub );
      img.setAttribute( "name", data[i].tag );
      
      pInner = document.createElement( "p" );
      $(pInner).css( {position : "relative"} );
      
      $(pInner).hover(
          function () {
            $(this).css( { backgroundColor : "#FFF" } );
            $(this).children("img").css( {opacity : "0.4"} );
            $(this).children("a").show();
          },
          function () {
            $(this).css( { backgroundColor : "#555" } );
            $(this).children("img").css( {opacity : "1"} );
            $(this).children("a").hide();
          }
      );
      linkToDetail   = document.createElement( "a" );
      linkToResearch = document.createElement( "a" );
      linkToExpand   = document.createElement( "a" );
      linkToDetail.innerHTML   = "詳細へ";
      linkToResearch.innerHTML = "類似商品検索";
      linkToExpand.innerHTML   = "拡大する";
      linkToResearch.setAttribute( "id", data[i].id );
      $(linkToResearch).bind( "click", function( event ) {
                                         //setQueryをする
                                         var targetParent = $(event.target).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;
                                         }
 
                                         //カテゴリ選択
                                         ct.selectCategoryFromTag( tag );

                                         data = {
                                                  id : this.id,
                                                  thub : src,
                                                  tag : tag,
                                                  imgUrl : imgUrl,
                                                  clickUrl : clickUrl,
                                                  Title : Title,
                                                  Price : Price
                                                };
                                         //左上にクエリ画像表示
                                         is.setQueryImage( data );
                                         
                                         is.doSearch( this.id, "imageClick" );
 
                                         //検索履歴に画像表示
                                         hs.setImageList( data );
                                       } );
      $(linkToDetail).addClass( "linkToDetail" );
      $(linkToResearch).addClass( "linkToResearch" );
      $(linkToExpand).addClass( "linkToExpand" );
      linkToDetail.setAttribute( "target", "_blank" );
      linkToDetail.setAttribute( "href", data[i].clickUrl );
      linkToExpand.setAttribute( "href", data[i].imgUrl );
      linkToExpand.setAttribute( "rel", "expandGroup" );
      $(linkToDetail).hide();
      $(linkToResearch).hide();
      $(linkToExpand).hide();

      priceBg = $("<p/>")
                  .addClass("imagePriceBg")
                  .css({opacity : 0.7});

      priceTxt = $("<span/>")
                  .addClass("imagePriceTxt")
                  .text(data[i].price + "円");
      
      $(pInner).append( img );
      $(pInner).append( linkToDetail );
      $(pInner).append( linkToResearch );
      $(pInner).append( linkToExpand );
      $(pInner).append( priceBg );
      $(pInner).append( priceTxt );
      $(p).append( pInner );
      }
      elms[i] = p;
    } 
    $("#dispSpace").append( elms );
  }

  //拡大表示のバインド
  ei.bindExpand();
};

