﻿function byId(id) {
	//return $(id);
	return document.getElementById(id);
};


//-------------------------------------------------------
//검색페이지로 이동한다.
//-------------------------------------------------------
function fnGoSearch(html) {
	/*<!-- omniture-->*/
	var search_words = byId('visualsearch_inputitem').value;
	var pagename = '';
	var str = window.parent.getTextNode('pathindicatorENG');
	if ( str != false ) {   pagename=  'fr:' + str.toLowerCase(); }
	else { pagename = window.parent.tagURL('fr');  }
	
	/* us
	window.parent.s_control_clickEx('events,eVar14,prop15', 'event34', ['event34', search_words, search_words + '__' + pagename], 'o', 'predictive product search');
	*/
	/* non-us */
	window.parent.s_control_clickEx('events,eVar19,prop21', 'event12', ['event12', search_words, search_words + '__' + pagename], 'o', 'predictive product search');
	/*<!-- /omniture-->*/
	
	html += makeHiddenHtml("input_keyword", byId("visualsearch_inputitem").value);
	var submitForm = document.getElementById("frmSubmit");
	submitForm.innerHTML = html;
	submitForm.submit();
};

//-------------------------------------------------------
//검색버튼을 눌렀을 때 처리함수
//-------------------------------------------------------		
//function fnSearch() {
var fnSearch = function() {
	var html = "";

	//new keywords
	html += makeHiddenHtml("keywords", byId("visualsearch_inputitem").value);
	
	fnGoSearch(html);
};

function fnClickRecenkKeyword (recent_keyword){
	byId("visualsearch_inputitem").value = recent_keyword;
	fnSearch();
}

//-------------------------------------------------------
//keyword 입력창에서 enter key를 눌렀는지 체크한다.
//-------------------------------------------------------		
//function fnCheckEnter(event) {
var fnCheckEnter = function(event) {	
	if(event && event.keyCode  == 13) {
		fnSearch();
		//return false;
	}
};


//자동완성단어 show
//function showListener1(showFlag) {
var showListener1 = function(showFlag) {	
	if(showFlag) {
		parent.document.getElementById('layer_findquick_close').style.display='block'; // 위 레이어 끄는 x 버튼 보이기
	} else {
		parent.document.getElementById('layer_findquick_close').style.display='none'; // 위 레이어 끄는 x 버튼 감추기
	}		
};


//function makeSuggestionsHtml(id, queryString, terms0, transQueryString, terms1, recommendTerms) {
var makeSuggestionsHtml = function(id, queryString, terms0, transQueryString, terms1, recommendTerms) {		
	var text = "";
	text += "<div class='listbox'><ul>";
	for(var i=0; i<terms0.length; i++) {
		text += "<li onmouseover='mouseOver(" + id + "," + i + ")'><a id='visualSearchTooltip_" + i + "' href='javascript:mouseClick(" + id + "," + i + ");'>" + terms0[i].replace(queryString, "<span>" + queryString + "</span>") + "</a></li>";
	}

	for(var i=0; i<terms1.length; i++) {
		var idx = terms0.length + i;
		text += "<li onmouseover='mouseOver(" + id + "," + idx + ")'><a id='visualSearchTooltip_" + idx + "' href='javascript:mouseClick(" + id + "," + idx + ");'>" + terms1[i].replace(transQueryString, "<span>" +  transQueryString + "</span>") + "</a></li>";
	}
	text += "</ul></div>";

	if(recommendTerms.length > 0) {
		text += "<div class='listbox2'><ul>";		
		for(var i=0; i<recommendTerms.length; i++) {
			text += "<li><a href='javascript:unexpectedKeywordClick(" + id + "," + i + ");'>" + recommendTerms[i][1] + "</a></li>";
		}
		text += "</ul></div>";
	}
	text += "<div class='layer_findquick_bottom'>&nbsp;</div>";
	
	return text;
};



//visual show 한글
//function showListener2(showFlag) {
var showListener2 = function(showFlag) {	
//try {
	if(showFlag) {
		//getParentById("visualsearch_close_sub").style.display='none'; 			// 서브용 닫기 버튼 감추기
		//if(isMainPage) {
		//	getParentById("visualsearch_transparency").style.display='block'; 	// 배경에 깔려 있는 반투명 bg
		//}
		
		// dataing
		dataing.dataing();
	} else {
		//if(isMainPage) {			
		//	getParentById("visualsearch_transparency").style.display='none'; 		// 배경에 깔려 있는 반투명 bg 감추기
		//} else {
		//	getParentById("visualsearch_close_sub").style.display='block'; 			// 서브용 닫기 버튼 보이기
		//}
		
		// result datas init
		dataing.setDatas([]);
	}
//}catch(exp) {
//		alert(exp.message);
//}
};

//visual search query check
function checkQuery(query) {
	if(query.length > 1)
		return true;
	else 
		return false;
};

