/*
--------------------------------------------------
(c)Jana.BZ 2012 All Rights Reserved.
--------------------------------------------------
*/

var EDITOR;

/*
var EDITOR, HTML="";
function fncCreateEditor(_strHTMLData){
	if(EDITOR)return;
	if(typeof _strHTMLData!="undefined")HTML=_strHTMLData;
	var CONFIG={extraPlugins:'bbcode', 
							removePlugins:'bidi, button, dialogadvtab, div, filebrowser, flash, format, forms, horizontalrule, iframe, indent, justify, liststyle, pagebreak, showborders, stylescombo, table, tabletools, templates', 
							disableObjectResizing:true, 
							fontSize_sizes:"30/30%;50/50%;100/100%;120/120%;150/150%;200/200%;300/300%", 
							height:"240px"};
	EDITOR=CKEDITOR.appendTo('editor', CONFIG, HTML);
}
*/

function fncCreateEditor(callback){
	if(EDITOR)return;
	var CONFIG={extraPlugins:'bbcode', 
							removePlugins:'bidi, button, dialogadvtab, div, filebrowser, flash, format, forms, horizontalrule, iframe, indent, justify, liststyle, pagebreak, showborders, stylescombo, table, tabletools, templates', 
							disableObjectResizing:true, 
							fontSize_sizes:"8/100%;9/110%;10/120%;11/130%;12/140%;14/160%;16/180%;20/220%;30/320%;50/520%", 
							height:"240px"};
	EDITOR=CKEDITOR.appendTo('editor', CONFIG, "");
	if(typeof callback=="function")callback();
}

function fncRemoveEditor(){
	if(!EDITOR)return;
	document.getElementById('editorcontents').innerHTML=HTML=EDITOR.getData();
	document.getElementById('contents').style.display='';
	EDITOR.destroy();
	EDITOR=null;
}

function fncLoadImage(OBJ, callback){
	if(OBJ.attr("tagName")=="IMG"){
		var _image=OBJ.attr("src");
	}else{
		var _strFile=OBJ.css("backgroundImage");
		var regEX=/url\("?(.*?)"?\)/;
		var _arrResult=fncQueryString(_strFile, 1, regEX);
		var _image=_arrResult[1];
	}
	var _img=new Image();
	$(_img).load(function(){
		if(typeof callback=="function")callback(0);
	}).error(function(){
		if(typeof callback=="function")callback(1);
	}).attr("src", _image);
}

function fncShowFloat(_strFloatName, _intClose, callback){
	if(_intClose==1){
		$('#float div:first').fadeOut(500, function(){
			$('#float').html('').hide();
			typeof callback=="function"?callback():fncAppendOverlay(1);
		});
	}else{
		//$('#float').css({"z-index":1001});
		fncAppendOverlay();
		if(_strFloatName=="IMG"){
			if(typeof callback=="function")callback();
		}else{
			if($('#float #'+_strFloatName).length==0){
				$('#float').load("include/function.htm #"+_strFloatName, function(response, status, xhr){
					if(status=="error"){
						alert("Error on loading jQuery function!");
					}else{
						var _DIV=$('#float div:first');
						if(_DIV.css("background")!=""||_DIV.get(0).style.backgroundImage!=""){
							fncLoadImage(_DIV, function(e){
								if(e){
									alert(e);
									fncShowFloat(_strFloatName, 1);
								}else{
									if(typeof callback=="function")callback();
								}
							});
						}else{
							if(typeof callback=="function")callback();
						}
					}
					if($('.close a').length>0){
						$('.close a').click(function(){
							fncShowFloat(_strFloatName, 1);
							fncRemoveEditor();
							return false;
						});
					}
				});
			}
		}
	}
}
function fncAppendOverlay(_intClose, callback){
	if(_intClose==1){
		$('#overlay').fadeOut(500, function(){
			if(typeof callback=="function")callback();
		});
	}else{
		var _arrPageSize=fncGetPageSize(), _intHeight, _intTop, _intLeft;
		//$("#overlay").css({"width":"100%", "height":_arrPageSize[1]+"px", "background-color":"#FFF", "position":"absolute", "opacity":"0.8"});
		$("#overlay").css({"width":"100%", "height":_arrPageSize[1]+"px", "background-color":"#333", "position":"absolute", "opacity":"0.5"});
		$("#overlay").find('div').css({"margin":"-16px 0 0 -16px"});
		$("#overlay").fadeIn(500, function(){
			if(typeof callback=="function")callback();
			/*
			$(window).resize(function(){
				if($(window).height()>$('body').height()){
					$('body').css({"overflow":"hidden"});
					_intHeight=$(window).height();
				}else{
					$('body').css({"overflow":"visible"});
					_intHeight=$('body').height();
				}
			});
			*/
			var Fixed=setInterval(function(){
				if($("#overlay").css("display")=="block"){
					if($(window).height()>$('body').height()){
						$('body').css({"overflow":"hidden"});
						_intHeight=$(window).height();
					}else{
						$('body').css({"overflow":"visible"});
						_intHeight=$(document).height();
					}
					_intTop=_arrPageSize[3]/2+$(window).scrollTop();
					_intLeft=_arrPageSize[2]/2+$(window).scrollLeft();
					$("#overlay").find('div').css({"top":_intTop+"px", "left":_intLeft+"px", "margin":"-16px 0 0 -16px"});
					$("#overlay").css({"height":_intHeight+"px"});
				}else{
					clearInterval(Fixed);
				}
			}, 10);
		});
	}
}
function fncGetPageSize(){
	var _arrPageSize=new Array($(document).width(), $(document).height(), $(window).width(), $(window).height());
	return _arrPageSize;
}
function fncQueryString(_strRequest, _intSplit, _regEX){
	var _arrResult=new Array();
	for(var i=1; i<=_intSplit; i++){
		_arrResult[i]=_strRequest.replace(_regEX, "$"+i+"");
	}
	return _arrResult;
}

