// @ฉ (2010-03-04 16:55)

function GetGroupFolder(group) {
	var sGroup = String(group);
	var asGroup = sGroup.split('_');
	var iGroupMainID = asGroup[0];
	var asGroupFolder = asGroupFolderData[0].match(eval("/\\[" + iGroupMainID + "\\|([a-z0-9\\-\\_]+)\\]/"));
	var sGroupFolder = asGroupFolder[1];
	
	if(asGroup.length == 1) return sGroupFolder;
	
	if(asGroupFolderData[iGroupMainID]) {
		var sBuffer = iGroupMainID;
		for(var i=1,iLen=asGroup.length; i<iLen; i++) {
			sBuffer += '\\_' + asGroup[i];
			var asGroupFolder = asGroupFolderData[iGroupMainID].match(eval("/\\[" + sBuffer + "\\|([a-z0-9\\-\\_]+)\\]/"));
			
			sGroupFolder += (asGroupFolder != null) ? '/' + asGroupFolder[1] : '/' + asGroup[i];
		}
	} else {
		for(var i=1,iLen=asGroup.length; i<iLen; i++) {
			sGroupFolder += '/' + asGroup[i];
		}
	}
	
	return sGroupFolder;
}

function GetGroupID(group) {
	var asGroup = group.split('_');
	return asGroup[(asGroup.length-1)];
}

function GetGroupMainID(group) {
	var asGroup = group.split('_');
	return asGroup[0];
}

function GetGroupName(group) {
	return axGroupData[GetGroupID(group)][0]
}

function AjaxGetLocationData(val, selected) {
	var oLocatedInBox1 = document.forms['frm_filter'].elements['filter[located_in1]'];
	var oLocatedInBox2 = document.forms['frm_filter'].elements['filter[located_in2]'];
	var oProvRelate = document.forms['frm_filter'].elements['filter[prov_relate]'];
	
	var asLoading = { th:'กำลังโหลด..',en:'Loading..' }
	
	clearOptions(oLocatedInBox2);
	
	if(val.search(/^3_18[0-9_]+$/) == -1) {
		oProvRelate.disabled = true;
		oProvRelate.checked = false;
		return;
	}
	
	oProvRelate.disabled = false;
		
	var prov_id = val.substr((val.lastIndexOf('_')+1));
	var sUrl = BASE_URL + 'ajax_get_location_data.php?ln=' + LANG + '&prov_id=' + prov_id;
	
	var ajaxObjects = new sack();
	ajaxObjects.requestFile = sUrl;
	ajaxObjects.onLoading = function() {
		oLocatedInBox2.options[0] = new Option(asLoading[LANG], '');
		oLocatedInBox2.disabled = true;
	}
	ajaxObjects.onCompletion = function(){
		var oXML = ajaxObjects.responseXML.documentElement;
		
		if(oXML !== null && typeof(oXML.getElementsByTagName("row")) != "undefined") {
			createOptions(oLocatedInBox2, oXML, selected);
		}
	}
	ajaxObjects.runAJAX();
}

function createOptions(obj, xml, selected) {
	obj.options[0] = null;
	
	var asFirstOption = { th:'ทั้งหมด',en:'All' }
	var iLen = xml.getElementsByTagName("row").length;
	
	if(!iLen) return;
	
	var oFirstOption = document.createElement('option');
	oFirstOption.value = '';
	oFirstOption.innerHTML = '-' + asFirstOption[LANG] + '-';
	
	obj.appendChild(oFirstOption);
	
	for(var i=0; i<iLen; i++) {
		var sName = xml.getElementsByTagName("name")[i].childNodes[0].nodeValue;
		var sValue = xml.getElementsByTagName("value")[i].childNodes[0].nodeValue;
		
		var oOption = document.createElement('option');
		oOption.value = sValue;
		oOption.innerHTML = sName;
		
		if(typeof(selected) != "undefined" && (selected == sValue)) {
			oOption.selected = true;
		}
		
		obj.appendChild(oOption);
	}
	
	obj.disabled = false;
}

function clearOptions(obj) {
	if(obj.length > 0) {
		for(var i=(obj.length-1); i>=0; i--) {
			obj.options[i] = null;
		}
	}
	
	obj.options[0] = new Option('', '');
	obj.disabled = true;
}

function toggleDisabledProvinceRelate(val) {
	var oProvRelate = document.forms['frm_filter'].elements['filter[prov_relate]'];
	
	if(val != '') {
		oProvRelate.disabled = true;
		oProvRelate.checked = false;
	} else {
		oProvRelate.disabled = false;
	}
}

/******************************
 * DROP DOWN MENU
 ******************************/

