var liveid = getQuerystring("lsid");
var serverid = getQuerystring("svid");
var fileid = getQuerystring("id","error.flv");
var mobileid = getQuerystringMobile("error.flv");
var picLoad = false ;

if(fileid == "error.flv" && liveid == '' && mobileid == "error.flv")
{
	window.location = "/index.php";
}

if(fileid != "error.flv")
{
	fileid = 'http://cdn1.xsplit.com/'+fileid;
	//if(fileid.substring(0,6) == "XSplit")
	//	fileid = 'http://xsplit.s3.amazonaws.com/'+fileid;
	//else
	//	fileid = 'http://xsplitfree.s3.amazonaws.com/'+fileid;
	//fileid = 'http://share.xsplit.com/download.php?id='+fileid;
	//alert(fileid.substring(fileid.length - 4).toLowerCase());
	if(fileid.substring(fileid.length - 4).toLowerCase() == ".jpg" || fileid.substring(fileid.length - 4).toLowerCase() == ".png" || fileid.substring(fileid.length - 4).toLowerCase() == ".gif")
	{
		picLoad = true;
	}
}

if(picLoad)
{
	flowplayer("player",{src:"http://www.xsplit.com/flash/flowplayer.commercial-3.1.5.swf", wmode: 'opaque'},{
		key:'#$d6a4808a39900630dfd',
		version: [9, 115],  
		clip:{
		    url: fileid,
		    autoPlay:false,
		    autoBuffering:true,
			//bufferLength:20,
		    scaling:'orig'/*,
			onStart: function(){
			if ($("#overlay").overlay().isOpened()){
					$("#overlay").overlay().close();
				}
			},
			onFinish: function(){
				$("#overlay").overlay().load();	
			}*/
		  },
		  onError: function(errorCode, errorMessage) { 
        	window.location = "welcome.html";
    	} ,
		onLoad: function(){
			//setTimeout("$('#overlay').overlay().load()", 3000);
			this.getPlugin("play").hide();
		},
		/*
		logo: { 
			url: 'justeye.swf', 
			top: '5%',
			right: '10%',
			width: 50,
			height: 50,
			fullscreenOnly: false, 
			displayTime: 0 
			},
		  */
		
		canvas: {      
			backgroundColor: '#000000'         
			},
		
		plugins:{
		    controls: {
				url: 'http://www.xsplit.com/flash/flowplayer.controls-tube-3.1.5.swf',
				autoHide:'always',
				all: false,
				fullscreen: true
				}
			} 
		}
	);
}
else if(mobileid!="error.flv")
{
	flowplayer("player",{src:"http://www.xsplit.com/flash/flowplayer.commercial-3.1.5.swf", wmode: 'opaque'},{
		key:'#$d6a4808a39900630dfd',
		version: [9, 115],  
		clip:{
		    url: "http://www.xsplit.com/mobile/video.ashx?file=" + mobileid,
		    autoPlay:false,
		    autoBuffering:true,
			//bufferLength:20,
		    scaling:'orig'/*,
			onStart: function(){
			if ($("#overlay").overlay().isOpened()){
					$("#overlay").overlay().close();
				}
			},
			onFinish: function(){
				$("#overlay").overlay().load();	
			}*/
		  },
		  onError: function(errorCode, errorMessage) { 
        	//window.location = "welcome.html";
    	} ,
		onLoad: function(){
		},
		/*
		logo: { 
			url: 'justeye.swf', 
			top: '5%',
			right: '10%',
			width: 50,
			height: 50,
			fullscreenOnly: false, 
			displayTime: 0 
			},
		  */
		
		canvas: {      
			backgroundColor: '#000000'         
			},
		
		plugins:{
		    controls: {
				url: 'http://www.xsplit.com/flash/flowplayer.controls-tube-3.1.5.swf',
				autoHide:'always'
						}
		/*	 ,
		
		    endlogo: {
				url: 'flash/flowplayer.content-3.1.0.swf',
				top: '50%',
				width: 342,
				height: 260,
				borderRadius: 0,
				padding:0,
				backgroundColor: '#ffffff',
				//backgroundImage: 'url(back.png)',			  
				border:"0px solid #cccccc",
				html: '<img src="../flash/endlogo.swf" align="left" />',
				display: 'none',
				onClick: function() {
					$f("player").play();
					this.hide();
				} 
			} */
		}
	});
}
else if (liveid=='') //direct file
{
	flowplayer("player",{src:"http://www.xsplit.com/flash/flowplayer.commercial-3.1.5.swf", wmode: 'opaque'},{
		key:'#$d6a4808a39900630dfd',
		version: [9, 115],  
		clip:{
		    url: fileid,
		    autoPlay:false,
		    autoBuffering:true,
			//bufferLength:20,
		    scaling:'orig'/*,
			onStart: function(){
			if ($("#overlay").overlay().isOpened()){
					$("#overlay").overlay().close();
				}
			},
			onFinish: function(){
				$("#overlay").overlay().load();	
			}*/
		  },
		  onError: function(errorCode, errorMessage) { 
        	//window.location = "welcome.html";
    	} ,
		onLoad: function(){
		},
		/*
		logo: { 
			url: 'justeye.swf', 
			top: '5%',
			right: '10%',
			width: 50,
			height: 50,
			fullscreenOnly: false, 
			displayTime: 0 
			},
		  */
		
		canvas: {      
			backgroundColor: '#000000'         
			},
		
		plugins:{
		    controls: {
				url: 'http://www.xsplit.com/flash/flowplayer.controls-tube-3.1.5.swf',
				autoHide:'always'
						}
		/*	 ,
		
		    endlogo: {
				url: 'flash/flowplayer.content-3.1.0.swf',
				top: '50%',
				width: 342,
				height: 260,
				borderRadius: 0,
				padding:0,
				backgroundColor: '#ffffff',
				//backgroundImage: 'url(back.png)',			  
				border:"0px solid #cccccc",
				html: '<img src="../flash/endlogo.swf" align="left" />',
				display: 'none',
				onClick: function() {
					$f("player").play();
					this.hide();
				} 
			} */
		}
	});
}
else if(serverid == '')//live stream
{
   flowplayer("player",{src:"flash/flowplayer.commercial-3.1.5.swf", wmode: 'opaque'},{
      
	  play: null,
	  
	  key:'#$d6a4808a39900630dfd',
		
      clip:{
        url:liveid,
        autoPlay:true,
		//autoBuffering:true,
		bufferLength:0,
		// small buffer should help us with 264 live delay 
        scaling:'orig',
        live: true,
        provider: 'xsplitstream'
      },
	  
      plugins:{
          xsplitstream:{
            url: 'flash/flowplayer.rtmp-3.1.3.swf',
            netConnectionUrl:'rtmp://184.73.52.151/live'
          },

          controls: {
		  all: false,
		  fullscreen: true,
		  volume: true,
		  mute: true,
          url: 'flash/flowplayer.controls-tube-3.1.5.swf',
          autoHide:'always',
		  hideDelay: 2000
          }

		}
   });
}
else if(serverid == '0')//live stream
{
	var customurl = unescape(getQuerystring("target"));
   flowplayer("player",{src:"flash/flowplayer.commercial-3.1.5.swf", wmode: 'opaque'},{
      
	  play: null,
	  
	  key:'#$d6a4808a39900630dfd',
		
      clip:{
        url:liveid,
        autoPlay:true,
		//autoBuffering:true,
		bufferLength:0,
		// small buffer should help us with 264 live delay 
        scaling:'orig',
        live: true,
        provider: 'xsplitstream'
      },
	  
      plugins:{
          xsplitstream:{
            url: 'flash/flowplayer.rtmp-3.1.3.swf',
            netConnectionUrl:customurl
          },

          controls: {
		  all: false,
		  fullscreen: true,
		  volume: true,
		  mute: true,
          url: 'flash/flowplayer.controls-tube-3.1.5.swf',
          autoHide:'always',
		  hideDelay: 2000
          }

		}
   });
}
else if(serverid == '1')//live stream
{
   flowplayer("player",{src:"flash/flowplayer.commercial-3.1.5.swf", wmode: 'opaque'},{
      
	  play: null,
	  
	  key:'#$d6a4808a39900630dfd',
		
      clip:{
        url:liveid,
        autoPlay:true,
		//autoBuffering:true,
		bufferLength:0,
		// small buffer should help us with 264 live delay 
        scaling:'orig',
        live: true,
        provider: 'xsplitstream'
      },
	  
      plugins:{
          xsplitstream:{
            url: 'flash/flowplayer.rtmp-3.1.3.swf',
            netConnectionUrl:'rtmp://75.101.159.22/liverecord'
          },

          controls: {
		  all: false,
		  fullscreen: true,
		  volume: true,
		  mute: true,
          url: 'flash/flowplayer.controls-tube-3.1.5.swf',
          autoHide:'always',
		  hideDelay: 2000
          }

		}
   });
}
else if(serverid == '2')//live stream
{
   flowplayer("player",{src:"flash/flowplayer.commercial-3.1.2.swf", wmode: 'opaque'},{
      
	  play: null,
	  
	  key:'#$d6a4808a39900630dfd',
		
      clip:{
        url:liveid,
        autoPlay:false,
		autoBuffering:true,
		bufferLength:1,
		// small buffer should help us with 264 live delay 
        scaling:'orig',
        live: true,
        provider: 'xsplitstream'
      },
	  
      plugins:{
          xsplitstream:{
            url: 'flash/flowplayer.rtmp-3.1.1.swf',
            netConnectionUrl:'rtmp://184.73.52.155/oflaDemo'
          },

          controls: {
		  all: false,
		  fullscreen: true,
		  volume: true,
		  mute: true,
          url: 'flash/flowplayer.controls-tube-3.1.2.swf',
          autoHide:'always',
		  hideDelay: 2000
          }

		}
   });
}
else if(serverid == '3')//live stream
{
   flowplayer("player",{src:"flash/flowplayer.commercial-3.1.5.swf", wmode: 'opaque'},{
      
	  play: null,
	  
	  key:'#$d6a4808a39900630dfd',
		
      clip:{
        url:liveid,
        autoPlay:true,
		//autoBuffering:true,
		bufferLength:0,
		// small buffer should help us with 264 live delay 
        scaling:'orig',
        live: true,
        provider: 'xsplitstream'
      },
	  
      plugins:{
          xsplitstream:{
            url: 'flash/flowplayer.rtmp-3.1.3.swf',
            netConnectionUrl:'rtmp://hmelyoff.dyndns.org/live'
          },

          controls: {
		  all: false,
		  fullscreen: true,
		  volume: true,
		  mute: true,
          url: 'flash/flowplayer.controls-tube-3.1.5.swf',
          autoHide:'always',
		  hideDelay: 2000
          }

		}
   });
}
else if(serverid == '4')//live stream
{
   flowplayer("player",{src:"flash/flowplayer.commercial-3.1.5.swf", wmode: 'opaque'},{
      
	  play: null,
	  
	  key:'#$d6a4808a39900630dfd',
		
      clip:{
        url:liveid,
        autoPlay:true,
		//autoBuffering:true,
		bufferLength:0,
		// small buffer should help us with 264 live delay 
        scaling:'orig',
        live: true,
        provider: 'xsplitstream'
      },
	  
      plugins:{
          xsplitstream:{
            url: 'flash/flowplayer.rtmp-3.1.3.swf',
            netConnectionUrl:'rtmp://hmelyoff.dyndns.org/oflaDemo'
          },

          controls: {
		  all: false,
		  fullscreen: true,
		  volume: true,
		  mute: true,
          url: 'flash/flowplayer.controls-tube-3.1.5.swf',
          autoHide:'always',
		  hideDelay: 2000
          }

		}
   });
}
else if(serverid == '5')//live stream
{
   flowplayer("player",{src:"flash/flowplayer.commercial-3.1.2.swf", wmode: 'opaque'},{
      
	  //play: null,
	  
	  key:'#$d6a4808a39900630dfd',
		
      clip:{
        url:liveid,
        autoPlay:false,
		autoBuffering:true,
		bufferLength:0,
		// small buffer should help us with 264 live delay 
        scaling:'orig',
        live: true,
        provider: 'xsplitstream'
      },
	  
      plugins:{
          xsplitstream:{
            url: 'flash/flowplayer.rtmp-3.1.1.swf',
            netConnectionUrl:'rtmp://204.236.225.74/oflaDemo'
          },

          controls: {
		  all: false,
		  fullscreen: true,
		  volume: true,
		  mute: true,
          url: 'flash/flowplayer.controls-tube-3.1.2.swf',
          autoHide:'always',
		  hideDelay: 2000
          }

		}
   });
}
$(function() { 
	var api = $("#overlay").overlay({ finish:  { top: 10 }, api:true}); 
	
	// define function that opens the overlay 
	window.openOverlay = function() {
	    api.load(); 
	} 
});


