/*
 * Changelog
 * 
 * 2009-09-02	初始化版本,此js包必须依赖jquery库
 */
/**
 * webRoot上下文路径 
 */
var webRoot='http://www.zhekouzu.com';

/**
 * 返回数值型时间戳
 */
function getT(){
	return "?"+(new Date()).getTime();
}

/**
 * 返回对像的所有属性信息，调试所用
 */
function p(o){
	var s = "";
	for(var i in o){
		s = s + i + ":" + o[i]+"/";
	}
	return s;
}

/**
 * 显示message信息
 */
function showMsg(msg){
	$("#msgTip").html(msg);
}

/**
 * 搜索表单Form初始化工作
 * 选择区域模块初始化工作
 */
function navInit(){
	//message tip
	hideMessageBox();
	//area select 
	if($('#area-list').length>0){
	    $('#area-list').hide();
	    t = setTimeout("$('#area-list').fadeOut()",500);
	    $('#area').hover(function(){
	        clearTimeout(t)
	        $('#area-list').fadeIn();
	    },function(){
	        t = setTimeout("$('#area-list').fadeOut()",500);
	    })
	}
	
	if($("#searchForm").length>0){
		var defaultKey = "输入您感兴趣的商户或信息";
		//绑定事件
		$("#searchForm").bind("submit", function(){
			var q = $.trim($("#q").val());
			if(q=="" || q==defaultKey){
				$("#q").focus();
				return false;
			}
			if(q.length<2){
				alert("查询关键字太短啦");
				$("#q").focus();
				return false;
			}else{
				return true;
			}
		});
		
		if($.trim($("#q").val())=="" || $.trim($("#q").val())==defaultKey){
			$("#q").css("color","gray");
			textFocusTip($("#q"),defaultKey);
		}
	}
}


function textFocusTip(tempText,defaultKey){
	$(tempText).blur(function(){
		if($.trim($(tempText).val())==''){
			$(tempText).val(defaultKey);
			$(tempText).css("color","gray");
		}else{
			$(tempText).css("color","");
			$(tempText).val($(tempText).val());
		}
	});
	$(tempText).focus(function(){
		if($.trim($(tempText).val())==defaultKey){
			$(tempText).val('');
			$(tempText).css("color","");
		}
	});
	$(tempText).val(defaultKey);
}

/**
 * 显示缩略图功能
 */
function showSmallPreview(coords){
	if(imageWidth && imageWidth>0){
		if (parseInt(coords.w) > 0){
			var rx = smallX / coords.w;
			var ry = smallY / coords.h;
			
			var w = Math.round(rx * imageWidth)+ 'px';
			var h = Math.round(ry * imageHeight)+ 'px';
			$('#preview').css({
				width:  w,
				height: h,
				marginLeft: '-' + Math.round(rx * coords.x) + 'px',
				marginTop: '-' + Math.round(ry * coords.y) + 'px'
			});
		}
	}
	$("#imagesinfo").val(coords.x+','+coords.y+','+coords.w+','+coords.h);
}

/**
 * 按name设置checked选中状态
 */
function selectAllByName(checked,chkName){
	$("input:enabled[name='"+chkName+"']").attr('checked',checked);
}


/**
 * checkcode相关
 */

function refreshCode(){
	$("#checkCodeImg").attr("src",webRoot+ "/common/checkcode"+getT());
}

/*
 * 	ShopRadio follow相关
*/

function follow(shopId){
	jQuery.post(webRoot+"/user/shop/"+shopId+"/follow",{}, function(data){
		if(data.isError){
			alert(data.tip);
		}else{
			$("#followBtn").html('<a href="javascript:;" title="已关注 点击取消关注" onclick="unfollow(\''+shopId+'\');" class="followed">已关注</a>');
			showMessage("已成功关注");
		}
	},'json');
}

function unfollow(shopId){
	if(confirm("确定要取消关注此商户")){
		jQuery.post(webRoot+"/user/shop/"+shopId+"/unfollow", function(data){
			if(data.isError){
				alert(data.tip);
			}else{
				$("#followBtn").html('<a href="javascript:;" onclick="follow(\''+shopId+'\');" title="添加商户到我的关注列表" class="follow">我要关注</a>');
				showMessage("已取消关注");
			}
		},'json');
	}
}


/**
 * 信息举报
*/

