maleID=parseInt(maleID);
femaleID=parseInt(femaleID);

//=============================== pre Function ===============================
function get0or1(){
	var intValue;
	intValue=Math.round(Math.random());
	return intValue;
}

//=============================== Basic Function ===============================
//Set Cookies
function couple_SetCookie(sName, sValue,iExpireDays) {
	if (iExpireDays){
		var dExpire = new Date();
		dExpire.setTime(dExpire.getTime()+parseInt(iExpireDays*24*60*60*1000));
		document.cookie = sName + "=" + escape(sValue) + "; expires=" + dExpire.toGMTString() + "; path=/";
	}
	else{
		document.cookie = sName + "=" + escape(sValue) + "; path=/";
	}
}
function couple_SetCookieAlt(sName, sValue,iExpireMinutes) {
	if (iExpireMinutes){
		var dExpire = new Date();
		dExpire.setTime(dExpire.getTime()+parseInt(iExpireMinutes*60*1000));
		document.cookie = sName + "=" + escape(sValue) + "; expires=" + dExpire.toGMTString() + "; path=/";
	}
	else{
		document.cookie = sName + "=" + escape(sValue) + "; path=/";
	}
}

//Resize Image
function couple_resizeImage(objImage,maxWidth) {
  if(maxWidth>0){
   var objImg = $(objImage);
   var w = objImg.width();
   var h = objImg.height();
   if(objImg.width()>maxWidth){
    objImg.height(parseInt(maxWidth*(h/w)));
    objImg.width(maxWidth);
    return true;
   }
  }
}

//Parse Timeout
function pasIntTimeOut(){
	if (isNaN(intAjaxTimeOut) || intAjaxTimeOut==""){intAjaxTimeOut=6000;}
	if (parseInt(intAjaxTimeOut)<2000){intAjaxTimeOut=2000;}
}
//=============================== JQ Effect ===============================
//Toggle GuidePanel
function toggleGuide(obj,objID){
	//a.guide-show : when guide is visible
	//a.guide-hidden : when guide is visible
	objID=objID.replace("#","");
	var $guide=$("#"+objID);
	if(obj.className=="guide-hidden"){
		//apply class : a.guideBtn*
		var i=0;
		$guide.children("ul").children("li").each(function(){
			this.className="guideBtn"+i;
			i++;
		});
		//apply class : div.guideTab*
		var j=0;
		$guide.children("div").each(function(){
			this.className="guideTab guideTab"+i;
			j++;
		});

		$guide.fadeIn("normal",function(){$(this).fadeTo("normal",0.9);});
		obj.className="guide-show";
		$(obj).children("span").html("[-]");
		if($guide.children("h3").html()==""){switchGuide(objID,0);}
	}else{
		$guide.fadeTo("fast",1.0,function(){$(this).fadeOut("normal")});
		obj.className="guide-hidden";
		$(obj).children("span").html("[+]");
	}
}
//Switch GuidePanel
function switchGuide(objID,iID){
	objID=objID.replace("#","");
	var $guide=$("#"+objID);
	var $BtnSel=$guide.children("ul").children("li:eq("+iID+")").children("a");
	if ($BtnSel[0].className!=="guideBtn-sel"){
		//apply class to #pageGuide : div.guidePanel & div.guideBg(n)
		$guide.attr("class","guidePanel guideBg"+iID);
		//a.guideBtn : when not current
		$guide.children("ul").children("li").children("a").attr("class","guideBtn");
		$guide.children("div").css("display","none");
		//a.guideBtn-sel : when current
		$BtnSel.attr("class","guideBtn-sel");
		$guide.children("h3").html($BtnSel.html());

		var $guideTab=$("#"+objID).children("div:eq("+iID+")");
			$guideTab.fadeIn("normal");

		if ($guideTab.html()==""){
			reloadGuide(objID,iID);
		}
	}
}

