﻿/*******************************
*   LOCATE FLASH MOVE ON PAGE  *
*******************************/

function getFlashMovieObject(movieName) {
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName])
            return document.embeds[movieName];
    }
    else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    {
        return document.getElementById(movieName);
    }
}

/*******************************
*   FLASH REPLACEMENT TEXT     *
*******************************/

function runSifr() {
    if (typeof sIFR == "function") {
        
        sIFR.replaceElement(named({ sSelector: 'h3.page-title', sFlashSrc: '/flash/cholla.swf', sColor: '#000000', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h3.HL-cholla', sFlashSrc: '/flash/cholla.swf', sColor: '#FCAF17', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h4.page-subtitle', sFlashSrc: '/flash/cholla.swf', sColor: '#ffffff', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h3.product-title', sFlashSrc: '/flash/cholla.swf', sColor: '#000000', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h3.interior-header', sFlashSrc: '/flash/cholla.swf', sColor: '#ffffff', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h3.single-column-interior-header', sFlashSrc: '/flash/cholla.swf', sColor: '#e1f844', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h3.single-column-interior-header-dark', sFlashSrc: '/flash/cholla.swf', sColor: '#f8edc1', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h3.cholla', sFlashSrc: '/flash/cholla.swf', sColor: '#e1f844', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h4.cholla', sFlashSrc: '/flash/cholla.swf', sColor: '#e1f844', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h3.cholla-inactive', sFlashSrc: '/flash/cholla.swf', sColor: '#ffffff', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'span.cholla', sFlashSrc: '/flash/cholla.swf', sColor: '#000000', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'span.cholla-white', sFlashSrc: '/flash/cholla.swf', sColor: '#ffffff', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'span.cholla-dark', sFlashSrc: '/flash/cholla.swf', sColor: '#000000', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'span.cholla-yellow', sFlashSrc: '/flash/cholla.swf', sColor: '#e1f844', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'span.cholla-multi-column-title', sFlashSrc: '/flash/cholla.swf', sColor: '#FFFFFF', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'span.cholla-multi-column-description', sFlashSrc: '/flash/cholla.swf', sColor: '#000000', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'h4.product-description-intro', sFlashSrc: '/flash/cholla.swf', sColor: '#000000', sWmode: 'transparent' }));
        sIFR.replaceElement(named({ sSelector: 'span.cholla_green', sFlashSrc: '/flash/cholla.swf', sColor: '#e1f844', sWmode: 'transparent' }));
        sIFR.setup();
    }
}


/*******************************
*   JQUERY PAGE LOAD EVENT     *
*******************************/

$(document).ready(
    function() {
        var isIE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') !== -1);
        if (isIE6 == true) {
            //$("div,img,a,h2,h3,h4").ifixpng();
            //$(document).pngFix();
            suckerFishSetup();
        }
        runSifr();
        
        if ($("#home-callout-scroller").size() > 0) {
            setUpHomeCalloutScroll();
        }
        
        if ($("#character-microsite-scroller").size() > 0) {
            setUpCharacterMicrositeScroll($("#character-microsite-scroller")[0]);
        }
        
        if ($("#home-product-scroller").size() > 0) {
            setUpHomeProductScroll();
        }
        
        if ($("#other-left-slider").size() > 0) {
            setUpProductDetailSlider($("#other-left-slider")[0]);
        }
        
        if ($("#other-right-slider").size() > 0) {
            setUpProductDetailSlider($("#other-right-slider")[0]);
        }
        
        if ($("#home-callout-scroller").size() > 0) {
            $("a[rel^='prettyPhoto']").prettyPhoto();
        }
        
        if ($("#product-detail").size() > 0) {
            setUpProductDetailPageTracking();
        }
        
        if ($("#buy-info").size() > 0) {
            setUpGoogleTracking("#buy-info");
        }
        
        if ($(".game-callout").size() > 0) {
            setUpGoogleTracking(".game-callout");
        }
        
        if ($(".pdf-list").size() > 0) {
            setUpGoogleTracking(".pdf-list");
        }
        
    }
);

function setUpGoogleTracking(id) {
    var links = $(id + " a");
    
    for (var i = 0; i < links.length; i++) {
        var link = links[i];
        $(link).attr("href", "/ExternalLink.aspx?url=" + $(link).attr("href"));
    }
}

/********************************
*    SUCKERFISH DROP DOWN-MENUS *
*    FOR IE6                    *
********************************/

function suckerFishSetup() {

    var sfEls = document.getElementById("main-nav").getElementsByTagName("LI");
    for (var i = 0; i < sfEls.length; i++) {
        sfEls[i].onmouseover = function() {
            this.className += " sfhover";
        }
        sfEls[i].onmouseout = function() {
            this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
}

/********************************
*    PRODUCT DETAIL PAGE        *
********************************/

function setUpProductDetailPageTracking() {
    $("#product-detail .detail .where-to-buy-wrapper .where-to-buy a").click(function () {
        changeTabs('buy');
        if (pageTracker) {
            //alert(window.location + '/where-to-buy');
            pageTracker._trackPageview();
        }
    }).keypress(function() {
        changeTabs('buy');
        if (pageTracker) {
            //alert(window.location + '/where-to-buy');
            pageTracker._trackPageview();
        }
    });
    
    $("#buy-info table a").click(function () {
        if (pageTracker) {
            var trackString = window.location + "/retailer/" + $(this).attr("href");
            //alert(trackString);
            pageTracker._trackPageView(trackString);
        }
    }).keypress(function () {
        if (pageTracker) {
            var trackString = window.location + "/retailer/" + $(this).attr("href");
            //alert(trackString);
            pageTracker._trackPageView(trackString);
        }
    });
}

/********************************
*    PRODUCT DETAIL TABS        *
********************************/

function changeTabs(obj) {
    var compareId;
    if (typeof(obj) == "string") {
        compareId = obj;
    } else {
        compareId = obj.parentNode.id;
    }
    var contentId = compareId + "-info";
    var tabs = $(".product-info .tabs .tab");
    var content = $("#product-info-content-wrapper").children();
    
    for (var i=0; i<tabs.length; i++) {
        var el = tabs[i];
        if (el.id == compareId) {
            $(el).addClass("selected");
        } else {
            $(el).removeClass("selected");
        }
    }
    
    for (var i=0; i<content.length; i++) {
        var el = content[i];
        if (el.id == contentId) {
            $(el).css("display", "");
        } else {
            $(el).css("display", "none");
        }
    }
    
    if (window.location.toString().indexOf("#buy") < 0) {
        window.location = window.location + "#buy";
    } else {
        window.location = window.location.toString().substr(0,window.location.toString().indexOf("buy") - 1) + "#buy";
    }
}


/********************************
*    PRODUCT DETAIL SLIDERS     *
********************************/

//Need to use different enabled/disabled classes because of IE6
//problem with distinguishing elements with multiple classes.

var sliderPos = {left:0, right:0};

function setUpProductDetailSlider(slider) {
    var products = $(slider).children(".product");
    
    if (products.length > 1) {
        var backButton = $(slider).parent().parent().children(".back-inactive")[0];
        var forwardButton = $(slider).parent().parent().children(".forward-enabled")[0];
        
        $(backButton).css("visibility", "visible");
        $(forwardButton).css("visibility", "visible");
    }
}

function moveSlider(obj) {
    
    var direction = $(obj).parent()[0].className;
    var slider = $(obj).parent().parent().children(".window").children(".slider")[0];
    
    
    var sliderName = "left";
    
    if (slider.id.indexOf("right") > -1) {
        sliderName = "right";
    }
    
    var backButton = $(obj).parent().parent().children("#" + sliderName + "-back")[0];
    var forwardButton = $(obj).parent().parent().children("#" + sliderName + "-forward")[0];
    
    var totalProducts = $(slider).children(".product").length;
    var nextPos = sliderPos[sliderName];
    
    if (direction.indexOf("back") > -1 && nextPos > 0) {
        nextPos = nextPos - 1;
    }
    
    if (direction.indexOf("forward") > -1 && nextPos < totalProducts - 1) {
        nextPos = nextPos + 1;
    }
    
    if (nextPos == totalProducts - 1) {
        $(forwardButton).removeClass("forward-enabled");
        $(forwardButton).addClass("forward-disabled");
        $(backButton).removeClass("back-inactive");
        $(backButton).addClass("back-active");
    }
    if (nextPos > 0 && nextPos < totalProducts - 1) {
        $(forwardButton).addClass("forward-enabled");
        $(forwardButton).removeClass("forward-disabled");
        $(backButton).removeClass("back-inactive");
        $(backButton).addClass("back-active");
    }
    if (nextPos == 0) {
        $(forwardButton).addClass("forward-enabled");
        $(forwardButton).removeClass("forward-disabled");
        $(backButton).removeClass("back-active");
        $(backButton).addClass("back-inactive");
    }
    sliderPos[sliderName] = nextPos;
    var nextLeft = -(nextPos * 140);
    
    $(slider).animate({ left:nextLeft }, "normal", "swing");
}

/********************************
*    HOME PAGE PRODUCT SCROLL   *
********************************/

var homeSliderPos = 0;
var firstLoadedImage = 8;

function setUpHomeProductScroll() {
    var productLinks = $("#home-product-scroller .product .image a");
    
    $("#back-control a, #forward-control a").click(function () {
        homeScrollProducts(this);
    }).keypress(function(event) {
        homeScrollProducts(this);
    });
    
    for (var i=0; i<productLinks.length; i++) {
        var el = productLinks[i];
        $(el).mouseenter( function() {
            $(this).parent().parent().children(".rollover").css("visibility", "visible");
        }).mouseleave( function() {
            $(this).parent().parent().children(".rollover").css("visibility", "hidden");
        });
    }
}

function homeScrollProducts(obj) {
    
    var direction = $(obj).parent()[0].id;
    var slider = $(obj).parent().parent().children("#home-product-scroll-window").children("#home-product-scroller")[0];
    var backButton = $(obj).parent().parent().children("#back-control")[0];
    var forwardButton = $(obj).parent().parent().children("#forward-control")[0];
    
    var totalProducts = $(slider).children(".product").length;
    var nextPos = homeSliderPos;
    
    if (direction.indexOf("back") > -1 && nextPos > 0) {
        nextPos = nextPos - 1;
    }
    
    if (direction.indexOf("forward") > -1 && nextPos < totalProducts - 7) {
        nextPos = nextPos + 1;
        if (nextPos + 7 > firstLoadedImage) {
            checkHomeSliderImageLoaded(slider,nextPos + 7);
        }
    }
    
    if (nextPos == totalProducts - 7) {
        $(forwardButton).addClass("inactive");
        $(backButton).removeClass("inactive");
    }
    if (nextPos > 0 && nextPos < totalProducts - 7) {
        $(forwardButton).removeClass("inactive");
        $(backButton).removeClass("inactive");
    }
    if (nextPos == 0) {
        $(forwardButton).removeClass("inactive");
        $(backButton).addClass("inactive");
    }
    homeSliderPos = nextPos;
    var nextLeft = -(nextPos * 112);
    
    $(slider).animate({ left:nextLeft }, "normal", "swing");
}

function checkHomeSliderImageLoaded(slider,nextImage) {
    var product = $(slider).children(".product")[nextImage - 1];
    var link = $(product).children(".image").children("a")[0];
    var image = $(link).children("img")[0];
    var imageUrl = $(link).find(".image-url").text();
    
    if(imageUrl !== image.src) {
        image.src = imageUrl;
    }
}

/********************************
*    HOME PAGE CALLOUT SCROLL   *
********************************/

var totalCalloutScrollWidth = 0;
var maxCalloutScroll = 0;
var netCalloutScrollBar = 0;

function setUpHomeCalloutScroll() {

    var callouts = $("#home-callout-scroller .home-callout");
    
    for (var i=0; i<callouts.length; i++) {
        totalCalloutScrollWidth += $(callouts[i]).width() + 10;
    }
    
    if (totalCalloutScrollWidth > 980) {
        maxCalloutScroll = -(totalCalloutScrollWidth - 960);
        netCalloutScrollBar = 960 - $("#home-callout-scroll-handle").width();
        
        $("#home-callout-scroll-handle").draggable(
            {drag: handleCalloutDrag,
             containment: "#home-callout-scroll-bar"}
        )
    } else {
       $("#home-callout-scroll-handle").css("display", "none");
    }
}

function handleCalloutDrag(event, ui) {
    var percent = ui.position.left/netCalloutScrollBar;
    
    var newLeft = maxCalloutScroll * percent;
    
    $("#home-callout-scroller").css("left", newLeft + "px");
}

function clearTextField(obj) {
    obj.value = "";
}

/***************************************
* CHARACTER MICROSITE PRODUCT SCROLL   *
***************************************/

var characterMicroSliderPos = 0;
var characterMicroFirstLoadedImage = 4;

function setUpCharacterMicrositeScroll(slider) {
    /*var totalProducts = $(slider).children(".product").length;
    
    if (totalProducts <= 3) {
        var backButton = $(obj).parent().parent().children("#back-control")[0];
        var forwardButton = $(obj).parent().parent().children("#forward-control")[0];
        
        $(backButton).css("display", "none");
        $(forwardButton).css("display", "none");
    }*/
    
    var products = $(slider).children(".product");

    if (products.length > 3) {
        var backButton = $("#back-control")[0];
        var forwardButton = $("#forward-control")[0];
        //alert("backButton: " + backButton + "\nforwardButton: " + forwardButton);
        
        $(backButton).css("visibility", "visible");
        $(forwardButton).css("visibility", "visible");
    }
}

function characterMicrositeScrollProducts(obj) {
    //alert(obj);
    var direction = $(obj).parent()[0].id;
    var slider = $(obj).parent().parent().children("#character-microsite-scroll-window").children("#character-microsite-scroller")[0];
    var backButton = $(obj).parent().parent().children("#back-control")[0];
    var forwardButton = $(obj).parent().parent().children("#forward-control")[0];
    
    var totalProducts = $(slider).children(".product").length;
    var nextPos = characterMicroSliderPos;
    
    if (direction.indexOf("back") > -1 && nextPos > 0) {
        nextPos = nextPos - 1;
    }
    
    if (direction.indexOf("forward") > -1 && nextPos < totalProducts - 3) {
        nextPos = nextPos + 1;
        /*if (nextPos + 3 > firstLoadedImage) {
            checkImageLoaded(slider,nextPos + 3);
        }*/
    }
    
    if (nextPos == totalProducts - 3) {
        $(forwardButton).addClass("inactive");
        $(backButton).removeClass("inactive");
    }
    if (nextPos > 0 && nextPos < totalProducts - 3) {
        $(forwardButton).removeClass("inactive");
        $(backButton).removeClass("inactive");
    }
    if (nextPos == 0) {
        $(forwardButton).removeClass("inactive");
        $(backButton).addClass("inactive");
    }
    characterMicroSliderPos = nextPos;
    var nextLeft = -(nextPos * 207);
    
    $(slider).animate({ left:nextLeft }, "normal", "swing");
}

function checkImageLoaded(slider,nextImage) {
    var product = $(slider).children(".product")[nextImage - 1];
    var link = $(product).children(".image").children("a")[0];
    var image = $(link).children("img")[0];
    
    if(link.name !== image.src) {
        image.src = link.name;
    }
}


/************************************
* Huffy Home Flash Links        *
************************************/

function showDisneyPrincessCommercial() {
    $.prettyPhoto.open('flash/VideoPlayer.swf?width=720&height=550&flashvars=basePath=/flash/externals/&videoFile=DisneyPrincessCommercial.flv', 'Huffy Disney Princess&copy; Commercial', '');
}

/*function showGreenMachine360() {
    $.prettyPhoto.open('/flash/externals/flv/HufGreenMachine360.mov', 'Huffy Disney Green Machine', '');
}*/

function showGreenMachine360() {
    $.prettyPhoto.open('/flash/360.swf?width=680&height=430', 'Huffy Green Machine', '');
}

function showGreenMachine() {
    window.open('/Products/GreenMachine.aspx','_self'); 
}

function showGreenMachineSpecs() {
    window.open('/Products/Product.aspx?p=34&cat=2&subcat=10','_self'); 
}

function showCruisers() {
    window.open('/Products/Cruisers.aspx','_self'); 
}

function showPanamaJack() {
    window.open('/Products/PanamaJack.aspx','_self'); 
}

/************************************
* Huffy Timeline Video Links        *
************************************/
function show1994SpeedOfLight() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1994-SpeedOfLight.flv', '1994 - Speed of Light - Featuring the Huffy (need title) Bike.', '');
}

function show1995MetalMasters() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1995-MetalMasters.flv', '1995 - Metal Masters - Featuring the Huffy (need title) Bike.', '');
}

function show1995Blades() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1995-Blades.flv', '1995 - Blades - Featuring the Huffy Blades Mountain Bike.', '');
}

function show1995PopStar() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1995-PopStar.flv', '1995 - Pop Star - Featuring Huffy licensed products. ', '');
}

function show1995Credentials() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1995-Credentials.flv', '1995 - Credentials - Featuring Olympic Speed Skater Bonnie Blair.', '');
}

function show1995CallOfTheWild() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1995-CallOfTheWild.flv', '1995 - Call of the Wild - Featuring the Huffy Stone Mountain.', '');
}

function show1996Ironman() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=blades1995.flv', '1996 - Ironman - Featuring the Huffy Ironman Bikes.', '');
}

function show1996Creature() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1996-Creature.flv', '1996 - Creature - Featuring the Huffy (need title) Bike.', '');
}

function show1996MusclesAndSunglasses() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1996-MusclesAndSunglasses.flv', '1996 - Muscles and Sunglasses - Featuring the Huffy (need title) Bike.', '');
}

function show1998InEveryone() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1998-InEveryone.flv', '1998 - In Everyone - Featuring the Huffy (need title) Bike.', '');
}

function show1998TheFly() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=1998-TheFly.flv', '1998 - The Fly - Featuring the Huffy (need title) Bike.', '');
}

function show2009DisneyPrincess() {
    $.prettyPhoto.open('/flash/VideoPlayer.swf?width=720&height=480&flashvars=basePath=/flash/externals/&videoFile=DisneyPrincessCommercial.flv', '2009 - Disney Princesses - Featuring the Huffy Disney Princess Bikes.', '');
}

