if (typeof IGN === "undefined" || !IGN) {
    var IGN = {};
}

function html5VideoUa(){
    ua = navigator.userAgent.toLowerCase();
    return ua.match(/(ip(hone|ad|od))|webos/);
}



//var carouselTimeout = 3.2; // number is seconds
var fadeSpeed = 400;
var thumbHoverSpeed = 400;
var timeout;


VideoPlayer = function() { // activates video player
    jQuery(".cvr-main a").each(function() {
        if (jQuery(this).attr("ign_vid_id")) {
            jQuery(this).click(function(event) {
                event.preventDefault();
                clearTimeout(timeout);
                jQuery(this).parent().children("a").hide();
                jQuery(".cvr-thumbs li").css("visibility", "hidden");
                jQuery("#ignVidContainer, .vidHightlightExtras").show();
                jQuery(".cvr-subPointsWrapper").hide();
                jQuery(".cvr-videoComments").show();
                jQuery(".cvr-shareVideo").show();
                params = jQuery(this).attr("ign_vid_id");
                StartPlayer(params);
            });
        }
    });
};

CarouselThumbsHover = function() {
    jQuery(".cvr-thumbs li").each(function() { // stops the click on thumbs that are for videos
        jQuery(this).click(function(event) {
            jQuery(this).children("span").each(function() { // find the thumb with a video step 1
                if (jQuery(this).attr("class") == "cvr-icon") { // find the thumb with a video step 2
                    event.preventDefault();
                    jQuery(".cvr-display").children().each(function() {// on click of thumbs triggers the video player as if you clicked the main area.
                        if (jQuery(this).css("display") == "block") {
                            jQuery(this).children("div").children("a").trigger('click');
                        }
                    });
                }
            });
        });
        jQuery(this).hover(function() { // sets up the hover functionality
            clearTimeout(timeout); // stops carousel
            if (!jQuery(this).attr("class").match("active")) {
                jQuery(".cvr-thumbs li").each(function() {
                    jQuery(this).removeClass("active");
                });
                var max = (jQuery(".storyUnit").length) - 1;
                var id = jQuery(this).attr("class");
                var hoverID = id.substring(id.indexOf("cvr-thumb_") + 10);
                jQuery(".storyUnit").each(function(i) { // un highlights any/all thumbs
                    jQuery(this).fadeOut(thumbHoverSpeed);
                    if (i == max) {
                        jQuery(".cvr-display_" + hoverID).fadeIn(thumbHoverSpeed);
                    }
                });
                jQuery(this).addClass("active"); // highlight hovered over thumb
            }
        });
    });
};

Carousel = function(num) {
    IGN.HeroUnit.location = num;
    jQuery(".cvr-display_" + num).fadeOut(fadeSpeed);
    var max = (jQuery(".cvr-thumbs li").length) - 1;
    jQuery(".cvr-thumbs li").each(function(i) { // removes all 'active' class names
        jQuery(this).removeClass("active");
        if (i == max) { // once done removing class do below
            var nextActive = (num == max) ? 1 : num + 2;
            jQuery(".cvr-thumbs li:nth-child(" + nextActive + ")").addClass("active"); // adds class 'active' to current display
        }
    });
    num = (num == max) ? 0 : num + 1;
    jQuery(".cvr-display_" + (num)).fadeIn(fadeSpeed, function() {
        timeout = setTimeout("Carousel(" + num + ")", carouselTimeout * 1000); // start over
    });
};

jQuery(".coverStories").ready(function() { // it all starts here
    CarouselThumbsHover();
    VideoPlayer();
    IGN.HeroUnit.location = 0;
    timeout = setTimeout("Carousel(0)", carouselTimeout * 1000);
});


IGN.HeroUnit = function(){
    return {
        pause : function(){
            clearTimeout(timeout);
        },
        resume : function(){
            timeout = setTimeout("Carousel(" + IGN.HeroUnit.location + ")", carouselTimeout * 1000);
        }
    }
}();

