﻿// Video Detective Title Details Flash Code

// Define Global Variables
var VideoId; // Published Id
var VideoType; // Video Type (FLV or WMV) [Default: FLV]
var VideoUrlLq; // Video Url for WMV only
var VideoUrlHq; // Video Url for WMV only
var VideoBitRate; // Video BitRate
var VideoFmt; // Video Format
var LinkButtonHdSd; // Client ID
var a = 0; // Counter
var b = 0; // Counter
var aj_acid; // Extra Ad Variable

// Title Information
var TitleInfo_Title;

// JW Player
function loadFile(Url) {
    document.getElementById('mediaspace').sendEvent("LOAD", Url);
}

// Embed Player
function PlayVideo(Quality) {
    if (VideoType == "FLV") {
        // Flash Vars
        var FlashVars = {};
        
        // Parameters
        var Params = {
            allowfullscreen: "true",
            allowscriptaccess: "always",
            allownetworking: "all",
            wmode: "transparent"
        };
        
        // Attributes
        var Attributes = {
            name: "mediaspace"
        };

        swfobject.switchOffAutoHideShow();
        
        // Embed 4.x Player
        if (Quality == 1) {
            swfobject.embedSWF("http://www.videodetective.net/flash/players/?publishedid=" + VideoId + "&customerid=69249&pversion=4&playerid=4&videokbrate=2500&sub=vdTitlePage", "mediaspace", "980", "611", "9.0.0", "expressInstall.swf", FlashVars, Params, Attributes);
        } else {
            swfobject.embedSWF("http://www.videodetective.net/flash/players/?publishedid=" + VideoId + "&customerid=69249&pversion=4&playerid=4&sub=vdTitlePage&videokbrate=" + VideoBitRate + "&fmt=" + VideoFmt, "mediaspace", "640", "420", "9.0.0", "expressInstall.swf", FlashVars, Params, Attributes);
        }
    } else if (VideoType == "WMV") {
        // Embed WMV
        if (Quality == 1) {
            document.getElementById("mediaspace").innerHTML = "<object id=\"MediaPlayer\" width=\"980\" height=\"551\" style=\"filter: progid:DXImageTransform.Microsoft.BasicImage (Grayscale=1);\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" standby=\"Loading Windows Media Player components...\" type=\"application/x-oleobject\"><param name=\"URL\" value=\"" + VideoUrlHq + "&sub=vdTitlePage\"><param name=\"stretchToFit\" value=\"true\"><param name=\"fullscreen\" value=\"false\"><param name=\"AutoSize\" value=\"false\"><embed type=\"application/x-mplayer2\" pluginspage=\"http://www.microsoft.com/Windows/MediaPlayer/\" id=\"mediaPlayer\" name=\"mediaPlayer\" autosize=\"0\" bgcolor=\"darkblue\" showcontrols=\"1\" showtracker=\"1\" showdisplay=\"0\" showstatusbar=\"0\" videoborder3d=\"0\" width=\"980\" height=\"551\" src=\"" + VideoUrlHq + "&sub=vdTitlePage\" autostart=\"1\" designtimesp=\"5311\" loop=\"0\"></embed></object>";
        } else {
            document.getElementById("mediaspace").innerHTML = "<object id=\"MediaPlayer\" width=\"640\" height=\"420\" style=\"filter: progid:DXImageTransform.Microsoft.BasicImage (Grayscale=1);\" classid=\"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6\" standby=\"Loading Windows Media Player components...\" type=\"application/x-oleobject\"><param name=\"URL\" value=\"" + VideoUrlLq + "&sub=vdTitlePage\"><param name=\"stretchToFit\" value=\"true\"><param name=\"ShowStatusBar\" value=\"false\"><param name=\"ShowDisplay\" value=\"false\"><param name=\"fullscreen\" value=\"false\"><param name=\"AutoSize\" value=\"false\"><embed type=\"application/x-mplayer2\" pluginspage=\"http://www.microsoft.com/Windows/MediaPlayer/\" id=\"mediaPlayer\" name=\"mediaPlayer\" autosize=\"0\" bgcolor=\"darkblue\" showcontrols=\"1\" showtracker=\"1\" showdisplay=\"0\" showstatusbar=\"0\" videoborder3d=\"0\" width=\"640\" height=\"420\" src=\"" + VideoUrlLq + "&sub=vdTitlePage\" autostart=\"1\" stretchtofit=\"0\" loop=\"0\"></embed></object>";
        }
    }
}

