var $=jQuery;
var carousel_load_timer;
var init_delay;
var firstShelfCarousel = new Object;
function jCarouselInitCallback(carousel) {
  if($.browser.safari) {
    if( typeof(carousel.animating) == 'undefined' ) {
      GeoPalz.carousels.push( carousel );
      
      if(typeof(window.console) != 'undefined') { window.console.log('document.readyState: ' + document.readyState); }
      
      if(typeof(carousel_load_timer) == 'undefined') {
        carousel_load_timer = setInterval(function(){
          if(/loaded|complete/.test(document.readyState)){
            clearInterval(carousel_load_timer);
            for(var i=0; i < GeoPalz.carousels.length; i++) {
              if(typeof(window.console) != 'undefined') { window.console.log('Setting up carousel'); }
              var carousel = GeoPalz.carousels[i];
              if( typeof(carousel.animating) == 'undefined' ) {
                $(window).unbind('load');
                carousel.setup();
              }
            }
          }
        }, 10);
      }
    }
  }

 if(carousel.list[0].id == 'first_shelf') {
    firstShelfCarousel = carousel ;
}
}

/**
 * @description GeoPalz class, primary JS dispatcher for GeoPalz site
 * @requires jQuery >= v1.4: http://jquery.com/
 * @requires Colorbox >= v1.3.6: http://colorpowered.com/colorbox/
*/

var GeoPalz = {
  carousels : [],
  inited: false,
  jcarousel_defaults : {
    scroll : 1,
    buttonNextHTML  : '<a>next</a>',
    buttonPrevHTML  : '<a>previous</a>',
    initCallback    : jCarouselInitCallback
  },
  /**
   * @description GeoPalz.init method, used to get the whole thing started!
  */
  init:function(){
    if(this.inited)
        return;
    this.inited = true;
    if($(".cboxElement").length > 0) {
      $.getScript('/js/jquery.colorbox-min.js', function(){
        
        if($("#billboard").length > 0) {
          $.getScript('/js/homepage.js');
        }
        
        if($('.text-input').length > 0) {
          $.getScript('/js/text-clear.js');
        }
        
        if($('#register-new-1, #change-settings').length > 0) {
          $.getScript('/js/register.js');
        }
        
        if($('#dashboard #step-content').length > 0) {
          $.getScript('/js/dashboard.js?a=' + Math.random());
        }
        
        if($('#prizes-wall').length > 0) {
          $.getScript('/js/prizes1.js');
        }
    
        if($('#awards-case').length > 0) {
          $.getScript('/js/awards.js');
        }
        
        if($('#shipping-info').length > 0) {
          $.getScript('/js/checkout-shipping.js');
        }
        
        if($('.blog-community').length > 0) {
          $.getScript('/js/community.js');
        }
        
        $(".cboxElement").colorbox({
          onOpen    : GeoPalz.colorboxOnOpen,
          onClosed  : GeoPalz.colorboxOnClose,
          onCleanup  : GeoPalz.colorboxOnCleanup
        }, GeoPalz.colorboxComplete);
      });
    }
    
  }, // GeoPalz.init
  
  colorboxComplete : function() {
    if($('#change-geopal').length > 0) {
      if(typeof($.jcarousel) == 'undefined') {
        $.getScript('/js/jquery.jcarousel.pack.js', function(){
          $('#change-geopal').jcarousel( GeoPalz.jcarousel_defaults );
        });
      } else {
        $('#change-geopal').jcarousel( GeoPalz.jcarousel_defaults );
      }
    }
  },
  
  colorboxOnOpen : function() {
    if( $.fn.colorbox.element().parents('.printable').length > 0 || $.fn.colorbox.element().hasClass('printable') ) {
      $('#world, #footer').addClass('award-print-active');
      $('#cboxContent').addClass('award-certificate');
    }
  },
  
  colorboxOnClose : function() {
    if( $.fn.colorbox.element().parents('.printable').length > 0 || $.fn.colorbox.element().hasClass('printable') ) {
      $('#world, #footer').removeClass('award-print-active');
      $('#cboxContent').removeClass('award-certificate');
    }
  },

  colorboxOnCleanup : function() {
//alert(111);
//alert('1: ' + selectedUser);
    if($('#stride').length > 0) {
        var stride = $('#stride').val();
        if((stride < 12) || (stride > 48)) {
            alert('Stride must be a number between 12 and 48!');
            return false;
        }
        var stride_running = $('#stride_running').val();
        if((stride_running < 12) || (stride_running > 65)) {
            alert('Running stride must be a number between 12 and 65!');
            return false;
        }
        var weight = $('#weight').val();
        if((weight > 500) || (weight < 30)) {
            alert('Weight must be a number between 30 and 500!');
            return false;
        }
        var feet = $('#feet').val();
        if((feet != '') && ((feet > 7) || (feet < 1))) {
            alert('Feet must be a number between 1 and 7!');
            return false;
        }
        var inches = $('#inches').val();
        if((inches != '') && ((inches > 11) || (inches < 0))) {
            alert('Inches must be a number between 0 and 11!');
            return false;
        }
        var daily_step_target = $('#daily_step_target').val();
        if((daily_step_target > 50000) || (daily_step_target < 1000)) {
            alert('Daily step goal must be a number between 1,000 and 50,000!');
            return false;
        }
        var dob = $('#dob').val();
        if(dob.search(/^[0,1][0-9]\/[0-3][0-9]\/[1-2][0,9][0-9][0-9]$/) == -1) {
            alert('Date must have the MM/DD/YYYY format');
            return false;
        }
        var tmp = $('#encoded').attr('checked');
        if(tmp)
            var encoded = 'y';
        else
            var encoded = 'n';
//alert(selectedUserId + ' - ' + scriptName);
        var selectedUserId = $('#selectedUserIdPost').val();
        if(typeof(selectedUserId) == 'undefined')
            var selectedUserId = '';
        if($('#family').val() != 'y')
                selectedUserId = '';
//alert(selectedUser);
        $.post('/ajax/update_stride.php', {
            stride: stride,
            stride_running: stride_running,
            email: $('#parent_email').val(),
            weight: weight,
            feet: feet,
            inches: inches,
            encoded: encoded,
            daily_step_target: daily_step_target,
            selected_user: selectedUserId,
            dob: dob
        }, function(ret) {
//alert($('#selectedUserIdGet').val() + ' - ' + scriptName);
//alert(ret);
            if((typeof(scriptName) != 'undefined') && (scriptName == 'summary.php')) {
                self.location = "http://geopalz.com/dashboard/summary.php?eu=" + $('#selectedUserIdGet').val();
            }
            else {
                self.location = "http://geopalz.com/dashboard/index.php";
            }
        });
        if($('#summary_page').length == 1) {
            self.setTimeout('location.reload()', 1000);
}
    }
  }

}; // GeoPalz



jQuery(document).ready(function(){
  init_delay = setInterval(function(){
    if(typeof(document.readyState) != 'undefined') {
      if(/loaded|complete/.test(document.readyState)){
        clearInterval(init_delay);
        GeoPalz.init();
      }
    } else {
      clearInterval(init_delay);
      GeoPalz.init();
    }
  }, 100);

});

function recalc_steps_calories() {
    var val = new String(Math.floor(5280 * 12 / $('#stride').val()));
    var steps = val;
    val = val.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    $('#mile').text(val);

    var val = new String(Math.floor(5280 * 12 / $('#stride_running').val()));
    var steps_running = val;
    val = val.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    $('#mile_running').text(val);

    val = Math.round(steps / ($('#weight').val() * 0.65));
    $('#one_cal_steps').text(val);

}


