/**
  *  onComplete
  *  WidgetData[] データの代入
  */
function fnc_getWidgetEntries(xmlObj){
 // Definition
 var PageCondition;
 var WidgetData = new Array();
 // View Condition
 var isViewByWidgetID = portal.getViewWidgetsID();
 // Parse
 var Obj = xmlObj.responseXML;
 // Page Condition
 var PageCondition = {
  total_pages  : parseInt(Obj.getElementsByTagName('total_pages')[0].firstChild.nodeValue),
  current_page : parseInt(Obj.getElementsByTagName('current_page')[0].firstChild.nodeValue),
  page_start   : parseInt(Obj.getElementsByTagName('page_start')[0].firstChild.nodeValue),
  page_end     : parseInt(Obj.getElementsByTagName('page_end')[0].firstChild.nodeValue),
  total        : (Obj.getElementsByTagName('total')[0].firstChild != null)     ? parseInt(Obj.getElementsByTagName('total')[0].firstChild.nodeValue) : 'F',
  prev_page    : (Obj.getElementsByTagName('prev_page')[0].firstChild != null) ? parseInt(Obj.getElementsByTagName('prev_page')[0].firstChild.nodeValue) : 'F',
  next_page    : (Obj.getElementsByTagName('next_page')[0].firstChild != null) ? parseInt(Obj.getElementsByTagName('next_page')[0].firstChild.nodeValue) : 'F'
 };

 if (typeof (Obj.getElementsByTagName('id')) != 'undefined'){
  for(i = 0; i < Obj.getElementsByTagName('id').length; i ++){
   if (Obj.getElementsByTagName('id')[i].firstChild != null){
    WidgetData[i] = {
      id           : Obj.getElementsByTagName('id')[i].firstChild.nodeValue,
      default_line : Obj.getElementsByTagName('default_line')[i].firstChild.nodeValue,
      title        : Obj.getElementsByTagName('title')[i].firstChild.nodeValue,
      thumbnail    : (Obj.getElementsByTagName('thumbnail')[i].firstChild != null)   ? Obj.getElementsByTagName('thumbnail')[i].firstChild.nodeValue : '',
      description  : (Obj.getElementsByTagName('description')[i].firstChild != null) ? Obj.getElementsByTagName('description')[i].firstChild.nodeValue : '',
      isView       : (typeof (isViewByWidgetID[Obj.getElementsByTagName('id')[i].firstChild.nodeValue]) != 'undefined') ?
                              isViewByWidgetID[Obj.getElementsByTagName('id')[i].firstChild.nodeValue] : ''
    }
   }
  }
 }
 // 描画
 drawWidgetList(PageCondition, WidgetData);
}

/**
  *  HTML Draw
  */
function drawWidgetList(PageCondition, WidgetData){

 var HTMLLine = '';
 HTMLLine  = '<p>';
 HTMLLine += '<ul class="pagingNavi">';
 // HTMLLine += '<dt>' + '登録されている ウィジェット <b>' + PageCondition.total + '</b> 個</dt>';
 // HTMLLine += '<dt>' + '現在 <b>' + PageCondition.current_page + '</b> ページを見ています</dt>';

 // Page Prev/Next
 if (isNaN (PageCondition.prev_page) == false)
   HTMLLine += '<li class="prev">' + '<a href="JAVAScript:getWidgetList(' + PageCondition.prev_page + ');">' + '前のページ</a></li>';

 // Page Sequence
 var i = 1;
 for (i = 1; i <= PageCondition.total_pages; i ++){
  HTMLLine += '<li>';
  if (i != PageCondition.current_page)
    HTMLLine += '<a href="JAVAScript:getWidgetList(' + i + ');">';
  HTMLLine += i;
  if (i != PageCondition.current_page)
    HTMLLine += '</a>';
  HTMLLine += '</li>';
 }

 if (isNaN (PageCondition.next_page) == false)
   HTMLLine += '<li class="next">' + '<a href="JAVAScript:getWidgetList(' + PageCondition.next_page + ');">' + '次のページ</a></li>';

 HTMLLine += '</dt>';
 HTMLLine += '</ul>';
 HTMLLine += '</p>';

 for (i = 0; i < WidgetData.length; i ++){

  HTMLLine += '<dl class="widgetList">';

  if (WidgetData[i].thumbnail.length > 0){
    HTMLLine += '<dt><img src="' + WidgetTumbnailURL + WidgetData[i].thumbnail + '" alt="ウィジェットのイメージ" width="163" height="110"/></dt>';
  }else{
    HTMLLine += '<dt><img src="/top/common/widget_thumbnail/WidgetDummy.gif" width="163" height="110"/></dt>';
  }
  HTMLLine += '<dd class="title">';
  HTMLLine += WidgetData[i].title;
  HTMLLine += '</dd>';

  HTMLLine += '<dd class="exp">';
  if (WidgetData[i].thumbnail.length > 0)
    HTMLLine +=  WidgetData[i].description;
  HTMLLine += '<br /><br /><br /><br /><br /><br /><br /><br /></dd>';

  if (WidgetData[i].isView != 1){
   HTMLLine += '<dd class="btn">';
   HTMLLine += '<div id="addW_' + WidgetData[i].id +'">';
   HTMLLine += '<a href="JAVAScript:portal.addWidget('+ WidgetData[i].id + ',' + WidgetData[i].default_line +');">';
   HTMLLine += '<img src="/top/common/images/btn_insert.gif" alt="コンテンツを追加する" width="98" height="27" border="0" />';
   HTMLLine += '</a>'
   HTMLLine += '</div>';
   HTMLLine += '</dd>';
  }

  HTMLLine += '</dl>';
 }

 // Draw
 $('WidgetListUp').innerHTML = HTMLLine;
 $('currentPage').value = PageCondition.current_page;
}

/**
  *  onFailure
  */
function fnc_Err(){    }

/**
  *  ParseXML
  *  HTTP_COOKIE をパースし現状の表示・非表示を取得
  */
function getWidgetList(PageNo){
 PageNo = (typeof (PageNo) != 'undefined') ? PageNo : 1;
 //
 var DD  = new Date();
 var CV  = new Array();
 var CC  = new Array();
 var url = WidgetListURL + '?p=' + PageNo + '&t=' + DD.getTime();
 // Parse XML
 new Ajax.Request(url, {
      method: "get",
      requestHeaders: ['If-Modified-Since', ModifiedSince],
      onComplete : function (xmlObj){  fnc_getWidgetEntries(xmlObj);  },
      onFailure  : fnc_Err()
     });
}