function fncUploadFiles(_strURL, _arrUploadID, _intIndex, callback){
	var _strElementID;
	if(_intIndex<0||typeof _intIndex=="undefined"){
		_strElementID=_arrUploadID;
	}else{
		_strElementID="upload_"+_arrUploadID[_intIndex];
	}
	$.ajaxFileUpload({
		url:_strURL+"&id="+_arrUploadID[_intIndex], secureuri:false, fileElementId:_strElementID, dataType:"json", 
		complete:function(){
			
		}, 
		success:function(json, status){
			if(json.error!=""){
				alert(json.error);
			}else{
				if(_intIndex>=0){
					_intIndex++;
					if(_intIndex<_arrUploadID.length){
						//_strURL="index.php?site=action&action=doUploadFile&id="+_arrUploadID[_intIndex];
						fncUploadFiles(_strURL, _arrUploadID, _intIndex, callback);
					}else{
						if(typeof callback=="function")callback(json);
					}
				}else{
					if(typeof callback=="function")callback(json);
				}
			}
		}, 
		error:function(json, status, e){
			alert(e);
		}
	});
}

function fncQueryResult(_strAction, _strURL, _strPostInfo, callback){
	$.ajax({
		url:_strURL, type:"POST", dataType:"json", data:{postinfo:_strPostInfo}, 
		complete:function(){
			
		}, 
		success:function(json){
			if(typeof callback=="function")callback(json);
		}, 
		error:function(xhr, desc, e){
			alert(xhr+"/"+desc+"/"+e);
			fncAppendOverlay(1);
		}
	});
}

function fncBlockClick(){
	var FLOAT=$('#float'), OVERLAY=$('#overlay'), _intZIndex;
	if(parseInt(FLOAT.css("z-index"), 10)>parseInt(OVERLAY.css("z-index"), 10)){
		_intZIndex=parseInt(FLOAT.css("z-index"), 10);
		FLOAT.css({"z-index":parseInt(OVERLAY.css("z-index"), 10)});
		OVERLAY.css({"z-index":_intZIndex})
	}else{
		_intZIndex=parseInt(OVERLAY.css("z-index"), 10);
		OVERLAY.css({"z-index":parseInt(FLOAT.css("z-index"), 10)});
		FLOAT.css({"z-index":_intZIndex})
	}
}

function fncReduceLength(_strDataString, _intLength){
	if(_strDataString.length>_intLength)_strDataString=_strDataString.substr(0, _intLength)+"...";
	return _strDataString;
}

function fncCheckArray(_arrElements){
	var _arrElement=new Array();
	if(typeof _arrElements=="object"){
		_arrElement['is_array']=1;
		if(_arrElements.length)_arrElement=_arrElements;
	}else{
		_arrElement['is_array']=0;
		_arrElement[0]=_arrElements;
	}
	return _arrElement;
}

function fncEncode(_arrPostInfo){
	var _arrPost=fncCheckArray(_arrPostInfo);
	for(var i=0; i<_arrPost.length; i++){
		_arrPost[i]=_arrPost[i].replace(/\+/g, "%2B");
		_arrPost[i]=_arrPost[i].replace(/\^/g, "%5E");
		_arrPost[i]=encodeURIComponent(_arrPost[i]);
	}
	if(_arrPost['is_array']==0){
		return _arrPost[0];
	}else{
		return _arrPost;
	}
}

function fncSynchroData(_strData){
	_strData=_strData.replace(/^\n/, '');
	_strData=_strData.replace(/(https?)/g, ' $1');
	_strData=_strData.replace(/(href=")\s(https?)/g, '$1$2');
	_strData=_strData.replace(/(<a.*?>)\s(https?)/g, '$1$2');
	_strData=_strData.replace(/(src=")\s(https?)/g, '$1$2');
	_strData=_strData.replace(/\s(https?:\/\/[\w\d\.-]+(\/[\w\d\.\-\/\?=:&%]*)?)/g, '<a href="$1" target="_blank">$1</a>');
	_strData=_strData.replace(/\r\n/g, '\n');
	_strData=_strData.replace(/\r/g, '\n');
	_strData=_strData.replace(/\n/g, '<br>');
	return _strData;
}
