$(document).ready(function(){

	var memberForm = '<div id="DialogPopupTitleBG"></div>'+"\n";
	memberForm += '<div style="width:395px;">'+"\n";
	memberForm += '<div class="textAC szW13 szMAuto szP2_2">To access a Limited Chat Hosts Only area of www.JustWithMe.Com you have to login first. If you are not a registered chat host yet please <a href="index.php?loc1=registerHost">Become a Chat Host</a> now. Thank you!</div>';
	memberForm += '<div class="szW11 szMAuto"><form name="hostLoginForm" method="post" action="" class="szW11">'+"\n";
	memberForm += '<div class="loginError textColR szMAuto textAC szP2_2"></div>';
	memberForm += '<div class="szW16"><span class="flL textAR fontWBold szW8">Host Account Name:</span><input type="text" name="accountName" class="flR szWCustom1 szM1_2" /></div>'+"\n";
	memberForm += '<div class="clear"></div>'+"\n";
	memberForm += '<div class="szW16"><span class="flL textAR fontWBold  szW8">Password:</span><input type="password" name="accountPassword" class="flR szWCustom1 szM1_2" /></div>'+"\n";
	memberForm += '<div class="clear"></div>'+"\n";
	memberForm += '<div class="dispI flL szM4_2 szM3_1 szP3L_1"><input type="submit" name="login" value="" id="hostLoginSubmit" class="szM2_1 b78x23 butLogin_s" /><input type="button" name="login" value="" id="DialogPopupCancle" href="javascript:void(0);" class="szM2_1 b78x23 butCancle" /></div>'+"\n";
	memberForm += '<div class="clear"></div>'+"\n";
	memberForm += '</form>'+"\n";
	memberForm += '</div>'+"\n";
	memberForm += '</div>'+"\n";
	
	var closeBtnClickCounter = 0;
	var failedLoginAttempts = 0;
	//===============/ Initialize Dialog Popup Settings /==================//
	$('#DialogPopup').PopupInit({
		closeEl: '#DialogPopupClose',
		htmlTitle: '#DialogPopupTitle',
		htmlContainer: '#DialogPopupContainer',
		dragHandler: '#DialogPopupHandler'
	});
	
	countRet = function(){
		closeBtnClickCounter++;
		if ( closeBtnClickCounter == 4 ) window.location = 'index.php?loc1=loginFailed';
	}
	
	$('a[href="index.php?loc1=boardHost"],a[href="index.php?reg=new&loc1=boardHost"]').click(function(){
	
		$('#DialogPopup').PopupUpdateTitle('Chat Hosts Login Form');
		$('#DialogPopup').PopupUpdateHtml(memberForm);
		$('#DialogPopup').PopupShow(0,1);
		
		// Redirection for freaks :P
		$('#DialogPopupClose').click(function(){ countRet(); });
		$('#DialogPopupCancle').click(function(){ countRet(); $('#DialogPopup').PopupHide(); });
		
		$('#hostLoginSubmit').click(function(){
			fields_to_send = $('form[name="hostLoginForm"]').serialize();
			$.ajax({
				type: "POST",
		  	 	url: "ajax.php",
		   		data: fields_to_send,
		   		success: function(result){
		     		if (result == 'logged') window.location.reload(true);
		     		else {
		     			failedLoginAttempts++;
		     			if ( failedLoginAttempts == 3) window.location = 'index.php?loc1=loginFailed';
		     			$('.loginError').fadeOut('fast', function(){
		     				$(this).html('Invalid login name or password').fadeIn('fast');
		     			});
		  		 	}
		  		 }
			});
			return false;
		});
		return false;
	});
	
});