﻿// Copyright 2010 Microsoft Corporation
// Scripts to support the LandingV2 page - requires jQuery

var enterKey = 13;

var vars = {
    transitionTime: [100, 400],
    css: {
        factoidsTarget: "factoids target"
    },
    factoids: { delay: 15000 },
    ui: {
        factoidsOver: ".factoids.target > .over",
        factoidsUnder: ".factoids.target > .under",
        factoidsContainer: "div.factoids.container"
    }
};

// iterates and animates transitions between factoid views; self perpetuating
function nextFactoid() {
    if (vars.factoids.isStopped === true) return;
    var uls = $(vars.ui.factoidsContainer + " ul");
    if (!vars.factoids.started) return setupFactoids(uls);
    if (++vars.factoids.next >= vars.factoids.length) vars.factoids.next = 0;
    $(vars.ui.factoidsOver).fadeOut(vars.transitionTime[0], function() {
		$(vars.ui.factoidsUnder).fadeIn(vars.transitionTime[1], function() {
			$(vars.ui.factoidsOver).html($("<ul/>").append($(this).html())).show();
			$(this).hide().html($("<ul/>").append(uls.eq(vars.factoids.next).html()));
			setTimeout(nextFactoid, vars.factoids.delay);
		});
	});
}

// sets up factoid views and starts the animation/update process
function setupFactoids(uls){
	vars.factoids.started = true;
	vars.factoids.length = uls.length;
	vars.factoids.next = 2;
	var d = "<div/>", target = $(d).addClass(vars.css.factoidsTarget);
	target.append($(d).addClass("under").hide());
	target.append($(d).addClass("over").hide());
	$(vars.ui.factoidsContainer).hide().after(target);
	$(vars.ui.factoidsOver).html(uls.eq(0).html()).show();
	$(vars.ui.factoidsUnder).html(uls.eq(1).html()).hide();
	setTimeout(nextFactoid, vars.factoids.delay);
}

// updates elements marked for lazy-loading images with image tags so that the referenced images get displayed
function setupThumbnails() {
    $("span.thumbnail").each(function() {
        var el = $(this); if (!el.attr("url")) { el.hide(); return; }
        el.html("<img class=\"thumbnail\" src=\"" + el.attr("url") + "\" alt=\"" + (el.attr("alt") ? el.attr("alt") : "") + "\" />");
    });
}

// allows repetative animations to be stopped for debugging purposes
function disableAnimations(b) {
    vars.factoids.isStopped = b;
}

// runs when the DOM is loaded; launches all of relevant setup functions and starts a timeout process
// that will render the default view contents in the event that the profile service doesn't respond quickly
$(document).ready(function() {
	nextFactoid();
    $(window).load(function() {
        setupThumbnails();
    });
});

