﻿///<reference path="dep.js"/>

//Check if IE
var isIE = /msie|MSIE/.test(navigator.userAgent);
//Track ID of the video currently playing.


/* Script Click Handler */
dep.addEventHandler("ScriptClick", function (e) {
    eval(e.arguments["script"]);
    return true;
});

dep.addEventHandler("NoNavigation", function (e) {

    return false;
});

dep.addEventHandler("WebTrendsSetVar", function (e) {
    var s = "dcsSetVar(";
    for (var arg in e.arguments) {
        if (arg != "noNav") {
            s += "'" + arg + "',";
            s += "'" + e.arguments[arg] + "',";
        }
    }

    s = s.substring(0, s.lastIndexOf(","));
    s += ");";

    e.sender.setAttribute("onclick", s);

    eval(s);

    //Add an argument to specify whether it should navigate or not after the click.

    if (e.arguments["noNav"] == "true") {
        return false;
    }

    return true;


});

dep.addEventHandler("WebTrendsMultiTrack", function (e) {
    var s = "dcsMultiTrack(";
    for (var arg in e.arguments) {
        if (arg != "noNav") {
            s += "'" + arg + "',";
            s += "'" + e.arguments[arg] + "',";
        }
    }

    s = s.substring(0, s.lastIndexOf(","));
    s += ");";

    eval(s);

    //Add an argument to specify whether it should navigate or not after the click.

    if (e.arguments["noNav"] == "true") {
        return false;
    }

    return true;


}
);



/* Webtrends Page View Handler */
dep.addEventHandler("WebTrendsPageView", function (e) {

    dcsMultiTrack("DCS.dcssip", "www.microsoft.com", "DCS.dcsuri", dep.settings["site_root"] + e.arguments["dcsuri"], "WT.dl", "0");
}
);


/* Rio Click Handler */
dep.addEventHandler("RioClick", function (e) {

    RioTracking.click(e.arguments["cellCode"], e.arguments["actionTag"]);
    return true;
}
);

/* Rio Page View Handler */
dep.addEventHandler("RioPageView", function (e) {
    RioTracking.guestCellCode = e.arguments["cellCode"];
    RioTracking.uniqueActionTagCode = e.arguments["actionTag"];
}
);


/* Generic Tracking Click Handler */
dep.addEventHandler("GenericTrackingClick", function (e) {
    var img = new Image();
    img.src = e.arguments["url"];
    //img.src = dep.utils.makeUrlUnique(e.arguments["url"]);
    if (e.arguments["noNav"] == "true") {
        return false;
    }
    return true;
}
);

/* Generic Page View Handler -- Write the script at the top of the page. */
dep.addEventHandler("GenericPageView", function (e) {
    var img = new Image();
    img.src = e.arguments["url"];
    //img.src = dep.utils.makeUrlUnique(e.arguments["url"]);
});

/* Atlas-specific page load event - writes the actual script. */
dep.addEventHandler("AtlasPageView", function (e) {
    document.write("<s" + "cript language='JavaScript' src='" + e.arguments["url"] + "'></s" + "cript>");
});

dep.addEventHandler("MasterConversionPageViewTag", function (e) {
    var newIFrame = document.createElement("IFRAME");
    newIFrame.setAttribute("src", e.arguments["url"]);
    newIFrame.setAttribute("style", "height:1px; width:1px;");
    newIFrame.setAttribute("frameborder", "0");
    document.body.appendChild(newIFrame);
});

dep.addEventHandler("FormSubmitByName", function (e) {

    $("form[name=" + e.arguments["formName"] + "]").submit();
    return false;
});



function wtClear() {
    try {
        DCSext.vs_actiontype = "";
        DCSext.vs_actionlocation = "";
        DCSext.vs_slplayer_action = "";
        DCSext.vs_video_title = "";
        DCSext.dcssip = "";
        DCSext.dcsuri = "";
    }
    catch (e) { }
}

///* This is for anchor links. */
//dep.addEventHandler("JumpLink", function (e) {
//    var newUrl = GetPageUrlById(e.arguments["page"]) + "#" + e.arguments["linkId"];
//    alert(newUrl);
//    //window.location = newUrl;
//    return true;
//});

