

function Finder(_instanceName) {
	this.instanceName = _instanceName;
	this.columns = new Array;
	this.products = null;
	this.productList = null;
	this.ids = {
		  area1				: this.instanceName + "-phoneFinderArea"
		, area2				: this.instanceName + "-phoneFinder"
		, cover1			: this.instanceName + "-finderCover"
		, allPhoneCheckBox	: this.instanceName + "-allPhoneCheckBox"
		, leftMenuArea		: "allPhoneLeftMenuArea"
		, form				: "finderForm"
	}
	
	this.vars = {
		  finderWidth		: 146
		, errorMessage		: ""
		, noDataMessage1	: "The articles compatible with a search condition are not available."
		, noDataMessage2	: "You can see the products all examined on the net in the basket."
	}
	
	this.status = {
		  isLoadingCompleteMode	: false
		, hasNoResult			: false
	}
	
	this.addColumn = function(_title, _name, _searchType) {
		this.columns.push({
			  title : _title
			, name : _name
			, searchType : _searchType
			, options : new Array
		});
	}
	
	this.addOption = function(_checked, _title, _value, _minValue, _maxValue) {
		if (this.columns.length>0) {
			this.columns[this.columns.length-1].options.push({
				  title		: _title
				, checked	: _checked
				, value		: _value
				, minValue	: _minValue
				, maxValue	: _maxValue
			});
		}
	}
	
	this.appendProduct = function(_products) {
		this.products = _products;
	}
	
	this.appendProductList = function(_productList) {
		this.productList = _productList;
	}
	
	this.resetFinderHeight = function() {
		$(this.ids.area2).setStyle("height", "");
		$(this.ids.leftMenuArea).setStyle("height", "");
		$(this.ids.leftMenuArea).setStyle("height",$(this.ids.area1).getH());
		$(this.ids.area2).setStyle("height",$(this.ids.leftMenuArea).getH());
	}
	
	this.showFinder = function() {
		if (this.status.isLoadingCompleteMode) {
			$(this.ids.area1).setStyle("display",true);
			$(this.ids.leftMenuArea).setStyle("height",$(this.ids.area1).getH());
			$(this.ids.area2).setStyle("height",$(this.ids.leftMenuArea).getH());
			$(this.ids.area2).moveTo(0,0,"",3);
		}
	}
	
	this.hideFinder = function() {
		$(this.ids.area2).moveTo(this.vars.finderWidth,0,"$('"+this.ids.area1+"').setStyle('display',false)",3);
		$(this.ids.area2).setStyle("height", "");
		$(this.ids.leftMenuArea).setStyle("height", "");
	}
	
	this.displayAllProducts = function() {
		if ($(this.ids.allPhoneCheckBox).checked) {
			$(this.ids.allPhoneCheckBox).checked = true;
			for (var i=0; i<this.columns.length; i++) {
				for (var j=0; j<this.columns[i].options.length; j++) {
					$(this.columns[i].name+"-"+j).checked = false;
				}
			}
			this.displaySearchedProducts();
		} else {
			$(this.ids.allPhoneCheckBox).checked = true;
		}
	}
	
	this.displaySearchedProducts = function() {
		var isAllPhoneMode = true;
		for (var i=0; i<this.columns.length; i++) {
			for (var j=0; j<this.columns[i].options.length; j++) {
				if ($(this.columns[i].name+"-"+j).checked) {
					isAllPhoneMode = false;
					break;
				}
			}
			if (!isAllPhoneMode) break;
		}
		$(this.ids.allPhoneCheckBox).checked = isAllPhoneMode;
		this._displaySearchedProductsStep1();
	}
	
	this._displaySearchedProductsStep1 = function() {
		$(this.ids.cover1).setStyle("height", $(this.ids.area1).getH());
		$(this.ids.cover1).setStyle("display", true);
		
		$(this.productList.ids.basketAreaCover).setStyle("height", $(this.productList.ids.basketAreaDropZone).getH());
		$(this.productList.ids.basketAreaCover).setStyle("display", true);
		
		$(this.productList.ids.functionButtonAreaCover).setStyle("display", true);
		
		$(this.productList.ids.productAreaCover).setStyle("height", $(this.productList.ids.productArea).getH());
		$(this.productList.ids.productAreaCover).setStyle("display", true);
		
		if (this.status.isLoadingCompleteMode) {
			$(this.productList.ids.productArea).setOpacity(1);
			$(this.productList.ids.productArea).setFadeOut(0, null, this.instanceName + "._displaySearchedProductsStep2()");
		} else {
			$(this.productList.ids.productArea).setOpacity(0);
			this._displaySearchedProductsStep2();
		}
	}
	
	this._displaySearchedProductsStep2 = function() {
		var f = document[this.ids.form];
		for (var i=0; i<this.columns.length; i++) {
			var column = this.columns[i];
			var options = f[column.name];
			for (var j=0; j<this.products.length; j++) {
				var isSearchResult = this.products[j].status.isSearchResult;
				if (isSearchResult) {
					var productValue = this.products[j].options[column.name];
					var isMultiValue = (typeof productValue=='string') ? false : true;
					var isChecked = false;
					for (var k=0; k<options.length; k++) {
						if (column.searchType=="or") {
							if (isChecked&&isSearchResult) {
								break;
							}
						} else {
							if (!isSearchResult) {
								break;
							}
						}
						if (options[k].checked) {
							if (column.options[k].value!=null) {
								if (!isMultiValue) {
									isSearchResult = (""+column.options[k].value==""+productValue) ? true : false;
								} else {
									isSearchResult = (""+column.options[k].value==""+productValue[k]) ? true : false;
								}
							} else {
								if (column.options[k].minValue!=null&&column.options[k].maxValue!=null) {
									isSearchResult = (parseFloat(productValue)>=parseFloat(column.options[k].minValue)&&parseFloat(productValue)<=parseFloat(column.options[k].maxValue)) ? true : false;
								} else if (column.options[k].minValue!=null) {
									isSearchResult = (parseFloat(productValue)>=parseFloat(column.options[k].minValue)) ? true : false;
								} else if (column.options[k].maxValue!=null) {
									isSearchResult = (parseFloat(productValue)<=parseFloat(column.options[k].maxValue)) ? true : false;
								}
							}
							isChecked = true;
						}
					}
					this.products[j].status.isSearchResult = isSearchResult;
				}
			}
		}
		this.status.hasNoResult = true;
		var hasBasketModeProduct = false;
		var resultProducts = new Array;
		for (var i=0; i<this.products.length; i++) {
			if (this.products[i].status.isSearchResult) {
				resultProducts.push(this.products[i]);
				if (this.products[i].status.isBasketMode) {
					hasBasketModeProduct = true;
				} else {
					this.status.hasNoResult = false;
				}
			} else {
				if (this.products[i].status.isShowMode&&!this.products[i].status.isBasketMode) {
					this.products[i].removeHTML();
				}
			}
			this.products[i].status.isSearchResult = true;
		}
		if (this.status.hasNoResult) {
			if (hasBasketModeProduct) {
				this.vars.errorMessage = this.vars.noDataMessage2;
			} else {
				this.vars.errorMessage = this.vars.noDataMessage1;
			}
		}
		this.productList.displayProduct(resultProducts);
		setTimeout(this.instanceName + "._displaySearchedProductsStep3()", 1);
		//this._displaySearchedProductsStep3();
	}
	
	this._displaySearchedProductsStep3 = function() {
		$(this.productList.ids.productArea).setFadeIn(1, null, this.instanceName + "._displaySearchedProductsStep4()");
	}
	
	this._displaySearchedProductsStep4 = function() {
		$(this.productList.ids.productAreaCover).setStyle("display", false);
		$(this.productList.ids.functionButtonAreaCover).setStyle("display", false);
		$(this.productList.ids.basketAreaCover).setStyle("display", false);
		$(this.ids.cover1).setStyle("display", false);
		setTimeout(this.instanceName + "._displaySearchedProductsStep5()", 1);
		if (this.status.hasNoResult) {
			kidow.alert(this.vars.errorMessage, true);
		}
	}
	
	this._displaySearchedProductsStep5 = function() {
		if (this.status.isLoadingCompleteMode) {
			this.resetFinderHeight();
		} else {
			this.status.isLoadingCompleteMode = true;
			setTimeout(this.instanceName+".showFinder()",500);
		}
	}
	
	this.getHTML = function() {
		var s = '';
		s += '<div style="position:absolute;">\n';
		s += '	<div style="position:relative;left:128px;top:19px;"><img onClick="'+this.instanceName+'.showFinder();" style="cursor:pointer;" src="/img/products/mobile/all/tab_phonefinder.gif"></div>\n';
		s += '</div>\n';
		s += '<div id="'+this.ids.area1+'" style="position:absolute;z-index:48;display:none;width:'+this.vars.finderWidth+'px;overflow-x:hidden;">\n';
		s += '<div id="'+this.ids.area2+'" style="position:relative;left:'+this.vars.finderWidth+'px;top:0px;width:'+this.vars.finderWidth+'px;background-color:#000000;padding-bottom:30px;">\n';
		s += '	<div style="position:absolute;">\n';
		s += '		<div style="position:relative;left:128px;top:19px;"><img onClick="'+this.instanceName+'.hideFinder();" style="cursor:pointer;" src="/img/products/mobile/all/tab_gotomenu.gif"></div>\n';
		s += '	</div>\n';
		s += '	<div style="padding-top:19px;"><img src="/img/products/mobile/all/phone_finder.gif"></div>\n';
		s += '	<form name="'+this.ids.form+'" style="margin:0px;padding:0px;">\n';
		s += '	<div style="padding:10px 0 2px 16px;">\n';
		s += '		<div><input onClick="'+this.instanceName+'.displayAllProducts();" type="checkbox" name="'+this.ids.allPhoneCheckBox+'" id="'+this.ids.allPhoneCheckBox+'" class="finderCheckbox" onfocus="this.blur();"><label for="'+this.ids.allPhoneCheckBox+'" style="color:#FFA11A;font-weight:bold;">All Phones</label></div>\n';
		s += '	</div>\n';
		s += '	<div style="padding-top:8px;"><img src="/img/products/mobile/all/bar01.gif"></div>\n';
		for (var i=0; i<this.columns.length; i++) {
			s += '	<div style="padding:8px 0 3px 20px;color:#FFFFFF;font-weight:bold;">'+this.columns[i].title+'</div>\n';
			s += '	<div style="padding-left:16px;">\n';
			for (var j=0; j<this.columns[i].options.length; j++) {
				var option = this.columns[i].options[j];
				s += '		<div style="padding-top:3px;"><input onClick="'+this.instanceName+'.displaySearchedProducts();" type="checkbox" name="'+this.columns[i].name+'" id="'+this.columns[i].name+'-'+j+'" class="finderCheckbox" '+((option.checked)?"checked":"")+' onfocus="this.blur();"><label for="'+this.columns[i].name+'-'+j+'" style="color:#A2A2A2;">'+option.title+'</label></div>\n';
			}
			s += '	</div>\n';
			s += '	<div style="padding-top:8px;"><img src="/img/products/mobile/all/bar01.gif"></div>\n';
		}
		s += '	</form>\n';
		s += '	<div style="padding:20px 0 5px 20px"><img onClick="goToThisMenu(\'products_mobile_recommendations\');" style="cursor:pointer;" src="/img/pantech/main_banner03.gif" width="106" height="30" alt="" border="0"></div>\n';
		s += '</div>\n';
		s += '</div>\n';
		s += '<div id="'+this.ids.cover1+'" style="position:absolute;z-index:49;width:'+this.vars.finderWidth+'px;filter:alpha(opacity=40);opacity:0.4;background-color:#000000;display:none;cursor:progress;"></div>\n';
		return s;
	}
	
	this.writeHTML = function() {
		document.write(this.getHTML());
	}
}

