﻿function loadVideoPlayer(videoId, playClip, videoType, divId) {
    //playClip = ((playClip != '') ? 'true' : 'false');
    if (playClip == '') {
        playClip = 'false';
    }


    if (videoType == 'E') {
        $.ajax({
            type: 'GET',
            cache: false,
            url: '/ajax/Videoajax.aspx',
            processData: true,
            data: 'Source=video_embed' + '&VideoId=' + videoId + '&PlayClip=' + playClip + '&DivId=' + divId,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'html',
            success: function(result, textStatus) {
                $('#' + divId).empty();
				$('#' + divId).html(result);
            },
            complete: function() {
                //fixCols();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('There was a problem loading the video you requested. Please try again.');
            }
        });    
    
        //document.getElementById(divId).innerHTML = '<div id="videoPlayer"><iframe width="480" height="307" src="http://www.youtube.com/embed/zpWRIPTyhzY" frameborder="0" allowfullscreen></iframe></div>';


    }
    


    if (videoType == 'F') {
        document.getElementById(divId).innerHTML = '<div id="videoPlayer"></div>';
        
        var flashvars = {};
        flashvars.datasrc = "/xml/video/" + videoId + ".xml&rnd=" + guid();
        flashvars.autostart = playClip;
        
        var params = {};
        params.quality = "best";
        params.autostart = playClip;
        params.wmode = "transparent";
        params.allowfullscreen = "true";
        params.allowscriptaccess = "always";

        var attributes = {};
        attributes.id = "videoPlayer";
        attributes.name = "videoPlayer";

        swfobject.embedSWF("/flash/video_player/MediaPlayer_small.swf", "videoPlayer", "640", "368", "9.0.0", false, flashvars, params, attributes);

    }

    if (videoType == 'R') {
        $.ajax({
            type: 'GET',
            cache: false,
            url: '/ajax/Videoajax.aspx',
            processData: true,
            data: 'Source=video_real_media' + '&VideoId=' + videoId + '&PlayClip=' + playClip + '&DivId=' + divId,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'html',
            success: function(result, textStatus) {
                $('#' + divId).html(result);
            },
            complete: function(){
                //fixCols();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('There was a problem loading the video you requested. Please try again.');
                //alert('loadVideoPlayer [Real] Error: ' + errorThrown);
            }
        });
    }

    if (videoType == 'W') {
        $.ajax({
            type: 'GET',
            cache: false,
            url: '/ajax/Videoajax.aspx',
            processData: true,
            data: 'Source=video_windows_media' + '&VideoId=' + videoId + '&PlayClip=' + playClip + '&DivId=' + divId,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'html',
            success: function(result, textStatus) {
                $('#' + divId).html(result);
            },
            complete: function(){
               //fixCols();
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('There was a problem loading the video you requested. Please try again.');
                //alert('loadVideoPlayer [Windows] Error: ' + errorThrown);
            }
        });    
    }
}

function loadMultimediaText(videoId) {
	$.ajax({
		type: 'GET',
		cache: false,
		url: '/ajax/Videoajax.aspx',
		processData: true,
		data: 'Source=multimedia_text' + '&VideoId=' + videoId,
		contentType: 'application/x-www-form-urlencoded',
		dataType: 'json',
		success: function(result, textStatus) {
			document.title = result.VideoPageTitle
			$('#videoId').val(result.VideoId);
			$('#videoBreadcrumbTitle').html(result.VideoTitle);
			$('#videoTitle').html(result.VideoTitle);
			$('#videoKeywords').html(result.VideoKeywords);
			$('#videoSummary').html(result.VideoSummary);
			$('#videoViews').html(result.VideoViews);
			$('#videoRating').html(result.VideoRating);
			result.VideoEmbedText == '' ? $('#source_info').html('') : $('#source_info').html('<dt>Source:</dt> <dd class="source" id="videoSource">' + result.VideoEmbedText + '</dd>');
			result.VideoEmbedCreatedBy == '' ? $('#createdby_info').html('') : $('#createdby_info').html('<dt>Created By:</dt><dd id="videoSource">' + result.VideoEmbedCreatedBy + '</dd>');

			$(".addthis_button_email").attr("addthis:url", result.VideoCustomUrl).attr("addthis:title", result.VideoPageTitle);
			$(".addthis_button_print").attr("addthis:url", result.VideoCustomUrl).attr("addthis:title", result.VideoPageTitle);
			$(".addthis_button").attr("addthis:url", result.VideoCustomUrl).attr("addthis:title", result.VideoPageTitle);
			window.addthis.ost = 0;
			window.addthis.ready();

			//$('#fbIframeLike').attr('src', result.VideoLikeSource);
			//window.frames["fbIframeLike"].location.reload();
			$('#fbIframeLike').empty();
			$('#fbIframeLike').append('<iframe src="'+ result.VideoLikeSource +'" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:90px; height:21px;" allowtransparency="true"/>');
		},
		complete: function() {

			$('#two_column #main_content #multimedia #video_info div.rating_info').stars();
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert('loadMultimediaText Error: ' + errorThrown);
		}
	});
}

