/*Needs youtube, accordion, carousel*/

function setTopTab(uitab) {
    mediaCenterTopTabClick('#TT_MCFlash', uitab);
    mediaCenterTopTabClick('#TT_MCPhotos', uitab);
    mediaCenterTopTabClick('#TT_MCMov', uitab);
    mediaCenterTopTabClick('#TT_MCFP', uitab);
}

function mediaCenterTopTabClick(panelId, uitab) {
    var h = $(uitab).attr('href');
    h == panelId ? mediaCenterShowTopTab(panelId) : mediaCenterHideTopTab(panelId);
}

function mediaCenterShowTopTab(panelId) {
    var panel = $(panelId);
    if (panelId == "#TT_MCFlash") {
        var items = panel.find("a.TT_MCVideoItem");
        if (items.length > 0) {
            $(items[0]).click();
        }
    } else if (panelId == "#TT_MCPhotos") {
        if ($('.TT_MCPhotoItems img.BaseImage').length > 1) panel.find(".start").click();
    } else if (panelId == "#TT_MCMov") {
        var items = panel.find("a.TT_MCMovItem");
        if (items.length > 0) {
            $(items[0]).click();
        }
    } else if (panelId == "#TT_MCFP") {
        return;
    } else {
        return;
    }
}

function mediaCenterHideTopTab(panelId) {
    var panel = $(panelId);
    if (panelId == "#TT_MCPhotos") {
        if ($('.TT_MCPhotoItems img.BaseImage').length > 1) panel.find(".stop").click();
    } else if (panelId == "#TT_MCFlash") {
        if ($f('TT_MCFlashPlayer')) {
            $f('TT_MCFlashPlayer').pause();
        }
    } else if (panelId == "#TT_MCMov") {
        panel.find(".TT_MCMovPlayer").empty();
    } else if (panelId == "#TT_MCFP") {
        return;
    } else {
        return;
    }
}(function ($) {
    $.fn.mediaCenterMov = function (settings) {
        settings = jQuery.extend({
            targetSelector: "div.TT_MCMovPlayer",
            // (String) Container into which to load the mov file
            statusSelector: "div.TT_MCMovStatus",
            // (String) Place to display the label and possibly link
            videoUrl: "",
            // (String) Video url to be asssigned laterlink to enlarge
            videoLabel: "",
            // (String) Placeholder for status title
            width: 400,
            //320, // (int) Specify video width
            height: 300 //256 // (int) Specify video height
        }, settings);

        function _initialize() {
            try {
                _start(this); // This refers to object (link) which the user has clicked
            } catch (e) {
                alert(e);
            }
            return false; // Prevent the browser from following the link
        };

        function _start(clicked) {
            settings.videoUrl = $(clicked).attr("href");
            settings.videoLabel = $(clicked).attr("title");
            if ($(settings.statusSelector).length > 0) {
                $(settings.statusSelector).empty().append('<a href="' + settings.videoUrl + '">' + settings.videoLabel + '</a>');
            }
            $(settings.targetSelector).html(_embedHTML(settings.videoUrl, settings.width, settings.height)).show();
        };

        function _embedHTML(url, width, height) {
            var html = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="' + height + '" width="' + width + '"> ' + '<param name="src" value="' + url + '">' + '<param name="controller" value="true">' + '<param name="autoplay" value="true">' + '<param name="wmode" value="transparent">' + '<param name="type" value="video/quicktime" height="' + height + '" width="' + width + '">' + '<embed src="' + url + '" height="' + height + '" width="' + width + '" ' + getWmode() + 'controller="true" autoplay="true" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/">' + '</object>';
            return html;
        };

        function getWmode() {
            if (jQuery.browser.safari == true) {
                return 'wmode="transparent" ';
            }
            return '';
        }
        // Return the jQuery object for chaining


        return this.click(_initialize);
    };
})(jQuery); // Call and execute the function immediately passing the jQuery object 
var videoTag = 'TT_MCFlashPlayer';
var fpPlayer = {
    src: "http://common.interactrv.com/common/assets/flash/flowplayer/flowplayer.commercial-3.2.7-2.swf",
    wmode: "opaque"
}
var fpConfig = {
    plugins: {
        controls: {
            autoHide: false
        },
        youtube: {
            url: 'http://common.interactrv.com/common/assets/flash/flowplayer/flowplayer.youtube-3.2.6.swf',
            hd: 1
        }
    },
    clip: {
        onStart: function (clip) {
            var wrap = jQuery(this.getParent());
            var strD = clip.metaData.height / clip.metaData.width;
            var strH = strD * 520;
            wrap.css({
                width: 400,
                height: strH
            });
        },
        autoBuffering: true,
        provider: 'youtube',
        urlResolvers: 'youtube',
        autoPlay: true,
        metaData: false,
        scaling: 'fit'
    }
}
$(function () {
    // set initial height and width so video starts.
    $("#TT_MCFlashPlayer").css({
        'height': '250px',
        'width': '400px'
    })
    $('.TT_MCVideoItem').each(function () {
        var t = $(this);
        t.attr('rel', t.attr('rel').replace('&hd=1', ''));
        var vidHref = $(this).attr('href');
        var vidRel = $(this).attr('rel');
        $(this).attr('href', vidRel);
        var newVidRel = 'api:' + vidRel;
        $(this).attr('href', newVidRel);
    });
    fpConfig.key = fpkey;
	if ($('#TT_MCFlashPlayer').length) {
    $f(videoTag, fpPlayer, fpConfig).playlist(".TT_MCVideoList ul li", {
        loop: true
    });
	}
});
