$(function(){
    $('#search').each(function(){
        var $search = $(this);
        
        var currentTime = new Date();
        var feb_year = currentTime.getFullYear();
        
        var month = currentTime.getMonth() + 1;
        var day = currentTime.getDate();
        
        $('div.date select[name="depmonth"] option').each(function(){
            var $this = $(this);
            var $select = $this.parent();
            var val = parseInt($this.val());
            if(val < month)
            {
                if(val == 2)
                {
                    feb_year++;
                }
                $this.detach().appendTo($select);
            }
            else
            {
                return false;
            }
        });

        $('div.date select[name="depmonth"]').change(function(){
            var selmonth = parseInt($(this).val());
            var days = 31;
            
            switch(selmonth)
            {
                case 4: 
                case 6: 
                case 9: 
                case 11: 
                    days = 30;
                    break;
                case 2: 
                    days = 28;
                    if(feb_year % 4 == 0 && feb_year % 100 != 0 || feb_year % 400 == 0)
                    {
                        days = 29;
                    }
                    break;
            }
            var i = 0;
            
            var $selectDay = $('div.date select[name="depday"]');
            $('option', $selectDay).each(function(){
                var $this = $(this);
                var val = parseInt($this.val());
                if(val > days)
                {
                    $this.remove();
                }
                i++;
            });
            for(var j = i; j <= days; j++)
            {
                $selectDay.append('<option value="'+j+'">'+j+'</option>');
            }
        });

        $('div.date select[name="depday"] option[value="'+day+'"]').attr('selected', 'selected').parent().change();
        $('div.date select[name="depmonth"] option[value="'+month+'"]').attr('selected', 'selected').parent().change();
        
        $('div.options label input').change(function(){
            var $parent = $(this.parentNode.parentNode);
            var self = this;
            
            if($(this).is(':checked'))
            {       
                $('label.'+self.parentNode.className+' input:checked', $parent).each(function(){
                    if(this != self)
                    {
                        $(this).click();
                    }
                });
            }
        });
        
        $('div.submit a').click(function(){
            return false;
        }).hover(function(){
            $('#search div.warranty_tooltip').show();
        }, function(){
            $('#search div.warranty_tooltip').hide();
        });//.tipsy();
    });
});
