var Dom = YAHOO.util.Dom,Event = YAHOO.util.Event;

var CookieWidget = {
	getCookie: function(name) {
		var value = document.cookie.match('(?:^|;)\\s*'+name+'=([^;]*)');
		return value ? unescape(value[1]) : '';
	},
	setCookie: function(name, value, expire, domain, path) {
		value = escape(value);
		value += (domain) ? '; domain=' + domain : '';
		value += (path) ? "; path=" + path : '';
		if (expire){
			var date = new Date();
			date.setTime(date.getTime() + (expire * 86400000));
			value += "; expires=" + date.toGMTString();
		}
		document.cookie = name + "=" + value;
	}
};
var kbRefd = 'koubei';
if (CookieWidget.getCookie('_kb_refd').indexOf('yahoo') != -1) {
	kbRefd = 'yahoo';
}
//from yahoo
if (kbRefd == 'yahoo') {
	Event.onAvailable('tab-all', function(){
		Dom.get('tab-all').firstChild.href = 'http://bendi.koubei.com/search/searchyklocal.html';
	});
}

Event.on('member-tab', 'mouseover', function() {
	Dom.addClass('member-tab', 'active');
	Dom.removeClass('group-tab', 'active');
	Dom.addClass('member-wrapper', 'active');
	Dom.removeClass('group-wrapper', 'active');
});
Event.on('group-tab', 'mouseover', function() {
	Dom.addClass('group-tab', 'active');
	Dom.removeClass('member-tab', 'active');
	Dom.addClass('group-wrapper', 'active');
	Dom.removeClass('member-wrapper', 'active');
});
Event.on('member-wrapper', 'mouseover', zoomLi);
Event.on('group-wrapper', 'mouseover', zoomLi);
function zoomLi(ev) {
	var container = this.getElementsByTagName('ul')[0];
	var li = Event.getTarget(ev);
	while (li.tagName != 'LI' && Dom.isAncestor(container,li)) {
		li = li.parentNode;
	}
	if (li.tagName != 'LI') {
		return;
	}
	if (!Dom.hasClass(li, 'detail')) {
		//display this detail li
		var detailLi = Dom.getNextSibling(li);
		//hide this li
		Dom.removeClass(li, 'visible');
		//hide former detail li and display nomal li
		var detailLis = Dom.getChildren(container);
		for (var i=0; i<detailLis.length; i++) {
			var dLi = detailLis[i];
			if (Dom.hasClass(dLi, 'detail')) {
				if (Dom.hasClass(dLi, 'visible')) {
					Dom.removeClass(dLi, 'visible');
					Dom.addClass(Dom.getPreviousSibling(dLi), 'visible');
				}
			}
		}
		Dom.addClass(detailLi, 'visible');
	}
}

//community
var activeMenuId = '';
var openThread, closeThread;
var BLOCK_HEIGHT = 109, OPEN_DELAY_TIME = 200, CLOSE_DELAY_TIME = 3000, ANI_DURATION = 0.3;
var communityData = {};
function activeMenu(menuId) {
	var blockId = menuId.replace('menu', 'content');
	Dom.addClass(menuId, 'active');
	Dom.addClass(blockId, 'active');
	activeMenuId = menuId;
	fetchCommunityData(menuId);
}
function disActiveMenu() {
	activeblockId = activeMenuId.replace('menu', 'content');
	Dom.removeClass(activeMenuId, 'active');
	Dom.removeClass(activeblockId, 'active');
	activeMenuId = '';
}
function aniActiveMenu(menuId) {
	var blockId = menuId.replace('menu', 'content');
	Dom.get(blockId).style.height = '0px';
	activeMenu(menuId);
	var attributes = {height: { to:  BLOCK_HEIGHT}};
	var anim = new YAHOO.util.Anim(blockId, attributes);
	anim.duration = ANI_DURATION;
	anim.animate();
}
function aniDisActiveMenu() {
	var blockId = activeMenuId.replace('menu', 'content');
	var attributes = {height: { to: 0 }};
	var anim = new YAHOO.util.Anim(blockId, attributes);
	anim.duration = ANI_DURATION;
	anim.animate();
	anim.onComplete.subscribe(function(){
		disActiveMenu();
		Dom.get(blockId).style.height = BLOCK_HEIGHT + 'px';
	});
}
function callLater(menuId) {
	return (function() {
		aniActiveMenu(menuId);
	});
}
Event.on('community-menu', 'mouseover', function(ev) {
	var container = this;
	var li = Event.getTarget(ev);
	while (li.tagName != 'LI' && Dom.isAncestor(container,li)) {
		li = li.parentNode;
	}
	if (li.tagName != 'LI') {
		return;
	}
	window.clearTimeout(closeThread);
	var menuId = li.id;
	if (activeMenuId != '') {
		disActiveMenu(activeMenuId);
		activeMenu(menuId);
	} else {
		var aniActiveMenuCallLater = callLater(menuId);
		openThread = window.setTimeout(aniActiveMenuCallLater, OPEN_DELAY_TIME);
	}
});

