/*
============================================================
 文件名称 : /Common/Comm.js
 文件作用 : 系统后台通用JS文件
 最后更新 : 2009-6-15
============================================================
 版权声明 : Copyright 2005 Cayue All Rights Reserved.
 官方网站 : Http://www.cayue.com
============================================================
*/
//页面初始化加载
$(function(){
	lazyload();
});
//
function lazyload(){
 $("img[src*='"+GP_IMGDomain+"']").lazyload({
	  threshold:200,
		placeholder : GP_ImagePath+"ImageLoad.jpg",
		effect:"fadeIn"
 });
}
//获取验证码
function getCode(cPreName,cID,cAlt,cFontCss){
	var codeImg=Cayue.id(cPreName+cID+'Img');
	var codeFile=Cayue.id(cPreName+cID+'File');
	var codeExplain=Cayue.id(cPreName+cID+'Exp');
	var codePath=codeFile.value+"?"+Math.random();
	var ranChar=getRandomStr("","N",1);
	var codeStr="";
	codeStr+="<div class=\""+cFontCss+"\"><img src=\""+codePath+"\" alt=\""+cAlt+"\" onclick=\"this.src='"+codePath+"';\" class=\"hand left\" />";
	codeStr+=""+ranChar+"<input type=\"hidden\" name=\""+cPreName+cID+"R\" value=\""+ranChar+"\" /></div>";
	var img = new Image();
	img.src=codePath;
	img.onload = function(){
		codeImg.innerHTML=codeStr
	}
	codeExplain.style.display="inline";
}
function getRandomStr(_StrCaseType,_AssembleType,_StrLen){
	var RCodeStr="",
			CodeStr="",
			CharStr="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z",
			NumStr="0 1 2 3 4 5 6 7 8 9";
	if(_AssembleType.toUpperCase()!="N"){
		switch(_StrCaseType.toUpperCase()){
			case "A":
				CharStr=CharStr+" "+CharStr.toLowerCase();
				break;
			case "U":
				CharStr=CharStr;
				break;
			case "L":
				CharStr=CharStr.toLowerCase();
				break;
			default:
				CharStr=CharStr+" "+CharStr.toLowerCase();
				break;
		}
	}
	switch(_AssembleType.toUpperCase()){
		case "A":
			CodeStr=NumStr+" "+CharStr;
			break;
		case "N":
			CodeStr=NumStr;
			break;
		case "C":
			CodeStr=CharStr;
			break;
		default:
			CodeStr=NumStr+" "+CharStr;
			break;
	}
  var CodeStrArr=CodeStr.split(" ");
	for(var i=0;i<_StrLen;i++){
		RCodeStr+=CodeStrArr[Math.round(Math.random( )*(CodeStrArr.length-1))];
	}
	return(RCodeStr);
}
//Page Cache
function execPageCache(_PageType){
	setTimeout(function(){
		$.ajax({
				type:"get",
				async:false,
				url:GP_AjaxProcess_P,
				data:"Menu=ExecPageCache&AppName="+_PageType+"&Rnd="+new Date().getTime(),
				success:function(){
				}
		});
	},4000);//延迟4秒执行
}

//评论框 For 内嵌式登录显示事件
function ValidCommentEntry(_eventID){
	$("#"+_eventID).click(function(){
		if(getMemberIsEntry()==0){
			var dgMemberObj=diagMemberStatus(1,false);
			$("#"+dgMemberObj.id).bind( "dialogclose", function(event, ui) {
				//window.location.reload();
				$("#"+_eventID).focus();
			});
		}
	});
}
//页面数据提交弹窗提示反馈
function juiSubmitData(_frmID,_isReload,_curTitle){
	var __self=this;
	//数据处理函数
	var validData=function(){
		var $frmObj	= $("#"+_frmID),
				chkUrl	=	$frmObj.attr("action");
				//chkUrl	+="&Rnd="+new Date().getTime();
		var dgID		= 'dgValidComment';
		juiDialog(
			{
				id:dgID
			},{
				autoOpen:true,
				title:_curTitle,
				resizable:false,
				modal:true,
				width:400,
				open:function(event,ui){
					var $juiDG=$(this);
					$.ajax({
						 type: 'POST',
						 url: chkUrl,
						 dataType:'json',
						 data:$frmObj.serialize(),
						 success: function(returnData){
								if(returnData.Title!=''){
									$juiDG.dialog("option","title",returnData.Title);
								}
								$juiDG.html(returnData.Content);
								switch(returnData.Mode){
									case 90:
										break;
									case 91: //如果返回成功状态且表单设置刷新则让页面执行刷新操作
										$juiDG.bind("dialogclose",function(event,ui){
											if(_isReload){window.location.reload();}
										});
										break;
								}
						 }
					});
				}
		});
	}
//	if(getMemberIsEntry()==0){
//		var dgMemberObj=diagMemberStatus(1,true);
//		//$("#"+dgMemberObj.id).bind( "dialogclose", function(event, ui) {
//			//if(getMemberIsEntry()!=0){showPoComment.call(__self);}
//		//});
//	}else{
		validData.call(__self);
//	}
	return false;
}

