  function checkLogin() {
    $.ajax({
        url: "/process.aspx?action=checkLogin",
        context: document.body,
        success: function (data) {
            if(data.indexOf("wizard")>0)
              {
                 $("#startupWizard").fadeIn(500);
                  $.ajax({
                  url: "/wizard?step=1",
                  context: document.body,
                  dataType: "html",
                  success: function (data) {
                    $(".wizardStep").html(data);
                    makeMeFancy();
                  }
                });
              }
            if(data.indexOf("loggedin")<1)
            {
              $("#loginForm").fadeIn(300);
            }
            if(data.indexOf("loggedin")>0)
            {
              $("#loginForm").hide();
              $("#userName").html((data).replace(' wizard', '').replace(' loggedin', ''));
              $("#loginUser").attr('value', '');
              $("#loginPass").attr('value', '');
              $(".logoutButton").delay().show();
              $(".profileLink").delay().show();
              $(".loggedInTitle").delay().show()
            }
        }
      });
  }

function loadQtip()
{
  $('.uploadHelper').live('mouseover', function(event) {
     // Bind the qTip within the event handler
     $(this).qtip({
        overwrite: false, // Make sure the tooltip won't be overridden once created
        content: $("#agentUpload").clone(),
        show: {
           event: event.type, // Use the same show event as the one that triggered the event handler
           ready: true // Show the tooltip as soon as it's bound, vital so it shows up the first time you hover!
        }
     }, event); // Pass through our original event to qTip
  })
  $('.uploadHelper2').live('mouseover', function(event) {
     // Bind the qTip within the event handler
     $(this).qtip({
        overwrite: false, // Make sure the tooltip won't be overridden once created
        content: $("#schoolUpload").clone(),
        show: {
           event: event.type, // Use the same show event as the one that triggered the event handler
           ready: true // Show the tooltip as soon as it's bound, vital so it shows up the first time you hover!
        }
     }, event); // Pass through our original event to qTip
  })
}

function loadExtras() {
       $.ajax({
          url: "/ajax?data=profile&setting=extras",
          context: document.body,
          dataType: "html",
          success: function (data) {
            $("#profileWrapper").html(data);
            loadQtip();
          }
      });    
  return false;
}

function loadSocial() {
       $.ajax({
          url: "/ajax?data=profile&setting=social",
          context: document.body,
          dataType: "html",
          success: function (data) {
            $("#profileWrapper").html(data);
          }
      });  
  return false;
}

function loadPayment() {
       $.ajax({
          url: "/ajax?data=profile&setting=payment",
          context: document.body,
          dataType: "html",
          success: function (data) {
            $("#profileWrapper").html(data);
          }
      });    
  return false;
}
function loadAccom() {
       $.ajax({
          url: "/ajax?data=profile&setting=accommodations",
          context: document.body,
          dataType: "html",
          success: function (data) {
            $("#profileWrapper").html(data);
            loadQtip();
          }
      });    
  return false;
}
function loadCourses() {
       $.ajax({
          url: "/ajax?data=profile&setting=courses",
          context: document.body,
          dataType: "html",
          success: function (data) {
            $("#profileWrapper").html(data);
            loadQtip();
          }
      });    
  return false;
}

function loadProfile() {
       $.ajax({
          url: "/ajax?data=profile",
          context: document.body,
          dataType: "html",
          success: function (data) {
            $("#profileWrapper").html(data);
          }
      }); 
  return false;
}

function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}

var profileUpdated = function(){
  $("#alert").show(400).find("#message").text('Your changes has been saved!');
};

