/****
* c2c.̨js
* auth: szw
* time: 2010-12-6
* version: ver 1.0
*/

//全局变量
var searchId = 0; //搜索类型
var searchTips = ['请输入您想找的优惠券','请输入您想找的优惠商户','请输入您想找的优惠商品'];

/*************  account.html **************/
$(document).ready(function(){
	
	$("#searchtxt").blur();
	
	//搜索切换
	$("#searchitem a").click(function() {
		$(this).parent().find("a").removeClass("current");
		$(this).addClass("current");
		$("#searchtxt").focus();
		searchId = $(this).attr("val");
	});
	
	//搜索框聚焦
	$("#searchtxt").focus(function() {
		
		$("body").bind('keyup',function(event) {   
			if(event.keyCode==13){   
			   searchUhl($("#searchitem a.current").attr("val"));
			}      
		});
		
		var txt = $(this).val();
		$(this).addClass("focus");
		if (!in_array(txt, searchTips)) {
			return false;
		} else {
			$(this).val("");
		}
	});
	//搜索框离焦
	$("#searchtxt").blur(function() {
		
		$("body").unbind('keyup');
		
		var txt = $(this).val();
		$(this).removeClass("focus");
		if (txt != '' && !in_array(txt, searchTips)) {
			return false;
		} else {
			$(this).val(searchTips[searchId]);
		}
	});
	
	
	//左边菜单控制收起
	$("#cmenu li.t b").click( // 点击大项
		function () {
			var list = $(this).parent().next(".list");
			var parent = $(this).parent();
			var exist =  list.attr("class");
			var height = list.css("height");
			var coll = parent.hasClass("collapse");
			
			if (coll) {//切换图标
				parent.removeClass("collapse");
			} else {
				parent.addClass("collapse");
			}
			if (exist != 'undefined') {//隐藏list
				if (height != "0px") {
					list.animate({"height":"0px"},"fast"); 
				} else {
					list.animate({"height":"100%"},"fast"); 
				}
			} else {
				return false;
			}
		}
	);
})

/**
* 判断元素是否在数组中
*/
function in_array(needle, haystack) {
	type = typeof needle;
	if(type == "string" || type == "number") {
		for(var i in haystack) {
			if(haystack[i] == needle) {
				return true;
			}
		}
	}
	return false;
}

/*
 * 搜索优惠啦 优惠券,优惠商户,优惠商品
*/
function searchUhl(search_type){
	//搜索切换
    
	if($('#searchtxt').val() == "" || $('#searchtxt').val() == searchTips[search_type] ){
	   alert(searchTips[search_type]);
	   return false;	
	}
	if(search_type == 0){
		 setTimeout(function(){ location.href = "couList.php?kwy="+encodeURI($('#searchtxt').val());},10);
		
	}else if(search_type == 1){
		 setTimeout(function(){ location.href = "storeList.php?kwy="+encodeURI($('#searchtxt').val());},10);
	}else if(search_type == 2){
		 setTimeout(function(){ location.href = shop_url+ "/searchProduct.php?kwy="+encodeURI($('#searchtxt').val());},10);
	}
	return false;
	
}