//自动等比例缩放图片
function DrawImage(ImgD,iwidth,iheight){
    //参数(图片,允许的宽度,允许的高度)
		ImgD.onload=function(){
			var image=new Image();
			image.src=ImgD.src;
			if(image.width>0 && image.height>0){
			if(image.width/image.height>= iwidth/iheight){
					if(image.width>iwidth){
						ImgD.width=iwidth;
						ImgD.height=(image.height*iwidth)/image.width;
					}else{
						ImgD.width=image.width;
						ImgD.height=image.height;
					}
					//ImgD.alt=image.width+"×"+image.height;
					}
			else{
					if(image.height>iheight){
						ImgD.height=iheight;
						ImgD.width=(image.width*iheight)/image.height;
					}else{
						ImgD.width=image.width;
						ImgD.height=image.height;
					}
					//ImgD.alt=image.width+"×"+image.height;
					}
			}
		};
}
function loadAutoResize(_vesselAttrID,_setWidth,_setHeight){
	var vesselObj=Cayue.id(_vesselAttrID),
			vChildes=vesselObj.getElementsByTagName("IMG");
	for(var i=0;i<vChildes.length ;i++ ){
		DrawImage(vChildes[i],_setWidth,_setHeight);
	}
}
//调用设置页面
function openDivHtml(_url,_title,_width,_height,_isReload){
	var dgID='dgOpenDivHtml';
	if(arguments.length=6){dgID+=arguments[5];}
	juiDialog({
		id:dgID,
		type:'iframe',
		url:_url
	},{
		autoOpen:true,
		title:_title,
		resizable:true,
		modal:true,
		width:_width,
		height:_height
	});
}
function juiShowAlert(_tipTitle,_tipMsg,_width,_height){
	var dgID		= 'dgPageAlert';
	juiDialog(
		{
			id:dgID
		},{
			autoOpen:true,
			title:_tipTitle,
			resizable:false,
			modal:true,
			width:_width,
			height:_height,
			buttons:{
				"确定":function(){$(this).dialog("close");}
			},
			open:function(event,ui){
				$(this).html(_tipMsg);
			}
	});
}
//切换数据列表显示
/*
	格式:
		按钮: 前缀名称Btn_组别值_当前值
		内容: 前缀名称Cont_组别值_当前值
	参数:
		_event 按钮事件,必须为JQuery事件 ("click|mouseover"等)
		_vesselID 放置按钮及内容显示数据的容器ID 且为前缀名称
		_btnCurClass 按钮当前格式名称
*/
function tabDataList(_event,_vesselID,_btnCurClass){
	var btnName		= "Btn",
			contName	= "Cont",
			vesselID	= "#"+_vesselID,
			preName		= _vesselID,
			preBtnName= preName+btnName;
	$(vesselID).find("ul li[id^='"+preBtnName+"']").bind(_event,function(){execFun($(this));});
	var execFun=function($obj){
			var sufName=$obj.attr("id").replace(preBtnName,""),
					groupID=sufName.split('_')[1],
					curBtnNum=$obj.parent().children().index($obj);

			$.cookie('tabEventBtn',curBtnNum);

			$obj.addClass(_btnCurClass).siblings().removeClass(_btnCurClass);

			$(vesselID).find("div[id^='"+preName+contName+"_"+groupID+"']").each(function(index,domEle){
				//domEle==this
				if($(domEle).attr("id")!==(preName+contName+sufName)){
					$(domEle).hide();
				}else{
					$(domEle).show();
				}
			});
	};
}
function tabSwtich(_btnID,_contID,_eventName,_btnCss){
	$("#"+_btnID).bind(_eventName,function(){
		$("#"+_contID).toggle();
	}).hasClass(_btnCss);
}
//展开数据列表显示
function showMoreList(_event,_vesselID,_btnCurClass){
	var btnName	= "Btn",
			contName= "Cont",
			vesselID= "#"+_vesselID,
			preName	= _vesselID,
			contID	= "#"+preName+contName,
			btnID		= "#"+preName+btnName,
			indexNum=$(contID).find("li.hide").index(0)-1;
	$(btnID).bind(_event,function(){
			$(this).toggleClass(_btnCurClass);
			$(contID).find("li:gt("+indexNum+")").toggleClass("hide");
		});
}
//产品购买链接
function mallCount(_ppmID,_mallLink){
	var chkUrl=GP_AjaxProcess_P;
	$.get(chkUrl,{
				Menu:"MallCount",
				PPMID:_ppmID,
				Rnd:new Date().getTime()
			},function(data,textStatus){
			});
	window.open(_mallLink,"_blank");
}
/*广告点击链接*/
function adsLink(_type,_url){
	var urlPre="";
	switch(_type){
		case 'tb':
			urlPre="http://s.click.taobao.com/"
			break;
		case 'mytb':
			urlPre="http://item.taobao.com/"
			break;
		default:
			urlPre='';
			break;
	}
	var goUrl=urlPre+''+_url;
	window.open(goUrl,"");
}
