﻿/// <reference path="jquery-1.3.2.min-vsdoc.js"/>

// Cufon Initialization
var cufonInitialization = {
    init : function(){
        Cufon.replace('#rCol h1, #rCol h2, #info h1');
    }
}


// Truck Navigation
var truckNavigation = {
    init : function() {
        this.run();
    },
    run : function(){
    
        var newIndex = 0;
        var newHeight = 0;
        
        $('ul.switcher li a:not(ul.switcher li#brochure a)').click(function(e){
            $('ul.switcher li.on').removeClass('on');
            $(this).parent().addClass('on');
            
            $('rCol').height($('.slide.on').height() + 21);   
            newIndex = $('ul.switcher li a').index($(this));
            
            $('.slide.on').stop(true, true).fadeOut('normal', function(){
                $(this).removeClass('on');
                
                newHeight = $('.slide').eq(newIndex).height() + 21;
                
                $('#rCol').animate({
                    height: newHeight
                }, 'fast', function(){
                    $('.slide').eq(newIndex).stop(true, true).fadeIn('normal', function(){
                        $(this).addClass('on')
                    });
                })
            });        
            
            e.preventDefault();
        });
    }
}


// Dropdown
var dropDown = {
    dropper : Object,
    init : function() {
        this.dropper = $('div#navigation div.wrapper ul li');
        this.run();
    },
    run : function(){
        this.dropper.hover(function(){
            $(this).addClass('hovering');
        },function(){
            $(this).removeClass('hovering');
        });
    }
}



// Filter Control
var filterControl = {
    treeNav : Object,
    currentFilters : Array,
    init : function(){
        this.treeNav = $('#filters ul li a:not(#filters ul li li a)');
        this.currentFilters = $('#currentFilters ul li a');
        $('#filters ul li.on:not(#filters ul li.first.on)').removeClass('on');
        this.run();
    },
    run : function(){
        this.treeNav.click(function(e){
            $(this).parent().toggleClass('on');
            e.preventDefault();
        });
        this.currentFilters.click(function(e){
            $(this).parent().fadeOut('fast', function(){
                $(this).remove();
                                
                if($('#currentFilters ul li').length == 0){
                    $('div#currentFilters').slideUp('fast', function(){
                        $('h4#currentFiltersTitle').slideUp('fast', function(){
                            $(this).remove();
                        });
                        $(this).remove();
                    });
                }
                
            });
        });
    }
}


// Search Results Control
var searchResults = {
    entries : Object,
    link : String,
    init : function(){
        this.entries = $('.entry');
        this.run();
    },
    run : function(){
        this.entries.hover(function(){
            $(this).addClass('hovering');
        }, function(){
            $(this).removeClass('hovering');
        });
        
        this.entries.click(function(){
            this.link = $(this).find('.info h3 a').attr('href');
            window.location = this.link;
        });
    }
}


// used Truck gallery control
var usedGallery = {
    init : function(){
        $('ul#thumbs li:first').addClass('on');
        $('div#largeImage div.enlarger:first').fadeIn('fast').addClass('on');
        $('ul#thumbs li img:not(ul#thumbs li.on img)').fadeTo('fast', 0.65);
        $('ul#specList li:odd').addClass('zebra'); //<-- bad programming.  This line has nothing to do with the gallery.  Lazy bum.
        this.run();
    },
    run : function(){
    
        // Highlights on hover
        $('ul#thumbs li img:not(ul#thumbs li.on img)').live('mouseover', function(){
            $(this).stop(true, true).fadeTo('fast', 1);   
        });
        $('ul#thumbs li img:not(ul#thumbs li.on img)').live('mouseout', function(){
            $(this).stop(true, true).fadeTo('fast', 0.65);
        });
        
        // Switches the image on click
        $('ul#thumbs li a').click(function(e){
                    
            // Removes the 'on' class
            $('ul#thumbs li.on img').fadeTo('fast', 0.65, function(){
                $(this).parents('li').removeClass('on');
            });
        
            // Adds the 'on' class.  Image is highlighted by the hover function above.
            $(this).parents('li').addClass('on');
            
            var newImage = $(this).attr('rel');
            
            $('div#largeImage div.enlarger.on').fadeOut('fast', function(){
                $(this).removeClass('on');
                $('div#largeImage div.enlarger').eq(newImage).fadeIn('fast').addClass('on');
            })
                
            e.preventDefault();
        });
    }
}


// Page Initialization
function initialize(){
    cufonInitialization.init();
    truckNavigation.init();
    dropDown.init();
    filterControl.init();
    searchResults.init();
    usedGallery.init();
}


// Start
$(document).ready(function(){
    initialize();
});


