/*
 JS form popup support (thickbox style)
 GPL Intime It 2010
 --------------------
 dependencies 
   loginform.css style to be loaded
   jquery
   jquery.form
 
*/


// base object with default values and methods
var login_hndl = {
  // config vars
  loading_url:jsrelpath+'images/tbf_loading.gif',
  closebut_url:jsrelpath+'images/close_win.png',
  loading_img: new Image(),
  getfrm_url:jsrelpath+'script/login_frm.php',
  caption:'',
  login_height:550, login_width:830,  // default size
  login_maxheight:640,login_maxwidth:830,
  
  init: function () {
    login_hndl.loading_img.src = login_hndl.loading_url; // preload
    login_hndl.login_maxheight = getmyHeight();
    login_hndl.login_maxwidth = getmyWidth();
  },
  
  dilogin: function (u,p) {
     login_hndl.logincmd('dilogin',{username:u,userpass:p});
  },
  
  logincmd: function (cmd,params) {

     params.cmd = cmd;
     
     errhnd.logobj(params,'logincmd.params');

     if (params.cmd=='dilogin') {
       if (typeof params.username == "undefined") params.username = '';
       if (typeof params.userpass == "undefined") params.userpass = '';
       errhnd.log('dilogin(u='+params.username+', p='+((params.userpass=='')?'':'***')+')');
        
       $.ajax({type:'POST', url:login_hndl.getfrm_url, dataType:'json', data:params,
         success:	function(data){
           if (data.update) {
             eval(data.updateexp);
           }
           if (data.refresh) {
             location.reload(true);
           }
           if (data.gotologin) {
             login_hndl.loginbox(params);
           }
         }
       }); // end ajax
       return;
     }
     
     if (cmd=='login') {
     
       login_hndl.loginbox(params);
     
//     } else if (cmd=='elmnew') {
//       login_hndl.elmnew(params);
       
     } else {
     
      if (cmd=='logout') {
         $rep = confirm('Er du sikker p\u00E5 at du vil logge ud fra RIKs hjemmeside?');
         if (!$rep) return;
      }
     
       $.ajax({type:'POST', url:login_hndl.getfrm_url, dataType:'json', data:params,
           success:	function(data){
             if (data.update) {
               eval(data.updateexp);
             }
             if (data.refresh) {
               location.reload(true);
             }
             if (data.close) {
                login_hndl.remove();
             }
            
           }
       }); // end ajax
     }
  }, // end function logincmd
  

  loginbox: function (params) {
  
  	    if (typeof params == "undefined") {errhnd.error(33400,'loginform:params undefined');}
  	    
  	    login_hndl.caption = 'RIK bruger login';
  	    params.height = 420;  params.width = 380;
  	    
		login_hndl.show_win(params);
		
        $("#TB_ajaxContent").hide();
        params.cmd = 'getform';

     errhnd.logobj(params,'loginbox.params (bef getform)');

        $.ajax({type:'POST', url:login_hndl.getfrm_url, dataType:'json', data:params,
             success:	function(data){
                errhnd.logobj(data,'formdata');
 			     $("#TB_ajaxContent").html(data.formdata);
  			     login_hndl.position();
                 $("#TB_ajaxContent").show();
                 
 			     
 				 $("#TB_load").remove();
				 $("#TB_window").css({display:"block"});
				 
 	             params.cmd = 'loginform'; delete params.username; delete params.userpass;
  	             
     errhnd.logobj(params,'loginbox.params (bef loginform)');
     
				 $("#loginform").ajaxForm({data:params,url:login_hndl.getfrm_url, dataType:'json', 

                   beforeSubmit: function(a) {
                   }, // end beforeSubmit

                   success: function (responseText, statusText, xhr, $form) {
                     errhnd.logobj(responseText,'responseText');
                     if (responseText.update) {
                         eval(responseText.updateexp);
                     }
                     if (responseText.refreshmes) {
           			     $("#loginformmes").html(responseText.loginformmes).hide().show(1);
                     }
                     if (responseText.refresh) {
                        location.reload(true);
                     }
                     if (responseText.close) {
                        login_hndl.remove();
                     }
                     if (responseText.gotonuser) {
                        login_hndl.nuserbox({});
                     }
                   } // end success
				 }); // end ajaxForm
				 
 			  } 
		}); // end ajax
  }, // end function loginbox


  nuserbox: function (params) {
  
  	    if (typeof params == "undefined") {errhnd.error(33400,'nuserform:params undefined');}
  	    
  	    login_hndl.caption = 'RIK bruger konto oprettelse';
  	    params.height = 560;  params.width = 580;
  	    
		login_hndl.show_win(params);
		
        $("#TB_ajaxContent").hide();
        params.cmd = 'getnuserform';

     errhnd.logobj(params,'nuserbox.params (bef getnuserform)');

        $.ajax({type:'POST', url:login_hndl.getfrm_url, dataType:'json', data:params,
             success:	function(data){
                errhnd.logobj(data,'formdata');
 			     $("#TB_ajaxContent").html(data.formdata);
  			     login_hndl.position();
                 $("#TB_ajaxContent").show();
                 
 			     
 				 $("#TB_load").remove();
				 $("#TB_window").css({display:"block"});
				 
 	             params.cmd = 'nuserform'; delete params.username; delete params.userpass;
  	             
     errhnd.logobj(params,'nuserbox.params (bef nuserform)');
     
				 $("#nuserform").ajaxForm({data:params,url:login_hndl.getfrm_url, dataType:'json', 
				 
                   beforeSubmit: function(a) {
                   }, // end beforeSubmit

                   success: function (responseText, statusText, xhr, $form) {
                     errhnd.logobj(responseText,'responseText');
                     if (responseText.update) {
                         eval(responseText.updateexp);
                     }
                     if (responseText.refreshmes) {
           			     $("#nuserformmes").html(responseText.nuserformmes).hide().show(1);
                     }
                     if (responseText.refresh) {
                        location.reload(true);
                     }
                     if (responseText.gotoconf) {
                     
       			        $("#TB_ajaxContent").html(responseText.confdata);
       			        login_hndl.position();
                        $("#TB_ajaxContent").show();
                     
                     }
                     if (responseText.close) {
                        login_hndl.remove();
                     }
                   } // end success
				 }); // end ajaxForm
				 
 			  } 
		}); // end ajax
  }, // end function nuserbox

  
  show_win: function (params) {
  
       if (typeof params.height !== "undefined") login_hndl.login_height = params.height;
       if (typeof params.width!== "undefined")   login_hndl.login_width= params.width;

       if(login_hndl.login_height>login_hndl.login_maxheight) login_hndl.login_height = login_hndl.login_maxheight-10;
       if(login_hndl.login_width>login_hndl.login_maxwidth) login_hndl.login_width= login_hndl.login_maxwidth-10;
       
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(login_hndl.remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(login_hndl.remove);
			}
		}
		
//		if(tb_detectMacXFF()){
//			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
//		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
//		}
		
		$("body").append("<div id='TB_load'><img src='"+login_hndl.loading_img.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader
		
		ajaxContentW = login_hndl.login_width - 30;
		ajaxContentH = login_hndl.login_height - 45;
			
		if(0 /*url.indexOf('TB_iframe') != -1*/){// either iframe or ajax window		

/*				urlNoQuery = url.split('TB_');
					$("#TB_iframeContent").remove();
					if(params['modal'] != "true"){//iframe no modal
						$("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a> or Esc Key</div></div><iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;' > </iframe>");
					}else{//iframe modal
					$("#TB_overlay").unbind();
						$("#TB_window").append("<iframe frameborder='0' hspace='0' src='"+urlNoQuery[0]+"' id='TB_iframeContent' name='TB_iframeContent"+Math.round(Math.random()*1000)+"' onload='tb_showIframe()' style='width:"+(ajaxContentW + 29)+"px;height:"+(ajaxContentH + 17)+"px;'> </iframe>");
					}
*/			
 		} else {// not an iframe, ajax
			        
		  if($("#TB_window").css("display") != "block"){
		    if(params['modal'] != "true"){//ajax no modal
		      $("#TB_window").append("<div id='TB_title'><div id='TB_ajaxWindowTitle'>"+login_hndl.caption+"</div><div id='TB_closeAjaxWindow'><a href='#' id='TB_closeWindowButton'><img width='16px' style='border:0' src='"+login_hndl.closebut_url+"'></a></div></div><div id='TB_ajaxContent' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px'></div>");
   		    }else{//ajax modal
			  $("#TB_overlay").unbind();
			  $("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:"+ajaxContentW+"px;height:"+ajaxContentH+"px;'></div>");	
  	        }
		  } else {//this means the window is already up, we are just loading new content via ajax
		  	  $("#TB_ajaxContent")[0].style.width = ajaxContentW +"px";
			  $("#TB_ajaxContent")[0].style.height = ajaxContentH +"px";
			  $("#TB_ajaxContent")[0].scrollTop = 0;
			  $("#TB_ajaxWindowTitle").html(login_hndl.caption);
		  }
		} // endif iframe or ajax
					
		$("#TB_closeWindowButton").click(login_hndl.remove);
  
  },// end function show_win

  

  position: function () {
     $("#TB_window").css({marginLeft: '-' + parseInt((login_hndl.login_width / 2),10) + 'px', width: login_hndl.login_width + 'px'});
//	 if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((login_hndl.login_height / 2),10) + 'px'});
//     }
  }, // end function position
  
  
  remove: function () {


	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
//	document.onkeydown = "";
//	document.onkeyup = "";
	return false;
  } // end function remove
  
} // end var login_hndl


	  


