$('document').ready(function() {



    if (window.location.toString().indexOf('videosrc=') > -1) {
        var whichVideo = window.location.toString().split('videosrc=')[1].split('&')[0];
        //alert(browserLocation.indexOf('videosrc='))
		embedVideo(whichVideo);
        displayVideo();
    }
	
    //set up event handling
    $('#hero_cover, #video_close_btn').live('click', function() {
        closeVideo();
        return false;
    });

    $('.hero_lightbox_trigger, .green_lightbox_trigger').click(function() {
        whichVideo = $(this).attr('rel');
	embedVideo(whichVideo);
        displayVideo();
        return false;
    });

	brightcoveLightbox();
	
	if(window.location.toString().indexOf('vao=1') >= 0){
		autoPlay();
	}

});

	var player;//a reference to the experience module
    var videoPlayer;  //a reference to the videoPlayer module




 

// for IE, strip content when lightbox closes so video doesn't play while hidden
function closeVideo() {

whichVideo = $('.brightcoveLightbox').attr('rel');
experienceId = 'myExperience' + whichVideo;

if ( $.browser.msie ) {
	player = brightcove.getExperience(experienceId);
	videoPlayer = player.getModule(APIModules.VIDEO_PLAYER);
	videoPlayer.pause();
 } 
		
    $('#hero_wrapper').html('<div id="hero_lightbox"><a href="#" id="video_close_btn">close</a></div>');
    $('#hero_cover, #hero_wrapper').fadeOut('fast');
}

function embedVideo(whichVideo){
	var params = {};
    params.wmode = "transparent";
    params.allowScriptAccess = "sameDomain";
    params.allowFullScreen = "true";
    params.movie = "YouTubePlayer.swf";
    params.quality = "high";
    params.flashvars = whichVideo;

    var flashvars = {};
    flashvars.videoDescriptor = "/resources/youtube/" + whichVideo;

	swfobject.embedSWF("/resources/youtube/YouTubePlayer.swf", "hero_lightbox", "652", "370", "9.0.0", "", flashvars, params);
}

//this function creates the overlay and lightbox and fades it in
function displayVideo() {
    $("#hero_cover").css({
        opacity: 0
    });
    $("#hero_cover").animate({
        width: "100%",
        height: "100%",
        opacity: 0.8
    },"5000");
    $("#hero_wrapper").show();
    //$("#hero_wrapper").css({opacity:1});
}

//this function will launch a brightcove lightbox when anything with class brightcoveLightbox is clicked
function brightcoveLightbox(){
	$('.brightcoveLightbox').bind('click', function(){
		var params = $(this).attr('rel').split('&');
		var videoID = params[0];
		var width = (params[1]) ? params[1] : 652;
		var height = (params[2]) ? params[2] : 368;
		
		var embedCode = '<object id="myExperience' + videoID + '" class="BrightcoveExperience">';
		embedCode +=    '<param name="bgcolor" value="#FFFFFF" />';
		embedCode +=    '<param name="width" value="' + width + '" />';
		embedCode +=    '<param name="height" value="' + height + '" />';
		embedCode +=    '<param name="playerID" value="791151469001" />';
		embedCode +=    '<param name="playerKey" value="AQ~~,AAAAqwZdtKE~,HIulZ67sN0iLH9xgsDFIQQ1l4AWKDUp4" />';
		embedCode +=    '<param name="isVid" value="true" />';
		embedCode +=    '<param name="isUI" value="true" />';
		embedCode +=    '<param name="dynamicStreaming" value="true" />';
		embedCode +=    '<param name="wmode" value="transparent" />';
		embedCode +=    '<param name="videoSmoothing" value="true" />';
		embedCode +=    '<param name="@videoPlayer" value="' + videoID + '" />';
		embedCode +=    '</object>';
		
		$('#hero_lightbox').append(embedCode);
		brightcove.createExperiences(); //necessary for video to display
		displayVideo();
		return false;
	});
}

function autoPlay(){
	$('.brightcoveLightbox').eq(0).trigger('click');
	
	whichVideo = $('.brightcoveLightbox').attr('rel');
	experienceId = 'myExperience' + whichVideo;
	
	//because it takes a second to load the player and the 'experience'
	//we set a 3 second timeout and then auto play
	
	//setTimeout("player = brightcove.getExperience(experienceId); videoPlayer = player.getModule(APIModules.VIDEO_PLAYER); videoPlayer.play();", 3000);
	
}