//Reload GuidePanel
function reloadGuide(objID,iID){
	objID=objID.replace("#","");
	var $guideTab=$("#"+objID).children("div:eq("+iID+")");
	var sUrl="";
	var bLoaded=false;

	$guideTab.html("<p>"+iconAjaxLoading+langLoadingData+"</p>");

	switch(iID){
		case 0:
			sUrl=str00+"include/catalog.asp";
			break;
		case 1:
			sUrl=str00+"include/archives.asp";
			break;
		case 2:
			sUrl=str00+"include/tags.asp";
			break;
		case 3:
			sUrl=str00+"include/comments.asp";
			break;
		case 4:
			sUrl=str00+"include/guestcomments.asp";
			break;
		case 5:
			sUrl=str00+"include/trackbacks.asp";
			break;
	}

	pasIntTimeOut();

	$.ajax({
		type: 'GET', dataType: 'html', timeout: intAjaxTimeOut,
		url: sUrl,
		error: function(){
			$guideTab.html("<p><a href=\"javascript:void(0)\" onclick=\"reloadGuide('#pageGuide',"+iID+")\">"+langErrorThenRetry+"</a></p>");
			bLoaded=true;
		},
		success: function(data){
			data="<ul>"+data+"</ul>";
			$guideTab.html(data);
			bLoaded=true;
		}
	});

	setTimeout(function(){
			if(bLoaded==false){
				$guideTab.html("<p>"+iconAjaxLoading+langStillWorking+"</p>");
			}
		},parseInt(intAjaxTimeOut/2)
	);

}

//Switch ArticleList
function switchList(objID,iNum){
	objID=objID.replace("#","");
	var $obj=$("#"+objID);

	var iCount=$obj.children("div:eq("+iNum+")").children("ul").children("li").size()-$obj.children("div:eq("+iNum+")").children("ul").children("li.list-loading").size();
	if (iCount>0){
		$obj.height(intBridgePanelHeight-(intBridgePanelLiCount-iCount)*20);
	}else{
		if(!IE){$obj.height(80);}
	}

	if ($obj.children("ul").children("li:eq("+iNum+")").children("a").attr("class")!=="sel-curr")
	{
		$obj.children("ul").children("li").children("a").toggleClass("sel-curr");

		$obj.children("div:visible").fadeOut("normal");
		$obj.children("div:eq("+iNum+")").fadeIn("normal");
	}
}


//cut title
function articleTitle(){
	var iEdge=$("#pageContent h2.article-title:first").width()-10;
	if(iEdge<320){iEdge=320}
	$("#pageContent div.article-multi h2.article-title a").each(function(){
		if ($(this).width()>iEdge){
			titleTip(this,iEdge);
		}
	});
	$("#pageContent div.article-single h2.article-title a").each(function(){
		if ($(this).width()>iEdge){
			titleTip(this,iEdge);
		}
	});
}
function titleTip(obj,width){
	width=width+20;
	$(obj).hover(function(){
		$(this).before("<div class=\"full-title\" style=\"width:"+width+"px\">"+$(this).html()+"</div>");
		$(this).prev().fadeIn("normal",function(){$(this).fadeTo("normal",0.8);});
	},function(){
		$(this).prev().fadeOut("normal",function(){$(this).remove();});
	});
}

var titleTipTimer=0;
function cancleTitleTipTimer(){
	clearTimeout(titleTipTimer);
	titleTipTimer=0;
}
function titleTipClose(){
	cancleTitleTipTimer();
	titleTipTimer=setTimeout(function(){$("div.full-title",$("#articleMale,#articleFemale")).remove();},400);
}
document.onclick = titleTipClose;

//load Calendar
function loadCalendar(sDate){
	var $objContent = $("#divCalendarAjax");
	$objContent.children("div.cal-panel").attr("class","cal-panel year"+sDate.split("-")[0]+" month"+sDate.split("-")[1]);
	$objContent.children("div.cal-panel").children("div.cal-detail").html("<div style=\"padding:42px 0 0 0\">"+iconAjaxLoadingBig+"</div>");
	pasIntTimeOut();
	$.ajax({
		type: 'GET', dataType: 'html', timeout: intAjaxTimeOut,
		url: str00+"themes/"+strThemeName+"/plugin/calendar.asp?date="+sDate,
		error: function(){
			$objContent.children("div.cal-panel").children("div.cal-detail").html("<p style=\"text-align:center;padding:62px 0 0 0;\"><a href=\"javascript:void(0)\" onclick=\"loadCalendar('"+sDate+"')\">"+langErrorThenRetry+"</a></p>");
		},
		success: function(data){
			$objContent.html(data);
		}
	});
}

