package ads_external

import (
	"strconv"

	"alpha/adDelivery/helper"
	config "alpha/configuration"

	"crypto/md5"
	"encoding/hex"
	"math/rand"
)

func AdRenderOutstreamVideoAd_external(vasturl string, ad_type int) string {

	video_url := config.GetConfig("protocol", "webpath") + config.GetConfig("admin", "webpath") + `/plugins/Outstream/delivery/js/videoplayback.mp4`

	rt := rand.Intn(999999)
	id := "vjs_" + strconv.Itoa(rt)
	rand1 := helper.RandToken(5)
	var s, s1 string
	s = config.GetConfig("protocol", "webpath") + config.GetConfig("admin", "webpath") + `/plugins/Outstream/delivery/fluid/fluidplayer.min.js`
	s1 = config.GetConfig("protocol", "webpath") + config.GetConfig("admin", "webpath") + `/plugins/Outstream/delivery/fluid/jquery.min.js`

	encs := md5.New()
	encs.Write([]byte(s))
	encs1 := md5.New()
	encs1.Write([]byte(s1))

	var html string

	if ad_type == 1 { //In Article

		html = `<link rel="stylesheet" href="` + config.GetConfig("protocol", "webpath") + config.GetConfig("admin", "webpath") + `/plugins/Outstream/delivery/fluid/fluidplayer.min.css" type="text/css">

			<script>
		
				if (typeof fluidplayer_scripts == 'undefined') 
				{
						var fluidplayer_scripts = [];
				}
	
				function loadScript(url,md5, callback)
				{
					if(fluidplayer_scripts.includes(md5) == false)
					{
					
					
				    var script = document.createElement("script");
				    script.type = "text/javascript"; 

				    if (script.readyState){ 
				        script.onreadystatechange = function(){
				            if (script.readyState == "loaded" ||
				                    script.readyState == "complete"){
				                script.onreadystatechange = null;
				                callback();

				            }
				        };
				    } else {  
				        script.onload = function(){
							callback();	
				        };
				    }

				    script.src = url;
				    document.getElementsByTagName("head")[0].appendChild(script);
				    
				    fluidplayer_scripts.push(md5);
					
					}
					else
					{
					setTimeout(function() {
						callback();
						}, 1000); 
					}
				}
				
				loadScript("` + s + `","` + hex.EncodeToString(encs.Sum(nil)) + `", function() {  load_ply_` + id + `()   }  );
				loadScript("` + s1 + `","` + hex.EncodeToString(encs1.Sum(nil)) + `", function() {  load_ply_` + id + `()   }  );
			</script>

			<style>            
	            .video-js .vjs-time-control, .video-js .vjs-fullscreen-control, .video-js .vjs-progress-control
	            {
	                display: none;
	            }
	            
	            .video-js .vjs-time-control, .video-js .vjs-fullscreen-control, .video-js .vjs-progress-control
	            {
	                display: none;
	            }

	            @media only screen and (min-width: 10px) and (max-width: 299px)
	            {
	                #` + id + `
	                {
	                    height: 150px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 300px) and (max-width: 399px)
	            {
	                #` + id + `
	                {
	                    height: 200px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 400px) and (max-width: 650px)
	            {
	                #` + id + `
	                {
	                    height: 300px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 651px) and (max-width: 800px)
	            {
	                #` + id + `
	                {
	                    height: 400px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 801px) and (max-width: 1000px)
	            {
	                #` + id + `
	                {
	                    height: 500px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 1001px) and (max-width: 2000px)
	            {
	                #` + id + `
	                {
	                    height: 600px; 
	                    width: auto;  
	                }
	            }
	            @media only screen and (min-width: 2001px)
	                #` + id + `
	                {
	                    height: 700px; 
	                    width: auto; 
	                }
	            }
	            
            </style>
             
            <div id="test-ad` + rand1 + `">
	   			<style>
					.fluid_context_menu, .fluid_controls_left , .fluid_control_duration , .ad_countdown  ,.fluid_controls_progress_container
					{
					    display: none !important;

					}
		        	#admedia` + rand1 + ` img
		        	{
		            	position: relative !important;
		        	}
		        	
		        	
		        </style>

		        <div id="admedia` + rand1 + `">
					<div class="rm_videowrapper` + id + `" id="rm_videowrapper` + id + `">
				        <center><span style="font-size:1.5vh;">ADVERTISEMENT</span></center>
						<center><video id="` + id + `"  autoplay="false" muted="muted">
		                    <source src="` + video_url + `" type="video/mp4">
		                </video></center>
				        <span></span>
				        
			        </div>
		        </div>
	        </div>	
      		<script> 
        
	    		function tracking_event_` + id + `(track_type) {
	              
	            }


	            var fluidPlayer` + id + `_run = 1;  
		           
	            var icc` + id + ` = 0;
			    function load_ply_` + id + `() {   
					icc` + id + `++;

					if(icc` + id + ` != 2 ) {

					return false;
				}

	            var fluidPlayer` + id + ` = fluidPlayer("` + id + `", {
	                layoutControls: {
	                    primaryColor: false,
	                    playButtonShowing: true,
	                    playPauseAnimation: true,
	                    fillToContainer: false,
	                    autoPlay: false,
	                    preload: false,
	                    mute: true,
	                    doubleclickFullscreen: false,
	                    subtitlesEnabled: false,
	                    keyboardControl: true,
	                    layout: "default",
	                    allowDownload: false,
	                    playbackRateEnabled: false,
	                    allowTheatre: false,
	                    title: false,
	                    controlBar: {
	                        autoHide: false
	                    }
	                },
	                "vastOptions": {
	                    "allowVPAID": "true", 
	                    "adList": [
	                        {
	                            "roll": "preRoll",
	                            "vastTag": "` + vasturl + `",
	                            "adClickable": "true"
	                        }
	                    ],
	                    vastAdvanced: {
	                        vastLoadedCallback:       (function() {}),
	                        noVastVideoCallback:      (function() {}),
	                        vastVideoSkippedCallback: (function() { 
	                            $("#rm_videowrapper` + id + `").slideUp(800);
	                            show_vid = 4; 
	                            tracking_event_` + id + `("skip"); 
	                            fluidPlayer` + id + `_run = 0;
	                        }),
	                        vastVideoEndedCallback:   (function() { 
	                            $("#rm_videowrapper` + id + `").slideUp(800);
	                            show_vid = 4; 
	                            tracking_event_` + id + `("complete"); 
	                            fluidPlayer` + id + `_run = 0;
	                        })
	                    }
	                }
	            });

        	    fluidPlayer` + id + `.on("pause", function(){ tracking_event_` + id + `("pause"); });
	            fluidPlayer` + id + `.on("theatreModeOn", function(){ tracking_event_` + id + `("fullscreen"); });
	            fluidPlayer` + id + `.on("theatreModeOff", function(){ tracking_event_` + id + `("exitfullscreen"); });
           
                setTimeout(function(){ 
                    fluidPlayer` + id + `.pause(); 
                },2000);
                
                var height` + id + ` = $("#rm_videowrapper` + id + `").height();
                var width` + id + ` = $("#rm_videowrapper` + id + `").width();

                var div_position` + id + ` = $("#rm_videowrapper` + id + `").offset();
            	var visible_height` + id + ` = height` + id + `/2;
                
                $("#rm_videowrapper` + id + `").hide();
                var show_vid` + id + ` = 1;

                var visible_height` + id + ` = height` + id + `/2;
                var user_pause` + id + ` = 0;

                $("#` + id + `_fluid_control_playpause").click(function() {
                    (user_pause` + id + ` == 0) ? user_pause` + id + ` = 1 : user_pause` + id + ` = 0;
                });
                var first_occuresce` + id + ` = 0;

                $(window).scroll(function()
                {
                    var visible_height_up` + id + ` = div_position` + id + `.top+(visible_height` + id + `);
                    var screen_height` + id + ` = $(window).height();
                    var visible_height_down` + id + ` = (div_position` + id + `.top+visible_height` + id + `)-screen_height` + id + `;
                    scroll_position` + id + ` = $(window).scrollTop();

                    if(scroll_position` + id + ` >= visible_height_up` + id + ` || scroll_position` + id + ` <= visible_height_down` + id + `)
                    {
                        fluidPlayer` + id + `.pause();
                    }
                    else
                    {
                        if(user_pause` + id + ` == 0)
                        {
                            fluidPlayer` + id + `.play(); 	
                        }
                        
                        if(first_occuresce` + id + ` == 0)
                        {
                            $("#rm_videowrapper` + id + `").slideDown(800);
                            first_occuresce` + id + ` = 1;
                        }
                    }
                });

            	$(window).scroll();
         	}
                
    		</script> 
		</div>`

	} else if ad_type == 3 { // intersitial

		externalAdStyle := `<style>            
	            .video-js .vjs-time-control, .video-js .vjs-fullscreen-control, .video-js .vjs-progress-control
	            {
	                display: none;
	            }
	            
	            .video-js .vjs-time-control, .video-js .vjs-fullscreen-control, .video-js .vjs-progress-control
	            {
	                display: none;
	            }

	            @media only screen and (min-width: 10px) and (max-width: 299px)
	            {
	                #` + id + `
	                {
	                    height: 150px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 300px) and (max-width: 399px)
	            {
	                #` + id + `
	                {
	                    height: 200px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 400px) and (max-width: 650px)
	            {
	                #` + id + `
	                {
	                    height: 300px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 651px) and (max-width: 800px)
	            {
	                #` + id + `
	                {
	                    height: 400px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 801px) and (max-width: 1000px)
	            {
	                #` + id + `
	                {
	                    height: 500px; 
	                    width: auto; 
	                }
	            }
	            @media only screen and (min-width: 1001px) and (max-width: 2000px)
	            {
	                #` + id + `
	                {
	                    height: 600px; 
	                    width: auto;  
	                }
	            }
	            @media only screen and (min-width: 2001px)
	                #` + id + `
	                {
	                    height: 700px; 
	                    width: auto; 
	                }
	            }

	       		</style>`

		html = externalAdStyle + `<link rel="stylesheet" href="` + config.GetConfig("protocol", "webpath") + config.GetConfig("admin", "webpath") + `/plugins/Outstream/delivery/fluid/fluidplayer.min.css" type="text/css">
				<script>
			


					if (typeof fluidplayer_scripts == 'undefined') 
					{
							var fluidplayer_scripts = [];
					}
		
		
					function loadScript(url,md5, callback)
					{
						if(fluidplayer_scripts.includes(md5) == false)
						{
						
						
					    var script = document.createElement("script");
					    script.type = "text/javascript";

					    if (script.readyState){ 
					        script.onreadystatechange = function(){
					            if (script.readyState == "loaded" ||
					                    script.readyState == "complete"){
					                script.onreadystatechange = null;
					                callback();

					            }
					        };
					    } else {  
					        script.onload = function(){
								callback();	
					        };
					    }

					    script.src = url;
					    document.getElementsByTagName("head")[0].appendChild(script);
					    
					    fluidplayer_scripts.push(md5);
						
						}
						else
						{
						setTimeout(function() {
							callback();
							}, 1000); 
						}
					    			
					}
					loadScript("` + s + `","` + hex.EncodeToString(encs.Sum(nil)) + `", function() {  load_ply_` + id + `()   }  );
					loadScript("` + s1 + `","` + hex.EncodeToString(encs1.Sum(nil)) + `", function() {  load_ply_` + id + `()   }  );
				</script>

				<style>            
		            .video-js .vjs-time-control, .video-js .vjs-fullscreen-control, .video-js .vjs-progress-control
					{
						display: none;
					}  

					@media only screen and (min-width: 100px) and (max-width: 790px)
					{
						#rm_videowrapper` + id + `{
							margin-top: 50%;
						}

					}	

					@media only screen and (min-width: 10px) and (max-width: 299px)
					{
						#` + id + `
						{
							position: relative;
							margin-top: 50%;
				   			height: 150px; 
				   			width: auto; 
						};
					}

		            @media only screen and (min-width: 300px) and (max-width: 399px)
		            {
		                #` + id + `
		                {
		                	position: relative;
							margin-top: 50%;
		                    height: 200px; 
		                    width: auto; 
		                }
		            }
		            
		            @media only screen and (min-width: 400px) and (max-width: 650px)
		            {
		                #` + id + `
		                {
		                	position: relative;
							margin-top: 50%;
		                    height: 300px; 
		                    width: auto; 
		                }
		            }
		            
		            @media only screen and (min-width: 651px) and (max-width: 900px)
		            {
		                #` + id + `
		                {
		                    height: 400px; 
		                    width: auto; 
		                }
		            }
		            @media only screen and (min-width: 901px)
		            {
		                #` + id + `
		                {
		                    height: 400px; 
				   			width: auto; 
				   			margin-top: 20px;
		                }
		            }
		            @media only screen and (min-width: 100px) and (max-width: 980px)
		            {
		               	.volume_controls{
							position: relative;
							margin-top: -45px;
							float: left;
							left: 26%;
							z-index: 1;
							
						}
		            }
		            @media only screen and(min-width: 981px) and (max-width: 1080px)
		            {
		                .volume_controls{
							position: relative;
							margin-top: -45px;
							float: left;
							left: 28%;
							z-index: 1;
							
						}
		            }

		            @media only screen and (min-width: 1081px) and (max-width: 1420px)
					{
						.volume_controls{
							position: relative;
							margin-top: -45px;
							float: left;
							left: 32%;
							z-index: 1;
						}
					}

					@media only screen and (min-width: 1421px) and (max-width: 1560px)
					{
						.volume_controls{
							position: relative;
							margin-top: -45px;
							float: left;
							left: 35%;
							z-index: 1;
						}
					}

					@media only screen and (min-width: 1561px) and (max-width: 1825px)
					{
						.volume_controls{
							position: relative;
							margin-top: -45px;
							float: left;
							left: 37%;
							z-index: 1;
						}
					}

					@media only screen and (min-width: 1826px) 
					{
						.volume_controls{
							position: relative;
							margin-top: -45px;
							float: left;
							left: 42%;
							z-index: 1;
						}
					}
		
					.fluid_context_menu, .fluid_controls_left , .fluid_control_duration , .ad_countdown  ,.fluid_controls_progress_container
					{
					    display: none !important;

					}
    	
    				#rm_videowrapper` + id + `
					{
						margin-left: auto;
						margin-right: auto;
						width:  90%;
						height: 90%;
					}
					#intersitial_outstream` + id + `{
						position: fixed;
						z-index: 9999;
						background: rgba(0, 0, 0, 0.75);
						top: 0;
						    left: 0;

						width: 100%;
						height: 100%;
					}
					.text-white{
						color: #fff;
					}
					#interstitial-footer` + id + `{
						text-align : right;
						margin-left: 550px;
						padding-top: 10px;
					}
					
					
		        </style>

		        <div id="intersitial_outstream` + id + `">
					<div id="rm_videowrapper` + id + `">
						<center>
							<video id="` + id + `" autoplay="false" muted="muted" style="position: relative;"><source src="` + video_url + `" type="video/mp4"></video>
							<div class="interstitial-footer" id="interstitial-footer" >
						    	<button type="button" class="btn btn-primary" id="close_interstitial_ad` + id + `" data-dismiss="modal">Close</button>
						    </div>
						</center>
					</div>
				</div>

				<script> 
	    	var fluidPlayer` + id + `_run = 1;
	    	
	    	   function tracking_event_` + id + `(track_type) {
	        	
	        }
	        
	        
    	  	var icc` + id + ` = 0;
     		function load_ply_` + id + `() {   
				icc` + id + `++;
				if(icc` + id + ` != 2 ) {
					return false;
				}	  	
		  	

	    	var fluidPlayer` + id + ` = fluidPlayer("` + id + `", {
	    		layoutControls: {
		            primaryColor:           false,
		            playButtonShowing:      true,
		            playPauseAnimation:     true,
		            fillToContainer:        false,
		            autoPlay:               false,
		            preload:                false,
		            mute:                   true,
		            doubleclickFullscreen:  true,
		            subtitlesEnabled:       false,
		            keyboardControl:        true,
		            layout:                 "default",
		            allowDownload:          false,
		            playbackRateEnabled:    false,
		            allowTheatre:           true,
		            title:                  false,
		            controlBar: {
		                autoHide:           false
		            }
		        },
	            "vastOptions": {
	            	maxAllowedVastTagRedirects: 1,
	                "allowVPAID": "true",
	                "adList": [
	                    {
	                        "roll": "preRoll",
	                        "vastTag": "` + vasturl + `",
	                        adClickable: true
	                    }
	                ],
	                vastAdvanced: {
	                	vastLoadedCallback:       (function() {}),
	                	noVastVideoCallback:      (function() {}),
		                vastVideoSkippedCallback: (function() { 
		                	$("#intersitial_outstream").slideUp(800);
		                	show_vid = 4;
		                	tracking_event_` + id + `("skip"); 
		                	fluidPlayer` + id + `_run = 0; 
		                }),
		                vastVideoEndedCallback:   (function() { 
		                	$("#intersitial_outstream").slideUp(800);
		                	show_vid = 4; 
		                	tracking_event_` + id + `("complete"); 
		                	fluidPlayer` + id + `_run = 0;
		                })
		            }
	            }
	        });

	     

	        fluidPlayer` + id + `.on("play", function(){ tracking_event_` + id + `("resume"); });
	        fluidPlayer` + id + `.on("pause", function(){ tracking_event_` + id + `("pause"); });
	        fluidPlayer` + id + `.on("theatreModeOn", function(){ tracking_event_` + id + `("fullscreen"); });
	        fluidPlayer` + id + `.on("theatreModeOff", function(){ tracking_event_` + id + `("exitfullscreen"); });

			
			
		
			fluidPlayer` + id + `.play();


			fluidPlayer` + id + `.on("ended", function()
			{	  
    			close_vid` + id + `();				
			});
	     

	        $("#close_interstitial_ad` + id + `").click(function(){
	        	close_vid` + id + `();
	        });

			}

	        function close_vid` + id + `()
	        {
	        	$("#intersitial_outstream` + id + `").slideUp(1000);
	        }     
	    </script>`

	} else if ad_type == 4 { //Infeed

		html = `<link rel="stylesheet" href="` + config.GetConfig("protocol", "webpath") + config.GetConfig("admin", "webpath") + `/plugins/Outstream/delivery/fluid/fluidplayer.min.css" type="text/css"/>
		<script>
		
			if (typeof fluidplayer_scripts == 'undefined')  {
					var fluidplayer_scripts = [];
			}

			function loadScript(url,md5, callback)
			{
				if(fluidplayer_scripts.includes(md5) == false)
				{
			    var script = document.createElement("script");
			    script.type = "text/javascript";

			    if (script.readyState){ 
			        script.onreadystatechange = function(){
			            if (script.readyState == "loaded" ||
			                    script.readyState == "complete"){
			                script.onreadystatechange = null;
			                callback();

			            }
			        };
			    } else {  
			        script.onload = function(){
						callback();	
			        };
			    }

			    script.src = url;
			    document.getElementsByTagName("head")[0].appendChild(script);
			    
			    fluidplayer_scripts.push(md5);
				
				}
				else
				{
				setTimeout(function() {
					callback();
					}, 1000); 
				}
			}
			loadScript("` + s + `","` + hex.EncodeToString(encs.Sum(nil)) + `", function() {  load_ply_` + id + `()   }  );
			loadScript("` + s1 + `","` + hex.EncodeToString(encs1.Sum(nil)) + `", function() {  load_ply_` + id + `()   }  );
		</script>
		
		<style>
			.video-js .vjs-time-control, .video-js .vjs-fullscreen-control, .video-js .vjs-progress-control
			{
				display: none;
			}
			@media only screen and (min-width: 10px) and (max-width: 299px)
			{
				#` + id + `
				{
		   			height: 150px; 
		   			width: auto; 
				}
			}
			@media only screen and (min-width: 300px) and (max-width: 399px)
			{
				#` + id + `
				{
		   			height: 200px; 
		   			width: auto; 
				}
			}
			@media only screen and (min-width: 400px) and (max-width: 650px)
			{
				#` + id + `
				{
		   			height: 300px; 
		   			width: auto; 
				}
			}
			@media only screen and (min-width: 651px) and (max-width: 800px)
			{
				#` + id + `
				{
		   			height: 400px; 
		   			width: auto; 
				}
			}
			@media only screen and (min-width: 801px) and (max-width: 1000px)
			{
				#` + id + `
				{
		   			height: 500px; 
		   			width: auto; 
				}
			}
			@media only screen and (min-width: 1001px) and (max-width: 2000px)
			{
				#` + id + `
				{
		   			height: 600px; 
		   			width: auto; 
				}
			}
			@media only screen and (min-width: 2001px)
				#` + id + `
				{
		   			height: 700px; 
		   			width: auto; 
				}
			}
		</style>
		<div id="test-ad` + rand1 + `">
		   	<style>
          		.fluid_context_menu, .fluid_controls_left , .fluid_control_duration , .ad_countdown  ,.fluid_controls_progress_container
				{
				    display: none !important;

				} 
            	.admedia` + rand1 + ` img
            	{
                	position: relative !important;
            	}           	
            	
            </style>
	        <div class="admedia` + rand1 + `" >
				<center>
					<div id="outstream` + id + `" style="background-color: white;" >
						<video id="` + id + `" autoplay="false" muted="muted" style="position: relative;">	 
							<source src="` + video_url + `" type="video/mp4">	
						</video>
					</div>
				</center>
			</div>
		</div>

    	<script> 
		   var fluidPlayer` + id + `_run = 1;
         
        
        	function tracking_event_` + id + `(track_type) {
    			
        	}

     		var fluidPlayer` + id + `_run = 1;   
            
    		var icc` + id + ` = 0;
 			function load_ply_` + id + `() {   
				icc` + id + `++;
				if(icc` + id + ` != 2 ) {
					return false;
				}  	
	    		var fluidPlayer` + id + ` = fluidPlayer("` + id + `", {
    				layoutControls: {
			            primaryColor:           false,
			            playButtonShowing:      true,
			            playPauseAnimation:     true,
			            fillToContainer:        false,
			            autoPlay:               false,
			            preload:                false,
			            mute:                   true,
			            doubleclickFullscreen:  true,
			            subtitlesEnabled:       false,
			            keyboardControl:        true,
			            layout:                 "default",
			            allowDownload:          false,
			            playbackRateEnabled:    false,
			            allowTheatre:           true,
			            title:                  false,
			            controlBar: {
			                autoHide:           false
			            }
			        },
		            "vastOptions": {
		            	maxAllowedVastTagRedirects: 1,
		                "allowVPAID": "true", 
		                "adList": [
		                    {
		                        "roll": "preRoll",
		                        "vastTag": "` + vasturl + `",
		                        adClickable: true
		                    }
		                ],
                		vastAdvanced: {
                			vastLoadedCallback: (function() {}),
                			noVastVideoCallback: (function() {}),
	                		vastVideoSkippedCallback: (function() { 
                				$("#outstream` + id + `").slideUp(800);
	                			show_vid = 4;
	                			tracking_event_` + id + `("skip"); 
	                			fluidPlayer` + id + `_run = 0; 
                			}),
	                		vastVideoEndedCallback: (function() { 
	                			$("#outstream` + id + `").slideUp(800);
	                			show_vid = 4;
	                			tracking_event_` + id + `("complete"); 
	                			fluidPlayer` + id + `_run = 0; 
                			})
	            		}
            		}
        		});

		        fluidPlayer` + id + `.on("play", function(){ tracking_event_` + id + `("resume"); });
		        fluidPlayer` + id + `.on("pause", function(){ tracking_event_` + id + `("pause"); });
		        fluidPlayer` + id + `.on("theatreModeOn", function(){ tracking_event_` + id + `("fullscreen"); });
		        fluidPlayer` + id + `.on("theatreModeOff", function(){ tracking_event_` + id + `("exitfullscreen"); });

				setTimeout(function(){ 
					fluidPlayer` + id + `.pause(); 
				},2000);

				var height` + id + ` = $("#outstream` + id + `").height();
				var width` + id + `  = $("#outstream` + id + `").width();

				var div_position` + id + ` = $("#outstream` + id + `").offset();
				var visible_height` + id + ` = height` + id + `/2;

				$("#outstream` + id + `").css("opacity","0");
				$("#outstream` + id + `").hide(-9000);
				
				var show_vid` + id + ` = 1;

				var user_pause` + id + ` = 0;

				$("#` + id + `_fluid_control_playpause").click(function()  {
					(user_pause` + id + ` == 0) ? user_paus` + id + ` = 1 : user_pause` + id + ` = 0;
				});
			
				$(window).scroll(function()
				{ 					
					var visible_height_up` + id + ` = div_position` + id + `.top+(visible_height` + id + `);
					var screen_height` + id + ` = $(window).height();
					var visible_height_down` + id + ` = (div_position` + id + `.top+visible_height` + id + `)-screen_height` + id + `;

					scroll_position` + id + ` = $(window).scrollTop();

					if(scroll_position` + id + ` >= visible_height_up` + id + ` || scroll_position` + id + ` <= visible_height_down` + id + `)
					{
						if( show_vid` + id + ` != 1)
						{
							fluidPlayer` + id + `.pause();
						}
					}
					else
					{

						if( show_vid` + id + ` == 1 && user_pause` + id + ` == 0)
						{
							$("#outstream` + id + `").css("opacity","1");
							$("#outstream` + id + `").slideDown(800);
							show_vid` + id + ` = 2 ;
							fluidPlayer` + id + `.play();
						}
						else if(show_vid` + id + ` == 2 && user_pause` + id + ` == 0)
						{
							fluidPlayer` + id + `.play();
						}
					}
				});

				$(window).scroll();

				$("#outstream` + id + `").mouseenter(function()
				{
					tracking_event_` + id + `("unmute");
				    fluidPlayer` + id + `.setVolume(0.5); 
				});

				$("#outstream` + id + `").mouseout(function()
				{
					tracking_event_` + id + `("mute");
				    fluidPlayer` + id + `.setVolume(0); 
				});
			}
		</script>`
	}

	return html
}
