/*
--------------------------------------------------
(c)Jana.BZ 2012 All Rights Reserved.
--------------------------------------------------
*/
var intMaxWidth=600, intOpacityStep=0.1, intRollSubMenu=0, intSubMenuThumb=0;
(function($){
	$.fn.jquery=function(){
		if($('.img').length>0){
			$('.img').each(function(){
				$(this).LoadImage();
			});
		}
		$('#login .secdetail div').Login();
		if($('#saysomething').length>0){
			if($('#replylist').attr("value")>0){
				var _strAction="doQueryReply", _strURL="index.php?site=action&action="+_strAction, _strPostInfo="tid="+$('input[name=threadid]').val()+"&page=1";
				fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
					$('#loadingreplies').fadeOut(500, function(){
						var REPLYLIST=$(this).next();
						REPLYLIST.hide().html(json.data);
						$('#replylist').animate({"height":REPLYLIST.height()}, 500, function(){
							REPLYLIST.fadeIn(500);
							$('.posttime').find('div').ReplyFunction();
						});
					});
				});
				$('.pagelist').find('div:first').RedirectPage();
			}else{
				$('#loadingreplies').fadeOut(500, function(){
					$('#replylist > div:last').hide().css({"text-align":"center", "font-size":"11px", "padding-top":"20px", "border-top":"solid 1px #999"}).html('No Reply!').fadeIn(500);
				});
			}
			$('#saysomething').find('a').click(function(){
				$('#saysomething').ShowPostReply();
				return false;
			});
		}
	}
	$.fn.ShowPostReply=function(){
		var _this=$(this);
		fncAppendOverlay();
		$('#saysomething').find('a').fadeOut(500, function(){
			$('#saysomething').animate({"height":$('#postreply').height()+"px"}, 500, function(){
				if(_this.attr("id")=="saysomething"){
					$('input[name=type]').attr("value", "post");
				}else{
					var _parent=_this.parent().parent().parent().parent();
					if(_this.text()=="Edit"){
						var EQ=_parent.prevAll().length;
						var _strComment=_parent.find('.replycomment span').html().replace(/^(<br\s?\/?>)+/ig, "");
						_strComment=_strComment.replace(/<br\s?\/?>/ig, "\n");
						$('#postreply').find('input[name=guestname]').val(_parent.find('.guestname span').text());
						$('input[name=type]').attr("value", "edit");
						$('input[name=type]').attr("rid", _parent.find('input[name=rid]').val());
					}else if(_this.text()=="Quote"){
						var _strComment=_parent.find('.replycomment span').html().replace(/\[quote\].*\[\/quote\]/ig, "");
						_strComment=_strComment.replace(/^(<br\s?\/?>)+/ig, "");
						_strComment=_strComment.replace(/<br\s?\/?>/ig, "\n");
						_strComment="[quote]"+_parent.find('.guestname span').text()+" wrote:\n"+_strComment+"[/quote]\n";
						$('input[name=type]').attr("value", "post");
					}
					$('#postreply').find('textarea').val(_strComment);
				}
				$('#postreply').fadeIn(500, function(){
					$('input[name=guestname]').focus();
					fncAppendOverlay(1);
				});
				$('input[value=Submit]').one("click", function(){
					var _intChecked=0;
					fncAppendOverlay();
					if($('input[name=privatemsg]').attr("checked")==true)_intChecked=1;
					var _strPostInfo="action=Edit&parent="+$('input[name=threadid]').val()+"&guestname="+$('input[name=guestname]').val()+"&email="+$('input[name=email]').val()+"&comment="+$('textarea[name=comment]').val()+"&privatemsg="+_intChecked;
					if($('input[name=type]').val()=="post"){
						var _strAction="doPostReply";
					}else if($('input[name=type]').val()=="edit"){
						var _strAction="doEditReply";
						_strPostInfo+="&rid="+$('input[name=type]').attr("rid");
					}else{
						alert("Undefined function!");
						return false;
					}
					var _strURL="index.php?site=action&action="+_strAction;
					fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
						if(json.code>0){
							alert(json.error);
							fncAppendOverlay(1);
							if(json.code==1){
								$('input[name=guestname]').focus();
							}else if(json.code==2){
								$('textarea[name=comment]').focus();
							}
						}else{
							if($('input[name=type]').val()=="edit"){
								var TEXTFIELD=$('#replylist > div:last').find('div:eq('+EQ+') .replycomment'), GUESTNAME=$('#replylist > div:last').find('div:eq('+EQ+') .guestname span');
								GUESTNAME.fadeOut(500);
								TEXTFIELD.find('span').fadeOut(500);
								TEXTFIELD.css({"overflow":"hidden"});
								TEXTFIELD.animate({"height":"10px"}, 500, function(){
									GUESTNAME.html(json.data.guestname);
									$(this).find('span').html(json.data.comment);
									$(this).animate({"height":$(this).find('span').height()}, 500, function(){
										$(this).find('span').fadeIn(500);
										GUESTNAME.fadeIn(500);
									});
								});
							}else{
								if($('#replylist').attr("value")==0){
									$('#loadingreplies').after('<div></div>');
									$('#replylist > div:last').hide(500, function(){
										$(this).remove();
									});
								}
								$('#replylist').css({"overflow":"visible"}).attr("value", parseInt($('#replylist').attr("value"), 10)+1);
								fncRedirectPage($('.pagelist div').find('span').prevAll().length+1, $('#replylist').attr("value"), 5);
								$('#loadingreplies').next().prepend(json.data);
								$('.posttime:first').find('div').ReplyFunction();
								$('#loadingreplies').next().find('div:first').show(500);
							}
							$('#postreply').fadeOut(500, function(){
								$(this).find('input[type=text]').removeAttr("value");
								$(this).find('input[type=checkbox]').removeAttr("checked");
								$(this).find('textarea').val("");
								$('#saysomething').animate({"height":"30px"}, 500, function(){
									$(this).find('a').fadeIn(500);
									fncAppendOverlay(1);
								});
							});
						}
					});
				});
				$('input[value=Close]').one("click", function(){
					$('#postreply').fadeOut(500, function(){
						$(this).find('input[type=text]').removeAttr("value");
						$(this).find('input[type=checkbox]').removeAttr("checked");
						$(this).find('textarea').val("");
						$('#saysomething').animate({"height":"30px"}, 500, function(){
							$(this).find('a').fadeIn(500);
							fncAppendOverlay(1);
						});
					});
				});
			});
		});
		return this;
	}
	$.fn.RedirectPage=function(){
		$(this).find('a').click(function(){
			var _intPageValue=$(this).text(), REPLYLIST=$('#loadingreplies').next();
			fncAppendOverlay();
			fncRedirectPage(_intPageValue, $('#replylist').attr("value"), 5);
			REPLYLIST.fadeOut(500);
			$('#loadingreplies').fadeIn(500);
			$('#replylist').animate({"height":"60px"}, 500, function(){
				var _strAction="doQueryReply", _strURL="index.php?site=action&action="+_strAction, _strPostInfo="tid="+$('input[name=threadid]').val()+"&page="+_intPageValue;
				fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
					$('#loadingreplies').fadeOut(500, function(){
						REPLYLIST.hide().html(json.data);
						$('#replylist').animate({"height":REPLYLIST.height()}, 500, function(){
							REPLYLIST.fadeIn(500);
							$('.posttime').find('div').ReplyFunction();
							fncAppendOverlay(1);
						});
					});
				});
			});
			return false;
		});
	}
	$.fn.ReplyFunction=function(){
		$(this).find('a').click(function(){
			if($(this).text()=="Quote"||$(this).text()=="Edit"){
				$(this).ShowPostReply();
			}else{
				fncAppendOverlay();
				if($(this).text()=="Hide"||$(this).text()=="Show"||$(this).text()=="Delete"||$(this).text()=="Recover"){
					var _strPostInfo, _strAction, _strURL, _intValue, ACTION, _this=$(this), arrText=new Array();
					switch($(this).text()){
						case "Hide":		_intValue=1;
														ACTION="Hide";
														arrText=Array("Hide", "Show");
						break;
						case "Show":		_intValue=0;
														ACTION="Hide";
														arrText=Array("Hide", "Show");
						break;
						case "Delete":	_intValue=1;
														ACTION="Delete";
														arrText=Array("Delete", "Recover");
						break;
						case "Recover":	_intValue=0;
														ACTION="Delete";
														arrText=Array("Delete", "Recover");
						break;
					}
					_strPostInfo="action="+ACTION+"&rid="+$(this).parent().parent().prev().find('input[name=rid]').val()+"&value="+_intValue;
					_strAction="doEditReply";
					_strURL="index.php?site=action&action="+_strAction;
					fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
						_this.text(arrText[_intValue]);
						fncAppendOverlay(1);
					});
				}else{
					alert("Undefined function!");
				}
			}
			return false;
		});
		return this;
	}
	$.fn.Login=function(){
		if($('#admin').length>0){
			$('#admin a').click(function(){
				if($(this).attr("id")=="postthread"){
					fncShowFloat("frmPostThread", 0, function(){
						fncSubmitThread("post");

						fncCreateEditor();

					});
				}else if($(this).attr("id")=="resetblog"){
					fncAppendOverlay();
					var _strAction="doResetBlogData", _strURL="index.php?site=action&action="+_strAction;
					fncQueryResult(_strAction, _strURL, 0, function(json){
						$('#recentlypost .secdetail div').hide(500, function(){
							$(this).html(json.data.post).show(500, function(){
								$('#recentlyreply .secdetail div').hide(500, function(){
									$(this).html(json.data.reply).show(500, function(){
										alert("Reset blog data has finished!");
										fncAppendOverlay(1);
									});
								});
							});
						});
					});
				}else if($(this).attr("id")=="adminfunction"){
					window.location="?site=admin";
				}
				return false;
			});
		}
		if($('#editthread').length>0){
			$('#editthread').click(function(){
				fncShowFloat("frmPostThread", 0, function(){
					fncSubmitThread("edit");
				});
				return false;
			});
		}
		$(this).find('a').click(function(){
			var FLOAT=$('#float'), _strAction, _strURL, _strPostInfo;
			if($(this).text()=="Log in"){
				$(window).scrollTop(0);
				fncShowFloat("frmLogin", 0, function(){
					var FRM=$('#frmLogin');
					FLOAT.fadeIn(500);
					FRM.find('input[name=username]').get(0).focus();
					FLOAT.find('#submit').click(function(){
						fncBlockClick();
						_strPostInfo="username="+FRM.find('input[name=username]').val()+"&password="+FRM.find('input[name=password]').val()+"&blog=1";
						_strAction="doLogin";
						_strURL="index.php?site=action&action="+_strAction;
						fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
							if(json.code>0){
								alert(json.error);
								fncBlockClick();
							}else{
								if($('input[name=threadid]').length>0)$('.threadinfo_B').prepend('<a href="#" style="float:right;" id="editthread">EDIT</a>');
								$('#login .secdetail div').hide(500, function(){
									$(this).html(json.data.logout).show(500, function(){
										$('#login').after('<div id="admin"></div>');
										$('#login').next('div').hide().html(json.data.admin).show(500);
										fncShowFloat("frmLogin", 1, function(){
											fncBlockClick();
											fncAppendOverlay(1);
										});
										$(this).Login();
									});
								});
							}
						});
						return false;
					});
				});
			}else if($(this).text()=="Log out"){
				fncAppendOverlay();
				_strAction="doLogout";
				_strURL="index.php?site=action&action="+_strAction;
				_strPostInfo="blog=1";
				fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
					if(json.code>0){
						alert(json.error);
					}else{
						$('#login .secdetail div').hide(500, function(){
							$(this).html(json.data.login);
							$(this).show(500, function(){
								$('#admin').hide(500, function(){
									$(this).remove();
									if($('#editthread').length>0)$('#editthread').remove();
									fncAppendOverlay(1);
								});
								$(this).Login();
							});
						});
					}
				});
			}else{
				alert("Undefined function!");
			}
			return false;
		});
	}
	$.fn.LoadImage=function(){
		var _img=$(this).find('img'), _this=$(this);
		if(_this.attr("class")=="thumb")var _intBackgroundSize=240, _intScale=1, _intTop, _intLeft;
		_this.prepend('<img src="images/loading.gif" class="loading" />');
		fncLoadImage(_img, function(_intError){
			if(_intError==0){
				if(typeof _intBackgroundSize=="undefined"){
					if(_img.width()>intMaxWidth)_img.css({"width":intMaxWidth});
					_this.animate({"height":_img.height()}, 500, function(){
						_img.fadeIn(500);
						_this.find('.loading').fadeOut(500, function(){
							_this.find('.loading').remove();
						});
					});
				}else{
					_this.show();
					var _intWidth=_img.width(), _intHeight=_img.height(), _intMarginTop=10, _intMarginLeft=30, _intPadding=5, _intMaxSize=_intBackgroundSize-_intPadding*2;
					if(_intWidth>_intMaxSize||_intHeight>_intMaxSize){
						if(_intWidth>=_intHeight){
							_intScale=Math.floor(_intMaxSize/_intWidth*100)/100;
						}else{
							_intScale=Math.floor(_intMaxSize/_intHeight*100)/100;
						}
					}
					_intWidth*=_intScale, _intHeight*=_intScale;
					_intTop=_intMarginTop+(_intMaxSize-_intHeight)/2+_intPadding;
					_intLeft=_intMarginLeft+(_intMaxSize-_intWidth)/2+_intPadding;
					_this.find('.loading').fadeOut(500, function(){
						_this.find('.loading').remove();
						_img.css({"width":_intWidth, "height":_intHeight, "top":_intTop, "left":_intLeft}).fadeIn(500);
					});
				}
			}else{
				_this.find('.loading').fadeOut(500, function(){
					_this.find('.loading').remove();
					if(_this.attr("class")=="img"){
						_this.html('<div class="f11" style="text-align:center; padding:5px;">Cannot load image '+_img.attr("src")+'</div>');
					}else if(_this.attr("class")=="thumb"){
						_this.html('<div class="f11" style="position:absolute; top:50%; left:50%; width:220px; margin-left:-100px; line-height:150%;">Cannot load image '+_img.attr("src")+'</div>');
						_this.find('div').css({"margin-top":0-(_this.find('div').height())/2+"px"});
					}
				});
			}
		});
		_img.click(function(){
			if(_this.attr("class")=="thumb"){
				// Set preview
			}else{
				var _strElement='<img src="'+$(this).attr("src")+'" />';
				fncShowFloat("IMG", 0, function(){
					$('#float').html('<div class="abscenter" style="padding:10px; background-color:#FFF;"><img src="'+$(_strElement).attr("src")+' "style="display:none;" /></div>').show();
					var _DIV=$('#float').find('div'), _intScrollHeight=$(window).scrollTop(), _intTop, _intLeft;
					if(_DIV.find('img').height()<$(window).height()){
						_intTop=0-parseInt(_DIV.find('img').height(), 10)/2;
					}else{
						_intTop=0-parseInt($(window).height(), 10)/2-$(window).scrollTop()+20;
					}
					_intLeft=0-parseInt(_DIV.find('img').outerWidth(), 10)/2;
					_DIV.css({"margin":(_intTop+_intScrollHeight)+"px 0 0 "+_intLeft+"px"}).find('img').fadeIn(500);
					$('#float').click(function(){
						fncShowFloat("IMG", 1);
					});
				});
			}
		});
		return this;
	}
	$.fn.ThreadFunction=function(){
		var _DIV=$('.choosefile'), _arrCategoryList=new Array(), _strCategoryMenu='<div class="submenu">', _this=$(this);
		var CHECK=0, BG, CATEGORYMENU=$('#categorymenu'), ATTMENU=$('#attachmentmenu');
		_arrCategoryList=$('#categorylist input[type=hidden]');
		_DIV.find('input:first').AddUpload();
		for(var i=0; i<_arrCategoryList.length; i++){
			if($(this).attr("id")=="frmPostThread"){
				if($('input[name=all]').val()>1){
					CHECK=$('input[name=all]').val()%$('#categorylist input[type=hidden]:eq('+i+')').val()==0?1:0;
				}
			}
			BG=CHECK==1?' style="background-position:15px 0%"':"";
			_strCategoryMenu+='<div><span class="checked" check="'+CHECK+'"'+BG+'></span><span class="menutext">'+$(_arrCategoryList[i]).attr("title")+'</span></div>';
		}
		_strCategoryMenu+='</div>';
		CATEGORYMENU.after(_strCategoryMenu);
		CATEGORYMENU.next('.submenu').ShowMenu().find('div').each(function(){
			$(this).MouseFunction();
		});

		//var BODY=$('#editor1').next().find('iframe').contents().find('body');
		var BODY=$('#editor1').next().find('iframe').contents().find('body');

		if($('input[name=attachment]').val()>0){
			ATTMENU.after('<div class="submenu"></div>');
			var _intRollOver=0, _arrLoaded=new Array(0), _arrTop=new Array(0), _arrLeft=new Array(0);
			_this.prepend('<div style="position:absolute; z-index:1200;"></div>');

			var ATT=BODY.find('.attachment');
			var _parent=_this.find('div:first');

			ATTMENU.next('.submenu').AddUpload().ShowMenu().find('div').each(function(){
				var _intSort=$(this).prevAll().length+1;
				_arrLoaded.push(0);
				_parent.append('<div style="z-index:'+(500+_intSort)+';" class="thumb"><img src="images/_attachment/'+$(this).find('span:last').attr("title")+'" class="image" /></div>');
				$(this).MouseFunction();
				$(this).mouseenter(function(){
					_arrTop[_intSort]	=90+$(this).position().top;
					_arrLeft[_intSort]=395;
					_intRollOver=$(this).prevAll().length+1;
					intRollSubMenu=_intRollOver;
				}).mouseleave(function(){
					_intRollOver=intRollSubMenu=0;
				});
			});
			var THUMB=_parent.find('div');
			/*
			ATT.each(function(){
				var _intSort=parseInt($(this).prevAll('.attachment').length, 10), _intTopValue=95, _intLeftValue=235;
				var _intSort=$(this).attr("rel").replace(/attachment_(\d+)/, "$1");
				$(this).mouseenter(function(){
					_arrTop[_intSort]	=$(this).position().top+_intTopValue-BODY.scrollTop();
					_arrLeft[_intSort]=$(this).position().left+_intLeftValue-BODY.scrollLeft();
					_intRollOver=_intSort;
				}).mouseleave(function(){
					_intRollOver=0;
				});
			});
			*/
			$('#editor').find('.cke_toolbar_start').next().find('a').click(function(){
				if($('#editor').find('iframe').length>0){
					/*
					var _strData=$('#editor').find('iframe').contents().find('body').html();
					$('#editor').find('iframe').contents().find('body').html(_strData.replace(/\[attachment_(\d+)\]/, '<img src="images/_attachment/$1.jpg" />'));
					alert(EDITOR.getData());
					*/
				}else{
					
				}
			});
			THUMB.each(function(){
				var _this=$(this), _intRollOverThis=0, _intLoaded=0, _intSort=$(this).prevAll().length;
				$(this).mouseenter(function(){
					_intRollOverThis=1;
					intSubMenuThumb=_intSort+1;
				}).mouseleave(function(){
					_intRollOverThis=intSubMenuThumb=0;
				});
				var fncRollOverThis=setInterval(function(){
					if(_intRollOverThis==1||_intRollOver==_this.prevAll().length+1){
						if(_intLoaded==0){
							_this.LoadImage();
							_intLoaded=1;
						}
						_this.css({"top":_arrTop[_intRollOver], "left":_arrLeft[_intRollOver]});
						_this.show();
					}else{
						_this.hide();
					}
				}, 100);
			});
		}else{
			/*
			$('input[name=plaintext]').change(function(){
				if($(this).attr("checked")==true){
					BODY.html(BODY.text(BODY.html($('#original').html().replace(/<br>/ig, "\n"))).replace(/\n/ig, "<br>"));
				}else{
					var _strData=$('#original').html();
					BODY.html(_strData);
				}
			});
			*/
		}
		$('#addupload').click(function(){
			fncBlockClick();
			if(_DIV.find('input:first').val()==""){
				alert("You have to select a file to upload!");
			}else{
				var _intTotalUpload=$('.choosefile').find('input').length+1, _intSort, regEX=/(C:\\fakepath\\)?(.*)/;
				var _arrResult=fncQueryString(_DIV.find('input:last').val(), 2, regEX), _strFileName=_arrResult[2];
				if(ATTMENU.next().attr("class")=="submenu"){
					
				}else{
					ATTMENU.after('<div class="submenu"></div>');
					ATTMENU.next('.submenu').ShowMenu();
				}
				_intSort=_DIV.find('input:last').attr("name").replace(/\w+_(\d+)/, "$1");
				ATTMENU.next().append('<div attachmentid="'+_intSort+'"><span class="closeicon" check="0"><a href="#"></a></span><span class="menutext" title="'+_strFileName+'">'+(fncReduceLength(_strFileName, 12))+'</span></div>');
				ATTMENU.next().find('div:last').MouseFunction();
				ATTMENU.next().find('div:last a').MouseFunction();

				_DIV.find('div a').fadeOut(500);
				_DIV.find('div span').fadeOut(500, function(){
					$('.choosefile input:last').hide();
					$('.choosefile').find('input:last').after('<input type="file" class="file" name="upload_'+_intTotalUpload+'" id="upload_'+_intTotalUpload+'" enabled="1" />');
					$('.choosefile').find('input:last').AddUpload();
					$(this).html('').show();
					fncBlockClick();
				});
			}
			return false;
		});
		$('#action').find('a').click(function(){
			var ID=$(this).attr("id");
			if(ID=="submit"||ID=="edit"){
				var UPLOAD=$('.choosefile').find('input[type=file]');
				var _strURL="index.php?site=action&action=doUploadFile", _arrUploadID=new Array(), _intIndex=0, _strAction, _strPostInfo;
				fncBlockClick();
				for(var i=1; i<UPLOAD.length; i++){
					if(parseInt($(UPLOAD[(i-1)]).attr("enabled"), 10)==1)_arrUploadID.push(i);
				}
				var _intCategory=$('input[name=all]').val(), _intThreadID=$('input[name=threadid]').val(), _arrChecked=new Array();
				var _arrPost=fncEncode(EDITOR.getData());
				$('input[name=title]').val(fncEncode($('input[name=title]').val()));
				if(ID=="edit"){
					for(var i=1; i<$('#action input[type=hidden]').length; i++){
						_arrChecked.push($('#action input[type=hidden]:eq('+i+')').attr("check"));
					}
				}
				_strPostInfo="tid="+_intThreadID+"&title="+$('input[name=title]').val()+"&comment="+_arrPost+"&category="+_intCategory+"&checked="+_arrChecked;
				_strAction=ID=="submit"?"doPostThread":"doEditThread";

				if(_arrUploadID.length>0){
					fncUploadFiles(_strURL, _arrUploadID, _intIndex, function(json){
						_strURL="index.php?site=action&tid="+_intThreadID+"&action="+_strAction;
						fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
							if(json.code>0){
								alert(json.error);
								fncBlockClick();
							}else{
								window.location="?site=blog&tid="+json.id;
							}
						});
					});
				}else{
					_strURL="index.php?site=action&tid="+_intThreadID+"&action="+_strAction;
					fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
						if(json.code>0){
							alert(json.error);
							fncBlockClick();
						}else{
							window.location="?site=blog&tid="+json.id;
						}
					});
				}
			}else if($(this).attr("id")=="reset"){
				
			}
			return false;
		});
		return this;
	}

	$.fn.AddUpload=function(){
		var _DIV=$('.choosefile'), regEX=/(C:\\fakepath\\)?(.*)/, _strFileName, _this=$(this);
		if(_this.attr("tagName")=="INPUT"){
			_this.change(function(){
				if(_DIV.find('div a').css("display")=="none")_DIV.find('div a').fadeIn(500);
				_arrResult=fncQueryString(_this.val(), 2, regEX);
				_strFileName=_arrResult[2];
				_DIV.find('div span').text(fncReduceLength(_strFileName, 12)).attr("title", _strFileName);
			});
		}else if(_this.attr("tagName")=="DIV"){
			var _strAttachmentMenu="";
			if($('input[name=attachment]').val()>0){
				for(var i=1; i<=$('input[name=attachment]').val(); i++){
					_strAttachmentMenu+='<div attachmentid="'+i+'" exist="1"><span class="closeicon" check="0"><a href="#"></a></span><span class="menutext" title="'+$('#action').find('input[type=hidden]:eq('+i+')').val()+'">'+(fncReduceLength($('#action').find('input[type=hidden]:eq('+i+')').val(), 12))+'</span></div>';
				}
			}
			_this.html(_strAttachmentMenu).find('a').each(function(){
				$(this).MouseFunction();
			});
		}
		return this;
	}
	$.fn.ShowMenu=function(){
		var _parent=$(this).prev(), _this=$(this), _intSumSubmenuHeight=0, _intRollOver=0, _intOpacity=0;

		_parent.mouseenter(function(){
			_intRollOver=1;
		}).mouseleave(function(){
			_intRollOver=0;
		});
		_this.mouseenter(function(){
			_intRollOver=1;
		}).mouseleave(function(){
			_intRollOver=0;
		});
		var fncShowSubmenu=setInterval(function(){
			if(_intRollOver==1){
				_this.show();
				if(_intOpacity<1)_intOpacity+=intOpacityStep;
			}else{
				if(intSubMenuThumb==0&&intRollSubMenu==0){
					_intOpacity>0?_intOpacity-=intOpacityStep:_this.hide();
				}
			}
			_this.css({"opacity":_intOpacity});
		}, 10); // 1000=1 sec
		return this;
	}
	$.fn.MouseFunction=function(){
		var _this=$(this), CHECKED, _strTagName=_this.attr("tagName"), _parent, _strColor, _intExist=$('#action input[type=hidden]').length-1, _intSort=$(this).prevAll().length;
		if(_strTagName=="A"){
			var _strPosition;
			_parent=$(this).parent().next(), CHECKED=$(this).parent();
			_this.mouseenter(function(){
				if(_parent.css("text-decoration")=="none")_strPosition="15px 0";
			}).mouseleave(function(){
				_strPosition=_parent.css("text-decoration")=="line-through"?"15px 0":"0 0";
			});
		}else{
			var _intCheckedValue=1, _arrList, _intRollOver=0, _strClassName=_this.find('span').attr("class");
			_parent=$(this).parent().prev();
			if(_strClassName=="checked")CHECKED=_this.find('.checked');
			_this.mouseenter(function(){
				_intRollOver=1;
			}).mouseleave(function(){
				_intRollOver=0;
			});
		}
		var fncShowSubmenu=setInterval(function(){
			if(intSubMenuThumb==_intSort+1){
				_strColor="#BBB";
			}else{
				_strColor=_intRollOver==1?"#BBB":"#FFF";
			}
			_this.css({"background-color":_strColor, "background-position":_strPosition});
		}, 1); // 1000=1 sec
		_this.click(function(){
			var _strPosition;
			if(_strTagName=="A"){
				var _intEQ=_this.parent().parent().prevAll().length-_intExist;
				if(_parent.css("text-decoration")=="line-through"){
					_parent.css({"text-decoration":"none", "color":"#333"});
					$('.choosefile').find('input[type=file]:eq('+_intEQ+')').attr("enabled", "1");
					_this.css({"background-position":"0 0"});
				}else{
					_parent.css({"text-decoration":"line-through", "color":"#BBB"});
					$('.choosefile').find('input[type=file]:eq('+_intEQ+')').attr("enabled", "0");
					_this.css({"background-position":"15px 0"});
				}
				CHECKED.attr("check", Math.abs(parseInt(CHECKED.attr("check"), 10)-1));
				$('#action input[type=hidden]:eq('+(_intEQ+1)+')').attr("check", CHECKED.attr("check"));
				return false;
			}else{
				if(_strClassName=="checked"){
					_strPosition=CHECKED.attr("check")==0?"15px 0%":"0% 0%";
					CHECKED.attr("check", Math.abs(CHECKED.attr("check")-1)).css({"background-position":_strPosition});
					_arrList=$('#categorylist input[type=hidden]');
					for(var i=0; i<_arrList.length; i++){
						if(_parent.next().find('.checked:eq('+i+')').attr("check")==1)_intCheckedValue*=$(_arrList[i]).val();
					}
					$('input[name=all]').val(_intCheckedValue);
				}else{
					var _strInsertText, FILE=$('#action').find('input[type=hidden]:eq('+$(this).prevAll().length+')');
					_strInsertText=$(this).attr("exist")==1?"attachment":"insert";
					if(document.all){ // IE
						alert($('#editor1').next().find('iframe').length);
						if($('#editor1').next().find('iframe').length>0){
							
						}else{
							var _TEXTAREA=$('#editor1').next().find('iframe').get(0).contentWindow.document.selection.createRange();
							_TEXTAREA.pasteHTML("["+_strInsertText+"_"+$(this).attr("attachmentid")+"]");
							_TEXTAREA.collapse(false);
							_TEXTAREA.select();
						}
					}else{ // FF, Opera, Safari, Chrome
						if($('#editor1').next().find('iframe').length>0){
							$('#editor1').next().find('iframe').get(0).contentWindow.document.execCommand('insertHTML', false, "["+_strInsertText+"_"+$(this).attr("attachmentid")+"]");
						}else{
							var TEXTAREA=$('#editor1').next().find('textarea').get(0);
							var _intStart=TEXTAREA.selectionStart;
							var _intEnd=TEXTAREA.selectionEnd;
							TEXTAREA.value=TEXTAREA.value.substring(0, _intStart)+"["+_strInsertText+"_"+$(this).attr("attachmentid")+"]"+TEXTAREA.value.substring(_intEnd, TEXTAREA.value.length);
						}
					}
				}
			}
		});
		return this;
	}
})(jQuery);