//Search Function
var findHintTimer=0;
function doSearch(){
	$("#divSearchPanel>form:first").submit();
}
function startFindHint(sWords,event){
	var ev = null;
	var n = -1;
	var i = 0;
	if (window.event){ev = window.event;}else{ev = event;}
	var $obj = $("#searchHint>ul>li").each(function(){
		if ($(this).attr("class")=="hint-sel"){
			n = i;
		}
		i++;
	});
	if(ev !=null){
		switch(ev.keyCode){
			case 13: //enter
				if ($("#searchHint>ul>li.hint-sel").size()>0){
					window.location.href=$("#searchHint>ul>li.hint-sel").children("a").attr("href");
					return false;
				}
				break;
			case 38: //up
				$("#searchHint>ul>li").eq(n).removeAttr("class");
				n=n-1;
				if (n<=-1){n=i-1;}
				if (n>=0){$("#searchHint>ul>li").eq(n).attr("class","hint-sel");}
				return false;
				break;
			case 40: //down
				$("#searchHint>ul>li").eq(n).removeAttr("class");
				n++;
				if (n>=i){n=0;}
				if (n<=i){$("#searchHint>ul>li").eq(n).attr("class","hint-sel");}
				return false;
				break;
			default :
				cancleFindHint();
				findHintTimer=setTimeout(function(){findHint(sWords);},300);
				break;
		}
	}

}
function cancleFindHint(){
	clearTimeout(findHintTimer);
	findHintTimer=0;
}
function hideHint(){
	cancleFindHint();
	setTimeout(function(){$("#searchHint").hide();},200);
}
function findHint(sWords){
	var $objContent = $("#searchHint");
	//if (sWords==""){
	//	cancleFindHint();
	//	setTimeout(function(){$objContent.hide();},200);
	//}else{
		pasIntTimeOut();
		$.ajax({
			type: 'GET', dataType: 'html', timeout: parseInt(intAjaxTimeOut/4), data:"inpWords="+encodeURIComponent(sWords),
			url: str00+"themes/"+strThemeName+"/plugin/hint.asp",
			success: function(data){
				$objContent.html(data);
				if (data!==""){
					$objContent.show();
				}else{
					$objContent.hide();
				}
			}
		});
		/*
		$.get(str00+"themes/"+strThemeName+"/plugin/hint.asp",{"inpWords":encodeURIComponent(sWords)},function(data){
			$objContent.html(data);
			if (data!==""){
				$objContent.show();
			}else{
				$objContent.hide();
			}
		});
		*/
	//}
}


//=============================== JQ Fixed ===============================
// Go Top
function goTop(){
	$("body:first").append("<div id=\"GoTop\"><strong><span onclick=\"window.scrollTo(0,0);\">"+langGoTop+"</span></strong></div>");
}

// Blockquote Image And External Link
function blockquote(){
	$("div.article-content blockquote").each(function(){
		var blockquote=$(this);
		var blockquoteValue=blockquote.html();
		if (blockquoteValue==""){blockquoteValue="<p>\u0026nbsp;</p>";}
		if (blockquoteValue.indexOf("<p>")==-1){blockquoteValue="<p>"+blockquoteValue+"</p>"}
		blockquote.addClass("quote-jq");
		blockquote.html("<div class=\"quote-begin\"></div>"+blockquoteValue+"<div class=\"quote-end\"></div>");
	});
}
function imageSize(){
	var imgWidthStd=$("#pageContent div.article-content:first").width()-imgPadding;
	var imgWidthTmp=imgWidthStd;
	$("#pageContent div.article-multi div.article-content img").each(function(){
		if ($(this).parent("p").size()==1){imgWidthTmp=$(this).parent("p").width()-imgPadding;}else{imgWidthTmp=imgWidthStd;}
		if(!IE6 && !IE7){imgWidthMulti=imgWidthTmp;}
		if (couple_resizeImage(this,imgWidthMulti)==true){
			if ($(this).parents("a").size()==0){
				$(this).css("cursor","pointer").attr("title",langClickIntoPost).click(function(){
					window.location.href=$(this).parents("div.article-multi:first").find("h2.article-title>a").attr("href");
				});
			}
		}
	});
	$("#pageContent div.article-single div.article-content img").each(function(){
		if ($(this).parent("p").size()==1){imgWidthTmp=$(this).parent("p").width()-imgPadding;}else{imgWidthTmp=imgWidthStd;}
		if(!IE6 && !IE7){imgWidthSingle=imgWidthTmp;}
		couple_resizeImage(this,imgWidthSingle);
	});
	$("#pageContent div.article-content img[@src*=/image/face/]").css("border","none").css("background","none").css("padding","0");
}
function externalLink(){
	$("div.article-content a[@href*=://]").each(function(){$(this).attr("href").toLowerCase().indexOf(str00.toLowerCase()) == -1?$(this).attr("target","_blank").addClass("external"):null;});
}
function currCate(){
	$("#divCatalog li").each(function(){encodeURIComponent($(this).text()).indexOf(cateMark)!==-1?$(this).addClass("curr-cate"):null;});
}