$(document).ready(function () {
  
          $.ajaxSetup({cache: false});

          loadQtip();
  
          $(".extraShow").live("click", function() {
             var elem = $(this);
             $(".schoolExtraContent").slideUp();
             $(".extraShow").css({'background-color':'white','color':'#151515'});
             if($("#extra" + $(this).attr('id')).is(':visible') == true)
             {
               elem.css({'background-color':'white','color':'#151515'});
               $("#extra" + $(this).attr('id')).slideUp();
             }
             else
             {
               elem.css({'background-color':'#4D6AAE','color':'white'});
               $("#extra" + $(this).attr('id')).slideDown();
             }
          }); 
  
          $("#paymentLink").live("click", function() {
             loadPayment();
             return false;
          });
          $("#socialLink").live("click", function() {
             loadSocial();
             return false;
          });
          $("#courseLink").live("click", function() {
             loadCourses();
             return false;
          });
          $("#accomLink").live("click", function() {
             loadAccom();
             return false;
          });
          $("#extrasLink").live("click", function() {
             loadExtras();
             return false;
          });
          $("#profileLink").live("click", function() {
             loadProfile();
             return false;
          });
  
    $("#logoutButton").live("click", function () {
        $.ajax({
            url: "/process.aspx?action=logout",
            context: document.body,
            success: function (data) {
              $(".profileLink").delay().hide();
              $("#startupWizard").fadeOut(500);
              $(".logoutButton").fadeOut(300);
              $(".loggedInTitle").fadeOut(300)
              $("#userName").html("");
              $(".statsBox").animate({
                left: '0'
              }, 1000);
              $("#loginForm").delay(900).fadeIn(300);
              window.location = "http://www.schoolsandagents.com/";
            }
        });
    });
  
    $(".closeAlert").live("click", function() {
      $("#alert").hide(400);      
    });
  
    $("#submitProfile").live("click", function() {
        event.preventDefault ? event.preventDefault() : event.returnValue = false;
          $.ajax({
              url: "/process.aspx?action=saveProfile",
              data: $("#profileForm").serialize(),
              context: document.body,
              success: function (data) {
                loadProfile();
                $("#alert").show(400).find("#message").text('Your profile settings has been saved!');
              }
        });
    });

  
    $("#loginUser").live("focus", function() {
       if($(this).attr('value') == 'Username')
       {
          $(this).attr('value', '');
       }    
    });
                         
    $("#loginUser").live("blur", function() {
       if($(this).attr('value') == '')
       {
          $(this).attr('value', 'Username');
       }    
    });
  
    $(".newsletterField").live("focus", function() {
        if($(this).attr('value') == 'Your email here')
        {
          $(this).attr('value', '');
        }
    });
  
    $(".newsletterField").live("blur", function() {
        if($(this).attr('value') == '')
        {
          $(this).attr('value', 'Your email here');
        }
    });
  
    $(".newsletterSubmit").live("click", function() {
    event.preventDefault ? event.preventDefault() : event.returnValue = false;
        $.ajax({
            url: "/process.aspx?action=signupNewsletter",
            data: "email=" + $(".newsletterField").attr('value'),
            context: document.body,
            success: function (data) {
              if(data.indexOf("done")>=0)
              {
                alert("You've successfully signed up!");
              }
            }
        });
    });
    
    $(".registerLinkMem").live("click", function() {
        $("#registerWindow").fadeIn(500);
    });
  
    $(".signupClose").live("click", function() {
        $("#registerWindow").fadeOut(500);
    });
  
    $(".videoSelector").live("change", function() {
        var ytId = $(this).attr('value');
        $("#youTubeFrame").attr('src', 'http://www.youtube.com/embed/' + $(this).attr('value') + '?wmode=transparent')
        $(".videoDescription").text($("#" + ytId).text());
    });

    $(".article").find(".addthis_toolbox").hide();
  
    $("#memberListFrontpage").find("li").hover(function() {
      $("#memberListFrontpage").find("li").removeClass("active"); 
      $(this).addClass("active");
      $("#memberContentFrontpage").css("background-image", "url('/imagegen.ashx?image=" + $(this).find('span').attr('title') + "&height=200&width=250&constrain=true')");
      $("#memberStoryFrontpage").find("span").text($(this).find("div").text());
    });
  
    $("#memberPost_1").addClass("active");
  $("#memberContentFrontpage").css("background-image", "url('/imagegen.ashx?image=" + $('#memberPost_1').find('span').attr('title') + "&height=200&width=250&constrain=true')");
    $("#memberStoryFrontpage").find("span").text($('#memberContent_1').text());

    $("#newsListFrontpage").find("li").hover(function() {
      $("#newsListFrontpage").find("li").removeClass("active"); 
      $(this).addClass("active");
      $("#newsContentFrontpage").css("background-image", "url('" + $(this).find('span').attr('title') + "')");
      $("#newsStoryFrontpage").find("span").text($(this).find("div").text());
      $("#newsTitleFrontpage").find("span").text($(this).find("span").text());
    });
    
    $("#newsPost_1").addClass("active");
    $("#newsContentFrontpage").css("background-image", "url('" + $('#newsPost_1').find('span').attr('title') + "')");
    $("#newsStoryFrontpage").find("span").text($('#newsContent_1').text());
    $("#newsTitleFrontPage").find("span").text($('#newsPost_1').find('span').text());
  
  $(".resultHeader").click(function() {
     var id = $(this).attr('id').replace('header', '');
     if($("#result_" + id).css('display') == 'none') {
       $("#result_" + id).css('display', 'block'); 
     }
  });

  $("#registerForm").submit(function (event) {
    event.preventDefault ? event.preventDefault() : event.returnValue = false;
    var form = $("#registerForm");
    $("#registerFields").fadeOut(200);
    $("#signUpUser").css('background-color', 'transparent');
    $("#signUpPass").css('background-color', 'transparent');
    $("#loadingBar").delay(200).fadeIn(200);
    $.ajax({
      url: "/process.aspx?action=newAccount",
      type: "POST",
      data: form.serialize(),
      success: function (data) {
              if(data.indexOf("done")>=0)
              {
                $("#loadingBar").fadeOut(200);
                $("#registerWindow").fadeOut(200);
                $("#loginBox").html("");
                window.location = "http://www.schoolsandagents.com/"
              }
              if(data.indexOf("terms")>=0) 
              {
                $("#signUpAgreeLabel").css('color', '#be2727');
                $("#loadingBar").fadeOut(200);
                $("#registerFields").delay(200).fadeIn(200);
              }
              if(data.indexOf("username")>=0) 
              {
                $("#signUpUser").css('background-color', 'red');
                $("#loadingBar").fadeOut(200);
                $("#registerFields").delay(200).fadeIn(200);
              }
              if(data.indexOf("password")>=0) 
              {
                $("#signUpPass").css('background-color', 'red');
                $("#loadingBar").fadeOut(200);
                $("#registerFields").delay(200).fadeIn(200);
              }
              if(data.indexOf("double")>=0)
              {
                $("#signUpUser").css('background-color', 'red');
                $("#signUpUser").val("Already exists.");
              }
              $("#loadingBar").fadeOut(200);
              $("#registerFields").delay(200).fadeIn(200);
      },
      error: function(jqXhr, textStatus, errorThrown) {

      },
      complete: function () {
        
      }
    });
  });
  
 $("#loginForm").submit(function (event) {
    event.preventDefault ? event.preventDefault() : event.returnValue = false;
    var form = $("#loginForm");
    $.ajax({
      url: "/process.aspx?action=login",
      type: "POST",
      data: form.serialize(),
      success: function (data) {
          if (data.indexOf("uname")>0)
          {
            $("#loginUser").css('background-color', 'red');
          }
          if (data.indexOf("pwd")>0)
          {
            $("#loginPass").css('background-color', 'red');
          }
          checkLogin();
      },
      error: function(jqXhr, textStatus, errorThrown) {

      },
      complete: function () {
        
      }
    });
  });
  
  $(".resultBookNow").click(function() {
          var id = $(this).attr('id').replace('readMore_', '');
          if($("#result_" + id).css('height') == '540px') {
            $("#hidden_" + id).fadeOut(1000, function() {
              $("#hidden_" + id).removeClass('Showing');
              $("#result_" + id).animate({height:'220px'}, 500).css('overflow', 'visible');
            });
          }
          else
          {
          $("#result_" + id).animate({height:'540px'}, 500, function() {
          $("#hidden_" + id).fadeIn(1000);
          $("#hidden_" + id).addClass('Showing');
          }).css('overflow', 'visible');
          }
  });
  checkLogin();
});
