$(document).ready(function(){  
  // Hide and show the text in a form field on focus on blur, respectivley.
  function clear_it(object){
    object.each(function(){
      $(this).bind("focus", function(){
        var orig_text = $(this).val();
        $(this).val('');
        $(this).bind("blur", function(){
          orig_text = ($(this).val() !== "") ? $(this).val() : orig_text ;
          $(this).val(orig_text);
        });
      });
    });
  }
  clear_it($("#global-search input"));
  clear_it($("#global-login input"));
  clear_it($("#global-login-openid input"));
  
  
  // Prepare the dashboard
  $('#dashboard').hide()
  
  // Prepare the service-options
  $('.service-options .options').hide()
  
  // Prepare the homepage status form.
  $('form.status-update').hide()
  $('#text-update').show();
  
  // Show/hide status forms
  $('#show-text-update').click(function(e) {
    $('form.status-update').hide();
    $('#text-update').show();
    $('#show-text-update, #show-photo-update, #show-video-update, #show-event-update, #show-bookmark-update').parent().removeClass('active');
    $("#show-text-update").parent().toggleClass("active");
    e.preventDefault();
  })
  
  $('#show-photo-update').click(function(e) {
    $('form.status-update').hide();
    $('#photo-update').show();
    $('#show-text-update, #show-photo-update, #show-video-update, #show-event-update, #show-bookmark-update').parent().removeClass('active');
    $("#show-photo-update").parent().toggleClass("active");
    e.preventDefault();
  })
  
  $('#show-video-update').click(function(e) {
    $('form.status-update').hide();
    $('#video-update').show();
    $('#show-text-update, #show-photo-update, #show-video-update, #show-event-update, #show-bookmark-update').parent().removeClass('active');
    $("#show-video-update").parent().toggleClass("active");
    e.preventDefault();
  })
  
  $('#show-bookmark-update').click(function(e) {
    $('form.status-update').hide();
    $('#bookmark-update').show();
    $('#show-text-update, #show-photo-update, #show-video-update, #show-event-update, #show-bookmark-update').parent().removeClass('active');
    $("#show-bookmark-update").parent().toggleClass("active");
    e.preventDefault();
  })

  $('#show-event-update').click(function(e) {
    $('form.status-update').hide();
    $('#event-update').show();
    $('#show-text-update, #show-photo-update, #show-video-update, #show-event-update, #show-bookmark-update').parent().removeClass('active');
    $("#show-event-update").parent().toggleClass("active");
    e.preventDefault();
  })
  
  // Show/hide dashboard
  $('#dashboard-link').click(function(e) {
    $('#dashboard').toggle();
    $("#dashboard-link").toggleClass("active");
    e.preventDefault();
  })
  
  // Show/hide service options
  $('.service-options h5 a, .service-options a img').click(function(e) {
    e.preventDefault();
    var container = $(this).parent().parent()
    container.toggleClass("active");
    container.find('.options').toggle('fast');
  })
  $('.service-options a.service-options-cancel').click(function(e) {
    e.preventDefault();
    var container = $(this).parent().parent().parent()
    container.toggleClass("active");
    container.find('.options').toggle('fast');
  })
  
  
  // Mail message table show/hide
  $('.message-table .message-reply, .message-table .message-details').hide();
  $('.message-controls .reply').click(function(e) {
    e.preventDefault();
    var container = $(this).parent().parent().parent().parent().parent();
    container.find('.message-reply').show();
  });
  $('.message-controls .panel-close').click(function(e) {
    e.preventDefault();
    $(this).parent().parent().parent().parent().toggle();
  });
  $('.message-controls .delete').click(function(e) {
    e.preventDefault();
    $(this).parent().parent().parent().parent().toggle();
  });
  $('.message-table .message-title').click(function(e) {
    e.preventDefault();
    var container = $(this).parent().parent().parent().parent().parent();
    container.find('.message-details').show();
  });
  
  
  // Active/deactivate tools search filter
  $('#show-all-search').click(function(e) {
    $('#show-web-search, #show-desktop-search, #show-mobile-search, #show-all-search').parent().removeClass('active');
    $("#show-all-search").parent().toggleClass("active");
    e.preventDefault();
  })
  $('#show-web-search').click(function(e) {
    $('#show-web-search, #show-desktop-search, #show-mobile-search, #show-all-search').parent().removeClass('active');
    $("#show-web-search").parent().toggleClass("active");
    e.preventDefault();
  })
  $('#show-mobile-search').click(function(e) {
    $('#show-web-search, #show-desktop-search, #show-mobile-search, #show-all-search').parent().removeClass('active');
    $("#show-mobile-search").parent().toggleClass("active");
    e.preventDefault();
  })
  $('#show-desktop-search').click(function(e) {
    $('#show-web-search, #show-desktop-search, #show-mobile-search, #show-all-search').parent().removeClass('active');
    $("#show-desktop-search").parent().toggleClass("active");
    e.preventDefault();
  })
  
  // Show/hide message contact list
  $('.contact-dropdown-list').hide();
  $('#contact-dropdown a').click(function(e) {
    e.preventDefault();
    $('.contact-dropdown-list').slideDown();
  });
  $('.contact-dropdown-list a').click(function(e) {
    e.preventDefault();
    $('.contact-dropdown-list').slideUp();
  });
  
  // Show/hide OpenID login form
  $("#openid-button").click(function(e) {
  	$("#global-login").fadeOut("def", function() {
  		$("#global-login-openid").fadeIn();
  	});
  	e.preventDefault();
  });
  $("#cliqset-button").click(function(e) {
  	$("#global-login-openid").fadeOut("def", function() {
  		$("#global-login").fadeIn();
  	});
  	e.preventDefault();
  });
  
  // Show/hide Forgot Password overlay
  $(".forgot-password-link").click(function(e) {
  
  	windowshade(true);
  
  	// Make sure overlay shows up inside window/browser
	  new_top = $(document).scrollTop() + 150;
	  $("#forgot-password-overlay").css('top', new_top);
  
  	$("#forgot-password-overlay").toggle("drop", {direction: 'up'});
  	e.preventDefault();
  });
  $("#forgot-password-cancel").click(function(e) {
  	$("#forgot-password-overlay").hide("drop", {direction: 'up'});
  	windowshade(false);
  	e.preventDefault();
  });
  $("#forgot-password-submit").click(function(e) {
        $("#forgot-id").attr("disabled", "true");
        $.get("/forgot-password?id=" + $("#forgot-id").val(), function(e) {
                $("#forgot-password-overlay").hide("drop", {direction: 'up'});
		windowshade(false);
        });
 	$("#forgot-id").removeAttr("disabled");
        e.preventDefault();

  });

  $("#forgot-password-form .button-group a:last").click(function(e) {
        $("#forgot-id").attr("disabled", "true");
        $.get("/forgot-password?id=" + $("#forgot-id").val(), function(e) {
                $("#forgot-password-overlay").hide("drop", {direction: 'up'});
                windowshade(false);
        });
        $("#forgot-id").removeAttr("disabled");
        e.preventDefault();

  });

  // Do stupid form magic because form fields suck and are eeeevil
  $(".field-wrapper input, .field-wrapper textarea").each(function(i) {
  	parent_width = $(this).parent().width();
  	$(this).width(parent_width - 20);
  });
  
  // Do some IE rounded corner magic
	if(jQuery.support.style == false) {
		$(".cta, .cta-alt").each(function() {
			$(this).prepend("<span class='top-round-left'></span><span class='top-round-right'></span>");
			$(this).append("<span class='bottom-round-left'></span><span class='bottom-round-right'></span>");
			$(this).addClass('cta-ie');
		});
		
		// buttons
		$(".button").each(function() {
			$(this).prepend("<span class='top-round-left'></span><span class='top-round-right'></span>");
			$(this).append("<span class='bottom-round-left'></span><span class='bottom-round-right'></span>");
			$(this).addClass('button-ie');
			$(this).contents('span').width( $(this).width() );
		});
		
		// Submit buttons
		$("#signup-form :submit").each(function() {
			$(this).wrap("<div class='submit-wrapper'></div>");
			$(this).parent().attr('style', $(this).attr('style'));
			$(this).attr('style', '');
			$(this).parent().width( $(this).width() );
		});
	};
	//
	
	// Custom form input
	//$('#user-type input').customInput();
	
	// Add/remove the windowshade
	function windowshade(show_hide) {
		if(show_hide) {
			$("body").prepend("<div id='windowshade'></div>");
	  	$("#windowshade").width( $(document).width() );
	  	$("#windowshade").height( $(document).height() );
	  	$("#windowshade").fadeIn("fast");
		} else {
			$("#windowshade").fadeOut("fast");
  		$("#windowshade").remove();
		}
	}
	//
  
});
