﻿/* Works with hideshowlist Div */
function hideshow(showdiv) {
    $(".hideshowlist div.hideshowlistitem").removeClass("show").addClass("hide");
    $(".hideshowlist #hideshowlistitem" + showdiv).removeClass("hide").addClass("show");
    if (typeof doBodySifr == "function") {
        doBodySifr();
    }
}
function makeSelected(selectedDiv) {
    oldA = $(".left_nav li.selected .sIFR-alternate a")
    oldA.removeClass("white")
    $(".left_nav li.selected .sIFR-alternate").html(oldA)
    temp = sIFR.getReplacementByFlashElement($(".left_nav li.selected .sIFR-flash")[0])
    temp.replaceText($(".left_nav li.selected .sIFR-alternate").html());

    $(".left_nav li").removeClass("selected");

    $(".left_nav #itemLI" + selectedDiv).addClass("selected");
    newA = $(".left_nav #itemLI" + selectedDiv + " .sIFR-alternate a")
    $(".left_nav #itemLI" + selectedDiv + " .sIFR-alternate").html(newA)
    html = $(".left_nav #itemLI" + selectedDiv + " .sIFR-alternate").html();
    html = html.replace('<A', '<a').replace('<a class=""', '<a class="white"')
    temp = sIFR.getReplacementByFlashElement($(".left_nav #itemLI" + selectedDiv + " .sIFR-flash")[0])
    temp.replaceText(html);
}

var safeclick = false;
var safeclick2 = false;

function toggleDropDown2(drop) {
    if (safeclick2 == false) {
        if ($("#" + drop + "_list").hasClass("hide")) {
            $(".dropdownlist").addClass("hide");
        }
        $("#" + drop + "_list").toggleClass("hide")
        if ($("#" + drop + "_list").hasClass("hide")) {
        } else {
            safeclick = true;
        }
    }
}
function toggleDropDown(drop) {
    safeclick2 = true;
    if ($("#" + drop + "_list").hasClass("hide")) {
        $(".dropdownlist").addClass("hide");
    }
    $("#" + drop + "_list").toggleClass("hide")
    if ($("#" + drop + "_list").hasClass("hide")) {
    } else {
        safeclick = true;
    }
}

function selectDropDown(txt, drop) {
    var id = $("[class*=" + drop + "_span]").attr("id");
    var objInput = document.getElementById(id);
    objInput.value = txt;
}

function selectDropDownValue(txt, val, drop) {
    var id = $("[class*=" + drop + "_span]").attr("id");
    var objInput = document.getElementById(id);
    objInput.value = txt;

    var idHid = $("[class*=" + drop + "_hidden]").attr("id");
    var objHid = document.getElementById(idHid);
    objHid.value = val;
}

$(document.body).click(function() {
    if (safeclick == false && $.fn != null) {
        $(".dropdownlist").addClass("hide");
    } else {
        safeclick = false;
        safeclick2 = false;
    }
});





function toggleRadio(target, txt) {
    $input = $("#" + target + "_input input");
    $(".radio_img_country").attr("src", "/Controls/Templates/images/radio_off.jpg");
    $input.attr("value", txt);
    $("#radio_img_" + txt).attr("src", "/Controls/Templates/images/radio_on.jpg");
}

function toggleSingleRadio(target) {
    $input = $("#" + target + "_input input");
    if ($input[0] == null) {
        $input = $("#" + target + "_input");
    }
    if ($input.attr("value") == "false") {
        document.getElementById("radio_img_" + target).src = "/Controls/Templates/images/radio_on.jpg";
        $input.attr("value", "true");
    }
    else if ($input.attr("value") == "true") {
        document.getElementById("radio_img_" + target).src = "/Controls/Templates/images/radio_off.jpg";
        $input.attr("value", "false");
    }
}

function swapImage(src) {
    $("#dest_img").attr("src", src);
}

