/*************************************************/
/* Taskenstein Javascript (Used On Public Pages) */
/*************************************************/

jQuery(document).ready(function($) {
  
  /*************/
  /* Tomb Tabs */
  /*************/
  
  // allow a click on any portion of the tomb tab to navigate to the link location
  $('div#tskn-content div.tomb-tab-navigation div.tomb-tab').click(function(e) {
    var a = $(this).find('a');
    var href = a.attr('href');
    window.location.href = href;
  });
  
  /**************************/
  /* Testimonial Slide Show */
  /**************************/
  
  var d = $('div#tskn-content div.tskn-testimonial-slide-show');
  if (d.size())
  {
    // time to wait between slides
    d.data('delay', 7); // seconds
    
    // retrieve all the slides from the page division
    d.data('slides', d.find('div.slides').children());
    d.data('current', 0);
    
    // always begin with the first slide and hide all other slides
    for (var i=1; i<d.data('slides').size(); i++) $(d.data('slides').get(i)).hide();
    
    // define a global rotation function
    // which can be schedule with timeout
    d.data('rotate', function() {
      var d = $('div#tskn-content div.tskn-testimonial-slide-show');
      
      // determine the next index using a modulus operator
      d.data('next', ((d.data('current') + 1) % d.data('slides').size()));
      var c = $(d.data('slides').get(d.data('current')));
      var n = $(d.data('slides').get(d.data('next')));
      
      // begin the animation
      // of current and next
      c.fadeOut(3000);
      n.fadeIn(3000);
      
      // update our current offset
      d.data('current', d.data('next'));
      
      // schedule the next rotation of the slide show images using timeout
      setTimeout('jQuery(\'div#tskn-content div.tskn-testimonial-slide-show\').data(\'rotate\')();', d.data('delay') * 1000);
    });
        
    // schedule the first rotation of the slide show images using timeout
    setTimeout('jQuery(\'div#tskn-content div.tskn-testimonial-slide-show\').data(\'rotate\')();', d.data('delay') * 1000);
  }
  
  /*************************/
  /* Sign-Up & Login Panel */
  /*************************/
  
  // add necessary functionality to power the signup and login
  // panel which may be placed on any page so detect and execute
  var d = $('div#tskn-content div.tskn-signup-and-login-panel');
  if (d.size())
  {
    // establish a global variable to the sign and login panel to allow repeated function access
    var tskn__signup_and_login_panel = $('div#tskn-content div.tskn-signup-and-login-panel');
    
    // embed helper functions into the panes for showing messages
    d.find('div.pane.signup').data('show_error', function(msg) {
      var p = $('div#tskn-content div.tskn-signup-and-login-panel div.pane.signup p.message');
      p.addClass('error').empty().append(msg);
    });
    d.find('div.pane.login').data('show_error', function(msg) {
      var p = $('div#tskn-content div.tskn-signup-and-login-panel div.pane.login p.message');
      p.addClass('error').empty().append(msg);
    });
    d.find('div.pane.forgot').data('show_error', function(msg) {
      var p = $('div#tskn-content div.tskn-signup-and-login-panel div.pane.forgot p.message');
      p.addClass('error').empty().append(msg);
    });
    d.find('div.pane.signup').data('clear_message', function(msg) {
      var p = $('div#tskn-content div.tskn-signup-and-login-panel div.pane.signup p.message');
      p.removeClass('error').empty();
    });
    d.find('div.pane.login').data('clear_message', function(msg) {
      var p = $('div#tskn-content div.tskn-signup-and-login-panel div.pane.login p.message');
      p.removeClass('error').empty();
    });
    d.find('div.pane.forgot').data('clear_message', function(msg) {
      var p = $('div#tskn-content div.tskn-signup-and-login-panel div.pane.forgot p.message');
      p.removeClass('error').empty();
    });
    
    // embed a function to easily toggle
    // to a different pane hiding others
    d.data('show_pane', function(pane) {
      var p = tskn__signup_and_login_panel;
      p.removeClass('signup');
      p.removeClass('login');
      p.removeClass('forgot');
      p.addClass(pane);
      
      // foreign code may have 
      // altered display styles
      // so force visibility
      p.find('div.pane').hide();
      p.find('div.pane.'+pane).show();
      
      // use a best efforts approach to remember this 
      // compose an execute the ajax request to handler
      $.ajax({ url: 'html__signup_and_login_panel.php',
               type: "POST",
               cache: false,               
               data: { tskn__action: 'set_active_pane',
                       pane: pane
                     },
               dataType: 'json'});
    });
    
    // enable the panel tabs to switch panes and the special links to also switch panes including the forgot password pane
    d.find('div.tab.signup').click(function(e) { tskn__e_stop(e); tskn__signup_and_login_panel.data('show_pane')('signup'); });
    d.find('div.tab.login, div.body a.login').click(function(e) { tskn__e_stop(e); tskn__signup_and_login_panel.data('show_pane')('login'); });
    d.find('div.body a.forgot-password').click(function(e) { tskn__e_stop(e); tskn__signup_and_login_panel.data('show_pane')('forgot'); });
    
    // when the join button is clicked start ajax process
    d.find('div.pane.signup a.join').click(function(e) {
      tskn__e_stop(e);
      var f = $(this).parents('form');
      var pn = $(this).parents('div.pane.signup');
      
      // the first task in our attempt to 
      // send the message is to show status
      var aja = f.find('img.ajax-activity');
      var btn = $(this);
      aja.show();
      btn.hide();
      pn.data('clear_message')();
      
      // define a function which will handle
      // both success and failure of our post
      var complete = function(data, status) {
        if (!(data instanceof Object)) data = new Object();
        var msg = null;
        
        // determine if any error was reported by the
        // process of simply from the ajax request
        if ((!data.success) || (status != 'success'))
        {
          msg = data.error || 'System or Network Error; Please try again, or contact the site administrator if the problem persists.';
          pn.data('show_error')(msg);
        }
        
        // show success message
        // on the login pane 
        else 
        {
          /********************************/
          /* SIGNUP SUCCESSFUL, LOG EVENT */
          /********************************/
          
          if (typeof(pageTracker) !== 'undefined') pageTracker._trackEvent('members', 'signup');
          
          /********************/
          /* END OF LOG EVENT */
          /********************/
          
          // locate the message are on the login pane and provide
          // suitable text on that pane, then switch and make active
          var p = tskn__signup_and_login_panel.find('div.pane.login p.message');
          p.empty().append('You have successfully signed up for a Taskenstein.com account!  Enter your username and password below to access your account now.');
          tskn__signup_and_login_panel.data('show_pane')('login');
        }
        
        // after message or action
        // taken restore button
        aja.hide();
        btn.show();
      };
      
      // compose an execute the ajax request to handler
      $.ajax({ url: 'html__signup_and_login_panel.php',
               type: "POST",
               cache: false,               
               data: { tskn__action: 'signup',
                       username: f.find('input[name=username]').val(),
                       password: f.find('input[name=password]').val(),
                       password_again: f.find('input[name=password_again]').val(),
                       email: f.find('input[name=email]').val(),
                       i_agree: (f.find('input[name=i_agree]').attr('checked') ? 1 : 0)
                     },
               dataType: 'json',
               success: complete,
               error: complete });
    });
    
    // when the login button is clicked start ajax process
    // also when enter is pressed within the form submit
    d.find('div.pane.login a.login').click(function(e) {      
      tskn__e_stop(e);
      tskn__signup_and_login_panel.data('process_login')($(this));
    });
    d.find('div.pane.login form').keypress(function(e) {      
      if (e.which == 13) tskn__signup_and_login_panel.data('process_login')($(this).parents('div.pane.login').find('a.login'));
    });
    
    // establish a function for login which
    // can be called on click or enter key
    d.data('process_login', function(btn) {
      var f = btn.parents('form');
      var pn = btn.parents('div.pane.login');
      
      // the first task in our attempt to 
      // send the message is to show status
      var aja = f.find('img.ajax-activity');
      aja.show();
      btn.hide();
      pn.data('clear_message')();
      
      // define a function which will handle
      // both success and failure of our post
      var complete = function(data, status) {
        if (!(data instanceof Object)) data = new Object();
        var msg = null;
        
        // determine if any error was reported by the
        // process of simply from the ajax request
        if ((!data.success) || (status != 'success'))
        {
          msg = data.error || 'System or Network Error; Please try again, or contact the site administrator if the problem persists.';
          pn.data('show_error')(msg);
        }
        
        // on success 
        // redirect to 
        // members page
        else 
        {
          /*******************************/
          /* LOGIN SUCCESSFUL, LOG EVENT */
          /*******************************/
          
          if (typeof(pageTracker) !== 'undefined') pageTracker._trackEvent('members', 'login');
          
          /********************/
          /* END OF LOG EVENT */
          /********************/
          
          // simply send the member to the landing page
          window.location.href = 'members/index.php';
          return;
        }
        
        // after message or action
        // taken restore button
        aja.hide();
        btn.show();
      };
      
      // compose and execute the ajax request to handler
      $.ajax({ url: 'html__signup_and_login_panel.php',
               type: "POST",
               cache: false,               
               data: { tskn__action: 'login',
                       username: f.find('input[name=username]').val(),
                       password: f.find('input[name=password]').val()
                     },
               dataType: 'json',
               success: complete,
               error: complete });
    });
    
    // when the login button is clicked start ajax process
    d.find('div.pane.forgot a.retrieve').click(function(e) {
      tskn__e_stop(e);
      var f = $(this).parents('form');
      var pn = $(this).parents('div.pane.forgot');
      
      // the first task in our attempt to 
      // send the message is to show status
      var aja = f.find('img.ajax-activity');
      var btn = $(this);
      aja.show();
      btn.hide();
      pn.data('clear_message')();
      
      // define a function which will handle
      // both success and failure of our post
      var complete = function(data, status) { 
        if (!(data instanceof Object)) data = new Object();
        var msg = null;
        
        // determine if any error was reported by the
        // process of simply from the ajax request
        if ((!data.success) || (status != 'success'))
        {
          msg = data.error || 'System or Network Error; Please try again, or contact the site administrator if the problem persists.';
          pn.data('show_error')(msg);
        }
        
        // on success 
        // redirect to 
        // members page
        else 
        {
          // compose a descriptive success message of the password retrieval request which should be enough information for them
          pn.find('p.message').empty().append('Your password has been succesfully sent to the email address on your account.');
        }
        
        // after message or action
        // taken restore button
        aja.hide();
        btn.show();
      };
      
      // compose and execute the ajax request to handler
      $.ajax({ url: 'html__signup_and_login_panel.php',
               type: "POST",
               cache: false,               
               data: { tskn__action: 'forgot_password',
                       username: f.find('input[name=username]').val(),
                       email: f.find('input[name=email]').val()
                     },
               dataType: 'json',
               success: complete,
               error: complete });
    });
  }
  
});