//height balance
function alignHeight(){
	var $objBridge=$("#articleBridge");
	var $objMale=$("#articleMale");
	var $objFemale=$("#articleFemale");

	var maxHeight;
	maxHeight=$objBridge.height();
	if (maxHeight<$objMale.height()){maxHeight=$objMale.height();}
	if (maxHeight<$objFemale.height()){maxHeight=$objFemale.height();}

	if ($("body")[0].className.indexOf("multi")!==-1){
		$objBridge.height(maxHeight);
		$objMale.height(maxHeight);
		$objFemale.height(maxHeight);
	}else{
		$objBridge.height(maxHeight);
	}

}

var showMngTimer=0;
function showMng(iCmtID){
	if (GetCookie("password")){
		switch (iCmtID){
		case -1:
			$("div.article-content").hover(function(){activeShowMng(this,"atc");},function(){clearShowMng(this,"atc");});
			$("li.msgbody").hover(function(){activeShowMng(this,"msg");},function(){clearShowMng(this,"msg");});
			break;
		case 0:
			$("li.msgbody:last").hover(function(){activeShowMng(this,"msg");},function(){clearShowMng(this,"msg");});
			break;
		default:
			$("#comment"+iCmtID+">li.msgbody").hover(function(){activeShowMng(this,"msg");},function(){clearShowMng(this,"msg");});
			break;
		}
	}
}
function activeShowMng(obj,type){
	if (type=="atc"){
		showMngTimer=0;
		showMngTimer=setTimeout(function(){$(obj).children("div.article-mng").fadeIn("normal");},1000);
	}
	if (type=="msg"){
		showMngTimer=0;
		showMngTimer=setTimeout(function(){$(obj).children("div.msgmng").fadeIn("normal");},1000);
	}
}
function clearShowMng(obj,type) {
	if (type=="atc"){
		clearTimeout(showMngTimer);
		showMngTimer=0;
		$(obj).children("div.article-mng:visible").fadeOut("normal");
	}
	if (type=="msg"){
		clearTimeout(showMngTimer);
		showMngTimer=0;
		$(obj).children("div.msgmng:visible").fadeOut("normal");
	}

}

function commentAlign(){
	var $obj=$("#commentsArea>ul.msg");
	var m=$obj.size();
	var i=0;
	var j=0;

	var pucker=1;
	var sUrl=document.URL;
	if(sUrl.indexOf("#cmt")>0 || sUrl.indexOf("#tb")>0){pucker=0;}
	
	$obj.each(function(){
		if (m>10 && i>1 && i<(m-5) && pucker==1){
			$(this).addClass("msg-else");
			j++;
		}else{
			$(this).show();
		}
		if (i%2==1 && this.className.indexOf("cmt")!==-1){
			$(this).addClass("cmt-alt");
		} else {
			$(this).removeClass("cmt-alt");
		}
		i++;
	});

	if (m>10 && pucker==1){
		$("#commentsArea>ul.msg").eq(1).after("<div id=\"showMoreMsg\"><a href=\"javascript:void(0)\" onclick=\"$('#commentsArea>ul.msg-else').show();$('#showMoreMsg').hide();\">"+langShowMoreComments.replace("%n",j)+"</a></div>");
	}
	
	$("#commentsLoading").hide();
}

function commentAlignAlt(){
	var $obj=$("#commentsArea>ul.cmt");
	var i=0;

	$obj.each(function(){
		if (i%2==1){
			$(this).addClass("cmt-alt");
		} else {
			$(this).removeClass("cmt-alt");
		}
		$(this).find("span.msgcount").html("#"+(i+1));
		i++;
	});

}