/* This is to make the hover state for the product promos. */
$(document).ready(function () {
    $(".product-icon-heading").live("mouseenter", function (e) {
        $(this).toggleClass("over", true);
    });
    $(".product-icon-heading").live("mouseleave", function () {
        $(this).toggleClass("over", false);
    });
});

$(document).ready(function () {
    dep.utils.cookie.write("ofm_culture", dep.settings["culture"], "");
});



/* Hover event for campaign landing video thumbnail that displays the video title. */
$(document).ready(function () {
    $(".video_thumbnail").live("mouseenter", function (e) {
        $(this).parent().parent().find("div.video_thumbnail_hover").css("display", "block");
    });
    $(".video_thumbnail").live("mouseleave", function () {
        $(this).parent().parent().find("div.video_thumbnail_hover").css("display", "none");
    });
});

/* This is for the Product Feedback form version drop down initial display. */
$(document).ready(function () {
    try {
        $("select[name*='ddlVersion']").attr("style", "display:none");
        $("#ddlVersionYears").attr("style", "display:block");
    }
    catch (ex) {
    }
});


var videoLanding = null;
var titleArea = null;
var videoThumbnail = null;

$(document).ready(function () {
    videoLanding = $(".video_landing").get(0);
    titleArea = $(".video_title_area").get(0);
    videoThumbnail = $(".video_thumbnail").get(0);
});


/* Click event to change the campaign landing image. */
dep.addEventHandler("ShowCampaignVideoLanding", function (e) {

    //stop video
    qt.stop();

    //Hide all video player divs.
    $(".video-player").hide();

    //Hide all video title areas.
    //$(".video_title_area").hide();

    //Remove selected state from all images.
    $(".video_thumbnail").each(
        function (i) {
            var image = $(this).attr("src").replace(/on/ig, "");
            $(this).attr("src", image);
        });
    //Hide all the landing page images.
    //$(".video_landing").toggleClass("hide", true);


    $(videoLanding).fadeOut("fast", function () {
        videoLanding = $("div.block" + e.arguments["videoNum"]).get(0);
        $(videoLanding).fadeIn();
    });
    

    //Set selected thumbnail state.
    $(e.sender).find("img").attr("src", $(e.sender).find("img").attr("src").replace(/.jpg/ig, "on.jpg"));

    //Replace the video landing image with the correct one.
    //$("div.block" + e.arguments["videoNum"]).toggleClass("hide", false);

    //Show the correct title block for the video.
    var titleBlockNum = parseInt(e.arguments["videoNum"]) + 7;
    //$("div.block" + titleBlockNum.toString()).fadeIn();

    $(titleArea).fadeOut("fast", function () {
        titleArea = $("div.block" + titleBlockNum.toString()).get(0);
        $(titleArea).fadeIn();
    });

    return false;
});

/* Hover event for the play button on campaign landing page */
$(document).ready(function (e) {
    $(".landing_video_play_button").live("mouseenter", function (e) {
        var image = $(this).attr("src").replace(/.png/ig, "hover.png");
        $(this).attr("src", image);
    });
    $(".landing_video_play_button").live("mouseleave", function (e) {
        var image = $(this).attr("src").replace(/hover/ig, "");
        $(this).attr("src", image);
    });
});

dep.addEventHandler("PlayCampaignVideo", function (e) {
    var playerId = e.arguments["playerId"];
    //try {
    $("#" + e.arguments["playerId"]).css("display", "block");
    qt.play(e.arguments["playerId"]);

    //} catch (ex) { return false; }

    return false;
});

//Define function for DR Trial form - if Japan, redirect to Japan's own Trial page.
function countryChanged() {
    if ($("#country").val() == "JP") {
        document.location = "http://www.microsoft.com/japan/mac/trial";
    }
}

//Make scroller items display in random order upon refresh.
$(document).ready(function (e) {
    try {
        var totalScrollerItems = $("#footer-scroller > .scroller_container > #items li").size();
        var scrollerItems = $("#footer-scroller > .scroller_container > #items > li.item").get().sort(function () {
            return Math.round(Math.random()) - 0.5;
        }).slice(0, totalScrollerItems)
        $(scrollerItems).appendTo(scrollerItems[0].parentNode).show();
    }
    catch (ex) {
    }
});