function loadMultimediaThumbs(videoCategoryId, currentPage) {
    $.ajax({
        type: 'GET',
        cache: false,
        url: '/ajax/Videoajax.aspx',
        processData: true,
        data: 'Source=multimedia_thumbs' + '&VideoCategoryId=' + videoCategoryId + '&CurrentPage=' + currentPage,
        contentType: 'application/x-www-form-urlencoded',
        dataType: 'html',
        success: function(result, textStatus) {
            $('#other_video_wrap').html(result);

        },
        complete: function() {
		$('div#other_video_wrap > div#other_videos > div#video_category > ul').each(function() {
			$(this).categoryList();
		});
		$('div#other_video_wrap > div#other_videos > ul#videos > li > div.rating_info').each(function() {
			var starCount = parseFloat($(this).children('div.rating').text());
			$(this).children('div.rating').width(starCount * 10.0);
			if (starCount != 0){
				$(this).parent().children('div.view_info').after('<div class="separator">|</div>');			
			}
		});
		$('div#other_video_wrap div#other_videos ul#videos').equalHeights();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert('loadMultimediaThumbs Error: ' + errorThrown);
        }
    });
}

function videoRating(videoId, videoRating, elementId) {
    $.ajax({
        type: 'GET',
        cache: false,
        url: '/ajax/Videoajax.aspx',
        processData: true,
        data: 'Source=multimedia_rating' + '&VideoId=' + videoId + '&VideoRating=' + videoRating,
        contentType: 'application/x-www-form-urlencoded',
        dataType: 'text',
        success: function(result, textStatus) {
            //alert(result);
        },
        complete: function() {

        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert('RateItemMultiple Error: ' + errorThrown);
        }
    });
}

function videoTracking(videoId) {
    $.ajax({
        type: 'GET',
        cache: false,
        url: '/ajax/Videoajax.aspx',
        processData: true,
        data: 'Source=video_tracking' + '&VideoId=' + videoId,
        contentType: 'application/x-www-form-urlencoded',
        dataType: 'text',
        success: function(result, textStatus) {
            //alert(result);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert('videoTracking Error: ' + errorThrown);
        }
    });
}

function videoAdTracking(videoAdId, updateId, fileId, sectionId, stationId, startTime) {
    $.ajax({
        type: 'GET',
        cache: false,
        url: '/ajax/Videoajax.aspx',
        processData: true,
        data: 'Source=video_ad_tracking' + '&VideoAdId=' + videoAdId + '&UpdateId=' + updateId + '&FileId=' + fileId + '&SectionId=' + sectionId + '&StationId=' + stationId + '&StartTime=' + startTime,
        contentType: 'application/x-www-form-urlencoded',
        dataType: 'text',
        success: function(result, textStatus) {
            //alert(result);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert('videoAdTracking Error: ' + errorThrown);
        }
    });
}

function thisVideo(movieName) {
    if (window[movieName]) {
        return window[movieName]
    } else {
        return document[movieName]
    }
}

function stopVideo(movieName) {
    try {
        thisVideo(movieName).stopAll();
    }
    catch (err) {
        //    
    }    
}

function restartVideo(movieName) {
    try {
        thisVideo(movieName).restartVideo();
    }
    catch (err) {
        //    
    }  
}

function pauseVideo(movieName) {
    try {
        thisVideo(movieName).pauseVideo();
    }
    catch (err) {
        //    
    }  
}

function playVideo(movieName) {
    try {
        thisVideo(movieName).playVideo();
    }
    catch (err) {
        //    
    } 
}

function stoprestartVideo(movieName) {
    stopVideo(movieName);
    restartVideo(movieName);
}

function stopVideoPlayer(videoType, videoThumb, divId) {
    if (videoType == 'F') {
        stopFlashVideo();
    } else if (videoType == 'R') {
        stopRealVideo(videoThumb, divId);
    } else if (videoType == 'W') {
        stopWindowsVideo(videoThumb, divId);    
    }
}

function stopFlashVideo() {
    if (document.getElementById('videoPlayer')) {
        stoprestartVideo('videoPlayer');
    }
}

function stopRealVideo(videoThumb, divId) {
    if (document.videoPlayer) {
        try {
            document.videoPlayer.DoStop();
        }
        catch (err) {
            $('#videoPlayer').replaceWith(videoThumb);
        }
    } else {
        $('#videoPlayer').replaceWith(videoThumb);
    }
}

function stopWindowsVideo(videoThumb, divId) {
    if (document.videoPlayer) {
        try {
            document.videoPlayer.Stop();
        }
        catch (err) {
            $('#videoPlayer').replaceWith(videoThumb);
        }
    } else {
        $('#videoPlayer').replaceWith(videoThumb);
    }
}

function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