function fncSubmitThread(_strType){
	var FRM=$('#frmPostThread'), FLOAT=$('#float');
	if(_strType=="edit"){
		var _strAction="", _strURL="?site=action&action=doQueryPost", _strPostInfo="tid="+$('input[name=threadid]').val(), _strData, _img;
		fncQueryResult(_strAction, _strURL, _strPostInfo, function(json){
			if(json.code>0){
				alert(json.error);
			}else{
				FRM.find('#action a:first').attr("id", "edit");
				FRM.find('input[name=title]').val(json.data.title);
				//FRM.find('#commentpreview iframe').contents().find('body').html(fncSynchroData(json.data.comment));

				fncCreateEditor(function(){
					CKEDITOR.instances.editor1.setData(json.data.comment);
				});

				$('input[name=all]').val(json.data.category);
				FRM.find('input[name=attachment]').val(json.data.file[0]);
				for(var i=1; i<=json.data.file[0]; i++){
					FRM.find('#action').find('input[type=hidden]:last').after('<input type="hidden" name="attachment_'+i+'" value="'+json.data.file[i]+'" check="0" />');
				}
				$(window).scrollTop(0);
				FRM.append(json.data.imgs);
				_strData=EDITOR.getData();
				_img=FRM.find('#imgs img');
				FLOAT.fadeIn(500, function(){
					//_strData=fncConvertAttachment(_strData, _img);
					FRM.ThreadFunction();
				});
			}
		});
	}else{
		$(window).scrollTop(0);
		FLOAT.fadeIn(500, function(){
			FRM.ThreadFunction();
		});
	}
}

/*
function fncConvertAttachment(_strData, _img){
	for(var i=1; i<=_img.length; i++){
		_strData=_strData.replace("["+$(_img[(i-1)]).attr("rel")+"]", '<span style="background-color:#FE6; color:#666;" class="attachment" rel="'+$(_img[(i-1)]).attr("rel")+'">['+$(_img[(i-1)]).attr("rel")+']</span> ');
	}
	return _strData;
}
*/

function fncRedirectPage(_intPageValue, _intTotalItems, _intPageSize){
	var _strHTML="", _intTotalPages=Math.ceil(_intTotalItems/_intPageSize);
	for(var i=1; i<=_intTotalPages; i++){
		if(i==_intPageValue){
			_strHTML+='<span>'+i+'</span>';
		}else{
			_strHTML+='<a href="#">'+i+'</a>';
		}
	}
	$('.pagelist').find('div:first').html(_strHTML).RedirectPage();
}
/* Jana.BZ 2012 */