function showVideo(title, src) {
    if (src.match(".swf")) {
        $("#video_container").html("<div id=\"videoplayerFlash\"></div>")
        $('#videoplayerFlash').html("").flash({
            src: src,
            width: 840,
            height: 466,
            wmode: 'transparent'
        })
    }
    else if (src.match(".flv")) {
        $("#video_container").html("<div id=\"videoplayerFlash\"><div>");
        $('#videoplayerFlash').html("").flash({
            src: "/Controls/Templates/swf/videoPlayer.swf",
            flashvars: { path: src },
            width: 840,
            height: 466,
            wmode: 'transparent'
        });
    }
    else {
        $("#video_container").html("<img src=\"" + src + "\" />");
    }
    $("#video_player").removeClass("hide");
    $("#video_player #video_title").text(title).removeClass("sIFR-replaced");
    doVideoSIFR();
    try {
        temp = sIFR.getReplacementByFlashElement($("#video_player #video_title .sIFR-flash")[0])
        temp.replaceText(title);
    }
    catch (err) { }
    $("#footer , #body").addClass("hide");
}
function hideVideo() {
    $('#videoplayerFlash').html("")
    $("#video_player").addClass("hide");
    $("#footer , #body").removeClass("hide");
}
function swapVideo(src) {
    if (src.match(".swf")) {
        $("#video_container").html("<div id=\"videoplayerFlash\"><div>");
        $('#videoplayerFlash').html("").flash({
            src: src,
            width: 840,
            height: 466,
            wmode: 'transparent'
        });
    }
    if (src.match(".flv")) {
        $("#video_container").html("<div id=\"videoplayerFlash\"><div>");
        $('#videoplayerFlash').html("").flash({
            src: "/Controls/Templates/swf/videoPlayer.swf",
            flashvars: { path: src },
            width: 840,
            height: 466,
            wmode: 'transparent'
        });
    }
}
function showThumb(dest, src) {
    $(".videothumb").removeClass("hide");
    $(".videothumb").html("<img src=\"" + src + "\"/>");
    var dim = GetTopLeft($("#topnav #navspan" + dest)[0])
    $(".videothumb").css("left", dim.Left );
    $(".videothumb").css("top", dim.Top+15);
}

function GetTopLeft(elm) {

    var x, y = 0;

    //set x to elm’s offsetLeft
    x = elm.offsetLeft;

    //set y to elm’s offsetTop
    y = elm.offsetTop;

    //set elm to its offsetParent
    elm = elm.offsetParent;

    //use while loop to check if elm is null
    // if not then add current elm’s offsetLeft to x
    //offsetTop to y and set elm to its offsetParent

    while (elm != null) {

        x = parseInt(x) + parseInt(elm.offsetLeft);
        y = parseInt(y) + parseInt(elm.offsetTop);
        elm = elm.offsetParent;
    }

    //here is interesting thing
    //it return Object with two properties
    //Top and Left

    return { Top: y, Left: x };

}

function hideThumb(dest) {
    $(".videothumb").addClass("hide");
}

function swapVideoTip(videoTip) {

    $("#videoTopicLinks .sIFR-flash").blur();
    oldA = $("#videoTopicLinks span.selected .sIFR-alternate a")
    oldA.removeClass("orange")
    $("#videoTopicLinks span.selected .sIFR-alternate").html(oldA)
    temp = sIFR.getReplacementByFlashElement($("#videoTopicLinks span.selected .sIFR-flash")[0])
    temp.replaceText($("#videoTopicLinks span.selected .sIFR-alternate").html());

    $("#videoTopicLinks span").removeClass("selected");

    $("#videoTopicLinks #link" + videoTip).addClass("selected");
    newA = $("#videoTopicLinks #link" + videoTip + " .sIFR-alternate a")
    $("#videoTopicLinks #link" + videoTip + " .sIFR-alternate").html(newA)
    html = $("#videoTopicLinks #link" + videoTip + " .sIFR-alternate").html();
    html = html.replace('<A', '<a').replace('<a class=""', '<a class="orange"')
    temp = sIFR.getReplacementByFlashElement($("#videoTopicLinks #link" + videoTip + " .sIFR-flash")[0])
    temp.replaceText(html);

    sIFR.getReplacementByFlashElement($("#videoTitle .sIFR-flash")[0]).replaceText(videos[currentTopic][videoTip]["Title"]);

    //$("#videoThumbnailImg").attr("src", videos[currentTopic][videoTip]["Thumbnail"]);
    currentVideo = videoTip;
}

function swapTipFlash() {
    if (topicsAction != "none") {
        varWidth = 840;
        varHeight = 466;
        dest = "videoplayerFlash"
    } else {
        varWidth = 375;
        varHeight = 199;
        dest = "videoFlash"
    }

    if (videos[currentTopic][currentVideo]["Link"].match(".swf")) {
        $("#" + dest).html("").flash({
            src: videos[currentTopic][currentVideo]["Link"],
            width: varWidth,
            height: varHeight,
            wmode: 'transparent'
        });
    }
    if (videos[currentTopic][currentVideo]["Link"].match(".flv")) {
        $("#" + dest).html("").flash({
            src: "/Controls/Templates/swf/videoPlayer.swf",
            flashvars: { path: videos[currentTopic][currentVideo]["Link"] },
            width: varWidth,
            height: varHeight,
            wmode: 'transparent'
        });
    }
}

