/*
 * Text Authorization JS 0.1
 * By TACAN (http://www.tacan.nl)
*/ 
jQuery.noConflict();

(function(){
	jQuery.tc_dialog = { isIE : (jQuery.browser.msie && parseInt(jQuery.browser.version.substr(0,1)) < 8),
		show: function(cont, tit, dlgCls, onclose) {
			if (cont == '') cont = '<div id="tc_dlg_lodcn" class="tc_dlg_cn"><img class="tc_dlg_lod" src="' +  jQuery.tc_app.url + 'css/imgs/tc_dlg_loading.gif" alt="Loading" /></div>';
			var bNew = false; var outer = jQuery('#tc_dlg_outer');
			if (outer.size() > 0) { jQuery('#tc_dlg_content').html(cont);				
			} else {						
				jQuery.tc_dialog.create(cont); outer = jQuery('div#tc_dlg_outer').css({'position':'absolute'}).show();
				jQuery('#tc_dlg_overlay').css('opacity', 0.7).show();
				jQuery('#tc_dlg_overlay, #tc_dlg_close').bind('click', jQuery.tc_dialog.close);
				jQuery('#tc_dlg_close').show(); bNew = true;
			}
			var con = jQuery('#tc_dlg_content').show(); con.data('onclose', onclose);
			jQuery.tc_dialog.resize();
			if (bNew) outer.fadeIn('normal', function() { jQuery.tc_dialog.finish(); });			
			return con;
		},
		resize: function() { var outer = jQuery('#tc_dlg_outer'); if (outer.size() == 0) return;
			var con = jQuery('#tc_dlg_content'); jQuery.tc_dialog.center(outer, con.outerWidth(), con.outerHeight());
		},
		center: function(el, width, height) {
			var w = jQuery.tc_dialog.getViewport();
			var itemLeft = (width + 36)	> w[0] ? w[2] : (w[2] + Math.round((w[0] - width - 36) / 2));
			var itemTop	= (height + 50)	> w[1] ? w[3] : (w[3] + Math.round((w[1] - height - 50) / 2));
			var itemOpts = { 'left': itemLeft, 'top': itemTop, 'width': width + 'px', 'height': height + 'px' };
			el.css(itemOpts);
		},
		getViewport: function() {
			return [jQuery(window).width(), jQuery(window).height(), jQuery(document).scrollLeft(), jQuery(document).scrollTop() ];
		},
		finish: function() {
			jQuery(document).keydown(function(e) { if (e.keyCode == 27) { jQuery.tc_dialog.close(); return false; } });
		},
		create: function(cont) {
			var html = '';
			html += '<div id="tc_dlg_overlay"></div><div id="tc_dlg_wrap"><div id="tc_dlg_outer"><div id="tc_dlg_inner"><div id="tc_dlg_close"></div>' +
			'<div id="tc_dlg_content">'  + cont + '</div></div></div></div>';
			jQuery(html).appendTo('body');
			if (jQuery.tc_dialog.isIE) {
				jQuery("#tc_dlg_inner").prepend('<iframe class="tc_dlg_bigIframe" scrolling="no" frameborder="0"></iframe>');
				jQuery('embed, object, select').css('visibility', 'hidden');
			}
		},
		close: function() { var con = jQuery('#tc_dlg_content'); if (con.size() <= 0) return;
			jQuery(document).unbind('keydown'); var callback = null;
			if (con.size() > 0)  callback = con.data('onclose');
			jQuery('#tc_dlg_overlay, #tc_dlg_outer').hide(); jQuery('#tc_dlg_overlay, #tc_dlg_close').unbind();
			jQuery('#tc_dlg_wrap').remove(); jQuery('#tc_dlg_overlay').remove();
			if (jQuery.tc_dialog.isIE) { jQuery('embed, object, select').css('visibility', 'visible'); }
			if (callback)callback(); 
		}
	}
	
	jQuery.tc_app = { url : '/',
		init: function() { jQuery('script').each(function(){ var src=this.src; if (src.indexOf('textauth.js') > 0) jQuery.tc_app.url = src.substr(0, src.indexOf('js/textauth.js'));  });
		jQuery('.tcTxAuSec').click(function(){  jQuery.tc_app.show_login_info(); return false; });
		jQuery('.tcTxAuLgt').click(function(){  jQuery.tc_app.show_logout(); return false; });
		},
		show_dlg: function(tit, con, cls, onclose) {
			jQuery.tc_dialog.show('<div class="tc_dlg_cn ' + cls + '"><div class="tc_dlg_cn_tit">' + tit + '</div><div class="tc_dlg_cn_mn">' + con + '</div></div>', onclose);
		},
		show_login_info: function() {
			var con = '<div id="pgTcLgInf"><a class="pTcBtn" id="pTcBtnInfLg" href="#" title="Inloggen">Inloggen</a><a class="pTcBtn" id="pTcBtnInfNa" href="#" title="Een account aanmaken?">Een account aanmaken?</a></div>';
			jQuery.tc_app.show_dlg('Hoe wil je inloggen?', con, 'pgTcLgInfDlg', null);
			jQuery('#pTcBtnInfLg').click(function(){  jQuery.tc_app.show_login(); return false; });
			jQuery('#pTcBtnInfNa').click(function(){  jQuery.tc_app.show_register_info(); return false; });
		},
		show_login: function() { jQuery.tc_dialog.show('', null);
			jQuery.ajax({ url: jQuery.tc_app.url + 'pages/login.php', cache: false, success: function(html){ jQuery.tc_dialog.show(html, function(){ location.reload(); } ); jQuery.tc_app.handle_login(); }  });			
		},
		handle_login: function() {
			jQuery('#pTcFrmBtnFpw').unbind('click').click(function(){ jQuery.tc_app.show_forgot(); return false; });
			jQuery.tc_app.form_submit('pTcFrmLgFrm', function(){
				jQuery('#pTcBtnLgSucBtn').unbind('click').click(function(){ jQuery.tc_dialog.close(); location.reload(); return false; });
				jQuery.tc_app.handle_login();
			});
		},
		show_register_info: function() {
			var con = '<div id="pgTcRgInf"><a class="pTcBtn" id="pTcBtnRnfAm" href="#" title="Lid FNV MOOI">Lid FNV MOOI</a><a class="pTcBtn" id="pTcBtnRnfNm" href="#" title="Inschrijven voor FNV MOOI">Inschrijven voor FNV MOOI</a></div>';
			jQuery.tc_app.show_dlg('Nieuwe gebruiker', con, 'pgTcRgInfDlg', null);
			jQuery('#pTcBtnRnfAm').click(function(){  jQuery.tc_app.show_member(); return false; });
			jQuery('#pTcBtnRnfNm').click(function(){  jQuery.tc_app.show_register(); return false; });
		},
		show_register: function() { jQuery.tc_dialog.show('', null);
			jQuery.ajax({ url: jQuery.tc_app.url + 'pages/register.php', cache: false, success: function(html){ jQuery.tc_dialog.show(html, null); jQuery.tc_app.handle_register(); }  });
		},
		handle_register: function() {
			jQuery.tc_app.show_tip('#week_work_inp', 'uur'); 
			jQuery('input[name=auto_debut]').unbind('click').click(function(){ jQuery('#auto_debut_chk').val('1'); return true; });
			jQuery('input[name=i_am]').unbind('click').click(function(){ jQuery('#i_am_chk').val('1'); return true; });
			jQuery.tc_app.form_submit('pTcFrmRgFrm', function(){ 
				jQuery('#pTcBtnRgSucBtn').unbind('click').click(function(){ jQuery.tc_dialog.close(); return false; });
				jQuery.tc_app.handle_register();
			} );
		},
		show_member: function() { jQuery.tc_dialog.show('', null);
			jQuery.ajax({ url: jQuery.tc_app.url + 'pages/member.php', cache: false, success: function(html){ jQuery.tc_dialog.show(html, null); jQuery.tc_app.handle_member();  }  });
		},
		handle_member: function() {
			jQuery.tc_app.form_submit('pTcFrmMbFrm',function(){
				jQuery('#pTcBtnMbSucBtn').unbind('click').click(function(){ jQuery.tc_dialog.close(); return false; });
				jQuery.tc_app.handle_member();
			});
		},
		show_forgot: function() {
			jQuery.tc_dialog.show('', null);
			jQuery.ajax({ url: jQuery.tc_app.url + 'pages/forgot.php', cache: false, success: function(html){ jQuery.tc_dialog.show(html, null); jQuery.tc_app.handle_forgot(); }  });
		},
		handle_forgot: function() {
			jQuery.tc_app.form_submit('pTcFrmFgFrm', function(){
				jQuery('#pTcBtnFgSucBtn').unbind('click').click(function(){ jQuery.tc_dialog.close(); return false; });
				jQuery.tc_app.handle_forgot();
			});
		},
		form_submit: function(frmId, onload) { jQuery('#' + frmId).unbind('submit').submit(function(){ var frm = jQuery(this);
			if (jQuery.tc_app.form_valid(frm)) { var url = frm.attr('action'); var data = frm.serialize(); jQuery.tc_dialog.show('', null); 
				jQuery.ajax({ url: url, data: data, cache: false, type: 'POST', success: function(html){ jQuery.tc_dialog.show(html, null); if (onload) onload(); } });
			} jQuery('.pTcFrmFocus', frm).each(function(){ var el = jQuery(this); el.val(el.data('default')); }); return false; });
		},
		form_valid: function(frm) { jQuery('.pTcFrmFocus', frm).each(function(){ var el = jQuery(this); el.data('default', el.val()); el.val(''); });
			var nErr=0;frm.find('*').filter('.pTcFrmInp, .pTcFrmHid').each(function(){if(!isValid(this)){(nErr++);}});if(nErr>0)return false;return true;
			function isValid(obj){ var el =jQuery(obj); var er = jQuery('.err', el.parent('.pTcFrmRw')); var bVald=true; if (el.hasClass('pTcFrmRq') && (!el.val() || el.val() == '')) bVald = false;
			if (el.hasClass('pTcFrmEm') && el.val()){ var reg=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;if(reg.test(el.val())==false)bVald = false;}  
			if (!bVald) { er.addClass('errh'); return false;  } er.removeClass('errh'); return true; }
		},
		show_tip: function(sel, text) { 
			function setTip() { var el = jQuery(this); var val = el.val(); if (!val || val.length == 0 || val == text) { el.val(text);el.addClass('pTcFrmFocus');}} 
			jQuery(sel).focus(function(){ var el = jQuery(this); if (el.val() == text){ el.val(''); el.removeClass('pTcFrmFocus');}}).blur(setTip); 
		},
		show_logout: function() {
			jQuery.ajax({ url: jQuery.tc_app.url + 'pages/logout.php', cache: false, success: function(html){ location.reload(); } });
		}
	}
})(jQuery);

jQuery(document).ready(function(){ jQuery.tc_app.init();  });