function inputFocus(){
	$(".text",$("#divCommentPost")).focus(function(){$(this).addClass("text-focus");}).blur(function(){$(this).removeClass("text-focus");});
	$(".button",$("#divCommentPost")).hover(function(){$(this).addClass("button-hover");},function(){$(this).removeClass("button-hover");});
}

function anchorFixed(){
	var sUrl = window.location.href;
	if (sUrl.indexOf("#")!==-1){
		window.location.hash=sUrl.split("#")[1];
	}
}


//=============================== JQ ReBuild ===============================
function selectArticle(){

	// multi
	if ($("body")[0].className.indexOf("multi")!==-1){

		var nMale=0;
		var nFemale=0;

		$("#articleMale>div.authMale").each(function(){nMale++;});
		$("#articleFemale>div.authFemale").each(function(){nFemale++;});

	}

	articleBalance(nMale,nFemale);

}
//article balance
function articleBalance(nMale,nFemale){


	if ($("body.default").size()>0){
		if (nMale<1 && nFemale<1){
			$("#articleMale").append(articleBalanceNegative.replace("%nameThis%",maleName).replace("%nameOther%",femaleName))
			$("#articleFemale").append(articleBalanceNegative.replace("%nameThis%",femaleName).replace("%nameOther%",maleName))
		}else{
			if (nMale<1){
				$("#articleMale").append(articleBalancePositive.replace("%nameThis%",maleName).replace("%nameOther%",femaleName))
			}
			if (nFemale<1){
				$("#articleFemale").append(articleBalancePositive.replace("%nameThis%",femaleName).replace("%nameOther%",maleName))
			}
		}
	}else{
		if(nMale<1){$("#articleMale").append(articleBalanceNeutral.replace("%nameThis%",maleName).replace("%nameOther%",femaleName))}
		if(nFemale<1){$("#articleFemale").append(articleBalanceNeutral.replace("%nameThis%",femaleName).replace("%nameOther%",maleName))}
	}
	
	selected();

}
function selected(){
	$("#innerCreating").html(langPageCreating);
	$("#creating").fadeOut("slow");
}

//comments
function commentNum(){
	$("body.multi div.article-footer>h6>span.cmtnum").each(function(){
		$(this).text()=="0"?$(this).html(langNoComment):parseInt($(this).text())<2?$(this).html(langOneComment.replace("%n",$(this).text())):parseInt($(this).text())<6?$(this).html(langFewComments.replace("%n",$(this).text())):parseInt($(this).text())<18?$(this).html(langSomeComments.replace("%n",$(this).text())):$(this).html(langMoreComments.replace("%n",$(this).text()));
		$(this).html("<a href=\""+$(this).parent("h6").parent("div.article-footer").parent("div.article-multi").children("div.article-header").children("h2.article-title").children("a").attr("href")+"#comment\">"+$(this).text()+"</a>");
	});
	$("body.single div.article-single-footer>h6>span.cmtnum").each(function(){
		$(this).text()=="0"?$(this).html(langNoComment):parseInt($(this).text())<2?$(this).html(langOneComment.replace("%n",$(this).text())):parseInt($(this).text())<6?$(this).html(langFewComments.replace("%n",$(this).text())):parseInt($(this).text())<18?$(this).html(langSomeComments.replace("%n",$(this).text())):$(this).html(langMoreComments.replace("%n",$(this).text()));
		$(this).html("<a href=\"#comment\">"+$(this).text()+"</a>");
	});
}

//=============================== Rnd Click ===============================
function startRndClick(){
	if ($("body")[0].className.indexOf("single")!==-1 || $("body")[0].className.indexOf("guestbook")!==-1){
		$(window).load(function(){
			rndClick();
		});
	}else{
		$(document).ready(function(){
			rndClick();
		});
	}
}


function rndClick(){
	var intValue;

	var $bridgePanel=$("div.bridge-panel:visible");
	if ($bridgePanel.size()>0){
		intBridgePanelHeight=$bridgePanel.eq(0).height();
	}

	//Previous
	intValue=get0or1();
	switchList('#divPrevious',intValue);

	//hot post
	intValue=get0or1();
	switchList('#divHotPosts',intValue);

	//rnd post
	intValue=get0or1();
	switchList('#divRndPosts',intValue);
}