// ***** at_show_aux *****

function at_show_aux(parent, child, pos) {
	var p = document.getElementById(parent);
	var c = document.getElementById(child);

	var top  = (c["at_position"] == "y") ? p.offsetHeight+2 : 0;
	var left = (c["at_position"] == "x") ? p.offsetWidth+2 : 0;
	
	for(; p; p = p.offsetParent) {
		top  += p.offsetTop;
		left += p.offsetLeft;
	}
	
	if(pos) {
		top = top + pos['top'];
		left = left + pos['left'];
	}
	
	c.style.position = "absolute";
	c.style.top = top +'px';
	c.style.left = left+'px';
	c.style.display = "";
}

// ***** at_show *****

function at_show() {
	var p = document.getElementById(this["at_parent"]);
	var c = document.getElementById(this["at_child" ]);

	at_show_aux(p.id, c.id);
	clearTimeout(c["at_timeout"]);
	
	// Hide select box
	var oSelectBox = document.body.getElementsByTagName('select');
	if(oSelectBox && oSelectBox.length > 0) {
		for(var i=0,iLen=oSelectBox.length; i<iLen; i++) {
			if(oSelectBox[i].id == 'grp') continue;
			
			oSelectBox[i].style.visibility = 'hidden';
		}
	}
}

// ***** at_hide *****

function at_hide() {
	var p = document.getElementById(this["at_parent"]);
	var c = document.getElementById(this["at_child" ]);

	c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.display='none'", 100);
	
	// Hide select box
	var oSelectBox = document.body.getElementsByTagName('select');
	if(oSelectBox && oSelectBox.length > 0) {
		for(var i=0,iLen=oSelectBox.length; i<iLen; i++) {
			if(oSelectBox[i].id == 'grp') continue;
			
			oSelectBox[i].style.visibility = 'visible';
		}
	}
}

// ***** at_click *****

function at_click() {
	var p = document.getElementById(this["at_parent"]);
	var c = document.getElementById(this["at_child" ]);

	if(c.style.display != "") at_show_aux(p.id, c.id); else c.style.display = "none";
	return false;
}

function at_attach(group) {
	if(typeof(axBoardGroupDataByGroupValue) == "undefined") return;
	
	var parent = 'group' + group + '_nav';
	var child = 'subgroup' + group + '_nav';
	
	var showtype = 'hover'; /*click*/
	var position = 'y';
	var cursor = 'pointer';
	
	var p = document.getElementById(parent);
	p.style.cursor = cursor;
	
	var c = document.createElement("div");
	c.setAttribute('name', child);
	c.setAttribute('id', child);
	c.style.cssText = 'position:absolute;z-index:999;display:none;border-top:1px solid #9CAFC3;border-left:1px solid #9CAFC3;border-bottom:1px solid #036796;border-right:1px solid #036796;background-color:#FFF;padding:4px;font-size:13px;font-weight:bold';
	c.innerHTML = DisplayDropDownSubGroup(group);
	
	document.getElementsByTagName('body')[0].appendChild(c);
	
	p["at_parent"] = p.id;
	p["at_child"] = c.id;
	p["at_position"] = position;
	
	c["at_parent"] = p.id;
	c["at_child"] = c.id;
	c["at_position"] = position;
	
	if(showtype == 'click') {
		p.onclick     = at_click;
		p.onmouseout  = at_hide;
		c.onmouseover = at_show;
		c.onmouseout  = at_hide;
	} else if(showtype == 'hover') {
		p.onmouseover = at_show;
		p.onmouseout  = at_hide;
		c.onmouseover = at_show;
		c.onmouseout  = at_hide;
	}
}

