var FSI = (function (name) {return name;}(FSI || {}));
FSI.Controller.Navigation = (function(){
    var currentView, previousView,isLocked=false;
    var getView=function(){
        currentView = FSI.Model.Content.get();
    };
    var gotoView=function(view,dir){
        previousView = currentView;
        getView();
        FSI.Controller.Animation.start(previousView,FSI.Controller.Navigation,'callback',dir,true);
        FSI.Controller.Animation.start(currentView,FSI.Controller.Navigation,'callback',dir,false);
        $('#primary_navigation a').removeClass('current');
        $('#primary_navigation a[href=#'+currentView+']').addClass('current');
    };
    var gotoViewFromTopNav=function(view,dir){
        previousView = currentView;
        currentView = view; 
        FSI.Controller.Animation.start(previousView,FSI.Controller.Navigation,'callback',dir,true);
        FSI.Controller.Animation.start(currentView,FSI.Controller.Navigation,'callback',dir,false);
        FSI.Model.Content.set(currentView);
    };
    var getViewName = function(dir){ // get the next view from direction
        gotoView(FSI.Model.Content.get(dir),dir);  
    };
    var getDirection = function(newView){      
        return FSI.Model.Content.getIndex(newView) > FSI.Model.Content.getIndex() ? "next" : "prev";
    };
    var setBuildPriceBtn = function(){
        if(FSI.Model.Content.get() === "toyota_matrix"){
            $('.build_price_btn').attr("href", "http://www.toyota.ca/toyota/"+TPM.Core.getLocale()+"/build-price/mat");
            $('.build_price_btn').addClass("matrix_view");
        }else{
            $('.build_price_btn').attr("href", "http://www.toyota.ca/toyota/"+TPM.Core.getLocale()+"/build-price/cor");
            $('.build_price_btn').removeClass("matrix_view");
        }
    };
    var setListeners = function(){         
        $('.controls.prev').hover(
            function(){
                $('#linear_navigation').addClass('hovered');
                $('#linear_navigation #prev div').addClass('hovered');
                $('#prev label.'+FSI.Model.Content.getPrev()).show();
            },
            function(){
                $('#linear_navigation').removeClass('hovered');
                $('#linear_navigation #prev div').removeClass('hovered');
                $('#prev label.'+FSI.Model.Content.getPrev()).hide();
            });
        $('.controls.next').hover(
            function(){
                $('#linear_navigation').addClass('hovered');
                $('#linear_navigation #next div').addClass('hovered');
                $('#next label.'+FSI.Model.Content.getNext()).show();
            },
            function(){
                $('#linear_navigation').removeClass('hovered');
                $('#linear_navigation #next div').removeClass('hovered');
                $('#next label.'+FSI.Model.Content.getNext()).hide();
            });    
         
        $('.controls.next').click(function(){
            if(isLocked){return false;} // don't want any click action while animation is working
            $('#linear_navigation label').hide();
            getViewName('next');
            isLocked = true;
            setBuildPriceBtn();
            return false;
        });
        $('.controls.prev').click(function(){
            if(isLocked){return false;} // don't want any click action while animation is working
            $('#linear_navigation label').hide();
            getViewName('prev');
            isLocked = true;
            setBuildPriceBtn();
            return false;
        });
        $('#primary_navigation a').click(function(e){
            e.stopPropagation();        
            var v = $(this).attr('href').substr(1);          
            if(isLocked || v == currentView){return false;}          
            gotoViewFromTopNav(v,getDirection(v));
            $('#primary_navigation a').removeClass('current');
            $(this).addClass('current');
            isLocked = true;
            setBuildPriceBtn();
            return false;
        });
        
        $('.model_lineup #model_navigation a').click(function(e){
            e.stopPropagation();
            var model = $(this).attr('href').substr(1);
            $('.model_lineup.primary_content img').hide();
            $('.model_lineup .model_name_price div').hide();
            $('.model_lineup .model_name_price div.'+model).show();
            $('.model_lineup .model_description div').hide();
            $('.model_lineup .model_description div.'+model).show();
            $('.model_lineup.primary_content img.'+model).show();
            $('.model_lineup #model_navigation a').removeClass('current');
            $(this).addClass('current');
            return false;
            
        });
        $('.stylish_features.secondary_content li').mouseover(function(e){
            $('.stylish_features.secondary_content li').css({'background-position':'0 -1069px'});
            $(this).css({'background-position':'0 -1097px'});
            $('.stylish_features.secondary_content .features_image img').attr('src','/media/content/stylish_features/'+$(this).attr('class')+'.jpg');
        });
        //initial state of stylish features interface
        $('.steering_wheel').mouseover();
    };
    var preload = function(){
        new Image().src = '/media/content/stylish_features/air_con.jpg';
        new Image().src = '/media/content/stylish_features/steering_wheel.jpg';
        new Image().src = '/media/content/stylish_features/fog_lamps.jpg';
        new Image().src = '/media/content/stylish_features/power_steering.jpg';
        new Image().src = '/media/content/stylish_features/cruise_control.jpg';
        new Image().src = '/media/content/stylish_features/satellite_ready.jpg';
        new Image().src = '/media/content/stylish_features/star_safety.jpg';
        new Image().src = '/media/content/stylish_features/woodgrain_trim.jpg';
        new Image().src = '/media/content/stylish_features/alloy_wheels.jpg';
        new Image().src = '/media/content/stylish_features/bluetooth.jpg';
        new Image().src = '/media/content/stylish_features/power_windows.jpg';
        new Image().src = '/media/content/stylish_features/acoustics.jpg';
        new Image().src = '/media/content/stylish_features/keyless_entry.jpg';
    };
   return{
        init:function(){
            getView();
            setListeners();
            preload();          
        },
        getView:function(){
            return getView();
        },
        callback:function(){
            isLocked = false;
        }
   }; 
}());
