/****
* index.̨js
* auth: szw
* time: 2010-11-18
* version: ver 1.0
*/

//全局变量
var searchId = 0; //搜索类型
var searchTips = ['请输入您想找的优惠券','请输入您想找的优惠商户','请输入您想找的优惠商品'];
var t; //定时器

/*************  首页 **************/
$(document).ready(function(){
	
	$("#searchtxt").blur();
    searchId = $("#searchitem a.current").attr("val");
	$("#searchtxt").val(searchTips[searchId]);

	
	//搜索框聚焦
	$("#searchtxt").focus(function() {
        
		$("body").bind('keyup',function(event) {   
			if(event.keyCode==13){   
			   searchUhl(0);
			}      
		});
		
		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]);
		}


	});
	
	
});


/**
* 显示首页导航悬浮弹出框
* param: obj 悬浮对象
*/
function showNavBox(obj) {
	hideNavBox();
	$("#dialog").empty();
	var showDialog = $(obj).find(".d").clone();
	var position = $(obj).offset();
	
	$(obj).addClass("current");
	posBox('dialog', position.top, position.left);
	$("#dialog").show();
	showDialog.appendTo("#dialog").show();
	
	$("#dialog .d").hover(function() {
		clear(t);
	}, function() {
		hideNavBox();
	});
}

/**
* 隐藏首页导航悬浮弹出框
*/
function hideNavBox(id) {
	$("#subnav").find("li.current").removeClass("current");
	$("#dialog").hide();
	clear(t);
}

/**
* 定时隐藏dialog
*/
function timeOutNavBox(id) {
	t = setTimeout("hideNavBox('"+id+"')", 300);
}

/**
* 清除定时器
*/
function clear(t) {
	if (t) {
		clearTimeout(t);
	}
}

/**
* 调整dialog位置
*/
function posBox(boxId, top, left) {
	if (navigator.userAgent.indexOf('Firefox') >= 0){
		$("#" + boxId).css({"top":top, "left":left + 162 - 32});
	} else {
		$("#" + boxId).css({"top":top, "left":left + 161 - 32});
	}
}

/**
* 切换选项卡
*/
function changeTab(id, ulid) {
	$("#"+ id).siblings().removeClass("current");
	$("#" + id).addClass("current");
	$("#" + ulid).siblings().hide();
	$("#" + ulid).show();
}

/**
* 显示回复框
*/
function showRepeat(id) {
	var a_rp = $("#a_" + id);
	var span = $("#a_" + id + " span");
	var a_b = $("#a_" + id + " b");
	var txt = span.html();

	$("#" +id).toggle("fast");
	
	if (a_b.hasClass("current")) {
		a_b.removeClass("current");
	} else {
		a_b.addClass("current");
	}
	
	if ($.trim(txt) == '显示回应') {
		span.html("收起回应");
	} else {
		span.html("显示回应");
	}
}

/**
* 鼠标悬浮提示框
*/
function showInfo(obj, offsetX, offsetY) {
	var position = $(obj).offset();
	$("#tipinfo").html($(obj).attr("info"));
	$("#tipinfo").css({"z-index":"4","top":position.top + offsetY, "left":position.left + offsetX});
	$("#tipinfo").show();
}
function hideInfo() {
	$("#tipinfo").html("");
	$("#tipinfo").hide();
}

/*展开/关闭分类*/
function expAndFoldKind(id) {
	var btn = $("#kbtn" + id);
	var container = $("#kind" + id);
	var baseClass = "type-kind-2";
	var currentClass = "type-kind-2-c";
	
	if (id == 3) {
		baseClass = "type-kind-3";
		currentClass = "type-kind-3-c";
	}
	
	if (!btn.hasClass(currentClass)) {
		container.css({"height":"auto"});
		btn.html("收起");
		btn.removeClass(baseClass).addClass(currentClass);
	} else {
		container.animate({height: "30px"}, 200);
		btn.html("展开");
		btn.removeClass(currentClass).addClass(baseClass);
	}
}

//追加推荐菜
function addCai(obj) {
	var cai = $("#cai");
	var oldValue = cai.val();
	if (oldValue != '') {
		oldValue += ' ';
	}
	cai.val(oldValue + $(obj).html());
	
	$(obj).remove();
}
/*显示更多*/
function showMore(obj, container, minheight) {
	var ohtml = $(obj).html();
	if (ohtml == '显示更多') {
		$(obj).html("取消");
		containerobj = $(obj).parent().find(container);
		if(containerobj.height()> minheight){
			 containerobj.css({"height":"auto"});
		}
	} else {
		$(obj).html("显示更多");
		$(obj).parent().find(container).css({"height":minheight});
	}
}

