jQuery(document).ready(function(){
    //ROUNDED CORNERS
    $(function(){
        DD_roundies.addRule('.box', 10, true);
        DD_roundies.addRule('.content', 10, true);
        DD_roundies.addRule('#tweets', 10, true);
        
        //kill the rounded corners in IE for application
        if(!$.browser.msie){
            DD_roundies.addRule('li.tabLink', 10, true);
        }
    });
    
    //HOMEPAGE CYCLE
    $('#postCard').cycle({ 
        fx: 'scrollLeft', 
        speed: 500,
        timeout: 8000,
        pager:  '#pager' 
    });

    $('#tips div').cycle({
        fx: 'scrollUp',
        speed: 300,
        timeout: 7000
    });

    //FORMS AUTO-FILL
    $('input.month').autofill({
        value: 'MM',
        defaultTextColor: '#b8c9d9',
        activeTextColor: '#1e5f97'
    });
    $('input.day').autofill({
        value: 'DD',
        defaultTextColor: '#b8c9d9',
        activeTextColor: '#1e5f97'
    });
    $('input.year').autofill({
        value: 'YYYY',
        defaultTextColor: '#b8c9d9',
        activeTextColor: '#1e5f97'
    });

    //HOME PAGE STORE LOCATOR AUTOFILL
    $('input.zip').autofill({
        value: 'Zip code',
        defaultTextColor: '#b8c9d9',
        activeTextColor: '#1e5f97'
    });


    //STORE LOCATOR AUTOFILL
    $('input.startPoint').autofill({
        value: 'Starting Address',
        defaultTextColor: '#b8c9d9',
        activeTextColor: '#1e5f97'
    });

    $('input#storelocator_submit').click(function(){
        $('input.destination').autofill({
            value: ''
        });
        setTimeout(destFill, 500);
    });
    
    function destFill()
    {
        $('input.destination').autofill({
            value: 'Destination (Optional)',
            defaultTextColor: '#b8c9d9',
            activeTextColor: '#1e5f97'
        });
    }

    destFill();

});



$(document).ready(function(){
    //Employment Application Wizard
    $('.tab').hide();
    $('div#general_tab').show();
    $('li#general').addClass('activeTab');

    function showTab(id)
    {
        $('.tab').hide();
        $('#' + id + '_tab').show();
        $('li.tabLink').removeClass('activeTab');
        $('li#general').removeClass('activeTab');
        $('#' + id).addClass('activeTab');
    }
    
    //shows the clicked tab
    $('li.tabLink').click(function(){
        var id = $(this).attr('id');
        showTab(id);
    });
    

    //application page turn -> next button

    // top nav page controls
    $('ul#appMenu li').click(function(){
        var rel = $('li.activeTab').attr('rel');
        $('span.pageNumber').html(rel);

        // if last page remove next button
        if(rel == '7'){
            $('a.nextTab').hide();
            $('input.hideSubmit').show();
        } else { 
            $('a.nextTab').show();
        }
    });

    // next tab page controls
    $('a.nextTab').click(function(){
        //looks for the rel of the next tab
        var rel = $('li.activeTab').next().attr('rel');
        $('span.pageNumber').html(rel);

        // if last page remove next button
        if(rel == '7'){
            $('a.nextTab').hide();
        } else {
            $('a.nextTab').show();
        }
    });

    var rel = $('li.activeTab').attr('rel');
    //adds page number
    $('span.pageNumber').html(rel);

    /* application mousedown buttons */

    $('div#application').find('a.nextTab').mousedown(function(){
        $(this).addClass('pressed');
    });
    $('div#application').find('a.nextTab').mouseup(function(){
        $(this).removeClass('pressed');
    });

    $('div#application').find('input.submit').mousedown(function(){
        $(this).addClass('pressed');
    });

    $('div#application').find('input.submit').mouseup(function(){
        $(this).removeClass('pressed');
    });

  function clearAlt() {
    $('div#application').find('input').each(function(){
      var val = $(this).val();
      var alt = $(this).attr('alt');
      if (val == alt) {
        if ($(this).attr('id') != '_csrf_token') {
          $(this).val('');
        }
      }
    });
  }

  function validateForm() {
    var formValid = true;
    $('div#general_tab').find('input.required').each(function(){
        var val = $(this).val();
        var alt = $(this).attr('alt');
        if(val == "" || val == alt){
            formValid = false;
            $(this).addClass('formError');
            $('.tab').hide();
            $('ul#appMenu').find('li').removeClass('activeTab');
            $('div#general_tab').show();
            $('li#general').addClass('activeTab');
        } else {
            $(this).removeClass('formError');
        }
    });

    return formValid;
  }

    // adds a selected class to the main nav anchor tag that is current
    $('#main_nav li.current').each(function(){
        $('a', $(this)).addClass('selected');
    });

    $("#agreement-toggle").fancybox({
        'width'             : '75%',
        'height'            : '75%',
        'autoScale'         : false,
        'overlayColor'      : '#000',
        'overlayOpacity'    : '0.7',
        'transitionIn'      : 'elastic',
        'transitionOut'     : 'fade',
        'easingIn'          : 'swing',
        'speedIn'           : 800,
        'speedOut'          : 300,
        'type'              : 'iframe'
    });

    

    //clear val
     $('div#application').find('input:text').each(function(){
        var curval = $(this).val();
        var altval = $(this).attr('alt');
        if (curval == "" || curval == altval) {
          $(this).css({"color":"#999"});
          $(this).val(altval);
        } else {
          $(this).css({"color":"#000"});
        }
    });
    
    $('div#application').find('input:text').focus(function(){
        var curval = $(this).val();
        var altval = $(this).attr('alt');
        $(this).css({"color":"#000"});
        if(curval==altval){
          $(this).val("");
        }
    });

    //insert val
    $('div#application').find('input:text').blur(function(){
        var curval = $(this).val();
        var altval = $(this).attr('alt');
        if(curval==""){
          $(this).css({"color":"#999"});
          $(this).val(altval);
        }
    });
   
    //NEXT Button on the Wizard
    $('a.nextTab').click(function(){
      var id = $('li.activeTab').next().attr('id');
      var retval = validateForm();
      if (retval) {
        showTab(id);
        
        // If this is the final tab and the form is valid, show the submit button.
        if (id === "final") {
          $('input.hideSubmit').show();
        }
      }
      return false;
    });

    $('div#application').find('input:submit').click(function(){
      clearAlt();
    });
   
    $('input.hideSubmit').hide();
    $('li#final').click(function(){
        $('input.hideSubmit').show();
    });
});


