//Used for Pages with only 1 video
var product = "";
var vidHeader = "";
var video = "";
var currentVideo = "";
var videoTXT = "txt/videoDiv.txt";

function addVideoHeader() {
    var imgTag = $("#videoImg").parent().html();
    $("#videoImg").parent().html("<h1 id='videoHeader'>" + vidHeader + "</h1><a href='#fixDiv' onclick='showVideo()'>" + imgTag + "</a>");
}

function writePlayer(solution, vidHead, vidSrc) {
    $(document).ready(function () {
        vidHeader = vidHead;
        product = solution;
        video = vidSrc;

        addVideoHeader();
        var closeButton = "<div id='closeButton' onclick='closeClick();' onmouseover='closeHover();' onmouseout='closeOut();'></div>";
        document.getElementById("fixDiv").innerHTML = "<div id='videoContainer'><div class='blackOut'><div id='videoWrapper'>" + closeButton + "<div id='replacedVideo'></div></div></div></div>";
        resizeVideo();
        $(window).resize(function () { resizeVideo(); });
    });
}

function showVideo() {
    if (video != currentVideo) {
        currentVideo = video;
        //Reset the innerHTML of the Div
        document.getElementById("replacedVideo").innerHTML = "";
        //Add the new Video, using var video which is set w/ the loadContent() function
        loadVideoXML(videoTXT, function () {
            if (xmlhttp4.readyState == 4 && xmlhttp4.status == 200)
                var vidTXT = xmlhttp4.responseText;
            vidTXT = videoParse(vidTXT);

            document.getElementById("replacedVideo").innerHTML = vidTXT;
        });
    }
    resizeVideo();
    $("#videoContainer").css("z-index", "1000");
    $("#videoContainer").css("visibility", "visible");
    if ($.browser.msie) { var t = setTimeout("msieResize()", 1); }
}
function msieResize() {
    try {
        resizeVideo();
        clearTimeout(t);
    } catch (err) { }
}

function videoParse(txt) {
    if (txt == null) {
        return "";
    }
    var imgSrc = document.getElementById("videoImg").src;
    imgSrc = imgSrc.substring(imgSrc.indexOf("images/"));
    imgSrc = imgSrc.replace("placeholders/", "placeholders/large/");
    while (txt.indexOf("PRODUCT") > -1)
        txt = txt.replace("PRODUCT", product);
    while (txt.indexOf("MP4VIDEO") > -1)
        txt = txt.replace("MP4VIDEO", video);
    while (txt.indexOf("OGVVIDEO") > -1)
        txt = txt.replace("OGVVIDEO", video);
    while (txt.indexOf("WEBMVIDEO") > -1)
        txt = txt.replace("WEBMVIDEO", video);
    while (txt.indexOf("PLACEHOLDERIMG") > -1)
        txt = txt.replace("PLACEHOLDERIMG", imgSrc);

    while (txt.indexOf("ApplicationPath") > -1)
        txt = txt.replace("ApplicationPath", location.hostname);

    return txt;
}

function resizeVideo() {
    var width = $(window).width();
    var height = $(window).height();

    if (width > 1280)
        width = 1280;
    if (height > 720)
        height = 720;

    if (height * (1280 / 720) > width)
        height = width * (720 / 1280);
    else
        width = height * (1280 / 720);

    $("#videoWrapper").width(width + "px");
    $("#videoWrapper").height(height + "px");
    if($("#flowplayer").length) $("#flowplayer").height(height + "px");

    var left = ($(window).width() - width) / 2;
    var top = ($(window).height() - height) / 2;

    $("#videoWrapper").css("left", left + "px");
    $("#videoWrapper").css("top", top + "px");
}

function closeClick() {
    $("#videoContainer").css("visibility", "hidden");
    $("#videoContainer").css("z-index", "-1000");
    if ($("#videoTag").length) document.getElementById("videoTag").pause();
    if ($.browser.msie) location.reload(false);
}

function closeHover() {
    $("#closeButton").css("background-position", "bottom");
}
function closeOut() {
    $("#closeButton").css("background-position", "top");
}

//Call XMLHttpRequest (call 1 for each element being populated)
function loadVideoXML(url, cfunc) {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp4 = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp4 = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp4.onreadystatechange = cfunc;
    xmlhttp4.open("GET", url, true);
    xmlhttp4.send();
}
