/*
  Custom ready function
  This should be used to reposition elements or to define and use custom functions
*/
$(document).ready(function(){

	$('.logo').prepend($('.photo-logo'));
	$('.logo').append($('.byline'));
	$('.logo').after($('.photo-header'));
	$('.photo-header').after($('.mainnav'));

	$('.content').after($('.bottom-content'));
	
	if($('.content-interior').length == 0){
		$('.content').after($('.footer'));
	}
	else{
		$('.content-interior').after($('.footer'));
	}
	
	$('.bottom-content').append($('.homearticles'));
    
    // remove header from home_articles
    $('.home_articles h3').remove();
    
	// append clear fix to each ul with floated children 
    $('.content-right ul, .sitemap ul, .home_articles ul, .state_articles ul').each(function(){
        $(this).append('<div class="clear0"><!--ie--></div>');   
    });


/* autocomplete form element initialization */

    if($('#theform').length > 0) {
        var current_state = $('#stateTo').attr('title');
        /**
         * Load all zip codes once HTML is ready.
         */
        $.post('./inc/transactions/index.php', {allzips: "yes"}, function(data) {
            data = data.split(',');
            $('input[name=z]').autocomplete(data);
        });
        if(current_state != '0') {
            /**
             * Initial AJAX call if state is already selected
             */
			 
            $.post('./inc/transactions/index.php', {state:current_state}, function(data) {
                data = data.split(',');
				
                $('input[name=cityTo]').autocomplete(data, {cacheLength:1});
				
				
            });
            /*$.post('/inc/transactions/', {state:current_state, zips: "yes"}, function(data) {
                data = data.split(',');
                $('input[name=zip]').autocomplete(data);
            });*/
        }
        $('#stateTo').change(function() {
            var state = $(':selected' ,this).attr('title');
            if(state == 'DC') {
                $('input[name=cityTo]').val('Washington');
            }
            current_state = state;
			
            /**
             * AJAX call to fill list of city names according to state
             * called when value of state dropdown changes
             */
            $.post('./inc/transactions/index.php', {state: state}, function(data) {
                data = data.split(',');
                $('input[name=cityTo]').autocomplete(data, {cacheLength:1});
            });
            /*$.post('/inc/transactions/', {state: state, zips: "yes"}, function(data) {
                data = data.split(',');
                $('input[name=zip]').autocomplete(data);
            });*/
        });
        $('#topform2 label').each(function() {
            var error = $(this).find('.error').html();
            if(error) {
                $('#topform2').addClass('has-errors');
                $(this).wTooltip({
                    content: error,
                    style: false,
                    className: 'error-tip',
                    offsetY: 20,
                    offsetX: -20
                })
                $(this).addClass('has-error');
                $(this).append('<img src="/images/exclamation.png" alt="error" />')
            }
        });


        // initialize the datepicker
        $('#moveDate').datepicker({
           buttonImage: '../images/icon-calender.gif',
           buttonImageOnly: true,
           buttonText: 'Show Calender',
           showOn: 'button',
           minDate: new Date(),
           onSelect: function(selectedDate, inst){
                var newDate = selectedDate.split('/');
                $('#moveMonth').val( newDate[0] );
                $('#moveDay').val( newDate[1] );
                $('#moveYear').val( newDate[2] );            
            }
        });
        
        // initialize date selectors
        var _today = new Date();
        var _day = _today.getDate();
        var _month = _today.getMonth() + 1;
        $('#moveDay').val( (_day < 10) ? ('0' + _day) : _day );
        $('#moveMonth').val( (_month < 10) ? ('0' + _month) : _month );
        $('#moveYear').val( _today.getFullYear() );

    }

});//ready


