// global vars:
var inlineVideo = "";
var popupVideo = "";

jQuery.noConflict();
jQuery(function($) {
    // ===== Two Types of Media Containers: Inline or Popups =====
    inlineVideo = $('#media_video.inlineMediaContainer');
    popupVideo = $('#media_video.popupMediaContainer');
    var oeVideoPlayer = ""; // video player;
    // ===== INLINE =========================
    if (inlineVideo.length) {
        var selected = "";
        // append events to the thumbnails
        inlineVideo.find('.media_thumbs li a').click(function(e) {
            oeVideoPlayer = document.getElementById("oeVideoPlayer");
            if (oeVideoPlayer) oeVideoPlayer.loadMedia($(this).attr('rel'), $(this).attr('href'));
            $(this).parent().addClass('selected');
            selected.removeClass('selected');
            selected = $(this).parent();
            return false;
        });

        // initialize first one
        var media = inlineVideo.find('.media_thumbs li a:first');
        media.parent().addClass('selected');
        selected = media.parent();

        // load the video player
        if (swfobject.hasFlashPlayerVersion("9.0.0")) {
            var att = { data:"flash/videoModule.swf", width:"300", height:"225", menu:"false", id:"oeVideoPlayer", name:"oeVideoPlayer" };
            var par = { wmode:"transparent", swliveconnect:"true", allowScriptAccess:"always", bgcolor:"#000000", flashvars:"Image=" + media.attr("rel") + "&Video=" + media.attr("href") };
            var id = "videoPlayer";
            var so = swfobject.createSWF(att, par, id);
        } else {
            $('videoPlayer').set('html', '<h2>Please <a href="http://www.adobe.com/products/flashplayer/" target="blank">upgrade your Flash Player</a><br /> to experience this site to its fullest.</h2>');
        }

        // ===== POPUP =========================
    } else if (popupVideo.length) {
        var openwin = false;
		// close button
        popupVideo.find('.btnClose').click(function(e) {
            oeVideoPlayer = document.getElementById("oeVideoPlayer");
            oeVideoPlayer.stopMedia();
            popupVideo.css('left', '-999px');
			resizePopupVideo(300,225);
            openwin = false;
        });
        // load the video player
        if (swfobject.hasFlashPlayerVersion("9.0.0")) {
            var att = { data:"flash/videoModule.swf", width:"100%", height:"100%", menu:"false", id:"oeVideoPlayer", name:"oeVideoPlayer" };
            var par = { swliveconnect:"true", allowScriptAccess:"always", bgcolor:"#000000" };
            var id = "videoPlayer";
            var so = swfobject.createSWF(att, par, id);
        } else {
            $('videoPlayer').set('html', '<h2>Please <a href="http://www.adobe.com/products/flashplayer/" target="blank">upgrade your Flash Player</a><br /> to experience this site to its fullest.</h2>');
        }
        // ===== Table (e.g. Stakes Schedule) =====
        if ($('#video_table').length) {
            $('#video_table a.greenlink_icons').click(function(e) {
                // position window
                var $this = $(this);
                var pos = $this.offset();
                var width = popupVideo.outerWidth();
                popupVideo.css({ 'top': (pos.top - 10) + 'px', 'left': (pos.left - width) + 'px', 'display': 'block' });
                // race date
                popupVideo.find('.date').text($this.parent().parent().find(':nth-child(2)').text());
                // screenshot / video
                oeVideoPlayer = document.getElementById("oeVideoPlayer");
                if (oeVideoPlayer) {
                    if (openwin) {
                        oeVideoPlayer.loadMedia($this.attr('rel'), $this.attr('href'));
                    } else {
                        setTimeout(function() { oeVideoPlayer.loadMedia($this.attr('rel'), $this.attr('href')); openwin = true; }, 1000);
                    }
                }
                return false;
            });
        }
        // ===== Calendar (e.g. Race Replay) =====
        else if ($('ul.calendar .raceday a').length) {
            var raceday = $('ul.calendar .raceday a');
            var videos = [];
            var nSelectedRace = 0;
            var selectRace = popupVideo.find('select');
            selectRace.change(function() {
                var media = videos[$(this).val()];
                oeVideoPlayer = document.getElementById("oeVideoPlayer");
                oeVideoPlayer.loadMedia(media.thumb, media.video);
            });
			
            raceday.click(function(e) {
                videos = []; // reset;
				
                // position window
                var $this = $(this);
                var pos = $this.offset();
                var width = popupVideo.outerWidth();
                var content = $('#body_subpage').offset();
                var left = ((pos.left - width) > content.left) ? (pos.left - width) : (pos.left + $this.outerWidth());
                popupVideo.css({ 'top': (pos.top - 10) + 'px', 'left': left + 'px', 'display': 'block' });
				
                // race date
                popupVideo.find('.date').text($.param('day', $(this).attr('href')));
                oeVideoPlayer = document.getElementById("oeVideoPlayer");
                if (oeVideoPlayer && openwin) oeVideoPlayer.stopMedia();
				
                $.ajax({
                    type: "GET",
                    url: $(this).attr('href'),
                    dataType: "xml",
                    success: function(xml) {
                        selectRace.empty();
						
                        $(xml).find('Event').each(function(i) {
                            videos[videos.length] = {
                                title: $(this).find('Title').text(),
                                video: $(this).find('LinkUrl').text(),
                                thumb: $(this).find('Thumb').text()
                            };
                            selectRace.append("<option value=" + i + ">Race " + (i + 1) + "</option>")
                        });
						
                        // screenshot / video
                        oeVideoPlayer = document.getElementById("oeVideoPlayer");
                        if (oeVideoPlayer) {
                            setTimeout(function() { oeVideoPlayer.loadMedia(videos[0].thumb, videos[0].video); }, 500);
                            openwin = true;
                        }
						
                    }
                });
				
                return false;
            });
			
        };
    };
    $.param = function(name, href) {
        var exp = new RegExp('[\\?&]' + name + '=([^&#]*)');
        var param = exp.exec((href) ? href : window.location.href);
        return (param) ? param[1] : null;
    };
});
// ===== Dynamically Resize Popup Video =====
function resizePopupVideo(w,h){
	var mainWidth = w + (32*2); //32px padding;
	var mainHeight = h + (41+78); //41px top / 78px btm;
	popupVideo.css({'width':mainWidth+'px','height':mainHeight+'px'});
	popupVideo.find('div.videoPlayer').css({'width':w+'px','height':h+'px'});
	popupVideo.find('tr.ctr td').css({'height':(mainHeight-24)+'px'});
};
