function signinOverlayOn() {
   $('div#signin_overlay').fadeIn(400);
   $('#modal_signin').animate({top: "-15px"}, 350)
                     .animate({top: "-35px"}, 250)
                     .animate({top: "-15px"}, 150)
                     .animate({top: "-20px"}, 100)
                     .animate({top: "-15px"}, 100);
}

function signinOverlayOff() {
   $('#modal_signin').animate({top: "0px"}, 100)
                     .animate({top: "-300px"}, 300);
   $('div#signin_overlay').fadeOut(700);
}

function applyThemeOverlayOn() {
   if (typeof themes != "undefined") {
      title = themes[selected_theme_id].title
   }
   if (typeof title != "undefined") {
      $('#tweetz').text("Just applied a new Twitter theme called \"" + title + "\" to my profile, take a look! Got it from http://TweetyGotBack.com")
   }
   $('div#apply_overlay').fadeIn(400);
   $('#modal_apply').animate({top: "-15px"}, 450)
                    .animate({top: "-35px"}, 250)
                    .animate({top: "-15px"}, 150)
                    .animate({top: "-20px"}, 100)
                    .animate({top: "-15px"}, 100);
}

function applyThemeOverlayOff() {
   $('#modal_apply').animate({top: "0px"}, 100)
                    .animate({top: "-478px"}, 300);
   $('div#apply_overlay').fadeOut(700);
}

function applyThemeClick() {
   if (logged_in && user != null) {
      applyThemeOverlayOn();
   } else {
      signinOverlayOn();
   }
}

function randomLoadingMessage() {
    var lines = [
        "queuing up some lovely elevator music...",
		"at least you're not on-hold with a customer service representative...",
		"don't you dare close your eyes. hold your breath, it gets better...",
		"now would be a great time to do some computer desk <a href='http://www1.athabascau.ca/hr/gen_info/ohs/exercise.jpg' target='_blank'>stretches</a>."
    ];
    return lines[Math.round(Math.random()*(lines.length-1))];
}

function applyThemeConfirm() {
   if (selected_theme_id != null) {
      var should_tweet = $('#tweet_this').attr('checked');
      var tweet_text = $('#tweetz').val();
      var should_follow = $('#follow_tgb').attr('checked');
      var url = "/themes/apply/" + selected_theme_id;
      $('#apply_theme_spinner').show();
      $('#apply_theme_form').hide();
      $('#apply_theme_confirm_link').hide();
      $('#apply_theme_close_link').show();
      $('#apply_theme_message').html("<h3>"+randomLoadingMessage()+"</h3><div class='tip'>Please wait while we apply this theme to your Twitter profile.</div>");
      $.postJSON(url, {
         should_tweet: should_tweet,
         tweet_text: tweet_text,
         should_follow: should_follow
      }, function(data) {
         $('#apply_theme_spinner').hide();
         $('#apply_theme_message').html(data.msg);
      });
   }
}

function applyThemeCloseLink() {
	applyThemeOverlayOff();
	if ($('#goto_twitter').attr('checked')) {
		if (user && logged_in) {
			window.open("http://twitter.com/" + user.screen_name, '_blank');
		}
	}
}

function login() {
   $.getJSON("/login", {}, function(data) {
      if (data) {
         if (data.success) {
            logged_in = data.logged_in;
            user = data.user;
            signinOverlayOff();
         } else {
            if (data.msg) {
               // show it
            } else if (data.redirect_url) {
               signinOverlayOff();
               window.location = data.redirect_url
            }
         }
      }
   })
}

function rateTheme(layout_id, rating) {
   if (logged_in) {
      $.getJSON("/themes/rate/" + layout_id, {rating: rating}, function(data) {
         if (data && data.layout_id) {
            $('#rating_text_'+data.layout_id).html("Rating: <strong> " + data.rating.toFixed(1) + "</strong>/5 (" + data.total_votes + " votes cast)").fadeOut(200).fadeIn(200);
            $('#ratingblock_'+data.layout_id).children('ul').children('li.current-rating').css('width',data.rating / 5 * 150);
         }
      })
   } else {
      signinOverlayOn();
   }
}