window.dommune = window.dommune || {};
dommune.flag = false;
dommune.animation = true;
dommune.checkLogin = function(status){
  var alink = $('.DommuneTwitterLogin>a').unbind('click');
  if(status){
		this.flag = true;
		alink.attr('href','twitteroauth/clearsessions.php');
		$('img',alink).attr('src','/images/bg_logouttw_off.jpg');
	} else {
		alink.attr('href','#');
		$('img',alink).attr('src','/images/bg_logintw_off.jpg');
		alink.click(function(){
			window.open('twitteroauth/redirect.php','signIn','width=800,height=420,left=' + ((screen.width / 2) - 420) + ',top=' + ((screen.height / 2) - 210) + ',location=0,toolbar=0,menubar=0,scrollbars=0,resizable=1');
			return false;
		})
  }
}
dommune.message = function(msg){
  $('.twinput').val(msg); setTimeout(function(){ 
		$('.twinput').val('');
		dommune.animation = true;
	},10000);
}
dommune.tweet = function(){
  if(this.flag){
		if (this.animation) {
			this.animation = false;
			var tweet = $('.twinput').val();
			var msg = (onair == 'true') ? " <#dommune live at http://www.dommune.com >" : " <#dommune http://www.dommune.com >"
			if (tweet && tweet.length <= max) {
				tweet += msg;
				$.post('twitteroauth/tweet.php', { status: tweet }, function(response){
					if (response) {
						response = $.parseJSON(response);
						if (response.status) {
							dommune.message('Message send successfully!!!!!!!!!!');
						} else {
							dommune.message('Message send failed. Please retry!!!!!!!!!!');
						}
					} else {
						dommune.message('Twitter is Over Capacity!!!!!!!!!!');
					}
				})
			} else {
				dommune.message('Message send failed. Please retry!!!!!!!!!!');
			}
		}
  } else {
    window.open('twitteroauth/redirect.php','signIn','width=800,height=420,left=' + ((screen.width / 2) - 420) + ',top=' + ((screen.height / 2) - 210) + ',location=0,toolbar=0,menubar=0,scrollbars=0,resizable=1');
  }
  return false;
}

$(function(){
	//Login Check 
  $.getJSON('twitteroauth/logingCheck.php',function(response){
    dommune.checkLogin(response.status)
  })
	//Image Rollover
	$('.DommuneTwitterLogin img, input:image').each(function(){
		if(this.src.match('_off')){
			$(this).hover(function(){
				$(this).attr('src',$(this).attr('src').replace('_off','_on'));
			},function(){
				$(this).attr('src',$(this).attr('src').replace('_on','_off'));
			})
		}}
	)
	//INPUT ACTION
	$('.twinput').keyup(function(){
		var c = $('.tweet-count');
		var g = parseInt($(this).val().length,10)
		c.text(max-g);
	})		
})
