﻿//Navigation Controller
var navigation = {
    trigger     : Object,
    init        : function(){
        this.trigger = $('#navigation li');
        this.run();
    },
    run         : function(){
        this.trigger.hover(function(){
            $(this).addClass('on');
        }, function(){
            $(this).removeClass('on')
        });
    }
}

//Cufon Controller
var cufon = {
    replaced    : Object,
    init        : function(){
        Cufon.replace('#header ul li h3, #header ul li a, #secondNav a',{hover:true});
        Cufon.replace('h1, #nextGenList h5, div.title h3', {color: '-linear-gradient(0.5=#ffffff, 0.8=#86b9da)'});
        Cufon.replace('h2#headline', {color: '-linear-gradient(0.5=#ffffff, 0.8=#414f5f)'});
        Cufon.replace('.r, h3#subhead, .infoBox *, h2.fromGM');
        Cufon.replace('#content .DealerResultsTable th');
    }
}

// anchor controller
var anchor = {
    init : function(){
        $('a[rel*=external]').click(function(){
            window.open(this.href);
            pageTracker._trackEvent('Yamaha V6', 'External Link Clicked', $(this).attr('href'));
            return false;
        });
        
        $('a[rel=pdf]').click(function(){
            pageTracker._trackEvent('PDF', 'Open PDF', $(this).text());
        });
        
        $('#carousel li a').click(function(){
            pageTracker._trackEvent('Gallery', 'View Image', $(this).find('img').attr('alt'));
        });
        
    }
}

// Gallery Controller
var gallery = {
    thumbs  : Object,
    large   : Object,
    margin  : Object,
    width   : Object,
    init    : function(){
        this.thumbs = $('#carousel ul li a');
        this.margin = 0;
        this.width = parseInt($('#carousel ul').width() - $('#carousel').width());
        this.run();
    },
    run     : function(){
        
        $('#previous').fadeTo('fast',0.5);
        $('#carousel ul li a img:not(#carousel ul li a.on img)').fadeTo('fast', 0.5);
    
        this.thumbs.click(function(e){
            $('#carousel ul li a.on').removeClass('on').find('img').fadeTo('fast',0.5);
            $(this).addClass('on');
            $('#gallery .on').fadeOut('fast', function(){
                $(this).removeClass('on')
                $('#gallery .' + $('#carousel ul li a.on').attr('rel')).addClass('on').fadeIn('fast');
            });
            e.preventDefault();
        });
        
        this.thumbs.hover(function(){
            $(this).find('img').fadeTo('fast',1);
        }, function(){
            if(!$(this).hasClass('on')){
                $(this).find('img').fadeTo('fast',0.5);
            }
        });
        
        $('#thumbs #next').click(function(e){
        
            if(gallery.margin > -gallery.width){
                $('#previous').fadeTo('fast',1);
                $(this).fadeTo('fast',1);
                gallery.margin = gallery.margin - 92;
                $('#carousel ul').animate({marginLeft : gallery.margin + 'px'}, 'fast');
                if(gallery.margin == -gallery.width){
                    $(this).fadeTo('fast',0.5);
                }
                
            }
            e.preventDefault();
        });
        
        $('#thumbs #previous').click(function(e){
            if(gallery.margin < 0){
                $('#next').fadeTo('fast',1);
                $(this).fadeTo('fast',1);
                gallery.margin = gallery.margin + 92;
                $('#carousel ul').animate({marginLeft : gallery.margin + 'px'}, 'fast');
                if(gallery.margin == 0){
                    $(this).fadeTo('fast',0.5);
                }
            }
            e.preventDefault();
        });
        
        
    }
}


// Page Initialization
var animation = {
    a       : Object,
    b       : Object,
    c       : Object,
    d       : Object,
    e       : Object,
    f       : Object,
    init    : function(){
        this.a = $('img#lighter');
        this.b = $('img#stronger');
        this.c = $('img#faster');
        this.d = $('img#smarter');
        this.e = $('#sub-title img');
        this.f = $('#home-title');
        this.run();
    },
    run     : function(){
        setTimeout(function(){
            animation.a.fadeIn(800, function(){
                animation.f.animate({'marginLeft':'-215px'}, 1000, function(){
                    animation.b.fadeIn(800, function(){
                        animation.f.animate({'marginLeft':'-302px'}, 1000, function(){
                            animation.c.fadeIn(800, function(){    
                                animation.f.animate({'marginLeft':'-404px'}, 1000, function(){
                                    animation.d.fadeIn(800, function(){
                                        animation.e.fadeIn(800);
                                    });
                                });
                            });
                        });    
                    });
                });
            });
        }, 500);
    }
}


// Page Initialization
function initialize(){
    navigation.init();
    cufon.init();
    anchor.init();
    gallery.init();
    animation.init();
}


// Start
$(document).ready(function(){
    initialize();
});
