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 signinOverlayOnFb() {
   $('div#signin_overlay_fb').fadeIn(400);
   $('#modal_signin_fb').animate({top: "-15px"}, 350)
                     .animate({top: "-35px"}, 250)
                     .animate({top: "-15px"}, 150)
                     .animate({top: "-20px"}, 100)
                     .animate({top: "-15px"}, 100);
}

function signinOverlayOffFb() {
   $('#modal_signin_fb').animate({top: "0px"}, 100)
                     .animate({top: "-300px"}, 300);
   $('div#signin_overlay_fb').fadeOut(700);
}

function setApplyThemeModal(modal) {
   if (modal != current_modal) {
      current_modal = typeof modal == "undefined" ? "apply_theme" : modal

      el = $('#apply_overlay #modal_apply div.modal_content');
      window.setTimeout(function() { el.html(modal_dialogs[current_modal]) }, 150);
      el.fadeOut(150);
      el.fadeIn(150);
   }
}

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 applyThemeClickIndexPage(theme_id) {
   if (logged_in && user != null) {
      selected_theme_id = theme_id
      setApplyThemeModal("apply_theme");
      applyThemeOverlayOn();
   } else {
      signinOverlayOn();
   }
}

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

function downloadFbClick(fb_id) {
	setApplyThemeModal("download_fb_cover");
	if (typeof fb_id != "undefined") {
   	selected_theme_id = fb_id;
   	window.setTimeout(function() {
         $(".modal_content .fb_image").hide();
         $(".modal_content #fb_" + fb_id).show();
   	},150);
   }
	applyThemeOverlayOn(); 
}

function applyFbClick(fb_id) {
	setApplyThemeModal("apply_fb_cover");
	if (typeof fb_id != "undefined") {
   	selected_theme_id = fb_id;
   	window.setTimeout(function() {
         $(".modal_content .fb_image").hide();
         $(".modal_content #fb_" + fb_id).show();
   	},150);
   }
	applyThemeOverlayOn(); 
}

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='/images/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_pro_confirm_link').hide();
      $('#apply_theme_close_link').show();
      $('#apply_theme_message').html("<div class='latte_plug'><a href='/lattes' title='Love our themes? Consider buying us a latte!' alt='Love our themes? Consider buying us a latte!'><img src='/images/latte_plug.png'><a/></div><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 applyThemeProConfirm() {
   if (selected_theme_id != null) {
      $('#apply_theme_spinner').show();
      $('#apply_theme_form').hide();
      $('#apply_theme_confirm_link').hide();
      $('#apply_theme_pro_confirm_link').hide();
      $('#apply_theme_close_link').show();
      var url = "/pro/check/" + selected_theme_id;
      $.postJSON(url, {}, function(data) {
         $('#apply_theme_spinner').hide();
         $('#apply_theme_message').html(data.msg);
         $('#apply_theme_back_link').show();
      });
   }
}

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

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();
   }
}