/**
* 商户名片--弹出举报框
* pid  评论id
*/
function jubao(pid) {
	$("#pid").val(pid);
	$("#opinion").hide();
	
	$.common.dialog({'id':'jubao',"title":"提交不正当原因"});
}
/*隐藏不正当原因*/
function hideJubao() {
	$("#pid").val("");
	$("#jubao").hide();
}
/*选择不正当原因*/
function setJubao(val) {
	if (val == -1) {
		$("#opinion").show();
	} else {
		$("#opinion").hide();
	}
}


/**
* 判断元素是否在数组中
*/
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;
}

/*积分乐园--设置tab*/
function setJFnav(obj, id) {
	var items = $("#jfNav").find("a.current");
	$("#jfItem li").hide();
	$(obj).blur();
	
	switch(id) {
		case 1:
		if (!$(obj).hasClass("current") ){
			items.removeClass("jf_zq current").addClass("jf_zq1");
			}
			$(obj).removeClass("jf_hf1").addClass("jf_hf current");
			$("#jf_hf").show();
			$("#jf_renwu").hide();
			$("#jf_duihuang").show();
			break;
		case 2:
			if (!$(obj).hasClass("current") ){
			items.removeClass("jf_hf current").addClass("jf_hf1");
			}	
			$(obj).removeClass("jf_zq1").addClass("jf_zq current");
			$("#jf_zq").show();
			$("#jf_duihuang").hide();
			$("#jf_renwu").show();
			break;
		default:
			break;
	}
}

/*积分乐园--切换子导航*/
function setJFsubnav(obj, id) {
	$(obj).siblings().removeClass("current");
	$(obj).blur().addClass("current");
	
	if (id == 2) {
		$("#choujiang").show();
		$("#jf_duihuang").hide();
	} else {
		$("#choujiang").hide();
		$("#jf_duihuang").show();
	}
}

/*特卖会--切换推荐大图*/
function showtm(obj, id) {
	$("#tm .tmDIV").hide();
	$("#" + id).show();
	
	$(obj).siblings().removeClass("current");
	$(obj).blur().addClass("current");
}


/*****菜单项展开和关闭的cookie记录****/
function changeCookie(c){
	
	var v_old = getCookie(c);
	
	if (v_old == null || v_old==''||v_old==0) {
			delCookie(c);
			setCookie(c,1);
			$("#"+c).val(1);
			changeCat();
		
	}else if(v_old==1){
		delCookie(c);
		setCookie(c,0);
		$("#"+c).val(0);
		changeCat();
	}
	
}

function changeCat() {

	var c1 = $("#c1").val();
	var c2 = $("#c2").val();
	var c3 = $("#c3").val();
	
	
	if (c1!=1) {
		c1 = 0 ;
	}
	if (c2!=1) {
		c2 = 0 ;
	}
	if (c3!=1) {
		c3 = 0 ;
	}
}

/*写cookies函数*/
function setCookie(name,value){//两个参数，一个是cookie的名子，一个是值
    name = cookie_pre + name;
	var Days = 1;
	//域
	var domain = ".localhost";
	//document.cookie = "domain="+domain
	var exp  = new Date();
	exp.setTime(exp.getTime() + Days*24*60*60*1000);
	document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/;";
}
/*取cookies函数*/    
function getCookie(name) { 
    name = cookie_pre + name;   
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr != null){
		return unescape(arr[2]);
	}
	return null;
}
/*删除cookie*/
function delCookie(name) {
	name2 = cookie_pre + name;
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval=getCookie(name);
	if(cval!=null){
		document.cookie= name2 + "="+cval+";expires="+exp.toGMTString();
	}
}
/**短信下载提示框**/
function showDX() {
	$.common.dialog({'id':'showDX',"title":"短信优惠券下载"});
}

/*显示商户介绍*/
function showJieshao() {
	var _windowWidth  = document.documentElement.clientWidth;   
	var _windowHeight = document.documentElement.clientHeight;   
	var _objHeight    = $("#jieshao").outerHeight();   
	var _objWidth     = $("#jieshao").outerWidth(); 
	
	$("#jieshao").css({
		"display":"block",
		"position": "absolute",
		"top":(_windowHeight - _objHeight)/2+$(document.documentElement).scrollTop() + "px",
		"left":(_windowWidth - _objWidth)/2 + "px"
	});
	$.overlay.show();
}
/*关闭商户介绍*/
function hideJieshao() {
	$('#popup_overlay').remove();
	$("#jieshao").css({"display":"none"});
}

/*
 * 状态页面
*/
function redirect(url){
	setTimeout(function(){ location.href = url;},10);
}