function changeVideoTopic(newTopic) {
    currentTopic = newTopic;
    currentVideo = 0;
    $("#topicList .sIFR-flash").blur();
    temp = sIFR.getReplacementByFlashElement($("#videoTopicTitle .sIFR-flash")[0])
    temp.replaceText(videos[currentTopic]["Title"]);
    swapVideoTip(0);
    testTopicLinks();
    swapTipFlash();
    try {
        if (topicsAction == "hide") {
            showTopics();
        }
    } catch (err) { }
}
function testTopicLinks() {
    links = $("#videoTopicLinks span")
    links.hide();
    for (var i = 0; i < videos[currentTopic].length; i++) {
        if (videos[currentTopic][i]) {
            $("#videoTopicLinks span#link" + i).show();
        }
    }

    sIFR.replace(albatross, {
        selector: '#videoTopicLinks span',
        css: '.sIFR-root { font-size:12px; color: #999999; } .sIFR-root a {color:#999999; text-decoration:none;} .sIFR-root .orange {color:#faae18; text-decoration:none;} .sIFR-root a:hover {color:#faae18; text-decoration:none;}',
        wmode: 'transparent',
        forceSingleLine: true
    });
}
function showPrevTip() {
    if (currentVideo > 0) {
        currentVideo = currentVideo - 1;
        swapVideoTip(currentVideo);
        swapTipFlash();
    }
}
function showNextTip() {
    if (videos[currentTopic][currentVideo + 1]) {
        currentVideo = currentVideo + 1;
        swapVideoTip(currentVideo);
        swapTipFlash();
    }
}


function flashStringToHTML(string, flashTextWidth) {    
    $("#" + flashObj).after("<div id=\"flashContent\" style=\"display:none;\"></div>");
    $("#flashContent").css("width", flashTextWidth).html(unescape(string));

    result = flashContentToFlashHTML();
    $("#flashContent").remove();
    if (window[flashObj]) {
        window[flashObj].SetVariable("htmlContent", result);
    }
    else if (document[flashObj]) {
        document[flashObj].SetVariable("htmlContent", result);
    }
}

function convertHTMLToFlashContent(string, flashTextWidth, destObj) {
    var oNumber = Math.floor(Math.random() * 11);
    var popupDiv;

    if ($("#popupContent").html() != null) {
        popupDiv = document.getElementById("popupContent");
    }
    else if ($("#popup").html() != null) {
        popupDiv = document.getElementById("popup");
    }

    var newDiv = document.createElement('div');
    newDiv['id'] = 'div_' + oNumber;
    popupDiv.appendChild(newDiv);

    $('#div_' + oNumber).css("width", flashTextWidth).html(unescape(string));

    var oRetVal = flashContentToFlashHTML('div_' + oNumber);

    getMovieName(destObj).setNewGenericFlashText(oRetVal);
}

