//********************************************************************************
//** Suggest機能諸定義・設定

//********************************************************************************
//** 初期定義

//変数格納
var prd_val = "";

//関数定義
var ary_push_prd_val = function( value, key ){
	prd_val = prd_val + value + "&";
}

var ary_add = function( value, key ){
	itemlist[itemlist.length] = value;
}

//クッキー書き込み
function WriteCookie(key, value, days) {
     var str = key + "=" + escape(value) + ";";
     if (days != 0) {
          var dt = new Date();
          dt.setDate(dt.getDate() + days);
          str += "expires=" + dt.toGMTString() + ";";
          str += "path=/;";
     }
     document.cookie = str;
}

//クッキー読み込み
function ReadCookie(key) {
     var sCookie = document.cookie;
     var aData = sCookie.split(";");
     var oExp = new RegExp(" ", "g");
     key = key.replace(oExp, "");

     var i = 0;
     while (aData[i]) {
          var aWord = aData[i].split("=");
          aWord[0] = aWord[0].replace(oExp, "");
          if (key == aWord[0]) return unescape(aWord[1]);
          if (++i >= aData.length) break;
     }
     return "";
}

/*  Prototype JavaScript framework, version 1.6.0.3
 *  (c) 2005-2008 Sam Stephenson
 *
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *  For details, see the Prototype web site: http://www.prototypejs.org/
 *
 *--------------------------------------------------------------------------*/

Array.prototype._each = function(iterator){
	for(var i=0, l=this.length; i<l; i++){
		iterator(this[i]);
	}
}

Array.prototype.each = function(iterator){
	var index = 0;
	this._each(function(value){
		iterator(value, index++);
	});
}

Array.prototype.uniq = function(){
    for (var i=0,l=this.length; i<l; i++) {
    
        if (this.indexOf(this[i]) < i) {
            this.splice(i--, l-- && 1);
        }
    }
    return this;
};

Array.prototype.last=function(){
	return this[this.length-1];
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

//********************************************************************************
//** ユーザー関数

//----------------------------------------------------------
// KeywordRegist
// キーワード登録
// key : キーワード
// cname : クッキー名
function KeywordRegist( key, cname ) {
	var str_prd = ReadCookie( cname );

	var prd = str_prd.split("&");

	if( !prd[0] )
	{
		prd = new Array();
	}

	prd.unshift( key );

	prd = prd.uniq();

	prd.each(ary_push_prd_val);
	prd_val = prd_val.replace( "&&", "" );

	//alert(prd_val);
	WriteCookie( cname, prd_val, 365 );
}

//----------------------------------------------------------
// KeywordRegist
// サジェスト表示
// key : キーワード
// cname : クッキー名

function SetSuggest( textstyle, divstyle, list )
{
	var start = function() {new Suggest.Local( textstyle , divstyle, list, {dispMax: 10, highlight: true});};
	window.addEventListener ?
	window.addEventListener('load', start, false) :
	window.attachEvent('onload', start);
}

//********************************************************************************
//** 初期定義処理

//クッキーから処理読み込み
var list_item = ReadCookie( 'hist_item' );
list_item = list_item.split("&");

//商品登録されている場合、その要素が優先される
if( itemlist[0] )
{
	list_item.each(ary_add);
	list_item = itemlist;
	list_item = list_item.uniq();
}

var list_parts = ReadCookie( 'hist_parts' );
list_parts = list_parts.split("&");

var list_keyword = ReadCookie( 'hist_keyword' );
list_keyword = list_keyword.split("&");