var finder = new Finder("finder");

finder.addColumn("Released year", "year", "or");
finder.addOption(true, "2007", "2007", null, null);
finder.addOption(true, "2006", "2006", null, null);
finder.addOption(false, "Before 2005", "2005", null, null);

finder.addColumn("Network", "network", "or");
finder.addOption(false, "GSM", "GSM", null, null);
finder.addOption(false, "UMTS", "UMTS", null, null);
finder.addOption(false, "CDMA", "CDMA", null, null);

finder.addColumn("Style", "style", "or");
finder.addOption(false, "Bar type", "Bar type", null, null);
finder.addOption(false, "Clamshell type", "Clamshell type", null, null);
finder.addOption(false, "Slid type", "Slid type", null, null);

finder.addColumn("Camera", "camera", "or");
finder.addOption(false, "1.3 M Pixels or less", null, null, 1.3);
finder.addOption(false, "1.4 M Pixels or more", null, 1.4, null);

finder.addColumn("Ringtone", "ringtone", "or");
finder.addOption(false, "16 Poly", 16, null, null);
finder.addOption(false, "32 Poly", 32, null, null);
finder.addOption(false, "40 Poly", 40, null, null);
finder.addOption(false, "64 Poly", 64, null, null);
finder.addOption(false, "72 Poly or more", null, 72, null);

finder.addColumn("Features", "features", "and");
finder.addOption(false, "MP3 Player", "Y", null, null);
finder.addOption(false, "Blutooth", "Y", null, null);
finder.addOption(false, "MMS", "Y", null, null);
finder.addOption(false, "EMS", "Y", null, null);

finder.addColumn("LCD Color", "lcdColor", "or");
finder.addOption(false, "65K or less", null, null, 65);
finder.addOption(false, "260K or more", null, 260, null);

finder.addColumn("LCD Size", "lcdSize", "or");
finder.addOption(false, "1.7Inch or less", null, null, 1.7);
finder.addOption(false, "1.8Inch ~ 2.0Inch", null, 1.8, 2.0);
finder.addOption(false, "2.1Inch or more", null, 2.1, null);




