﻿// Navigation Controller
var nav = {
    navLink     : Object,
    init        : function(){
        navLink = $('#navigation li a');
        this.run();
    },
    run         : function(){
    
        // Removes default highlight, adds span to attatch fade highlight
        navLink.each(function(){
            $(this).removeClass('highlight').append('<span class="hover">&nbsp;</span>'); 
        });
        
        // Fade In/Out Highlight
        navLink.hover(function(){
            $(this).find('span').stop(true, true).fadeIn(300);
        }, function(){
            $(this).find('span').stop(true, true).fadeOut(1000);
        });
    
    }
}


// Sub Navigation Controller
var subNav = {
    subNavLink     : Object,
    init        : function(){
        subNavLink = $('#subNavigation a');
        this.run();
    },
    run         : function(){
    
        // Removes default highlight, adds span to attatch fade highlight
        subNavLink.each(function(){
            $(this).append('<span class="hover"></span>'); 
        });
        
        // Fade In/Out Highlight
        subNavLink.hover(function(){
            $(this).find('span.hover').stop(true, true).fadeIn(300);
        }, function(){
            if($(this).hasClass('on')){}else{
                $(this).find('span.hover').stop(true, true).fadeOut(1000);
            }
        });
    
    }
}


// Crossfade Controller
var crossfade = {
    container   : Object,
    init        : function(){
        this.container = $('#crossfade');
        this.run();
    },
    run         : function(){   
        this.container.innerfade({
            speed: 1000,
            timeout: 6000,
            containerheight: '450px' 
        });
    }
}


// Cufon Controller
var cufon = {
    replaced: Object,
    replacedWithHover: Object,
    init: function () {
        this.replaced = $('.r, .infoBox *');
        this.replacedWithHover = $('#subNavigation a, #feature #playBtn');
        this.run();
    },
    run: function() {
        Cufon.replace(this.replaced);
        Cufon.replace(this.replacedWithHover, {hover:true});
    }
}


// Pros Controller
var pros = {
    stage       : Object,
    trigger     : Object,
    init        : function(){
        this.stage = $('ul#pros');
        this.trigger = $('ul#pros li');
        this.run();
    },
    run         : function(){
   
        this.stage.hover(function(){
            $('.thumb').fadeTo('normal',.5);
        }, function(){
            $('.thumb').fadeTo('normal',1);
        });
   
        this.trigger.hoverIntent(function(){
            $(this).find('a img').stop(true, true).fadeOut();
            $(this).find('.proInfo').stop(true, true).fadeIn();
        }, function(){
            $(this).find('a img').fadeIn();
            $(this).find('.proInfo').fadeOut();        
        });
    }
}


// anchor controller
var anchor = {
    init : function(){
        $('a[rel*=external]').click(function(){
            window.open(this.href);
            pageTracker._trackEvent('Yamaha V MAX SHO', '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);
            }
        });
        
        $('#next').click(function(e){
        
            if(gallery.margin > -gallery.width){
                $('#previous').fadeTo('fast',1);
                $(this).fadeTo('fast',1);
                gallery.margin = gallery.margin - 54;
                $('#carousel ul').animate({marginLeft : gallery.margin + 'px'}, 'fast');
                if(gallery.margin == -gallery.width){
                    $(this).fadeTo('fast',0.5);
                }
                
            }
            e.preventDefault();
        });
        
        $('#previous').click(function(e){
            if(gallery.margin < 0){
                $('#next').fadeTo('fast',1);
                $(this).fadeTo('fast',1);
                gallery.margin = gallery.margin + 54;
                $('#carousel ul').animate({marginLeft : gallery.margin + 'px'}, 'fast');
                if(gallery.margin == 0){
                    $(this).fadeTo('fast',0.5);
                }
            }
            e.preventDefault();
        });
        
        
    }
}

var email = {
    toggler : Object,
    form    : Object,
    init    : function(){
        this.toggler = $('#emailAFriend a, #emailForm a.controls');
        this.form = $('#emailForm');
        this.run();
    },
    run     : function(){
    
        this.toggler.click(function(e){
            email.form.toggle();
            e.preventDefault();
        });
    
//        $('input:text, textarea').focus(function(){
//            if($(this).val() == $(this).attr('defaultValue')){
//                $(this).val('');
//            }
//        });

//        $('input:text, textarea').blur(function(){
//            if($(this).val() == ''){
//                $(this).val($(this).attr('defaultValue'));
//            }
//        });
    }
}


// Page Initialization
function initialize(){
    nav.init();
    subNav.init();
    crossfade.init();
    cufon.init();
    pros.init();
    anchor.init();
    gallery.init();
    email.init();
}


// Start
$(document).ready(function(){
    initialize();
});