Event.on('community-menu', 'mouseout', function(ev) {
	var container = this;
	var li = Event.getTarget(ev);
	while (li.tagName != 'LI' && Dom.isAncestor(container,li)) {
		li = li.parentNode;
	}
	if (li.tagName != 'LI') {
		return;
	}
	closeThread = window.setTimeout(aniDisActiveMenu, CLOSE_DELAY_TIME);
	window.clearTimeout(openThread);
});
Event.on(['content-feed', 'content-jiayuan', 'content-group'], 'mouseover', function(ev) {
	var blockId = this.id;
	var menuId = blockId.replace('content', 'menu');
	activeMenu(menuId);
	window.clearTimeout(closeThread);
});
Event.on(['content-feed', 'content-jiayuan', 'content-group'], 'mouseout', function(ev) {
	window.clearTimeout(closeThread);
	closeThread = window.setTimeout(aniDisActiveMenu, CLOSE_DELAY_TIME);
});

function fetchCommunityData(menuId) {
	if (typeof(loginState) == 'undefined' || !loginState) return false;
	if (communityData[menuId]) return false;
	communityData[menuId] = true;
	var blockId = menuId.replace('menu', 'content');
	Dom.get(blockId).innerHTML = '<div class="loading">正在加载数据...</div>';
	var sUrl = Dom.get(menuId).getAttribute('url');
	var callback = {
		success: function(o) {
			Dom.get(blockId).innerHTML = o.responseText;
		},
		failure: function(o) {
		}
	}
	YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
}

//search
var optionKeyPressed = false;
var optionKeyFocus = false;
var activeTabId = '';
Event.on('search-tab', 'click', function(ev) {
	var container = this;
	var li = Event.getTarget(ev);
	while (li.tagName != 'LI' && Dom.isAncestor(container,li)) {
		li = li.parentNode;
	}
	if (li.tagName != 'LI') {
		return;
	}
	if (Dom.hasClass(li, 'quick-toggle')) {
		Event.preventDefault(ev);
		toggleToThisTab(li.id);
	}
});
function toggleToThisTab(id) {
	if (activeTabId == id) return;
	if (activeTabId != '') {
		Dom.removeClass(activeTabId, 'active');
		var aidLi = Dom.get(activeTabId.replace('tab', 'aid'));
		if (aidLi) {
			Dom.removeClass(aidLi, 'active');
			var aidInputs = aidLi.getElementsByTagName('input');
			var aidSelects = aidLi.getElementsByTagName('select');
			for (var i=0; i<aidInputs.length; i++) {
				aidInputs[i].disabled = true;
			}
			for (var i=0; i<aidSelects.length; i++) {
				aidSelects[i].disabled = true;
			}
		}
	} else {
		//first rendering, user may input key words before js render
		if (optionKeyFocus) {
			optionKeyPressed = true;
		}
	}
	Dom.addClass(id, 'active');
	var aidLi = Dom.get(id.replace('tab', 'aid'));
	if (aidLi) {
		Dom.addClass(aidLi, 'active');
		var aidInputs = aidLi.getElementsByTagName('input');
		var aidSelects = aidLi.getElementsByTagName('select');
		for (var i=0; i<aidInputs.length; i++) {
			aidInputs[i].disabled = false;
		}
		for (var i=0; i<aidSelects.length; i++) {
			aidSelects[i].disabled = false;
		}
	}
	
	Dom.get('search-form').action = Dom.get(id).firstChild.href;
	
	var initValue = Dom.get(id).getAttribute('init-value');
	Dom.get('option-key').setAttribute('init-value', initValue);
	if (!optionKeyPressed) {
		Dom.get('option-key').value = initValue;
		Dom.get('option-key').style.color = '#999';
	}
	if (id == 'tab-store') {
		Dom.get('search-form').elements['option.category'].disabled = false;
	} else {
		Dom.get('search-form').elements['option.category'].disabled = true;
	}
	activeTabId = id;
}
Event.onAvailable('local-search', function() {
	toggleToThisTab('tab-all');	
});