function flashContentToFlashHTML(contentdiv) {

    //al ert('test odiv ' + $('#' + contentdiv).html());
    
    if (contentdiv == null) {
        contentdiv = "flashContent";
    }

    //al ert(contentdiv);

    var objFormatted = $("#" + contentdiv + " #flashformattedtest");

    //al ert($("#" + contentdiv).html());

    if ($(objFormatted).text() == "flashformattedtest=true") {
        var flashTemp = $("#" + contentdiv).html();
        return flashTemp;
    }
    else {
        var temp = $("#" + contentdiv).html();
        temp = temp.replace(/\n/g, "");
        $("#" + contentdiv).html(temp);
        $("#" + contentdiv + " img[class!=flashimage]").each(function() {
            var height = 0;
            height = this.height;

            if (height == 0) {
                height = $(this).attr("height");
            }

            if (height == 0) {
                height = $(this).attr("css");
            }

            if (height == 0) {
                height = $(this).css("height");
            }

            if (height == 0) {
                height = $(this).css("height");
            }

            var align = "";
            var margin;
            margin = parseInt($("#" + contentdiv).width()) - parseInt($(this).attr("width"));
            rightMargin = "rightMargin='" + margin + "'"

            if ($(this).css("float") == "left" || $(this).css("float") == "right") {
                align = "align='" + $(this).css("float") + "'";
                rightMargin = "";
            }
            if ($(this).attr("align")) {
                align = "align='" + $(this).attr("align") + "'";
                rightMargin = "";
            }
            leading = 0;

            tempText = $(this).outerHTML();

            $(this).replaceWith("<TEXTFORMAT " + align + " LEADING='" + (parseInt(height) + leading) + "' " + rightMargin + ">" + tempText + "</TEXTFORMAT><P class='imageP' marginRight='" + $("#" + contentdiv).width() + "'>&nbsp;</P>");
        });

        $("#" + contentdiv + " img.flashimage").each(function() {

            if (this.parentNode.nodeName == "P") {
                $(this).parent().replaceWith(this);
            }
            var height = 0;
            height = this.height;

            if (height == 0) {
                height = $(this).attr("height");
            }

            if (height == 0) {
                height = $(this).attr("css");
            }

            if (height == 0) {
                height = $(this).css("height");
            }

            if (height == 0) {
                height = $(this).css("height");
            }

            var align = "";
            var margin = $("#" + contentdiv).width() + 100;
            rightMargin = "rightMargin='" + margin + "'"

            if ($(this).css("float") == "left" || $(this).css("float") == "right") {
                align = "align='" + $(this).css("float") + "'";
                rightMargin = "";
            }
            if ($(this).attr("align")) {
                align = "align='" + $(this).attr("align") + "'";
                rightMargin = "";
            }
            leading = 20;
            tempText = $(this).outerHTML();
            $(this).replaceWith("<TEXTFORMAT " + align + " LEADING='" + (parseInt(height) + leading) + "' " + rightMargin + ">" + tempText + "</TEXTFORMAT><P class='imageP' marginRight='" + $("#" + contentdiv).width() + "'>&nbsp;</P>");
        });

        $("#" + contentdiv + " p[class!=imageP]").each(function() {
            if ($(this).html() != "") {
                tempText = $(this).outerHTML();
                var align = "left";
                if ($(this).css("float") != "none") {
                    align = $(this).css("float");
                }
                if ($(this).attr("align") == "left" || $(this).attr("align") == "right") {
                    align = $(this).attr("align");
                }
                $(this).replaceWith("<TEXTFORMAT align='" + align + "' leading='5' leftMargin='10'>" + tempText + "</TEXTFORMAT><br/>");
            }
            //al ert('p end');
        });

        $("#" + contentdiv + " td").each(function() {
            $(this).replaceWith("<TEXTFORMAT leading='5' leftMargin='10'><p>" + $(this).html() + "</p></TEXTFORMAT><br/>");
        });

        $("#" + contentdiv + " * b").each(function() {
            $(this).replaceWith("<font color=\'#000000\'>_</font><font color=\'#faae18\'>" + $(this).html() + "</font>");
        });

        $("#" + contentdiv + " * strong").each(function() {
            $(this).replaceWith("<font color=\'#000000\'>_</font><font color=\'#faae18\'>" + $(this).html() + "</font>");
        });


        $("#" + contentdiv + " li").each(function() {
            $(this).prepend("<font color=\'#faae18\'>&gt;&gt;</font>");
        });

        $("#" + contentdiv + " h1").each(function() {
            $(this).replaceWith("<textformat leftMargin='10' >" + $(this).outerHTML() + "</textformat>");
        });

        $("#" + contentdiv + " h2").each(function() {
            $(this).replaceWith("<textformat leftMargin='10' >" + $(this).outerHTML() + "</textformat>");
        });
        $("#" + contentdiv + " > br").each(function() {
            $(this).replaceWith("<p></p>");
        });
        $("#" + contentdiv + " > #text").each(function() {
            $(this).wrap("<textformat leftMargin='10' ></textformat>");
        });

        $('a[href^="http://"]').attr("target", "_blank");

        $("#" + contentdiv).append("flashformattedtest=true");

        var flashTemp = $("#" + contentdiv).html();

        //al ert('first ' + flashTemp);
        flashTemp = flashTemp.replace(/^\s+|\s+$/g, "") //trims all white space from the start and end, flash won't start with them
        //flashTemp.replace(/px/g, "") //takes px out
        flashTemp = flashTemp.replace(/>\s{2,}</g, '><') //takes out extra spaces between closing and opening tags
        flashTemp = flashTemp.replace(/jQuery[0-9]*=\D[\w]*\D/g, ''); //takes out the jquery atrribute
        flashTemp = flashTemp.replace(/([\w]*)='?([\w]*)'?\s/g, "$1='$2' "); //adds single quotes to elements in ie
        flashTemp = flashTemp.replace(/"/g, "'"); //turns double quotes into singles
        flashTemp = flashTemp.replace(/color\=#faae18/g, "color=\'#faae18\'"); //IE fix for font color
        flashTemp = flashTemp.replace(/color\=#000000/g, "color=\'#000000\'"); //IE fix for font color
        flashTemp = flashTemp.replace(/ximg/g, "img"); //IE fix for font color

        //al ert('wrapup end');

        //al ert(flashTemp);
        return flashTemp;
    }
}

jQuery.fn.outerHTML = function() {
    return $('<div>').append(this.eq(0).clone()).html();
};


/* These functions are used to set the language cookie for the master page footer drop down */
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function setLang(toWhat, whereNext) {
    createCookie("langCookie", toWhat, 365);
    document.location.href = whereNext;
}

function getMovieName(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}