$(document).ready(function () {
    //toggleLoading();
	//afterLoading();
});
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function afterLoading(){
    $.each(jQuery.browser, function(i, val) {
	  if(i=='msie' && val){
		  try { document.execCommand("BackgroundImageCache", false, true);} catch(err) {}
	  }
    });

}
function ej(json,msg,j){
	try{
		return eval("("+json+")");
	}
	catch(e){
		if(msg){
			if(j){
				alert(msg+":"+json);
			}else{
				alert(msg);
			}
		}
		return null;
	}
}

function toggleLoading(){
	var loadObj = $('#loading');
	if(loadObj.is(":visible"))
		loadObj.fadeOut(2000);
	else {
		moveToHorizontalCenter(loadObj);
		loadObj.fadeIn(0);
	}
}
function toggleDialogLoading(){
	var loadObj = $('#dialogLoading');
	if(loadObj.is(":visible"))
		loadObj.fadeOut(3000);
	else {
		//NNIDlg.veilShow(true);
		loadObj.center();
		loadObj.fadeIn(0);
	}
}
function toggleBlockLoading( id ) {
	var obj = $('#'+id);
	var loadobj = $('#block_loading_'+id);
	//alert(loadobj.attr("id")+","+obj.children('#block_loading_'+id));
	if( loadobj.length==0 )
		obj.append('<div id="block_loading_'+id+'"><img src="/images/loading.gif" /></div>');
	else
		loadobj.hide();
}
function moveToHorizontalCenter(elm){
	elm.center({
		vertical: false // only vertical
	});
}
function moveToCenter(elm){
	elm.center({
		vertical: true // only vertical
	});
}
function insertReturnMsg(id, msgId, isOk){
  var className = isOk ? 'okmsg' : 'errormsg';
  $('#'+id).html(errorMsgSet(msgId)).removeAttr('class').attr({'class':className});
}

function clearErrorMsg(id){
  $('#'+id).empty().removeAttr('class');
}

function loginFormSubmit(){
    toggleLoading();
    $.post("/action/login.jsp", $("#loginForm").serialize(),
    function(data){
      rsobj = ej($.trim(data));
      if(rsobj.ok){
		if( rsobj.auth )
		  window.location = rsobj.next ? rsobj.next : '/index.jsp';
		else
		  window.location = '/reauth.jsp?token='+rsobj.token;
      }
      else{
		if( rsobj.res )
		  alert(errorMsgSet(rsobj.res));
		else
          alert(errorMsgSet('205'));
      }
    });
	toggleLoading();
}

function loginDialogFormSubmit(){
    $.post("/action/login.jsp", $("#loginDialogForm").serialize(),
    function(data){
      rsobj = ej($.trim(data));
      if(rsobj.ok){
		if( rsobj.auth )
		  parent.window.location = ( rsobj.next ? rsobj.next : '/index.jsp' ) ;
		else
		  parent.window.location = '/reauth.jsp?token='+rsobj.token;
      }
      else{
		insertReturnMsg('msg-login', ( rsobj.res ? rsobj.res : '205' ), ( rsobj.isOK ? rsobj.isOK : false ) );
      }
    });
}

function checkLogin(){
	var args = arguments;
	if( isUserLogined ) {
		return true;
	}
	NNIDlg.show('login_dialog', '/login_dialog', args[0], '');
	return false;
}

function formatDate( d ){
	var date = new Date(d);
	var year = date.getFullYear();
	var month = date.getMonth()+1;
	var _date = date.getDate();
	var hours = date.getHours();
	var minu = date.getMinutes();
	var sec = date.getSeconds();
	return year+"-"+(month<10?"0":"")+month+"-"+(_date<10?"0":"")+_date+" "+(hours<10?"0":"")+hours+":"+(minu<10?"0":"")+minu+":"+(sec<10?"0":"")+sec;
}

function alertMsg(id){
	if(id && id!='')
	  alert(errorMsgSet(id));
}


function startUpload(){
	toggleDialogLoading();
    return true;
}

function stopUpload(rmsg,next){
  var result = '';
  //console.log(rmsg);
  //alert(rmsg);
  toggleDialogLoading();
  if(next && next!=null && next!='')  MM_goToURL('window',next);
  return true;
}

function veilShow(ifID, src){
	var id = "dlgVeil";
	var veilZ = 90; 
	$('body').prepend('<div id="'+id+'" class="dlgVeil">&nbsp;</div>');
	var veil = $('#'+id);
	veil.css("z-index", veilZ).show();
	veilSetSize(id);
	/*
	veil.click(function(){
		veil.hide();
	});
	*/
	$('body').prepend('<iframe id="' + ifID  + '" name="' + ifID + '" style="z-index:' + (veilZ+1) + '; position:absolute; left:-10000px;top:-10000px;" width="400" height="125" src="' + src + '" frameborder="0" scrolling="no" allowtransparency="true" class="iframeStyle"></iframe>');
	moveToScreenCenter($('#'+ifID));
	
	$(window).resize(function(){
		veilSetSize(id);
		moveToScreenCenter($('#'+ifID));
	});
	$(window).scroll(function(){
		veilSetSize(id);
		moveToScreenCenter($('#'+ifID));
	});
}
function veilSetSize(id){
	var height = $(window).height() > $(document).height() ? $(window).height() :  $(document).height();
	var width = $(window).width() > $(document).width() ? $(window).width() :  $(document).width();	
	$('#'+id).height(height);
	$('#'+id).width(width);
}
function moveToScreenCenter(elm){
	moveToHorizontalCenter(elm);
	var height = $(window).height();
	var elmHeight = elm.height();
	elm.css("top", (height-elmHeight)/2+$(window).scrollTop()+"px");
	//console.log($(window).height()+","+$(document).height()+","+$(window).scrollTop());
}
function resizeImage(width, height, maxSize){
	  var w = width;
	  var h = height;
	  if (width > maxSize || height > maxSize) {
	    w = width > height ? maxSize : maxSize * width / height;
	    h = width > height ? maxSize * height / width : maxSize;
	  }
	  return [w, h];
	}

String.prototype.getPercentnumber = function(obj){
	var target = /^(\d*(\.\d*)?)%$/.exec(this);
	return result = target ? parseFloat(target[1]) : 0;
};
String.prototype.getPixelnumber = function(obj){
	var target = /^(-?(\d*(\.\d*)?))px$/.exec(this);
	return result = target ? parseFloat(target[1]) : 0;
};