// JavaScript Document
$(document).ready(function(){
	
	//show example location
	example_location="Example: 221b Baker Street, London, United Kingdom"
	
	if ($(".home_borderless_input3").val()==''){
		$(".home_borderless_input3").css("color","#999999");
		$(".home_borderless_input3").val(example_location);
	}
	$(".home_borderless_input3").bind("focus", function(){
		if ($(".home_borderless_input3").val()==example_location){
			$(".home_borderless_input3").css("color","#000");
			$(".home_borderless_input3").val("");	
		}else{$(".home_borderless_input3").css("color","#000");}
	});
	$(".home_borderless_input3").bind("keydown", function(){
		$(".home_borderless_input3").css("color","#000");
	});
	$(".home_borderless_input3").bind("blur", function(){
		if ($(".home_borderless_input3").val()==""){
			$(".home_borderless_input3").css("color","#999999");
			$(".home_borderless_input3").val(example_location);	
		}
	});

	//detect resize and adjust correct size
	$(window).bind("resize", checkWindowSize);
	checkWindowSize();
	
	if ($("#tx_username").val()==''){	
		$("#tx_username").focus();
	}else{
		$("#tx_password").focus();
	}
						   
	simple_tooltip(".hastooltip","tooltip");
	$('#dialog').jqm({modal:true,overlay: 70});
	$('#remember').jqm({modal:true,overlay: 70});
	$('#moreinfo_dialog').jqm({modal:true,ajax: 'moreinfo.php', trigger: 'a.moreinfo_dialog_trigger',overlay: 70});
	
});

function checkWindowSize() {
	currentTop=$("#home_bottom").offset()
	currentTop=currentTop.top;
	winHeight  = $(window).height(); 
	
	if (winHeight>(currentTop+51)){
		$("#home_bottom").css("top", (winHeight-51) );
	}
}


function form_submit_remember(){
	errors=0;
	my_onsubmit=document.form1.onsubmit;
	if (document.form1.remember_tx_location.value.length<2){
		alert('Please inform your current location in order to continue');
		errors++;
		}
	if (errors==0){
		document.form1.action='login_process.php?is_remember=1&u=&tx_location='+document.form1.remember_tx_location.value;
		document.form1.onsubmit=function(){validate2();};
		document.form1.submit();
	}else{
		document.form1.onsubmit=my_onsubmit;
		}
}
function register_submit(){
	if (document.form1.bl_terms.checked==false){
		$("#terms_span").show();
		document.form1.submit_form.disabled=false;
	} else {
		$("#submit_form").attr("value","Please wait..."); 
		$("#terms_span").hide();	
		document.form1.submit();			
	}
}
function form_submit(){
	if ($(".home_borderless_input3").val()=='Example: 221b Baker Street, London, United Kingdom'){
		$(".home_borderless_input3").val('');
		}
	
	$("#home_loading_overlay").css("opacity", 0.8);
	$("#home_loading_overlay").show();
	$("#loading_dialog").show();
	
	$("#home_info").hide();
  	$("#home_info_text_content").remove();
	resultado=false;
	errors=0;
	
	if ((errors==0) && (document.form1.tx_username.value.length<1)){
		showmessage='Please inform your Twitter username';
		errors++;
		}
	if ((errors==0) && (document.form1.tx_username.value.indexOf('@')>0)){
		showmessage='Please use your Twitter username (and not your e-mail)';
		errors++;
		}
	if ((errors==0) && (document.form1.tx_password.value.length<1)){
		showmessage='Please inform your Twitter password';
		errors++;
		}
	if ((errors==0) && (document.form1.tx_location.value.length<2)){
		showmessage='Please inform your location';
		errors++;
		}
	
	
	if (errors>0){
		$("#home_loading_overlay").hide();
		$("#loading_dialog").hide();
	 	$("#home_info_text_content").remove();
		$("#home_info_text").prepend('<span id="home_info_text_content">'+showmessage+'</span>&nbsp;&nbsp;')
		$("#home_info").show("fast");
	}
	

	if (errors==0){
		var dataString = 'tx_username='+document.form1.tx_username.value+'&tx_password='+document.form1.tx_password.value;   
		$.ajax({   
		  type: "POST",   
		  timeout:7000,
		  url: "index_checklogin.php",   
		  data: dataString,   
		  async:   false,
		  error: function(msg) {
			  $("#home_loading_overlay").hide()
			  $("#loading_dialog").hide();
			  alert('Error ocurred, please reload the page and try again.') 
 			  },
		  success: function(msg) { 
			if (msg=='0'){
				document.form1.action='login_process.php';
				resultado=true;
			}
			if (msg=='1'){
				$("#home_loading_overlay").hide()
				$("#loading_dialog").hide();
				$('#dialog').jqmShow(); 
				document.form1.action='register_process.php';
				resultado=false;
			}
			if (msg=='2'){
				$("#home_loading_overlay").hide()
				$("#loading_dialog").hide();
			 	$("#home_info_text_content").remove();
				$("#home_info_text").prepend('<span id="home_info_text_content">Twitter login or password incorrect.</span>&nbsp;&nbsp;')
				$("#home_info").show("fast");
				document.form1.tx_password.value='';
				resultado=false;
				
			}
			if (msg=='3'){
				$("#home_loading_overlay").hide()
				$("#loading_dialog").hide();
			 	$("#home_info_text_content").remove();
				$("#home_info_text").prepend('<span id="home_info_text_content">Please use your Twitter username and not your email.</span>&nbsp;&nbsp;')
				$("#home_info").show("fast");
				document.form1.tx_password.value='';
				resultado=false;
			}
			

		} 
		

	}); 

	}
	
if (resultado==false){
	$("#btn_rockroll").removeAttr("disabled"); 
	$("#home_loading_overlay").hide();
	$("#loading_dialog").hide();

}

return resultado;
}




function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
		var my_tooltip = $("#"+name+i);

		$(this).mouseover(function(){
					my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
		}).mousemove(function(kmouse){
				var border_top = $(window).scrollTop();
				var border_right = $(window).width();
				var left_pos;
				var top_pos;
				var offset = 15;
				if(border_right - (offset *2) >= my_tooltip.width() + kmouse.pageX){
					left_pos = kmouse.pageX+offset;
					} else{
					left_pos = border_right-my_tooltip.width()-offset;
					}

				if(border_top + (offset *2)>= kmouse.pageY - my_tooltip.height()){
					top_pos = border_top +offset;
					} else{
					top_pos = kmouse.pageY-my_tooltip.height()-offset;
					}	

				my_tooltip.css({left:left_pos, top:top_pos});
		}).mouseout(function(){
				my_tooltip.css({left:"-9999px"});
		});
	});
}

function eu(){
$('#remember').jqmShow();
}
