$(function(){
	var $total = $('#total_nutritions_facts'),
		link = $total.find('a.resultlink').attr('href');
	
	$total[0] && $total.load(link + '?' + new Date().getTime(), function(){
		attach_quantity_controls();
	});
})		

Date.firstDayOfWeek = 7;
Date.format = 'mm/dd/yyyy';

function init_date_picker(){
	if (!$('.date-pick')[0]) return;
	
    var checkForMouseout = function(event){
        var el = event.target;
        
        while (true) {
            if (el == cal) {
                return true;
            }
            else 
                if (el == document) {
                    $this.dpClose();
                    return false;
                }
                else {
                    el = $(el).parent()[0];
                }
        }
    };
    
    $('.date-pick').datePicker({
        clickInput: true,
        startDate: '01/01/2008'
    });
    
    $('a.edit').datePicker({
        createButton: false,
        startDate: '01/01/2008'
    }).bind('click', function(){
        $(this).dpDisplay();
        this.blur();
        //alert("xxx");
        return false;
    }).bind('dateSelected', function(e, selectedDate, $td){
        Date.format = 'yyyymmdd';
        url = 'my_foods/edit/' + selectedDate.asString() + '/' + this.id;
        
        location.href = url;
        
        Date.format = 'dd/mm/yyyy';
    });
    /*
    
     .bind(
    
     'dpDisplayed',
    
     function(event, datePickerDiv)
    
     {
    
     cal = datePickerDiv;
    
     $this = $(this);
    
     $(document).bind(
    
     'mouseover',
    
     checkForMouseout
    
     );
    
     }
    
     )
    
     .bind(
    
     'dpClosed',
    
     function(event, selected)
    
     {
    
     $(document).unbind(
    
     'mouseover',
    
     checkForMouseout
    
     );
    
     }
    
     );
    
     */
    
}

$(init_date_picker);
