/*======================================================================================
 Home JS handler
 Created: TH - 03/11/2009
======================================================================================*/



//======================================================================================
// Handles Puma Home Pane Slider Widgets
//======================================================================================
ppanes = { 
    
    pane_builder: function(){
    
        
        //Set all of the buttons for initial state
        // checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 4);
        checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
        checkButtons(false, "#news_arrows .dn a", "#news_arrows .up a", iNewsBottom, iTotalNewsFeatures, 3);
        
        function gotoSelectedPane(iPaneNumber){
            iPainDifference =  iCurrentFeature - iPaneNumber;

            stMod = ((iPainDifference < 0)? '+' : '-');
            
            iMoveLocation = iPainDifference*968;
            
            $("#feature_panes_wrapper").animate({"left": "+="+iMoveLocation+"px"}, "slow");
            iCurrentFeature = iPaneNumber;
        }
        
        
        //Add an "all" class to any product that does not have a class
        $("#product_list_wrapper li").addClass("all");
        $("#product_list_wrapper li.women").removeClass("all");
        $("#product_list_wrapper li.men").removeClass("all");
        
        
        $("#all").click(function () {
            productTab('all');

            $(".women, .men, .all").animate({width: "194px"}).show();
        
            iVisibleProducts = ($("#product_list_wrapper li").length);
            
            resetProductShift(iVisibleProducts);
            checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
          return true;
        })
        
        $("#women").click(function () {
            productTab('women');
            $(".women, .all").animate({width: "194px", height: "210px"});
            $(".men").animate({opacity: 'hide', width: "0px"});
                
            /*
            $(".women").show(500,function(){
                    $(".women h3").animate({opacity:100},500);
            });
            $(".men h3 , .all h3").animate({opacity:0},500,null,function(){
                    $(".men , .all").hide(500);
            });
            */
            iVisibleProducts = $("#product_list_wrapper li.women").length + $("#product_list_wrapper li.all").length;
            
            
            //console.log("here = " + iVisibleProducts)
            
            resetProductShift(iVisibleProducts);
            checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
          return true;
        })
        
        $("#men").click(function () {
            productTab('men');
            $(".men, .all").animate({width: "194px", height: "210px"});
            $(".women").animate({opacity: 'hide', width: "0px"});

            iVisibleProducts = ($("#product_list_wrapper li.men").length) + $("#product_list_wrapper li.all").length;
            
            resetProductShift(iVisibleProducts);
            checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
          return true;
        })
        
        function resetProductShift(iVisibleProducts){
            iShiftAmount = (iProductsRight-5)*194;
            
            //console.log('iShiftAmount = ' + iShiftAmount);
            
            //iShiftAmount = iProductsRight*194;
            $("#product_list_wrapper").animate({"left": "+="+iShiftAmount+"px"}, "slide");
            iProductsRight = 5;
            checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
        }
        
        function productTab(theTab){
            $("#products_selection li").removeClass("selected");
            $("#" + theTab).addClass("selected");
        }
        
        /* $("#feature_arrows .rev a").click(function(event){
            controlClick('stop');
            iFeaturesRight = moveFrames("#features_list_wrapper", 222, 'left', iTotalFeatures, 3, iFeaturesRight);
            checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 3);
            return false;
        }); */
        
        /* $("#feature_arrows .fwd a").click(function(event){
            controlClick('stop');
            iFeaturesRight = moveFrames("#features_list_wrapper", 222, 'right', iTotalFeatures, 3, iFeaturesRight);
            checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 3);
            return false;
        }); */
        
        
        
        $("#product_arrows .rev a").click(function(event){
            iProductsRight = moveFrames("#product_list_wrapper", 194, 'left', iVisibleProducts, 5, iProductsRight);
            checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
            return false;
        });
        
        $("#product_arrows .fwd a").click(function(event){
            iProductsRight = moveFrames("#product_list_wrapper", 194, 'right', iVisibleProducts, 5, iProductsRight);
            checkButtons(true, "#product_arrows .rev a", "#product_arrows .fwd a", iProductsRight, iVisibleProducts, 5);
            return false;
        });
        
        
        $("#news_arrows .dn a").click(function(event){
            iNewsBottom = moveFrames("#news_wrapper", 134, 'down', iTotalNewsFeatures, 3, iNewsBottom);
            checkButtons(false, "#news_arrows .dn a", "#news_arrows .up a", iNewsBottom, iTotalNewsFeatures, 3);
            return false;
        });
        
        $("#news_arrows .up a").click(function(event){
            iNewsBottom = moveFrames("#news_wrapper", 134, 'up', iTotalNewsFeatures, 3, iNewsBottom);
            checkButtons(false, "#news_arrows .dn a", "#news_arrows .up a", iNewsBottom, iTotalNewsFeatures, 3);
            return false;
        });
        
        //this script was disabled, but can be used if uncommented
        //it's a method for moving the main picture based on what's clicked
        
        
        
        /* $('#features_list li').bind('click', function(event){
            $(document).stopTime("features");
            
            $('#features_list li').each(function(){
                $(this).removeClass('selected_feature');
            });
            $(this).addClass('selected_feature'); 
            
            stFeatureSelected = $(this).attr('id').substr(13);
            iFeatureSelected = parseFloat(stFeatureSelected);
            gotoSelectedPane(iFeatureSelected);
            return false;
        }) */
        
        
        
        
    
    },
    
    /* pane_auto_play: function(){
    
            isLooping = 1;
            inc = 0;
            
            checkFeatures = function() {
                
                $("#features_list li").removeClass("selected_feature");
                $("#features_list li").eq(iCurrentFeature).addClass("selected_feature");
                
                //3 at a time
                if (iCurrentFeature < iTotalFeatures) {
                    if(iCurrentFeature > 0 && iCurrentFeature%3 == 0){
                        iFeaturesRight = moveFrames("#features_list_wrapper", 222, 'right', iTotalFeatures, 3, iFeaturesRight);
                    }
                    if (iCurrentFeature == 0 && inc > 0) {
                        iFeaturesRight = moveFrames("#features_list_wrapper", 222, 'zero', iTotalFeatures, iTotalFeatures, iFeaturesRight);
                    }
                }

                checkButtons(true, "#feature_arrows .rev a", "#feature_arrows .fwd a", iFeaturesRight, iTotalFeatures, 3);

                inc++;
                
            }

            slideTo = function(pos) {
                currentFeature = pos;
                pos = (pos) * -968;
                pos = pos + "px";
                $("#feature_panes_wrapper").animate({ 
                    left: pos
                }, 500 );
                checkFeatures();
            }
            
            $("#feature_panes li a").click(function () {
                controlClick('stop');
            });
            
            
            nextFeature = function() {
                if (iCurrentFeature < iTotalFeatures-1 ) {
                    iCurrentFeature =  ++iCurrentFeature;
                    slideTo(iCurrentFeature);
                } else {
                    iCurrentFeature = 0;
                    slideTo(0);
                }
            }
            
            checkFeatures();

            $(document).everyTime(5000, "features", function(i) {
                nextFeature();
            });
            
        
            $("#loop_controls").click(function () { controlClick() });
                                                                                 
            controlClick = function (request) {
                if ((isLooping == 1) || (request == "stop")) {
                    $(document).stopTime("features");
                    $("#loop_controls").addClass('paused');
                    isLooping = 0;
                } else {
                    $("#loop_controls").removeClass('paused');
                    $(document).everyTime(5000, "features", function(i) {
                        nextFeature();
                    });
                    isLooping = 1;
                }
            };
    }, */
    
    get_latest_listener: function(){
        $('#news_items li').bind('click', function(){
            window.location = this.title;
            //console.log(this.title);
        })
    },
    
    
    
    init: function(){
        // iCurrentFeature = 0;
        // iCurrentPane = 0;
    
        // iTotalFeatures = $("#features_list li").length;
        // iFeaturesRight = 3; // Keep this many visible in frame
        
        iTotalProducts = $("#product_list_wrapper li").length;
        iProductsRight = 5; // Keep this many visible in frame
        iVisibleProducts = iTotalProducts;
        
        
        iTotalNewsFeatures = $("#news_wrapper li").length;
        iNewsBottom = 3; // Keep this many visible in frame
        
        //iTotalProducts = ($("#product_list_wrapper li").length);
        //iTotalNewsFeatures = ($("#news_wrapper li").length);
        //iVisibleProducts = iTotalProducts;
        //iCurrentFeature = 0;
        //iCurrentPane = 0;
        //iFeaturesRight = 3;
        //iProductsRight = 4;
        //iNewsBottom = 3;
        
        
        ppanes.pane_builder();
        // ppanes.pane_auto_play();
        ppanes.get_latest_listener();
    }
}



