// JavaScript Document

// Dialog for video

   jQuery(".videotrigger").click(function() {

        jQuery("#playerContainer").dialog({
            bgiframe: true,
            autoOpen: false,
            width: 790,
            closeOnEscape: true,
            position: 'center',
            resizable: false,  
            draggable: false,
            modal: true,
            title: "abbywinters.com DVDs",
            beforeclose: function(e, ui) {
                if (videoPlayer) {
                    // this only seems to work with document.getElementById()
                    if (document.getElementById("ccvideo")) {
                        //typeof document.getElementById("ccvideo").sendEvent == "function") {
                        try {
                            document.getElementById("ccvideo").sendEvent("STOP");
                        } catch (err) {}
                    }
                    delete(videoPlayer);
                    videoPlayer = null;
                }
                jQuery("#videoPlayer").empty();
				jQuery("#activeData").empty();
            }
        });

		// Get the video filename to display
        //var video = $("span.filename", this).text();
		// load up the first video
		//var bitrate = jQuery("#flv_quality").val();
		//console.log(bitrate);
		//alert($(this).html());
		
		var video = jQuery("span.flvData span.flvurl", jQuery(this)).text();
		
		var height = jQuery("span.flvData span.flvheight", jQuery(this)).text();
		var width = jQuery("span.flvData span.flvwidth", jQuery(this)).text();
		
		// put the video data in activeData for ease of switching between bitrates/sizes
		jQuery("#activeData").append(jQuery("span.flvData", jQuery(this)).html());

        var s1 = new SWFObject("/wp-content/themes/aw_dvd/js/MediaPlayer/player.swf","ccvideo",width,height,"8");
        s1.addParam("allowfullscreen","true");
        s1.addParam("allowscriptaccess", "always");
        s1.addParam("name", "ccvideo");		
        s1.addVariable("controlbar","over");
        s1.addVariable("bufferlength","20");		
		s1.addVariable("skin", "/wp-content/themes/aw_dvd/js/MediaPlayer/skins/modieus.swf");
        s1.addVariable("width",width);
        s1.addVariable("height",height);
        s1.addVariable("file", video);
        /// NOTE: addVariable "id" with the value of the id given in the constructor is required
        s1.addVariable("id", "ccvideo");
        s1.addVariable("javascriptid","ccvideo");
        s1.addVariable("enablejs","true");
        s1.addVariable("autostart","true");
        s1.write('videoPlayer');
        videoPlayer = s1;
		
        jQuery("#playerContainer").dialog("open");
		
		jQuery("#flv_quality").change(function() {

			// get the current bitrate value
			bitrate = jQuery(this).val();
			videoPlayer = document.getElementById("ccvideo");

			// load the video data from #activeData
			var content = jQuery("#activeData span.bitrate"+bitrate);
			video = jQuery('span.flvurl', content).text();
			height = jQuery('span.flvheight', content).text();
			width = jQuery('span.flvwidth', content).text();
			title = jQuery('span.flvtitle', content).text();
			if (title) {
				jQuery('.ui-dialog-title').text(title);
			}
			
            try {
                videoPlayer.sendEvent('STOP');
                videoPlayer.sendEvent('LOAD', video);
            } catch (Exception) {
                return;
            }
			
			jQuery("#ccvideo").css({
				"width" : width+"px",
				"height" : height+"px"
			});

		});
		return false;
	});