// Embed Review Player
function EmbedReview(PublishedId) {
    // Flash Vars
    var FlashVars = {};

    // Parameters
    var Params = {
        allowfullscreen: "true",
        allowscriptaccess: "always",
        allownetworking: "all",
        wmode: "transparent"
    };

    // Attributes
    var Attributes = {
        //name: PublishedId
    };

    // Embed 4.x Player
    swfobject.embedSWF("http://www.videodetective.net/flash/players/?publishedid=" + PublishedId + "&customerid=69249&pversion=4&playerid=111&sub=vdTitlePage&videokbrate=450", "VID_" + PublishedId, "320", "240", "9.0.0", "expressInstall.swf", FlashVars, Params, Attributes);
}

// Ads
function initAd() {
    aj_server = "http://rotator.adjuggler.com/servlet/ajrotator/";
    aj_tagver = "1.0";
    aj_zone = "video";
    aj_adspot = "217421";
    aj_page = "0";
    aj_dim ="368573";
    aj_ch = "";
    aj_ct = "";
    aj_kw = "";
    aj_pv = true;
    aj_click = "";
    aj_acid = "222537";
}

function AdDone() {
    a++;

    if (a == 1) {
        replaceSwfWithEmptyDiv("ad_player");
        //replaceSwfWithEmptyDiv("ad_companion")
        document.getElementById("ad_companion").style.display = "none";
        document.getElementById("ad_info").innerHTML = "";

        setTimeout(function() {
            $("#title_ad_container").animate({ width: "640px" }, 400);
            //$("body").animate({ backgroundColor: "#666666" }, 1000);
        }, 400);
        
        document.getElementById("title_hdsd").style.display = "block";

        setTimeout(function() { PlayVideo(0); }, 800);
    }
}

function loadAd(Xml) {
    // Flash Vars
    var FlashVars = {
        AD_PAGENAME: "XmlAdParser.aspx?asid=" + aj_acid + "%26src=" + Xml + "%26"
    };
    
    // Parameters
    var Params = {
        swliveconnect: "true",
        allowscriptaccess: "always"
    };
    
    // Attributes
    var Attributes = {};

    // Embed
    swfobject.embedSWF("http://www.videodetective.net/flash/general_ad_player.swf", "ad_player", "320", "240", "9.0.0", "expressInstall.swf", FlashVars, Params, Attributes);
}

function FLA_HandleEmbedBanner(bannerHTML) {
    // takes html that needs to be inserted into page
}

function FLA_HandlePlaybackComplete() {
    // Same as AdDone. Some Ad netorks needed this function
    AdDone();
}

function FLA_HandleAdPlaybackComplete() {
    // Same as AdDone. Some Ad netorks needed this function
    AdDone();
}

function LoadCompanionByUrl(sUrl) {
    // takes a full url to load the companion.
    // this works in new RadWindow or loading an iFrame
    // Be careful. For some reason this gets called twice from flash player
    b++;

    if (b == 1) {
        CreateCompanion(sUrl, "ad_companion");
        //document.getElementById("title_player_ad_companion").style.display = "block";
        //document.getElementById("companion").style.display = "block";
    }
}

function WriteToCompanion(sCompanionHTML, Div)
{
    var bannerHTML = sCompanionHTML;
    var adFrame = document.createElement("iframe");

    adFrame.setAttribute("src", "about:blank");
    adFrame.setAttribute("frameborder", 0);
    adFrame.id = "bannerIFrame";
    adFrame.frameBorder = 0;
    adFrame.scrolling = "no";
    adFrame.width = "300px";
    adFrame.height = "250px";
    adFrame.className = "BannerWindow";

    try {
        document.getElementById(Div).innerHTML = "";
        document.getElementById(Div).appendChild(adFrame,0);
    }
    catch (err) {
        window.status = 'error loading companion.';
    }

    document.getElementById("bannerIFrame").contentWindow.document.open();
    document.getElementById("bannerIFrame").contentWindow.document.write("<html><body style='background-color: #000;padding:0px;overflow:hidden;margin:0'>"+bannerHTML+"</body></html>");
    document.getElementById("bannerIFrame").contentWindow.document.close();
}