function moveFrames(stFrameID, iFrameWidth, stDirection, iTotalQuantity, iMoveAmount, iCurrentEdgeElement){
            
    if(stDirection == 'right'){
        iFeaturesLeft = iTotalQuantity-(iCurrentEdgeElement);
        
        if(iFeaturesLeft <= 0){
            return iCurrentEdgeElement;
        }
        
        if(iMoveAmount >= iFeaturesLeft){
            iMoveAmount = (iFeaturesLeft);
        }
        
        iCurrentEdgeElement += iMoveAmount;
        if(iCurrentEdgeElement >= iTotalQuantity){
            iCurrentEdgeElement = iTotalQuantity;
        }
        
        iFullMove = iMoveAmount*iFrameWidth;
        $(stFrameID).animate({"left": "-="+iFullMove+"px"}, "slow");
        
        
        
    }else if(stDirection == 'left'){
        
        if(iCurrentEdgeElement == iMoveAmount){
            return iCurrentEdgeElement;
            
        }else if((iCurrentEdgeElement-iMoveAmount) > iMoveAmount){
            iShiftAmount = iMoveAmount;
            
        }else{
            iShiftAmount = (iCurrentEdgeElement-iMoveAmount);
        }

        iFullMove = iShiftAmount*iFrameWidth;
        $(stFrameID).animate({"left": "+="+iFullMove+"px"}, "slow");

        iCurrentEdgeElement -= iShiftAmount;
                
        
    }else if(stDirection == 'up'){
        
        iFeaturesLeft = iTotalQuantity-(iCurrentEdgeElement);
        
        if(iFeaturesLeft <= 0){
            return iCurrentEdgeElement;
        }
        
        if(iMoveAmount >= iFeaturesLeft){
            iMoveAmount = (iFeaturesLeft);
        }
        
        iCurrentEdgeElement += iMoveAmount;
        if(iCurrentEdgeElement >= iTotalQuantity){
            iCurrentEdgeElement = iTotalQuantity;
        }

        
        iFullMove = iMoveAmount*iFrameWidth;

        $(stFrameID).animate({top: "-="+iFullMove+"px"}, "slow");
        
        iCurrentEdgeElement;
        
    }else if(stDirection == 'down'){
        
        if(iCurrentEdgeElement == iMoveAmount){
            return iCurrentEdgeElement;
            
        }else if((iCurrentEdgeElement-iMoveAmount) > iMoveAmount){
            iShiftAmount = iMoveAmount;
            
        }else{
            iShiftAmount = (iCurrentEdgeElement-iMoveAmount);
        }
        
        iFullMove = iShiftAmount*iFrameWidth;
        
        $(stFrameID).animate({top: "+="+iFullMove+"px"}, "slow");

        iCurrentEdgeElement -= iShiftAmount;
        
    }else{
        iCurrentEdgeElement = 3;
        $(stFrameID).animate({"left": "0px"}, "slow");
    }
    
    return iCurrentEdgeElement;
    
}

