﻿//assumes you have jquery.jqplugin.js

(function($){  
    $.fn.slideshow_tool = function(options) {
        var defaults = {};
        var options = $.extend(defaults,options);
        
        //embed functions
        function insertSLSlideshow(TargetID, ObjectID, mediaPath, mediaWidth, mediaHeight, initParams) {
            var windowless = true;
            if ($.browser.safari) { windowless = false; }
            $("#"+TargetID)[0].innerHTML = "<object id='" + ObjectID + "' data='data:application/x-silverlight-2,' type='application/x-silverlight-2'" +
                "    width='"+mediaWidth+"' height='"+mediaHeight+"'>" +
                "    <param name='source' value='/uk/windows/CampaignsFramework/rookies/ClientBin/RookiesPlayer.xap' />" +
                "    <param name='onerror' value='onSilverlightError' />" +
                "    <param name='background' value='#ffffff' />" +
                "    <param name='minRuntimeVersion' value='2.0.31005.0' />" +
                "    <param name='windowless' value='" + windowless + "' />" +
                "    <param name='initParams' value='VideoPath=" + mediaPath + ",jsEventFunction=VideoEvent,ObjectId="+ ObjectID + initParams + "' />" +
                "    <span style='display: none;'></span>" +
                "    <div id='no_sl_div'><a href='http://go.microsoft.com/fwlink/?LinkID=124807' style='text-decoration: none;'>" +
				"    <img src='http://go.microsoft.com/fwlink/?LinkId=108181' alt='Get Microsoft Silverlight' style='border-style: none'/>" +
				"    </a></div>" +
                "</object>" +
                "<iframe style='visibility: hidden; height: 0; width: 0; border: 0px'></iframe>";
        }
        function insertWMPSlideshow(TargetID, ObjectID, mediaPath, mediaWidth, mediaHeight) {
            $("#"+TargetID)[0].innerHTML = "<object width='"+mediaWidth+"' height='"+mediaHeight+"' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6' type='application/x-ms-wmp' id='" + ObjectID + "'>" +
                "<param value='" + mediaPath + "' name='url' id='wmv_url' /> " +
                "<param value='false' name='ShowStatusBar'/> " +
                "<param value='true' name='windowlessVideo'/> " +
                "<embed width='"+mediaWidth+"' height='"+mediaHeight+"' stretchToFit='true' autostart=1 " +
                "showstatusbar='false' showtracker=0 showdisplay=0 showcontrols=0 " +
                "name='" + ObjectID + "' id='" + ObjectID + "' " +
                "src='" + mediaPath + "' " +
                "pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' " +
                "type='application/x-mplayer2' />" +
                "</object>";
        }
        
        return this.each(function() {
            obj = $(this);
            obj.attr("class","slideshow "+obj.attr("class")); //append slide show class
            var body_html = ""; //define body html var
            
            //player container
            body_html += "<div class='slideshow_player'><div id='slideshow_player_host'></div><a class='slideshow_still_a' id='slideshow_still'></a></div>";
            //get slide count
            var slide_count = 0;
            for(item in options) {slide_count++;}
            //selection
            if(slide_count>1) {
                body_html += "<ul class='slideshow_touts'>";
                for(item in options) {body_html += "<li class='slideshow_tout'><a id='tout_"+item+"' class='slideshow_tout_a'>"+item+"</a></li>";}
                body_html += "</ul>";
            }
            
            obj.html(body_html);
            //style
            function deactivate_tout(item_id) {
                var tout_id = "tout_"+item_id;
                $('#'+tout_id).css("height",options[item_id].tout_height+"px");
                $('#'+tout_id).css("width",options[item_id].tout_width+"px");
                $('#'+tout_id).css("background","url("+options[item_id].tout_path+") top left");
                
                $('#'+tout_id).hover(
                    function() {
                        $(this).css("background-position","0px -"+options[item_id].tout_height+"px");
                        //$(this).css("background-color","Yellow");
                    },
                    function() {
                        $(this).css("background-position","0px 0px");
                        //$(this).css("background-color","White");
                    }
                );
                
                $('#'+tout_id).click(function() {
                    var item_id = $(this).attr("id").split("_")[1];
                    $('#slideshow_still').show();
                    $("#slideshow_player_host").hide();                   
                    //swap still
                    $('#slideshow_still').css("background","url("+options[item_id].still_path+") top left");
                    $('#slideshow_still').css("width",options[item_id].still_width+"px");
                    $('#slideshow_still').css("height",options[item_id].still_height+"px");
                    //do hover
                    if(options[item_id].still_hover) {
                        $('#slideshow_still').hover(function(){
                            $(this).css("background-position","0px -"+options[item_id].still_height+"px");
                        },function(){
                            $(this).css("background-position","0px 0px");
                        });
                    }
                    //init touts
                    for(this_item in options) {
                        if(this_item == item_id) {activate_tout(this_item);}
                        else{deactivate_tout(this_item);}
                    }
                });
            }
            
            function activate_tout(item_id) {
                var tout_id = "tout_"+item_id;
                $('#'+tout_id).css("height",options[item_id].tout_height+"px");
                $('#'+tout_id).css("width",options[item_id].tout_width+"px");
                $('#'+tout_id).css("background","url("+options[item_id].tout_path+") 0px -"+options[item_id].tout_height+"px");
                $('#'+tout_id).css("cursor","default");
                $('#'+tout_id).unbind();
                //remove any embed if present
                $("#slideshow_player_host").html("");
                //bind player
                $('#slideshow_still').unbind().click(function() {
                    $(this).hide();
                    $("#slideshow_player_host").css("height",options[item_id].sl_height+"px");
                    $("#slideshow_player_host").show();                   
                    if($.browser.sl) {
                        //stop top player
                        pauseVideo("Rookies");
                        //embed
                        insertSLSlideshow(
                            "slideshow_player_host",
                            "slideshow_"+item_id,
                            options[item_id].sl_path,
                            options[item_id].sl_width,
                            options[item_id].sl_height,
                            options[item_id].init_params
                        );
                        //track
                        //var slide_show = getPage();
                        //CallAtlas("mrtwxp_winftprookiesUKsl"+slide_show+"slideshow_start");
                        //CallOmnitureForSlideshow("event5", "rookies:"+slide_show);
                    }
                    else if($.browser.msie) {
                        
                        //stop top player
                        pauseWMPVideo("Rookies");
                        //embed
                        insertWMPSlideshow(
                            "slideshow_player_host",
                            "slideshow_"+item_id,
                            options[item_id].wmv_path,
                            options[item_id].wmv_width,
                            options[item_id].wmv_height
                        );

                        //track
                        //var slide_show = item_id=="david"?"dave":item_id;
                        var slide_show = getPage();
                        CallAtlas("mrtwxp_winftprookiesUKwmv"+slide_show+"slideshow_start");
                        CallOmnitureForSlideshow("event5", "rookies:"+slide_show);

                        $("#" + "slideshow_"+item_id)[0].attachEvent("PlayStateChange", function(newState) {
                            switch (newState) {
                                case 8:   // closed
                                    //VideoEvent("slideshow_"+item_id, "Completed", null);
                                    CallOmnitureForSlideshow("event6", "rookies:"+getPage());
                                    CallAtlas("mrtwxp_winftprookiesUKwmp"+getPage()+"slideshow_end");
                                    break;
                                case 3:   // playing
                                    pauseWMPVideo("Rookies");
                                    break;
                            }
                        });
                    }
                    else {
                        //$(this).show();
                        //$("#slideshow_player_host").hide();
                        //no_sl_div
                        $("#slideshow_player_host").css("width",options[item_id].no_sl_width+"px");                   
                        $("#slideshow_player_host").css("height",eval(options[item_id].no_sl_height)+"px");
                        $("#slideshow_player_host").css("background","url("+options[item_id].no_sl_still+") top left no-repeat");                   
                        insertSLSlideshow(
                            "slideshow_player_host",
                            "slideshow_"+item_id,
                            options[item_id].sl_path,
                            options[item_id].sl_width,
                            options[item_id].sl_height,
                            options[item_id].init_params
                        );
                    }
                });
            }
            
            var first_item = "";
            for(item in options) {
                if(!first_item) {first_item=item;activate_tout(item);}
                else {if(slide_count>1){deactivate_tout(item);}}
            }
            //init still
            $('#slideshow_still').css("background","url("+options[first_item].still_path+") top left");
            $('#slideshow_still').css("width",options[first_item].still_width+"px");
            $('#slideshow_still').css("height",options[first_item].still_height+"px");
            if(options[first_item].still_hover) {
                $('#slideshow_still').hover(function(){
                    $(this).css("background-position","0px -"+options[first_item].still_height+"px");
                },function(){
                    $(this).css("background-position","0px 0px");
                });
            }
        });
    };
})(jQuery);


$(document).ready(function(){
    var slides = {
        "danni8":{
            "still_path":"/uk/windows/CampaignsFramework/rookies/images/danni8_still.jpg",
            "still_width":510,
            "still_height":318,
            "still_hover":true,
            "tout_path":"",
            "tout_width":0,
            "tout_height":0,
            "wmv_path": "http://mediadl.microsoft.com/mediadl/www/u/uk/windows/rookies/slideshow_uk_danni8_wmp.wmv",
            "wmv_width":510,
            "wmv_height":318,
            "sl_path": "http://mediadl.microsoft.com/mediadl/www/u/uk/windows/rookies/slideshow_danni8.wmv",
            "sl_width":510,
            "sl_height":318,
            "no_sl_still":"/uk/windows/CampaignsFramework/rookies/images/danni8_slideshow_still.jpg",
            "no_sl_width":510,
            "no_sl_height":318,
            "init_params":",HideEmbedAndShare=true"
        }
    };
    $('#danni8_slideshow').slideshow_tool(slides);
});