function CreateCompanion(sUrl, CompanionElementName) {
    var bannerUrl = sUrl;
    var adFrame = document.createElement("iframe");

    adFrame.setAttribute("src", bannerUrl);
    adFrame.setAttribute("style", "background-color: Transparent;");
    adFrame.id = "bannerIFrame";
    adFrame.frameBorder = 0;
    adFrame.scrolling = "no";
    adFrame.width = "325px";
    adFrame.height = "280px";

    try {
        document.getElementById(CompanionElementName).innerHTML = "";
        document.getElementById(CompanionElementName).appendChild(adFrame, 0);
    }
    catch (err) {
        window.status = 'error loading companion.';
    }
}

function CreateBanner() {
    var bannerFrame = document.createElement("iframe");
    
    bannerFrame.setAttribute("src", "http://rotator.adjuggler.com/servlet/ajrotator/368571/0/vh?z=video&dim=176972");
    bannerFrame.id = "bannerFrame";
    bannerFrame.frameBorder = 0;
    bannerFrame.scrolling = "no";
    bannerFrame.width = "325px";
    bannerFrame.height = "280px";
    
    document.getElementById("companion").innerHTML = "";
    document.getElementById("companion").appendChild(bannerFrame, 0);
}

// Element Resizing

function ResizeElement(Name, Width, Height, Reverse) {
    if (Width == -1) { Width = $(Name).width(); }
    if (Height ==  -1) { Height = $(Name).height(); }

    if (Reverse == null) {
        $(Name).animate({ width: Width }, 400);
        $(Name).animate({ height: Height }, 400);
    } else {
        $(Name).animate({ height: Height }, 400);
        $(Name).animate({ width: Width }, 400);
    }
}

// Element HD/SD Toggle

function ToggleHdSd(Element) {
    if (Element.innerHTML.lastIndexOf("hd") != -1) {
        Element.innerHTML = "<img src='images/sdicon.gif' alt='' style='vertical-align: middle;' />Watch in SD";
        replaceSwfWithEmptyDiv("mediaspace");
        setTimeout(function() {
            ResizeElement("#title_container", -1, 551);
            ResizeElement("#title_ad_container", 980, 551);
        }, 400);
        setTimeout(function() { PlayVideo(1); }, 1400);
    } else {
        Element.innerHTML = "<img src='images/hdicon.gif' alt='' style='vertical-align: middle;' />Watch in HD";
        replaceSwfWithEmptyDiv("mediaspace");
        setTimeout(function() {
            ResizeElement("#title_ad_container", 640, 360, 1);
            ResizeElement("#title_container", -1, 360, 1);
        }, 400);
        setTimeout(function() { PlayVideo(0); }, 1400);
    }
}

//Support function: checks to see if target
//element is an object or embed element

function isObject(targetID) {
    var isFound = false;
    var el = document.getElementById(targetID);

    if (el && (el.nodeName === "OBJECT" || el.nodeName === "EMBED")) {
        isFound = true;
    }

    return isFound;
}

//Support function: creates an empty
//element to replace embedded SWF object

function replaceSwfWithEmptyDiv(targetID) {
    var el = document.getElementById(targetID);

    if (el) {
        var div = document.createElement("div");
        el.parentNode.insertBefore(div, el);

        //Remove the SWF
        swfobject.removeSWF(targetID);

        //Give the new DIV the old element's ID
        div.setAttribute("id", targetID);
    }
}

function AnimateBgColor(Obj, Color, Time) {
    //$(Obj).stop().animate({ backgroundColor: Color }, Time);
    $(Obj).css({"background-color" : Color});
}

function BrokenImage(Source) {
    Source.src = "http://www.videodetective.com/images/photoNotAvailablesmall.jpg";
    // Disable onerror to prevent endless loop
    Source.onerror = "";
    return true;
}