function DisplayDropDownSubGroup(group) {
	if(group != '') {
		var asGroup = group.split('_');
		var sGroupSet = axBoardGroupDataByGroupValue[asGroup[0]];
		
		var asMatches = sGroupSet.match(eval('/\\[(' + asGroup.join('\\_') + '\\_[0-9]+)\\|(0|1)\\|(0|1)\\]/g'));
		if(asMatches) {
			var iColNum = 3;
			var iCount = 0;
			
			var sData = '<table celspacing="0" celpadding="0"><tbody><tr>';
			for(var i=0,iLen=asMatches.length; i<iLen; i++) {
				if(iCount == iColNum) {
					sData += '</tr><tr>';
					iCount = 0;
				}
				
				var asBuffer = asMatches[i].match(eval('/\\[([0-9\\_]+)\\|(0|1)\\|(0|1)\\]/'));
				var sGroupValue = asBuffer[1];
				var sGroupName = GetGroupName(sGroupValue);
				var sGroupDir = GetGroupFolder(sGroupValue);
				
				var sPaddLeft = (iCount > 0) ? ' style="padding-left:10px"' : '';
				var sGroupLink = (sGroupDir.search(/\//) > -1) ? BASE_URL + sGroupDir + '/' : BASE_URL + sGroupDir + '/?grp=' + sGroupValue;
				
				sData += '<td' + sPaddLeft + '><a href="' + sGroupLink + '" target="_parent" style="text-decoration:none">' + sGroupName + '</a></td>';
				
				iCount++;
			}
		
			while(iCount < iColNum) {
				sData += '<td>&nbsp;</td>';
				iCount++;
			}
			
			sData += '</tbody></table>';
			
			return sData;
		} else {
			return '';
		}
	} else {
		var asMatches = axBoardGroupDataByGroupValue[0].match(eval('/\\[([0-9]+)\\|(0|1)\\|(0|1)\\]/g'));
		
		var iColNum = 3;
		var iCount = 0;
		
		var sData = '<table celspacing="0" celpadding="0"><tbody><tr>';
		
		for(var i=0,iLen=asMatches.length; i<iLen; i++) {
			if(iCount == iColNum) {
				sData += '</tr><tr>';
				iCount = 0;
			}
			
			var asBuffer = asMatches[i].match(eval('/\\[([0-9]+)\\|(0|1)\\|(0|1)\\]/'));
			var sGroupValue = asBuffer[1];
			var sGroupName = GetGroupName(sGroupValue);
			var sGroupDir = GetGroupFolder(sGroupValue);
			
			var sPaddLeft = (iCount > 0) ? ' style="padding-left:10px"' : '';
			
			sData += '<td' + sPaddLeft + '><a href="' + BASE_URL + sGroupDir + '/" target="_parent" style="text-decoration:none">' + sGroupName + '</a></td>';
			
			iCount++;
		}
		
		while(iCount < iColNum) {
			sData += '<td>&nbsp;</td>';
			iCount++;
		}
		
		sData += '</tbody></table>';
		
		return sData;
	}
}

/* Dropdown filer link menu */

function setFilerLinkMenu(mode, is_search) {
	if(typeof(axBoardGroupDataByGroupValue) == "undefined") return;
	
	var parent = 'filter_link_menu' + mode;
	var child = 'sub_filter_link_menu' + mode;
	
	var position = 'y';
	var cursor = 'pointer';
	
	var p = document.getElementById(parent);
	p.style.cursor = cursor;
	
	var c = document.createElement("div");
	c.setAttribute('name', child);
	c.setAttribute('id', child);
	c.style.cssText = 'position:absolute;z-index:999;display:none;';
	c.className = 'form_filter_popup_menu';
	
	var sUrl = document.location.toString();
	
	if(sUrl.indexOf("?") > -1) {
		var asUrl = sUrl.split("?");
		var asQS = asUrl[1].split("&");
		var sLink = asUrl[0].substr(0, asUrl[0].lastIndexOf('/'));
		
		var sNewQS = '';
		for(var i=0,iLen=asQS.length; i<iLen; i++) {
			if(asQS[i].search(/lst\=1/) > -1) {
				sNewQS += 'lst=1&';
			}
		}
		
		if(sNewQS != '') {
			sLink += '/?' + sNewQS;
		} else {
			sLink += '/?';
		}
	} else {
		var sLink = sUrl.substr(0, sUrl.lastIndexOf('/')) + '/?';
	}

	var asTextConfig = {
		th:{t_individual:'บุคคลทั่วไป',t_shop_com:'ร้านค้า/บริษัท',t_new:'ใหม่',t_used:'มือสอง'},
		en:{t_individual:'Individual',t_shop_com:'Shop/Company',t_new:'New',t_used:'Used'}
	}
	
	switch(mode) {
		case 1: 
			var sMenu = '<a href="' + sLink + encodeURI('act=filter&filter[seller_type]=I') + '" target="_self">' + asTextConfig[LANG]['t_individual'] + '</a>' + 
				'<a href="' + sLink + encodeURI('act=filter&filter[seller_type]=C') + '" target="_self">' + asTextConfig[LANG]['t_shop_com'] + '</a>';
			break;
		case 2: 
			var sMenu = '<a href="' + sLink + encodeURI('act=filter&filter[seller_type]=I&filter[member_type]=M') + '" target="_self">' + asTextConfig[LANG]['t_individual'] + '</a>' + 
				'<a href="' + sLink + encodeURI('act=filter&filter[seller_type]=C&filter[member_type]=M') + '" target="_self">' + asTextConfig[LANG]['t_shop_com'] + '</a>';
			break;
		case 3: 
			var sMenu = '<a href="' + sLink + encodeURI('act=filter&filter[product_status]=N') + '" target="_self">' + asTextConfig[LANG]['t_new'] + '</a>' + 
				'<a href="' + sLink + encodeURI('act=filter&filter[product_status]=U') + '" target="_self">' + asTextConfig[LANG]['t_used'] + '</a>';
			break;
	}
		
	c.innerHTML = sMenu;
	
	document.getElementsByTagName('body')[0].appendChild(c);
	
	p["at_parent"] = p.id;
	p["at_child"] = c.id;
	p["at_position"] = position;
	
	c["at_parent"] = p.id;
	c["at_child"] = c.id;
	c["at_position"] = position;
	
	p.onmouseover = showFilerLinkMenu;
	p.onmouseout  = hideFilerLinkMenu;
	c.onmouseover = showFilerLinkMenu;
	c.onmouseout  = hideFilerLinkMenu;
}

function showFilerLinkMenu() {
	var p = document.getElementById(this["at_parent"]);
	var c = document.getElementById(this["at_child" ]);
	var pos = {top:-3,left:0}
	
	at_show_aux(p.id, c.id, pos);
	clearTimeout(c["at_timeout"]);
}

function hideFilerLinkMenu() {
	var p = document.getElementById(this["at_parent"]);
	var c = document.getElementById(this["at_child" ]);

	c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.display='none'", 100);
}

/******************************
 * DISPLAY THUMBNAIL
 ******************************/

function displayExclusivePostThumbnail() {
	if(sExclusivePostThumb == '') return;
	var asThumb = sExclusivePostThumb.split(',');
	
	for(var i=0,iLen=asThumb.length; i<iLen; i++) {
		var asBuff = asThumb[i].split('|');
		var sAdsFolder = asBuff[0].toString().charAt(asBuff[0].toString().length-1);
		
		_g('S_thumb' + asBuff[0]).innerHTML = '<a href="' + BASE_URL + 'ads/' + asBuff[0] + '" target="_blank" class="image_fade">' + 
			'<img src="' + BASE_URL + '_data/ads/' + sAdsFolder + '/' + asBuff[0] + '/picture/' + asBuff[0] + asBuff[1] + '" width="' + asBuff[2] + '" height="' + asBuff[3] + '" alt="' + asBuff[0] + '" /></a>';
	}
}

function displayStandardPostThumbnail() {
	if(sStandardPostThumb == '' || bShowStandardPostThumb == false) return;
	var asThumb = sStandardPostThumb.split(',');
	
	for(var i=0,iLen=asThumb.length; i<iLen; i++) {
		var asBuff = asThumb[i].split('|');
		var sBoardID = '0' + asBuff[0];
		var sBoardFolder = sBoardID.substring(sBoardID.length-2);
		
		_g('N_thumb' + asBuff[0]).innerHTML = '<a href="' + BASE_URL + 'items/' + asBuff[0] + '" target="_blank" class="image_fade">' + 
			'<img src="' + BASE_URL + '_data/board/' + sBoardFolder + '/' + asBuff[0] + '/picture/' + asBuff[0] + 's.png" alt="' + asBuff[0] + '" /></a>';
	}
}

function displaySearchThumbnail() {
	if(sThumbnail == '' || bShowThumbnail == false) return;
	
	var asThumb = sThumbnail.split(',');
	for(var i=0,iLen=asThumb.length; i<iLen; i++) {
		var iBoardID = asThumb[i];
		var sBoardIDText = '0' + iBoardID;
		var sBoardFolder = sBoardIDText.substring(sBoardIDText.length-2);
		
		_g('thumb' + iBoardID + '_container').innerHTML = '<a href="' + BASE_URL + 'items/' + iBoardID + '" target="_blank" class="image_fade">' + 
			'<img src="' + BASE_URL + '_data/board/' + sBoardFolder + '/' + iBoardID + '/picture/' + iBoardID + 's.png" alt="' + iBoardID + '" /></a>';
	}
}

// Filter form tab
var iFilterTabTimer;

function setFilterTabClass(index) {
	clearTimeout(iFilterTabTimer);
	
	for(var i=1; i<=2; i++) {
		var sSuffix = (i == index) ? '_a' : '';
		
		_g('filter_tab' + i + '_L').className = 'filter_tab_L' + sSuffix;
		_g('filter_tab' + i + '_R').className = 'filter_tab_R' + sSuffix;
	}
	
	showFilterTabSubMenu(index);
}

function swapFilterTab() {
	for(var i=1; i<=2; i++) {
		var sSuffix = (i == iFilterTabMenu) ? '_a' : '';
		
		_g('filter_tab' + i + '_L').className = 'filter_tab_L' + sSuffix;
		_g('filter_tab' + i + '_R').className = 'filter_tab_R' + sSuffix;
	}
	
	showFilterTabSubMenu(iFilterTabMenu);
}

function restoreFilterTabClass() {
	iFilterTabTimer = setTimeout("swapFilterTab();", 1000);
}

function getFilterTabLinkUrl(index) {
	var sUrl = document.location.toString();
	
	if(sUrl.indexOf("?") > -1) {
		var asUrl = sUrl.split("?");
		var asQS = asUrl[1].split("&");
		var sLink = asUrl[0].substr(0, asUrl[0].lastIndexOf('/'));
		
		var sNewQS = '';
		for(var i=0,iLen=asQS.length; i<iLen; i++) {
			if(asQS[i] == 'lst=1') {
				sNewQS += 'lst=1&';
				break;
			}
		}
		
		if(sNewQS != '') {
			sLink += '/?' + sNewQS;
		} else {
			sLink += '/?';
		}
	} else {
		var sLink = sUrl.substr(0, sUrl.lastIndexOf('/')) + '/?';
	}
	
	switch(index) {
		case 1: 
			var sLink1 = sLink + encodeURI('act=filter&filter[seller_type]=C');
			var sLink2 = sLink + encodeURI('act=filter&filter[seller_type]=I');
			break;
		case 2: 
			var sLink1 = sLink + encodeURI('act=filter&filter[seller_type]=C&filter[member_type]=M');
			var sLink2 = sLink + encodeURI('act=filter&filter[seller_type]=I&filter[member_type]=M');
			break;
	}
	
	return [,sLink1,sLink2];
}

function showFilterTabSubMenu(index) {
	clearTimeout(iFilterTabTimer);
	
	var oDiv = _g('filter_tab_sub_menu');
	var p = _g('filter_tab_link' + index);
	var left = parseInt(p.offsetParent.offsetLeft)-5;
	var asLink = getFilterTabLinkUrl(index); 
	
	var asTextConfig = {
		th:{t_individual:'บุคคลทั่วไป',t_shop_com:'ร้านค้า/บริษัท',t_new:'ใหม่',t_used:'มือสอง'},
		en:{t_individual:'Individual',t_shop_com:'Shop/Company',t_new:'New',t_used:'Used'}
	}
	
	var asFilterTabSubMenuClass = [
		'',
		['','link_filter_tab','link_filter_tab'],
		['','link_filter_tab','link_filter_tab']
	];
	
	if(iFilterTabSubMenu != "") {
		asFilterTabSubMenuClass[iFilterTabMenu][iFilterTabSubMenu] = 'link_filter_tab_selected';
	}
	
	oDiv.style.cssText = 'margin-left:' + left + 'px;';
	oDiv.innerHTML = '<div>' + 
		'<a id="" href="' + asLink[1] + '" onclick="javascript:FilterTabMenuLink(this);" class="' + asFilterTabSubMenuClass[index][1] + '">' + asTextConfig[LANG]['t_shop_com'] + '</a>' + 
		'<a href="' + asLink[2] + '" onclick="javascript:FilterTabMenuLink(this);" class="' + asFilterTabSubMenuClass[index][2] + '">' + asTextConfig[LANG]['t_individual'] + '</a>' + 
		'<div class="clear"> </div>' + 
		'</div>';
}

function FilterTabMenuLink(obj) {
	var sUrl = obj.href;
	
	var oProductStatusTab = _g('product_status_tab');
	if(oProductStatusTab && oProductStatusTab.options[oProductStatusTab.selectedIndex].value) {
		obj.href = sUrl + '&filter[product_status]=' + oProductStatusTab.options[oProductStatusTab.selectedIndex].value;
	}
	
	return false;
}

function clearFilterTabTimer() {
	clearTimeout(iFilterTabTimer);
}

function initFilterTab() {
	setFilterTabClass(iFilterTabMenu);
}

function ProductStatusTabLink(obj) {
	if(iFilterTabSubMenu == "") return;
	
	var asLink = getFilterTabLinkUrl(iFilterTabMenu);
	var sLink = asLink[iFilterTabSubMenu];
	
	if(obj.value != '') {
		sLink += encodeURI('&filter[product_status]=' + obj.value);
	}
	
	self.location.href = sLink;
}

// Detect user screen
document.cookie = 'pmk_user_screen='+ screen.width + '|' + screen.height + '; path=/';