﻿$(document).ready(function() {
    $("div.Video a").live("click", function() { uvp.play($(this).attr("id")); return false; });
});

var uvp = {
    play: function(id) {
        playerName = $("#" + id).attr("rel");
        if (!playerName) {
            alert("Error: Video player container does not contain attribute rel='playername'");
            return false;
        };
        player = uvp[playerName];
        if (!player) {
            alert("Error: Video player: " + playerName + " not found.");
            return false;
        };
        uvp.unload();
        var embedContainer = $('#' + id + '_embed');
        embedContainer.show();
        player.play(embedContainer.children()[0]);
        $('#' + id).addClass("selectedVideo").hide();
        return false;
    },
    unload: function() {
        $(".selectedVideo").each(function() {
            var embedContainer = $('#' + $(this).attr("id") + '_embed');
            playerName = $(this).attr("rel");
            player = uvp[playerName];
            player.pause(embedContainer.children()[0]);
            embedContainer.hide();
            $(this).css('display', 'block').removeClass("selectedVideo");
        });
    },
    uvpTwistage: {
        play: function(player) { },
        pause: function(player) {
            if (player && player.fp_pause)
                player.fp_pause();
        }
    },
    uvpYouTube: {
        play: function(player) { },
        pause: function(player) {
            if (player && player.pauseVideo)
                player.pauseVideo();
        }
    },
    uvpHtml5: {
        play: function(player) {
            if (player)
                player.play();
        },
        pause: function(player) {
            if (player)
                player.pause();
        }
    },  
    
    
    uvpHulu: {
        play: function (player) { },
        pause: function (player) {
        }
    },
    
    uvpVimeo: {
        play: function (player) {
        },
        pause: function (player) {
        }
    },
	
	uvpFlickr:{
		play:function(player){
		},
		pause: function(player){
		}
	}
};

function onYouTubePlayerReady(playerId) {
}

// old cma.js

var slCtl = null;

//DO NOT FORGET TO REGISTER THIS FUNCTION WITH THE SILVERIGHT CONTROL
// OnPluginLoaded="pluginLoaded"
function pluginLoaded(sender) {
    slCtl = sender.get_element();

    //Set your custom parameter using javascript
    //This parameter will be available in the webservice and you can use it for your business logic
    //Or use it to identity the upload to a sinle row in your database
    //slCtl.Content.Files.CustomParams = "custom_id=1"; 
}


function ShowNumberOfFilesUploaded() {
    if (slCtl != null) {
        alert("Total Files Uploaded: " + slCtl.Content.Files.TotalUploadedFiles);
    }
}

function ShowTotalNumberOfFilesSelected() {
    if (slCtl != null) {
        alert("Total Files Selected: " + slCtl.Content.Files.TotalFilesSelected);
    }
}

function ShowUploadProgress() {
    if (slCtl != null) {
        alert("Progress: " + slCtl.Content.Files.Percentage);
    }
}



//This function is registred in the pluginLoaded function (slCtl.Content.Files.AllFilesFinished = AllFilesFinished;)
function AllFilesFinished() {

}

//This function is registred in the pluginLoaded function (slCtl.Content.Files.SingleFileUploadFinished = SingleFileFinished;)
function SingleFileFinished() {

}

//This function is registred in the pluginLoaded function (slCtl.Content.Files.ErrorOccurred = ShowErrorDiv;)
function ShowErrorDiv() {

}

//Actions
function StartUpload() {
    if (slCtl != null) {
        slCtl.Content.Control.StartUpload();
    }
}

function ClearList() {
    if (slCtl != null &&
                slCtl.Content.Files.TotalFilesSelected > 0) {
        slCtl.Content.Control.ClearList();
    }
}



/*
* Safari Hack for File Uploading
         
* A pretty little hack to make uploads not hang in Safari. Just call this

* immediately before the upload is submitted. This does an Ajax call to

* the server, which returns an empty document with the "Connection: close"

* header, telling Safari to close the active connection. A hack, but

* effective. 
*/

function closeKeepAlive(url) {
    if (/AppleWebKit/.test(navigator.userAgent)) {
        new Ajax.Request(url, { asynchronous: false });
    }
}

function getmaxZ() {
    return Math.max.apply(null, $.map($('body *'), function(e, n) {
        var position = $(e).css('position');
        if (position != 'static')
            return parseInt($(e).css('z-index')) || 1;
    }));
}

function InsertPreviewDiv() {

    if ($("#preview").length > 0) return;
    $("body").append("<div id=\"previewBackground\" class=\"overlay\"></div><a id=\"closePreview\" href=\"#\" ></a><div id=\"preview\" class=\"overlayPlaceholder\"></div>");
}
function ShowUrlPreview(url) {
    $.getJSON(url + "&callback=?", ShowOembedPreview);
}

function ShowOembedPreview(html) {
    var maxZ = getmaxZ();
    InsertPreviewDiv();
    $("#previewBackground").css('z-index', maxZ + 10).fadeIn();
    maximize($("#previewBackground"));
    $("#preview").css({ 'z-index': maxZ + 20, top: 150, left: 150 }).html(html.html == null ? html : html.html).fadeIn(function() {
        $("#closePreview").css({ "z-index": maxZ + 30, top: 130, left: 130 }).show().click(function() {
            $("#closePreview").css("z-index", 0).hide();
            $("#preview").css("z-index", 0).fadeOut();
            $("#previewBackground").css("z-index", 0).fadeOut();
        });
    });
}

function maximize(selector) {
    selector.css({
        height: $(window).height(),
        width: $(window).width()
    });
    $(window).bind("resize", function() {
        selector.css({
            height: $(window).height(),
            width: $(window).width()
        });
    });

}


var canClose = function checkIfUserCanClose() {
    if (slCtl != null) {
        var fileCount = slCtl.Content.Files.TotalFilesSelected;
        if (fileCount > 0) {
            if (fileCount == 1) {
                return "There is " + slCtl.Content.Files.TotalFilesSelected + " file upload in progress. Do you want to cancel it?";
            }
            return "There are " + slCtl.Content.Files.TotalFilesSelected + " file uploads in progress. Do you want to cancel them?";
        }
    }
}

if (canClose) {
    ClearList();
}
window.onbeforeunload = canClose;
