$(document).ready(function () {
    /*-------------------------------------------------------------------------------------------------------------------*/
    //Lavalamp
    $(".platforminfo ul").lavaLamp({ fx: "easeOutBack", speed: 400 })

    //Fixed Footer
    //stay();
    $(window).resize(function () {
        //stay();
    })

    // Brandbox Pic 
    $(".tabcontent:first, .brand-picbox:first").show();
    $("ul.tabnav li:first").addClass("active");
    $("ul.tabnav li").click(function () {
        if (!$(this).hasClass('active')) {
            $("ul.tabnav li").removeClass("active");
            $(this).addClass("active");
            $(".tabcontent, .brand-picbox").hide();
            var activeTab = $(this).find("a").attr("rel");
            var activeTabpic = $(this).find("a").attr("rev");
            $(activeTab).stop(1, 1).fadeIn(300);
            $(activeTabpic).stop(1, 1).fadeIn(300);
            // if not brands tab, reset platform selector
            if (activeTab != "#brands") {
                window.location.hash = '#';
                $('#brands').removeClass('lawls');
                $('.brandcolleft li').stop(1, 1).hide();
                $('.brandcolleft li a.all').parent('li').stop(1, 1).fadeIn();
                move($('#allplatforms'));
            }
            if (activeTab == "#mostpopular") {
                clickTrack("MostPopular", "None", "Home", "LandingPage", "View");
            }
            if (activeTab == "#newgames") {
                clickTrack("NewGames", "None", "Home", "LandingPage", "View");
            }
            if (activeTab == "#brands") {
                clickTrack("BrandsAll", "None", "Home", "LandingPage", "View");
            }
        }
        return false;
    });

    // parseInt for easy mathing
    function getInt(arg) {
        var myint = parseInt(arg);
        return (isNaN(myint) ? 0 : myint);
    }

    function move($el) {
        if (!$el) $el = $selected;
        $back = $('.backLava');
        bx = getInt($back.css('borderLeftWidth')) + getInt($back.css('borderRightWidth')) + getInt($back.css('paddingLeft')) + getInt($back.css('paddingRight')) - 0;
        by = getInt($back.css('borderTopWidth')) + getInt($back.css('borderBottomWidth')) + getInt($back.css('paddingTop')) + getInt($back.css('paddingBottom')) - 0;

        //if (!o.includeMargins) {
        ml = (getInt($el.css('marginLeft')));
        mt = (getInt($el.css('marginTop')));
        //}
        var dims = {
            'left': $el.position().left + ml,
            'top': $el.position().top + mt,
            'width': $el.outerWidth() - bx,
            'height': $el.outerHeight() - by
        };

        $back.stop().animate(dims, 400, "easeOutBack");
    };

    //Homeslider function
    $(".splashslider").cycle({
        fx: 'fade',
        speed: 1500,
        timeout: 5000,
        //cleartypeNoBg: true,
        cleartypeNoBg: true

    });

    //platform filtering
    $('.platforminfo a').click(function () {
        if (!$(this).parent('li').hasClass('selectedLava')) {

            //show the brands tab content
            $(".tabcontent, .brand-picbox").hide();
            $('#brands,#brandpic').stop(1, 1).fadeIn(300);
            // deselect other tabs and select brands tab
            $("ul.tabnav li").removeClass("active");
            var tt = $('a[rel="#brands"]');
            tt.parent('li').addClass('active');

            //other stuff
            if ($(this).attr('rel') == 'all') {
                $('#brands').removeClass('lawls');
            }
            else {
                $('#brands').addClass('lawls');
                $('#brands.lawls .brandcolleft li').removeClass('nomargin')
            }

            $('.brandcolleft li').stop(1, 1).hide();
            $('.brandcolleft li a.' + $(this).attr('rel')).parent('li').stop(1, 1).fadeIn();
            $('.platforminfo li').removeClass('selectedLava');
            $(this).parent('li').addClass('selectedLava');

            clickTrack("Brands" + $(this).attr('rel').toUpperCase(), "None", "Home", "LandingPage", "View");
        }
    })

    //auto-filter based on hash tag
    var hash = window.location.hash;
    if (hash != '') {
        hash = hash.slice(1);
        $('#brands').addClass('lawls');
        $('.brandcolleft li').stop(1, 1).hide();
        $('.brandcolleft li a.' + hash).parent('li').stop(1, 1).fadeIn();
        $('.platforminfo li').removeClass('current');
        $('a[rel="' + hash + '"]').parent('li').addClass('current');
    }

    /*------------------------------------------------------------------------------------------------------------------------
    Popup functon Javascript
    -------------------------------------------------------------------------------------------------------------------------*/

    $('a.poplight[href^=#]').click(function () {
        var popID = $(this).attr('rel');
        var popURL = $(this).attr('href');

        /*var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1];*/


        $('#' + popID).show();

        var popMargTop = ($('#' + popID).height()) / 2;
        var popMargLeft = ($('#' + popID).width()) / 2;

        $('#' + popID).css({
            'margin-top': -popMargTop,
            'margin-left': -popMargLeft
        });

        $('body').append('<div id="fade"></div>');
        $('#fade').css({ 'filter': 'alpha(opacity=80)' }).show();

        return false;
    });


    $('a.closepopup, #fade').live('click', function () {
        $('#fade , .popup_block').hide();
        $('#fade').remove();
        return false;
    });

    /*------------------------------------------------------------------------------------------------------------------------
    Popup functon Javascript end
    -------------------------------------------------------------------------------------------------------------------------*/

    /*-------------------------------------------------------------------------------------------------------------------*/
});

//Fixed Footer
function stay() {
	var dh = $(document).height();
	var wh = $(window).height();

	if (dh>=wh) {
		$('body').addClass('stay');
	}
	else {
		$('body').removeClass('stay');
	}
}

function clickTrack(name, campaign, channel, contenttype, action) {
    MATTEL.tracker.Tracker.enableShortCuts();
    Tracker.name = channel + ": " + campaign + ": " + contenttype + "_" + name; // "Home: None: LandingPage_BrandsAll";
    Tracker.campaign = campaign;
    Tracker.channel = channel;
    Tracker.contenttype = contenttype;
    Tracker.action = action;
    Tracker.track();
}