function getQuerystring(key, default_)
{
	if (default_==null) default_="";
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null){		
		return default_;
		}
	else
	{
		return qs[1];
	}
}

function getQuerystringMobile(default_)
{
	if (default_==null) default_="";
	var regex = new RegExp("[\\#&]([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null || serverid!='')
		return default_;
	else
		return qs[1];
}
$(document).ready(function(){
	$("#popdiv2").hide();
	$("#popdiv3").hide();
	$("#popdiv4").hide();
	$("#popdiv5").hide();
	$("#popdiv6").hide();
	$("#popdiv7").hide();
	
	// get the embedding code 
	var code = $f().embed({ 
    width: 400, 
    height: 300 
	}).getEmbedCode(); 
 
	// place this code in our textarea 
		// document.getElementById("embedtext").innerHTML = code; 
	// another way to add embed code using Jquery instead of using innerHTML
	$("textarea#embedtext").html(code);
	
	//embed testing
	//$("#embedplayer").html($f().getEmbedCode(true));
	
	$("#adownload").click(function (){
		$("#popdiv1, #popdiv2").toggle("400");
		
		return false;
	});
	
	$("#ashow").click(function (){
		$("#popdiv1, #popdiv2").toggle("400");
		
		return false;
	});
	
	$("#sendEmail").submit(function() {
		var str = $("#sendEmail").serialize();
		
		$("span.error").remove();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var email = $("#email").val();
		if(email == '') {
			$("#email").after('<span class="error">You forgot to enter your email address.</span>');
			hasError = true;
		} /*else if(!emailReg.test(email)) {
			$("#email").after('</br><span class="error">Enter a valid email address to send from.</span>');
			hasError = true;
		}*/

		var name = $("#name").val();
		if(name == '') {
			$("#name").after('<span class="error">You forgot to enter your name.</span>');
			hasError = true;
		}

/*
		var messageVal = $("#comment").val();
		if(messageVal == '') {
			$("#comment").after('<span class="error">You forgot to enter your comment.</span>');
			hasError = true;
		}
*/	
		if (!hasError)
		{
			$.post($("#sendEmail").attr("action"), str, function(data){ 
				if (data.search('Success') > 0) {
					$("#popdiv2, #popdiv3").toggle();
				}
				else { 
					$("#popdiv2, #popdiv7").toggle();
				}
				//$("#popdiv2, #popdiv3").toggle();
				/*$("#sendEmail").slideUp("normal", function() { 
					
					if (data.search('Success') > 0) {
						$("#popdiv2, #popdiv3").toggle("400");
					}
					else { 
						$("#popdiv2, #popdiv7").toggle("400");
					}
				});*/
			}, "html");
		}
	return false;
	});
	
	$("#sendEmail2").submit(function() {
		var str = $("#sendEmail2").serialize();
		
		$("span.error").remove();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var email = $("#sendto").val();
		if(email == '') {
			$("#sendto").after('<span class="error">You forgot to enter the email address to send to.</span>');
			hasError = true;
		} /*else if(!emailReg.test(email)) {
			$("#email").after('</br><span class="error">Enter a valid email address to send from.</span>');
			hasError = true;
		}*/

		if (!hasError)
		{
			$.post($("#sendEmail2").attr("action"), str, function(data){ 
				if (data.search('Success') > 0) {
					$("#popdiv4, #popdiv3").toggle();
				}
				else { 
					$("#popdiv4, #popdiv7").toggle();
				}
				/*$("#sendEmail2").slideUp("normal", function() { 
					
					if (data.search('/Success/') > 0) {
						$("#popdiv4, #popdiv3").toggle("400");
					}
					else { 
						$("#popdiv4, #popdiv7").toggle("400");
					}
				});*/
			}, "html");
		}
	return false;
	});
	
	$(function() {
		// email and embed tab actions
		$("#plmenu a").click(function(e) {
			// toggle tab class
			var tab = $(this);
			// show / hide panes
			$(tab.attr("href")).show();
			
			var clickedA = tab.attr("href");
			
			activeEmail = new Image();activeEmail = "url(images/xsplitEmailVidActive.jpg)";
			activeEmbed = new Image();activeEmbed = "url(images/xsplitEmbedVidActive.jpg)";
			activeCreate = new Image();activeCreate = "url(images/xsplitCreateVidActive.jpg)";
			createVid = new Image();createVid = "url(images/xsplitCreateVid.jpg)";
			embedVid = new Image();embedVid = "url(images/xsplitEmbedVid.jpg)";
			emailVid = new Image();emailVid = "url(images/xsplitEmailVid.jpg)";
			
			switch (clickedA)
			{
				case "#popdiv4":
				  $("#popdiv1").hide();
				  $("#popdiv2").hide();
				  $("#popdiv3").hide();
				  $("#popdiv4").show();
				  $("#larrow").css("top", "132px");
				  $("#emailVideo").css("background-image", activeEmail);
				  $("#startCreate").css("background-image", createVid);
				  $("#embedVideo").css("background-image", embedVid);
				  $("#popdiv5").hide();
				  $("#popdiv6").hide();
				  $("#popdiv7").hide();
				  break;
				case "#popdiv5":
				  $("#popdiv1").hide();
				  $("#popdiv2").hide();
				  $("#popdiv3").hide();
				  $("#popdiv4").hide();
				  $("#popdiv5").show();
				  $("#larrow").css("top", "242px");
				  $("#embedVideo").css("background-image", activeEmbed);
				  $("#startCreate").css("background-image", createVid);
				  $("#emailVideo").css("background-image", emailVid);
				  $("#popdiv6").hide();
				  $("#popdiv7").hide();
				  break;
				default:
				  $("#popdiv1").show();
				  $("#larrow").css("top", "22px");
				  $("#startCreate").css("background-image", activeCreate);
				  $("#embedVideo").css("background-image", embedVid);
				  $("#emailVideo").css("background-image", emailVid);
				  $("#popdiv2").hide();
				  $("#popdiv3").hide();
				  $("#popdiv4").hide();
				  $("#popdiv5").hide();
				  $("#popdiv6").hide();
				  $("#popdiv7").hide();
			}
			
			// prevent link's default behaviour
			return e.preventDefault();
		});
	}); 
	
	$("#curlocation").val(window.location);
	$("#curlocation2").val(window.location);
	/*$(function() {
		$f("player", "flash/flowplayer.commercial-3.1.3.swf");
		$("#embedtext").html($f().embed().getEmbedCode());
	}); */
});