function checkButtons(isHorizontal, stLeftArrowID, stRightArrowID, iRightFeature, iTotalFeatures, iQuantityVisible){
    
    if(isHorizontal){
        if(iRightFeature >= iTotalFeatures){
            $(stRightArrowID).removeClass('on').addClass('off');
            $(stRightArrowID).show();
        }else{
            $(stRightArrowID).removeClass('off').addClass('on');
            $(stRightArrowID).show();
        }
        
        if((iRightFeature-iQuantityVisible) > 0){
            $(stLeftArrowID).removeClass('off').addClass('on');
            $(stLeftArrowID).show();
        }else{
            $(stLeftArrowID).removeClass('on').addClass('off');
            $(stLeftArrowID).show();
        }
        
    }else{
        if(iRightFeature > (iTotalFeatures-1)){
            $(stRightArrowID).removeClass('on').addClass('off');
            $(stRightArrowID).show();
        }else{
            $(stRightArrowID).removeClass('off').addClass('on');
            $(stRightArrowID).show();
        }
        
        if((iRightFeature-iQuantityVisible) > 0){
            $(stLeftArrowID).removeClass('off').addClass('on');
            $(stLeftArrowID).show();
        }else{
            $(stLeftArrowID).removeClass('on').addClass('off');
            $(stLeftArrowID).show();
        }
        
    }
}




$(document).ready(function() {
    ppanes.init();
    
    $('#features_list li').click(function () {
        window.location = $(this).find('a').attr('href');
    })
    
});
