var open = false;
var open_height = "200px"; //height of topbar when opened
var animation_speed = "700";
var tweetmax = 140; //maximum characters for tweet input

//piwik stuff
var suffix = short_text.substr(short_text.lastIndexOf("/")+1,6);
var pkBaseURL = "http://twi.gy/pwk/";
piwik_action_name = suffix;
piwik_idsite = 1;
piwik_url = "http://twi.gy/pwk/piwik.php";
piwik_log(piwik_action_name, piwik_idsite, piwik_url);


$(document).ready(function(){
//------------------------------------------
//------------------------------------------
	var tweeted = false; //flag if this instance tweeted sth successfully
	var gonnaRegister = false;	//flag if registration will occur after tweeting
		
	var closed_height = $("#t").css("height");
	var body_top = $("body").css("padding-top");
	
	var comment_text = short_text;
	if (document.title != "twi.gy | shorten 'n' tweet"){
		comment_text =  document.title+" "+comment_text;
	}
	if (postedby){
		comment_text = "RT @"+postedby+" "+ comment_text;
	}
	
	$("#extension").hide();
	$("#retweet_pane").hide();
	$("#tweets_pane").hide();
	
	$("#credentials").hide();
	$("#comment").val($.trim(comment_text));
	charCalc();
	$("#loading").hide();
	
	$("#remail").parent().parent().hide();
	
	//UX
	//counter
	   function charCalc(){
		   var texts = $("#comment").val();
		   var len = texts.length;
		   $("#comment_count").text(tweetmax - len);
		   
		   if( (tweetmax - len) < 0){
			   $("#tweetit").attr("disabled","disabled");
		   }else{
			   $("#tweetit").removeAttr("disabled","disabled");
		   }
		   
	   }
	   
	   $("#comment").keydown(function(event){
		   charCalc();
		  });
	   
	   $("#comment").keyup(function(event){
		   charCalc();
		  });
	   
	   $("#comment").mouseup(function(event){  
		   charCalc();
	   	  });
		  
	   $("#register").click(function(){
	   		$("#remail").parent().parent().toggle();
		
	   });
	   
	//topbar open, close
	function toggle_pane(pane){
		if (open){
			$("#t").animate({height: closed_height},animation_speed);
			$("body").animate({paddingTop: closed_height},animation_speed);
			$("#extension").hide();
			$(pane).hide();
			open = false;
		}else{
			$("#t").animate({height: open_height},animation_speed);
			$("body").animate({paddingTop: open_height},animation_speed);
			$("#extension").show();
			$(pane).show();
			open = true;
		}
	}
	
	$("#retweet").click(function(){
		if (open && $("#tweets_pane:hidden").length == 0){
			$("#tweets_pane").hide();
			$("#retweet_pane").show();
		}else{
		toggle_pane($("#retweet_pane"));
		}
		return false;
	});	
	
//	$("#tweets").click(function(){
//		if (open && $("#retweet_pane:hidden").length == 0){
//			$("#retweet_pane").hide();
//			$("#tweets_pane").show();
//		}else{
//		toggle_pane($("#tweets_pane"));
//		}
//		return false;
//	});	
	
	
	//functs
	//retweeting ajax
	   function tweetDone(data){
		   $("#tweetit").removeAttr("disabled","disabled");
		   $("#tweetbar").text(data.message);    
		   if (data.pass == true){
			   	tweeted = true;
				if (!gonnaRegister){
					$("#retweet").unbind();
					$("#retweet").css("background","grey");
					$("#retweet_pane").fadeOut("slow",function(){
						toggle_pane($("#retweet_pane"));
					});
				}else{
					registerAjax(); //in case of async,
					//it is called here too
				}
		   } 	   
	   }
	   
	 //registration
	   function regDone(data){
		   if(data.pass){
			  $("#tweetbar").text(data.message);
			  location.reload(true);
		   }else{
			   $("#tweetbar").text(data.message);
		   }
	   }
	   
	    //first level validation 
   function regCheck(){
	   
	   //empty data check
	   var fill = "";
	   
	   switch ("") {
	   	//no need to check these as 
		//they must be valid if tweet is successful
		//and registration done just after tweet is succefful
//	   	case $.trim($("#cusername").val()) :{
//	   		fill = "username.";
//	   		$("#tweetbar").text("Please enter your "+fill);
//	 	   return false;		
//	   	}
//	   	case $.trim($("#cpassword").val()) :{
//	   		fill = "password.";
//	   		$("#tweetbar").text("Please enter your "+fill);
//	 	   return false;		
//	   	}
	   	case $.trim($("#remail").val()) :{
	   		fill = "email.";
	   		$("#tweetbar").text("Please enter your "+fill);
	 	   return false;		
	   	}
	   }
	   
	   //email format check
	   var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	   if (!filter.test($.trim($("#remail").val()))) {
	   		$("#tweetbar").text("Please enter a valid email.");
		 	   return false;	
	   }
	   return true;
   };
     	   
		function registerAjax(){
				$("#tweetbar").text("Registering...");
				//disable as these are already validated by successful tweet
				$("#cusername").attr("disabled", "disabled");
				$("#cpassword").attr("disabled", "disabled");
				$("#register").attr("disabled", "disabled");
				
				$("#comment").val("Tweet done successfully!");
				$("#comment").attr("disabled", "disabled");
				
		  		$("#tweetit").val("Register");
				if (regCheck()) {				
					$.post("actions", {
						act_type: "register",
						username: $("#cusername").val(),
						password: $("#cpassword").val(),
						email: $("#remail").val()
					}, function(data){
						regDone(data);
					}, "json");
				}
		}   
		   
	   $("#tweetit").click(function() {
	       $('#tweetbarcontainer').show();
            $("#tweetbarcontainer").animate({width: "300px"});
		   
		   //not logged in and credential form hidden?
		   if (username.length==0 && $("#credentials").is(":hidden")){
			   $("#tweetbar").text("Enter your Twitter Username & Password");
			   $("#credentials").show();
			   $("#credentials").animate({width: "300px"});
			   return false;
		   }
		   
		   //not talking about the shortened url?
		   if ( !tweeted && ($("#comment").val().indexOf(short_text) == -1 || short_text.length == 0)){
			   $("#tweetbar").text("Tweeting about something else?");
			   return false;
		   }
		   
		   if($("#register").attr("checked")){
		   	gonnaRegister = true;
		   } 
		   
		   if (!tweeted) {
			   	$("#tweetit").attr("disabled", "disabled");
			   	$("#tweetbar").text("Tweeting...");
			   	
			   	$.post("actions", {
			   		act_type: "tweet",
			   		comment: $("#comment").val(),
			   		cusername: $("#cusername").val(),
			   		cpassword: $("#cpassword").val(),
			   		short_text: short_text
			   	}, function(data){
			   		tweetDone(data); //if succ tweeted made true here
			   	}, "json");
		   }
		   
		   //go on with regstr'n if only tweet successful
		   if (tweeted && gonnaRegister)  {	   	
				registerAjax();
		   }
		  	   
		   return false;
		   } 
	   );
	   
//if username, follow by api (actions)
	
//------------------------------------------	
//------------------------------------------
});