//-------------------------------------------------------
//QuerySuggester enter or mouse click
//-------------------------------------------------------		
//function fnQuerySuggesterSubmit(index) {
var fnQuerySuggesterSubmit = function(index) {	
	//alert("fnQuerySuggesterEnter");
	//sqs.moveSuggestion(index);
	sqs2.propertyChange();
};


function initResultDatas(datas) {
	dataing.setDatas(datas);
};


function inputProduct(i, img, txt, link) {
	var objimg = parent.document.getElementById("color"+i);
	var objtxt = parent.document.getElementById("text"+i);
	//objimg.innerHTML = '<a href="' + link + '"><img src="'+img+'" alt="" /></a>';
	/* us
	objimg.innerHTML = '<a href="' + link + '" onclick="javascprit:s_control_clickEx(\'prop5,eVar5\', \'\', [\'visual product search\', \'visual product search\'], \'o\', \'Product Finding Method\'); return true;" onmouseover="findquick_viewDetail('+i+')" onmouseout="document.getElementById(\'visualsearch_resultlayer'+i+'\').style.display=\'none\';return false;"><img src="'+img+'" alt="" /></a>';
	*/
	/* non-us */
	objimg.innerHTML = '<a href="' + link + '" onclick="javascprit:s_control_clickEx(\'prop22,eVar18\', \'\', [\'visual product search\', \'visual product search\'], \'o\', \'Product Finding Method\'); return true;" onmouseover="findquick_viewDetail('+i+')" onmouseout="document.getElementById(\'visualsearch_resultlayer'+i+'\').style.display=\'none\';return false;"><img src="'+img+'" alt="" /></a>';
	objtxt.innerHTML = txt;
};


function shuffle(array) { 
	var len = array.length; 
	for(var i=len ; i>0 ; i--) { 
		array[len-1] = array.splice(Math.floor(Math.random() * i),1)[0]; 
	} 
};

//===========  Dataing  =========== //
function Dataing() {
	this.resultDatas = [];
	this.index = -1;
	this.tid = null;
	this.coloring = new Coloring(this);
};

Dataing.prototype.setDatas = function(datas) {
	this.resultDatas = datas;
	this.index = -1;
	if(this.tid) {
		clearTimeout(this.tid);
		this.tid = null;
	}
	
	this.coloring.init(datas.length);

	
	if(datas.length > 0) {
		shuffle(datas);
	}
};

Dataing.prototype.dataing = function() {
	this.coloring.coloring();
};

Dataing.prototype.run = function() {
	var totalCount = this.resultDatas.length;

	if(this.index + 1 < totalCount) {
		this.index++;
		var index = this.index;
		inputProduct(this.resultDatas[index][0], this.resultDatas[index][1], this.resultDatas[index][2], this.resultDatas[index][3]);
		
		if(this.index + 1 < totalCount) {
			var ring = this;
	        this.tid = setTimeout(function() {ring.run();}, 50);
		} else {
			clearTimeout(this.tid);
			this.tid = null;
		}
	} else if(this.tid){
		clearTimeout(this.tid);
		this.tid = null;
	}
};



//===========  Coloring  =========== //
function Coloring(Dataing) {
	this.tid = null;
	this.tatalCount = 0;
	this.colorset = [];
	this.index = -1;
	this._Dataing = Dataing;
};

Coloring.prototype.init = function(count) {
	this.tatalCount = count;
	this.index = -1;
	this.colorset = new Array();
	
	if(this.tid) {
		clearTimeout(this.tid);
		this.tid = null;
	}
	
	if(count > 0) {
		var colorset = new Array("#f5e983","#67c6be","#f2be0a","#eec0c0","#009edb","#ffde91","#f68b1f","#aae0fa","#d4d110","#01bce5","#ffeb8a","#ef448a","#fdd221","#007a85","#9cd3ab","#bde2da");
		shuffle(colorset);
		for(var i=0; i<count; i++) {
			this.colorset[i] = colorset[i];
		}
	}
};

Coloring.prototype.coloring = function() {
	if(this.tatalCount > 0) {
		this.index = -1;
		var ring = this;
     this.tid = setTimeout(function() {ring.run();}, 30);
	}
};

Coloring.prototype.run = function() {
	var totalCount = this.tatalCount;

	if(this.index + 1 < totalCount) {
		this.index++;
		parent.document.getElementById("color" + (this.index)).style.backgroundColor = this.colorset[this.index];
		
		if(this.index + 1 < totalCount) {
			var ring = this;
	        this.tid = setTimeout(function() {ring.run();}, 30);
		} else {
			clearTimeout(this.tid);
			this.tid = null;
			
			this._Dataing.run();
		}
	} else if(this.tid){
		clearTimeout(this.tid);
		this.tid = null;
	}
};

var dataing = new Dataing();
