var new_votes; var screen_mid_x; var popup_height; var popup_width; var new_position_x; var timer; var modal_timer;

/* -----------------------------------------------------------------------------------------
  Phoenix, rising from the ashes.
----------------------------------------------------------------------------------------- */  

var phx = {



  /* --------------------------------------------------------------------------------------
    Page Specific Stuff
  -------------------------------------------------------------------------------------- */    
  
  pages: {
  
    /* ------------------------------------------------------------------------------------
      Home Page
    ------------------------------------------------------------------------------------ */   

    home_page: {
    
      current_slide: 1,
      form_state: 'signup',
    
      /* ----------------------------------------------------------------------------------
        Forgot Password                                                        (working)
      ---------------------------------------------------------------------------------- */

      forgot_password: function() {
        if ($('#forgot_login').val() == '' || $('#forgot_login').val() == 'user name or email') {
          alert( "Please enter your e-mail address or login." );
        } else {
          location.href = "/home/forgot/?login_info=" + $('#forgot_login').val();
        }
      }, // end phx.pages.home_page.forgot_password

      /* ----------------------------------------------------------------------------------
        Swap Slide                                                             (working)
      ---------------------------------------------------------------------------------- */

      swap_slide: function(slide){      
      
        var the_id = $('div.current_slide').attr('id');
  
        $('div.current_slide').fadeOut('fast', function(){
          $('div#'+the_id).removeClass('current_slide');
          
          if (phx.pages.home_page.current_slide == 7) {
            phx.pages.home_page.current_slide = 0;
          }
          
          if (slide) {
            phx.pages.home_page.current_slide = slide * 1;
          } else {
            phx.pages.home_page.current_slide = phx.pages.home_page.current_slide + 1;
          }
          
          $('div#s'+phx.pages.home_page.current_slide).fadeIn('slow').addClass('current_slide');
            
        });
        
        timer = setTimeout('phx.pages.home_page.swap_slide()', 5000);
  
      } // end phx.pages.home_page.swap_slide
    
    }, // end phx.pages.home_page
    
    notifications: {
    
      confirm_friends: function(){
 
        $('.delete_this').each(function(i){
          
          var friend_id = $(this).attr('id').split('not_')[1];
        
          if ($(this).attr('checked') == true) {
            //console.log(i +': checked');
            
            $.post('/relationships/confirm?id='+friend_id, function(data){
 
              if (data === 'success'){
                
                phx.modals.show_modal('Friend request accepted!', 1, 'notice');
                $('li#li_'+friend_id).fadeOut('slow');
                
              } else {

                phx.modals.show_modal('Oops, there was an error!', 1, 'error');
                
              }
        
            });
            
            
          } else {
            //console.log(i +': not checked');
          
          }
      
        
        });

      } // end phx.pages.notifications.confirm_friends
    
    } // end phx.pages.notifications

  }, // end phx.pages

  /* ----------------------------------------------------------------------------------- */  


  /* --------------------------------------------------------------------------------------
    Forms/Validation
  -------------------------------------------------------------------------------------- */  
  
  forms: {

    /* ------------------------------------------------------------------------------------
      Validate Objects
    ------------------------------------------------------------------------------------ */

    validate: { 
    
      /* ----------------------------------------------------------------------------------
        we use the jQuery.validate() plugin, it's great. 
        http://bassistance.de/jquery-plugins/jquery-plugin-validation/
                      
        the following are the validation objs for our forms
      ---------------------------------------------------------------------------------- */
                   
      /* ----------------------------------------------------------------------------------
        Big Join Form
      ---------------------------------------------------------------------------------- */

      big_join_form: {
      
        rules: {
    			'user[email]': { required: true, email: true }, // to-do: remote 
          'user[login]': { required: true, minlength: 4, maxlength: 20 }, // to-do: remote 
          'user[password]': { required: true, minlength: 6 },
          tos_agree: { required: true },
          legal_check: { required: true }

  		  },
  		
  		  messages: {
          'user[login]': {
            required: 'Oops, your user name needs to be 4-20 characters.',
            minlength: 'Oops, your user name needs to be 4-20 characters.',
            maxlength: 'Oops, your user name needs to be 4-20 characters.'
          },
          'user[email]': {
            required: 'Hey, we need your email.',
            email: 'I think there&rsquo;s something wrong with your email address.'
          },
          'user[password]': {
            required: 'Hey, we need your password. It should be 6 or more characters.',
            minlength: 'Sorry, your password should be 6 or more characters.'
          },
          tos_agree: { 
            required: 'Did you read and accept Zivity&rsquo;s Terms of Service?'
          },
          legal_check: { 
            required: 'You must be 18 years old or older to join Zivity.'
          }
  
        },
      
      }, // end phx.forms.validate.big_join_form
      
      /* ----------------------------------------------------------------------------------
        Forgot Password Form
      ---------------------------------------------------------------------------------- */

      forgot_form: {
      
        rules: {
    			'forgot_login': { required: true, minlength: 4 }, // to-do: remote 
  		  },
  		
  		  messages: {
          'forgot_login': "Oops, we need your user name or password please."  
        },
        
        errorPlacement: function(error, element) {
          error.appendTo('#forgot_error');
    		}
      
      }, // end phx.forms.validate.forgot_form

      /* ----------------------------------------------------------------------------------
        Log In Form
      ---------------------------------------------------------------------------------- */

      log_in_form: {
      
        rules: {
    			'login': { required: true, minlength: 3 }, 
          'password': { required: true, minlength: 6 }
  		  },
  		
  		  messages: {
          'login': {
            required: 'Oops, your user name needs to be 4-20 characters.',
            minlength: 'Oops, your user name needs to be 4-20 characters.',
          },
          'user[password]': {
            required: 'Hey, we need your password. It should be 6 or more characters.',
            minlength: 'Sorry, your password should be 6 or more characters.'
          }
  
        },
        
        errorPlacement: function(error, element) {
          error.appendTo('#login_error');
    		}
      
      }, // end phx.forms.validate.log_in_form

      
      /* ----------------------------------------------------------------------------------
        Buy Votes CC Form
      ---------------------------------------------------------------------------------- */

      buy_votes_cc_form: {
      
        errorContainer: $('#validation_error'),
    		
    		rules: {
    			billing_name: { required: true, minlength: 4 },
    			credit_card_number: { required: true, minlength: 15, creditcard: true },
    			exp_month: "required",
    			exp_year: "required",
    			ccv: { required: true, minlength: 3, maxlength: 4 },
    			address_1: { required: true, minlength: 5 },
    			city: { required: true, minlength: 3 },
    			state: { required: true, minlength: 2 },
    			zip: { required: true, minlength: 3 },
    			country: "required"
    		},
  
    		errorPlacement: function(error, element) {
          error.appendTo('#invalid_list');
    		}
  
      }, // end phx.forms.validate.buy_votes_cc_form
      
      /* ----------------------------------------------------------------------------------
        Reply Form 
      ---------------------------------------------------------------------------------- */

      reply_form: {
      
        errorContainer: $('#reply_error'),
    	
    		rules: {
    			"message[subject]": { required: true, minlength: 4 },
    			"message[body]": { required: true, minlength: 10 }
    		},
  
    		errorPlacement: function(error, element) {
          error.appendTo('#invalid_list');
    		}    
      }, // end phx.forms.validate.reply_form
      
      /* ----------------------------------------------------------------------------------
        Fans Form 
      ---------------------------------------------------------------------------------- */

      fans_form: {
      
        errorContainer: $('#fans_error'),
        
    		rules: {
    			"message[subject]": { required: true, minlength: 4 },
    			"message[body]": { required: true, minlength: 10 }
    		},
  
    		errorPlacement: function(error, element) {
          error.appendTo('#invalid_list');
    		}
      }, // end phx.forms.validate.fans_form
      
      /* ----------------------------------------------------------------------------------
        Friends Form
      ---------------------------------------------------------------------------------- */

      friends_form: {
      
        errorContainer: $('#friends_error'),
        
    		rules: {
    			"message[subject]": { required: true, minlength: 4 },
    			"message[body]": { required: true, minlength: 10 }
    		},
  
    		errorPlacement: function(error, element) {
          error.appendTo('#invalid_list');
    		}
      
      }, // end phx.forms.validate.friends_form
      
      /* ----------------------------------------------------------------------------------
        Status Form
      ---------------------------------------------------------------------------------- */

      status_form: {
      
        errorContainer: $('#validation_error'),
        
    		rules: {
    			status_update_text: { required: true, minlength: 4 }
    		},
    		
    		errorPlacement: function(error, element) {
  
    		},
  
    		submitHandler: function(){
  
    		  var status_string = $('#status_update_text').val();
  
          $.ajax({ type: "POST", url: "/user/status/create", data: "status_text=" + status_string, 
          
            success: function(data) { 
              if (data == 'success') {
                track_page_view( 'STATUS_EDIT');
                $('p#user_quote').empty().html('Now you&rsquo;re &ldquo;' + status_string + '&rdquo;');
                $('#update_status').fadeOut('fast', function(){
                  $('#status').fadeIn('fast');
                });
              } else {
                $('p#user_quote').empty().html('THERE WAS AN ERROR UPDATING YOUR STATUS.');
                $('#update_status').fadeOut('fast', function(){
                  $('#status').fadeIn('fast');
                });
    
              }
            
            }
          
          });
    
    		} // submitHandler
  
      } // end phx.forms.validate.status_form
  
    } // end of phx.forms.validate
  
  }, // end phx.forms

  /* ----------------------------------------------------------------------------------- */  


  /* --------------------------------------------------------------------------------------
    Utilities
  -------------------------------------------------------------------------------------- */    
  
  utils: {
  
    /* ------------------------------------------------------------------------------------
      Variables
    ------------------------------------------------------------------------------------ */
  
    vars: {
      
      the_version: $.browser.version.substr(0,3),
      something_else: 0,
      modal_live: 0
      
    },


    /* ------------------------------------------------------------------------------------
      Hover Over State
    ------------------------------------------------------------------------------------ */
    
    img_over: function(element_id) {
      $("#"+element_id).attr("src", $("#"+element_id).attr("src").split('_off').join('_over'));
    },

    /* ------------------------------------------------------------------------------------
      Hover Off State
    ------------------------------------------------------------------------------------ */
    
    img_off: function(element_id) {
      $("#"+element_id).attr("src", $("#"+element_id).attr("src").split('_over').join('_off'));
    },

    /* ------------------------------------------------------------------------------------
      Update Status Bar
    ------------------------------------------------------------------------------------ */
    
    update_status_bar: function(percent, div, total_width) {
      var pixels_per_point = total_width/100;
      var new_width = percent * pixels_per_point;
      $("#"+div).animate( { width: new_width }, 800, 'easeIn');
    },
    
    /* ------------------------------------------------------------------------------------
      Set Cookie
    ------------------------------------------------------------------------------------ */

    set_cookie: function(name, value, days) {
      var expires;
      if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires="+date.toGMTString();
      } else {
        expires = "";
      }
      document.cookie = name+"="+value+expires+"; path=/";
    },

    /* ------------------------------------------------------------------------------------
      Clear Cookie
    ------------------------------------------------------------------------------------ */

    clear_cookie: function(name) {
      phx.utils.set_cookie(name,"",-1);
    },
    
    /* ------------------------------------------------------------------------------------
      Class Swap
    ------------------------------------------------------------------------------------ */

    class_swap: function(new_select, swap_class) {
      var cur_select = $("."+swap_class).attr("id"); //the id of the element with the swap_class class
      $("#"+cur_select).removeClass(swap_class); 
      $("#"+new_select).addClass(swap_class);
    },
    
    /* ------------------------------------------------------------------------------------
      Div Swap
    ------------------------------------------------------------------------------------ */

    div_swap: function(new_select, swap_class) {
      var cur_select = $("."+swap_class).attr("id"); //the id of the element with the swap_class class
    
      $("#ajax_"+cur_select).fadeOut('slow', function(){
        $("#ajax_"+new_select).fadeIn('slow');
      });
    },
  
  }, // end phx.utils

  /* ----------------------------------------------------------------------------------- */  


  /* --------------------------------------------------------------------------------------
    Modal Slider
  -------------------------------------------------------------------------------------- */    
    
  modals: {
  
    live: 0,
  
    show_modal: function(fill, is_flash, flash_type) {
    
      if (phx.modals.live > 0) {
        //console.log('there is a live modal already');
        clearTimeout(modal_timer);
        phx.modals.hide_modal();
      }
      
      var position = ($(window).width() / 2) - 485;
      
      if (is_flash == 0 && flash_type == 'login') {
      
        $('#login_modal').css({left: position, top: 0});
            
        $('#login_modal').slideDown('slow');
        phx.modals.type = 'login';

      } else {
      
        //console.log('time to clone!');
        $('#clone_modal').clone()
          .attr({id: 'modal_slider'})
          .css({left: position, top: 0})
          .insertAfter('#body_top');
      
        //console.log('is flash - set modal_timer');
        modal_timer = setTimeout('phx.modals.hide_modal()', 5000);
        
        if (flash_type == 'error') {
          $('#modal_slider').addClass('flash_error');
        }
        
        //console.log('fill the h3');
        $('#modal_slider h3').html(fill);
        //console.log('slide!');
        $('#modal_slider').slideDown('slow');
        phx.modals.type = 'flash';
      
      }
      
      phx.modals.live = 1;
      

    }, // end phx.modals.show_modal         
    
    hide_modal: function() {
    
      if (phx.modals.type == 'login') {
        $('#login_modal').slideUp('slow');
      
      } else {
        //console.log('hide & destroy');
        $('#modal_slider').slideUp('slow', function(){
          $('#modal_slider').remove();
        });
      }
      
      phx.modals.live = 0;
      
    }, // end phx.modals.hide_modal
    
  }, // end phx.modals

  /* ----------------------------------------------------------------------------------- */  


  /* --------------------------------------------------------------------------------------
    Buy Votes
  -------------------------------------------------------------------------------------- */    
   
  buy_votes: {
  
    votes_down: function() {
      var buy_votes = parseInt($("#purchase_num_votes").val(), 10);
      
      if (buy_votes > 5) { 
        new_votes = buy_votes - 5; 
      } else { 
        new_votes = 5; 
      }
      
      $("#purchase_num_votes").val(new_votes);
      $('#faux_votes').empty().prepend(new_votes);
      $("#cc_num_votes").val(new_votes);
      $('#cc_total').empty().prepend(new_votes);	 
       
    }, // end phx.buy_votes.votes_down
    
    votes_up: function() {
      var buy_votes = parseInt($("#purchase_num_votes").val(), 10);
      var new_votes = buy_votes + 5;
      
      $("#purchase_num_votes").val(new_votes);
      $('#faux_votes').empty().prepend(new_votes);
      $("#cc_num_votes").val(new_votes);
      $('#cc_total').empty().prepend(new_votes); 
      
    }, // end phx.buy_votes.votes_up
    
    get_position_buy_votes: function() {
      if ($('body').attr('id') == 'photoset') {
        screen_mid_x   = $(window).width() / 2;
        popup_height   = $('#buy_votes').outerHeight();
        popup_width    = $('#buy_votes').outerWidth();
        new_position_x = screen_mid_x - ( popup_width / 2) + 85; // centered on the photoset viewer :: adding 85 px nullifies the offset from the left area on the photoset viewer
        close_position_x = new_position_x + $('#buy_votes').outerWidth() - 20;
        return { x: new_position_x, c_x: close_position_x };
      } else {
        screen_mid_x   = $(window).width() / 2;
        popup_height   = $('#buy_votes').outerHeight();
        popup_width    = $('#buy_votes').outerWidth();
        new_position_x = screen_mid_x - ( popup_width / 2); // centered on the photoset viewer :: adding 85 px nullifies the offset from the left area on the photoset viewer
        close_position_x = new_position_x + $('#buy_votes').outerWidth() - 20;
        return { x: new_position_x, c_x: close_position_x };
      }
      
    }, // end phx.buy_votes.get_position_buy_votes
    
    get_position_thanks: function() {
      if ($('body').attr('id') == 'photoset') {
        screen_mid_x   = $(window).width() / 2;
        popup_height   = $('#buy_votes_thanks').outerHeight();
        popup_width    = $('#buy_votes_thanks').outerWidth();
        new_position_x = screen_mid_x - ( popup_width / 2) + 85; // centered on the photoset viewer :: adding 85 px nullifies the offset from the left area on the photoset viewer
        close_position_x = new_position_x + $('#buy_votes_thanks').outerWidth() - 20;
        return { x: new_position_x, c_x: close_position_x };
      } else {
        screen_mid_x   = $(window).width() / 2;
        popup_height   = $('#buy_votes_thanks').outerHeight();
        popup_width    = $('#buy_votes_thanks').outerWidth();
        new_position_x = screen_mid_x - ( popup_width / 2); // centered on the photoset viewer :: adding 85 px nullifies the offset from the left area on the photoset viewer
        close_position_x = new_position_x + $('#buy_votes_thanks').outerWidth() - 20;
        return { x: new_position_x, c_x: close_position_x };
      }
      
    }, // end phx.buy_votes.get_position_thanks
    
    form_reset: function() {
      $('#buy_votes_card_form').hide();
      $('#buy_votes_quantity_form').show();
      $('#buy_votes_footer').show();
      $("#purchase_num_votes").val(10);
      $("#cc_num_votes").val(10);
      $('#faux_votes').empty().prepend(10);
      $('#cc_total').empty().prepend(10);
      vote_button_open = 0;
      
    }, // end phx.buy_votes.form_reset
    
    votes_cancel_btn: function() {
      if (document.URL.charAt(4) == 's') {
        var redirect = 'http' + document.URL.split('https')[1];
        window.location = redirect;
        return;
      } else {
        $("#buy_votes").fadeOut('fast');
        $("#buy_votes_close").fadeOut('fast');
    
        $('#buy_votes').css({left: '-2000px', top: '-2000px'});
        $('#buy_votes').show();
        $('#buy_votes_close').css({left: '-2000px', top: '-2000px'});
        $('#buy_votes_close').show();
        $('#modal_overlay').fadeOut('fast');
    
        phx.buy_votes.form_reset();
      }
      
    }, // end phx.buy_votes.votes_cancel_btn
    
    thanks_cancel_btn: function() {
      $("#buy_votes_thanks").fadeOut('fast');
      $("#thanks_close").fadeOut('fast');
      $('#modal_overlay').fadeOut('fast');
      
    }, // end phx.buy_votes.thanks_cancel_btn
    
    show_buy_more_votes: function(){
      bv = phx.buy_votes.get_position_buy_votes();
      $("#buy_votes").hide();
      $('#buy_votes_close').hide();
      $("#buy_votes").css({left: bv.x, top: '65px'});
      $('#buy_votes_close').css({left: bv.c_x, top: '50px'});
      
      $('#modal_overlay').fadeIn('fast');
      $("#buy_votes").fadeIn('fast');
      $('#buy_votes_close').fadeIn('fast');
      vote_button_open = 1;
      
    }, // end phx.buy_votes.show_buy_more_votes
    
    show_thanks: function(){
      bv = phx.buy_votes.get_position_thanks();
      $("#buy_votes_thanks").hide();
      $('#thanks_close').hide();
      $("#buy_votes_thanks").css({left: bv.x, top: '65px'});
      $('#thanks_close').css({left: bv.c_x, top: '50px'});
      
      $('#modal_overlay').fadeIn('fast');
      $("#buy_votes_thanks").fadeIn('fast');
      $('#thanks_close').fadeIn('fast');
      
    }, // end phx.buy_votes.show_thanks
    
    resize_buy_more_votes: function(){
      bv = phx.buy_votes.get_position_buy_votes();
      
      if ($.browser.safari) {
        $("#buy_votes").css({ left: bv.x });
        $("#buy_votes_close").css({ left: bv.c_x });
      } else {
        $("#buy_votes").animate({ left: bv.x });
        $("#buy_votes_close").animate({ left: bv.c_x }); 
      }
      
    }, // end phx.buy_votes.resize_buy_more_votes
    
    resize_thanks: function(){
      bv = phx.buy_votes.get_position_thanks();
      
      $("#buy_votes_thanks").animate({ left: bv.x });
      $("#thanks_close").animate({ left: bv.c_x });
      
    } // end phx.buy_votes.resize_thanks
  
  } // end phx.buy_votes

  /* ----------------------------------------------------------------------------------- */  


  
}; // end phx