/*
* review相关的信息举报，
*/
function ireport(url,type,id,content){
	var reportContent=$.trim($('#'+content).text());
	var typeName = "信息";
	if(type=='eventreview' || type=='promoreview' || type=='couponreview' || type=='dmreview'){
		typeName="评论";
	}else if(type=='topic'){
		typeName="话题";
	}
	var msg = "您认为该"+typeName+"有不良内容，并举报它吗？ \n \n【确定】 提交此举报\n【取消】 撤销此操作";
	if(confirm(msg)){
		jQuery.post(webRoot+"/support/ireport",{'url':url,'type':type,'id':id,'content':reportContent},
		function(data){
			if(data.isError){
				alert(data.tip);
			}else{
				alert("您的举报信息已提交，感谢您的帮助。");
			}
		},'json'); 
	}
}


/*
 * Event,Promo,Coupon,Dm等删除评论
*/
function deleteReview(reviewType,id){
	if(confirm("确定删除此评论?")){
		jQuery.post(webRoot+"/shop/"+reviewType+"/review/"+id+"/delete",{},
		function(data){
			if(data.isError){
				alert(data.tip);
			}else{
				$("#topic_"+id).fadeOut("slow");
				currentReivewIndex--;
				if(currentReivewIndex<=0){
					currentReivewIndex = 0;
					$("#reviewCount").html("评论");
				}else{
					$("#reviewCount").html("评论("+currentReivewIndex+")");
				}
			}
		},'json'); 
	}
}

function reviewFormValidation(){
	$("#reviewForm").validate({	
		rules: {
			content: {
				required: true,
				maxlength:5000
			},
			checkCode: {
				required: true,
				minlength: 4,
				remote: webRoot+"/ajaxaccounts?m=checkcode"
			}
		},
		messages: {
			content: {
				required: "请输入评论内容",
				maxlength: "字数超过5000,请分两次评论"
			},
			checkCode: {
				required: "请输入验证码",
				minlength: "请输入四位验证码。",
				remote: "验证码错误，请重新输入"
			}
		}
	});
}


/*
 * Topic post 相关操作
*/

function topicPostValidation(){
	$("#topicPostForm").validate({	
		rules: {
			content: {
				required: true,
				minlength: 3,
				maxlength:5000
			},
			checkCode: {
				required: true,
				minlength: 4,
				remote: webRoot+"/ajaxaccounts?m=checkcode"
			}
		},
		messages: {
			content: {
				required: "内容不能为空",
				minlength: "你的内容太少啦!",
				maxlength: "字数在5000字以内"
			},
			checkCode: {
				required: "请输入验证码",
				minlength: "请输入四位验证码。",
				remote: "验证码错误，请重新输入"
			}
		}
	});
    $("#topicPostForm").resetForm();
}
/**
 * 删除Topic post
 */
function deleteTopicPost(topicId,id){
	if(confirm("确定删除此回复?")){
		jQuery.post(webRoot+"/group/topic/post/"+id+"/delete?tid="+topicId,{},
		function(data){
			if(data.isError){
				alert(data.tip);
			}else{
				$("#tr_"+id).fadeOut("slow");
			}
		},'json'); 
	}
}


/**
 * hide message box
 */
function hideMessageBox(){
	if($('.hide-errors').length>0){
		setTimeout("$('.hide-errors').fadeOut()",8000);
	}
}


function showMessage(msg){
	if($("#messageBox")!=null){
		$("#messageBox").html('<div align="center" class="hide-errors">'+msg+'</div>');
		hideMessageBox();
	}
}


function deleteTopic(topicId,groupId){
	if(confirm("确定要删除本话题?")){
		jQuery.post(webRoot+"/group/topic/"+topicId+"/delete",{},
		function(data){
			if(data.isError){
				alert(data.tip);
			}else{
				alert('删除成功');
				location.href=webRoot+"/group/"+groupId;
			}
		},'json'); 
	}
}


function changeProvince(provinceSel,countySelId){
	var provinceId = $(provinceSel).val();
	jQuery.post(webRoot + "/common/selectarea", {'p':provinceId,m:'citys'},function(str){
		$("#"+countySelId).attr('options').length=0;
		var selHtml = "<option value=''>请选择...</option>";
		var countyList = str.countyList;
		for(var i in countyList){
			selHtml = selHtml + "<option value='"+countyList[i].id+"'>"+countyList[i].name+"</option>";
		}
	  	$("#"+countySelId).html(selHtml);
	},'json');
}