﻿var embedSize = "normal";

// Meta-information for AddThis button.
var addthis_share =
{
    title: title,
    description: description    
};

$(document).ready(function() {

    $("#ToggleEmbedSelBtn").click(toggleEmbedSizeSelect);
    $("#CloseEmbedSelBtn").click(toggleEmbedSizeSelect);

    $(".share-field-input").click(function(e) {
        highlightField(this);
    });

    setLinkFieldText();
    setEmbedFieldText();
    setupEmbedSizeSelect();

    $("#EmbedSizeSel_normal").click();

});

function highlightField(field) 
{
    field.focus();
    field.select();
}

function getPresentationLink() 
{
    return StringFormat("{0}/{1}/watch/{2}.aspx", serverUrl, communityId, presentation_id);    
}

function getEmbedLink()
{
    var embedWidth = embedSizes[embedSize].width;
    var embedHeight = embedSizes[embedSize].height;
    var embedTagTemplate = "<object width=\"{0}\" height=\"{1}\">";
    embedTagTemplate += "<param name=\"movie\" value=\"{2}\"></param>"; 
    embedTagTemplate += "<param name=\"allowFullScreen\" value=\"true\"></param>";
    embedTagTemplate += "<param name=\"allowscriptaccess\" value=\"always\"></param>";
    embedTagTemplate += "<param name=\"wmode\" value=\"transparent\"></param>";  
    embedTagTemplate += "<embed src=\"{2}\" type=\"application/x-shockwave-flash\" ";
    embedTagTemplate += "allowscriptaccess=\"always\" allowFullScreen=\"true\" wmode=\"transparent\"";
    embedTagTemplate += "width=\"{0}\" height=\"{1}\"></embed>";
    embedTagTemplate += "</object>";

    return StringFormat(embedTagTemplate, embedWidth, embedHeight, getSwfUrl());

}

function getSwfUrl() {   
    var urlTemplate = "{0}?eventId={1}&themeLocation={2}&serverUrl={3}&templateType={4}";
    urlTemplate += "&templateDescriptorLocation={5}&templateView={6}&themeColor={7}"
    urlTemplate += "&localeSwfPath={8}&localeCode={9}&embedded=true&clickToGoHome=true&autoStart=false";
    return StringFormat(urlTemplate,
        formatPath(serverUrl, swfLoc),
        presentation_id,
        themeLocation,
        serverUrl,
        type,
        templateDescriptorLocation,
        templateView,
        themeColor,
        localeSwfPath,
        localeCode);
}
    

function setLinkFieldText() 
{    
    $("#LinkField").val(getPresentationLink());
}

function setEmbedFieldText()
{    
    $("#EmbedField").val(getEmbedLink());
}

function toggleEmbedSizeSelect()
{    
    $("#EmbedSizeSelArea").toggle();
}
    
function embedSizeChange(newSize)
{
    embedSize = newSize;
    setEmbedFieldText();
}

function setupEmbedSizeSelect() {

    $(".embed-size-sel").click(function(e) {
        $(".embed-size-sel").toggleClass("embed-size-sel-selected", false);
        $(this).toggleClass("embed-size-sel-selected", true);       
        embedSizeChange(this.id.split("_")[1]);
    });
        
    $(".embed-size-sel").mouseover(function(e) {        
        $(this).toggleClass("embed-size-sel-mouseover", true);
    });
    
    $(".embed-size-sel").mouseout(function(e) {
        $(this).toggleClass("embed-size-sel-mouseover", false);
    });

    setupEmbedSizeOption("normal");
    setupEmbedSizeOption("large");
    setupEmbedSizeOption("small");

}

function setupEmbedSizeOption(sizeName) {
    var sizeLblStr;
    var width = embedSizes[sizeName].width;
    var height = embedSizes[sizeName].height;
    sizeLblStr = width + "x" + height;
    labelSelector = StringFormat("#EmbedSizeSel_{0} > .embed-size-label", sizeName);
    $(labelSelector).html(sizeLblStr);
}