Event.on('option-key', 'focus', function(){
	if (!optionKeyPressed) {
		this.value = '';
		this.style.color = 'black';
	}
	if (Dom.get('option-city').value == Dom.get('option-city').getAttribute('init-value')) {
		Dom.get('option-city').value = Dom.get('option-city').getAttribute('city');
		Dom.get('option-city').style.color = 'black';
	}
});
Event.on('option-key', 'blur', function(){
	if (this.value != '' && this.value != this.getAttribute('init-value')) {
		optionKeyPressed = true;
	}
	if (!optionKeyPressed || this.value == '') {
		optionKeyPressed = false;
		this.value = this.getAttribute('init-value');
		this.style.color = '#999';
	}
});
Event.on('option-key', 'keyup', function(){
	optionKeyPressed = true;
});
Event.onAvailable('option-city', function() {
	YAHOO.KB.widget.renderInitValueElement(Dom.get('option-city'));	
});

Event.on('search-form', 'submit', function(){
	if (!optionKeyPressed) {
		Dom.get('option-key').value = '';
	}
	if (Dom.get('option-city').getAttribute('init-value') == Dom.get('option-city').value) {
		Dom.get('option-city').value = '';
	}
	var form = Dom.get('search-form');
	if (activeTabId == 'tab-rentout' || activeTabId == 'tab-sell') {
		var priceElement;
		if (activeTabId == 'tab-rentout') {
			priceElement = form.elements['option.price'][0];
		} else {
			priceElement = form.elements['option.price'][1];
		}
		
		var price = priceElement.value;
		if (price != '') {
			var priceArray = price.split(',');
			form.elements['option.priceMin'].disabled = false;
			form.elements['option.priceMax'].disabled = false;
			form.elements['option.priceMin'].value = priceArray[0];
			form.elements['option.priceMax'].value = priceArray[1];
		}
		form.elements['option.keyword'].disabled = false;
		form.elements['option.keyword'].value = Dom.get('option-key').value;
	}
	if (activeTabId == 'tab-fenlei') {
		form.elements['keyword'].disabled = false;
		form.elements['region'].disabled = false;
		form.elements['region'].value = form.elements['city'].value;
		if (typeof(UrlEncode) != 'undefined')
			form.elements['keyword'].value = UrlEncode(Dom.get('option-key').value);
		else 
			form.elements['keyword'].value = Dom.get('option-key').value;
	}
});


(function() {
	if (typeof KS == "undefined" || !KS) {
		KS={};
	}
	KS.bom = {
		setHomePage:function(ev){
			try {
				this.style.behavior = 'url(#default#homepage)';
				this.setHomePage('http://www.koubei.com');
			} catch(e) {
				alert('抱歉！您的浏览器不支持直接设为首页');
			}
		},
		addBookmark: function(ev) {
			var title = '口碑网';
			var url = 'http://www.koubei.com';
			if( document.all ) {
				window.external.AddFavorite( url, title);
			} else if (window.sidebar) {
				window.sidebar.addPanel(title, url,"");
			} else {
				alert('抱歉！您的浏览器不支持直接操作此功能');
			}
		}
	}
})();
Event.on("add-fav","click",KS.bom.addBookmark);
Event.on("set-homepage","click",KS.bom.setHomePage);

Event.on('post', 'mouseover', function() {
	Dom.get('post-list').style.visibility = 'visible';
});
Event.on('post', 'mouseout', function() {
	Dom.get('post-list').style.visibility = 'hidden';
});
Event.on('post-list', 'mouseover', function() {
	Dom.get('post-list').style.visibility = 'visible';
});
Event.on('post-list', 'mouseout', function() {
	Dom.get('post-list').style.visibility = 'hidden';
});

Event.onDOMReady(function() {
	TB.widget.SimpleSlide.decorate('SlidePlayer', {eventType:'mouse', effect:'fade',autoPlayTimeout:"5"});	
});


