jQuery(document).ready(function(){
    });
function uploadvideo(yt_url){
if(yt_url.toLowerCase().indexOf("youtube")>0){
							if(yt_url !=""){							
								var video_data = new BT.Base64().base64Encode(yt_url);
								if(video_data !=" "){
								jQuery.ajax({
									url: location.href,
									data: "action=get_video&from=youtube&method=playlist_video&data="+video_data,
									type: "post",
									beforeSend: function(){
										jQuery("#btss-loading").show();
									},
									success: function(response){						
										var data = jQuery.parseJSON(response);					
										if(data.success ==true){										
											jQuery("#btss-loading").hide();							 					 
											 if(data.data != ""){
												jQuery.ajax({
													url: location.href,
													data: "action=get_video&from=youtube&method=getvideos&data="+data.data,
													type: "post",
													beforeSend: function(){
													jQuery("#btss-loading").show();
													},
													success: function(response){
													var data = jQuery.parseJSON(response);
													if(data.success ==true){
													if(typeof(data.data)=="object" &&(data.data instanceof Array)){
														 Getcountvideo("youtube", data.data,0,false,false);
														
													}else{
													jQuery.ajax({
														url: location.href,
														data: "action=get_video&from=youtube&method=getvideo&videoid=" + data.data,
														type: "post",																						
														success: function(response){
															jQuery("#btss-loading").hide();
															var datavideo = jQuery.parseJSON(response);															
															if(datavideo.success == true){
																jQuery('#youtube_url').val('');
																jQuery("#sortable").append(datavideo.data);	
																jQuery('#sortable li:last-child ').find('a.edit').data('youid', datavideo.url);
																jQuery('#sortable li:last-child ').find('a.edit').data('youembed', datavideo.embed);
																jQuery('#sortable li:last-child ').find('a.edit').data('youdesc', datavideo.description);															
																jQuery('#sortable li:last-child ').find('a.edit').data('title', datavideo.title);
																reNewItem();
																jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
																 ClickEnable();
															}else{
																//console.log(datavideo.message);
																ClickEnable();
																jQuery("#btss-loading").hide();
																jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
															}									
														}
													});
													}
													}else{
														ClickEnable();
														jQuery("#btss-loading").hide();
														jQuery("#btss-message").html(data.message).fadeIn().fadeOut(1000);
													}
													
													}
												});
											 
											 }
										
										}else{											
											//console.log(data.message);
											ClickEnable();
											jQuery("#btss-loading").hide();
											jQuery("#btss-message").html(data.message).fadeIn().fadeOut(1000);
										}
									}
								});
								}
							
							}
							}
							else{
								if(yt_url.toLowerCase().indexOf("vimeo")>0){
									if(yt_url !=""){							
									var video_data = new BT.Base64().base64Encode(yt_url);
									if(video_data !=" "){
									jQuery.ajax({
										url: location.href,
										data: "action=get_video&from=vimeo&method=playlist_video&data="+video_data,
										type: "post",
										beforeSend: function(){
											jQuery("#btss-loading").show();
										},
										success: function(response){
											 var data = jQuery.parseJSON(response);
											 if(data.success ==true){	
											 jQuery("#btss-loading").hide();
												jQuery.ajax({
												 url: location.href,
												data: "action=get_video&from=vimeo&method=getvideos&data="+data.data,
												type: "post",
												beforeSend: function(){
													jQuery("#btss-loading").show();
												},											
												success: function(response){												
												var data = jQuery.parseJSON(response);
												 if(data.success ==true){	
													if(typeof(data.data)=="object" &&(data.data instanceof Array)){
														 Getcountvideo("vimeo", data.data,0,false,false);
														
												}else{
												jQuery("#btss-loading").hide();
												  jQuery.ajax({
													url: location.href,
													data: "action=get_video&from=vimeo&method=getvideo&videoid="+data.data,
													type: "post",
													beforeSend: function(){
														jQuery("#btss-loading").show();
													},
													success: function(response){
														jQuery("#btss-loading").hide();
														var datavideo = jQuery.parseJSON(response);															
														if(datavideo.success == true){
															jQuery('#youtube_url').val('');
															jQuery("#sortable").append(datavideo.data);	
															jQuery('#sortable li:last-child ').find('a.edit').data('youid', datavideo.url);
															jQuery('#sortable li:last-child ').find('a.edit').data('youembed', datavideo.embed);
															jQuery('#sortable li:last-child ').find('a.edit').data('youdesc', datavideo.description);														
															jQuery('#sortable li:last-child ').find('a.edit').data('title', datavideo.title);
															reNewItem();
															jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
															ClickEnable();
														}else{
															ClickEnable();
															jQuery("#btss-loading").hide();
															//console.log(datavideo.message);
															jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
															}									
														}
													});
												}
												}
												else{
													ClickEnable();
													jQuery("#btss-loading").hide();
													jQuery("#btss-message").html(data.message).fadeIn().fadeOut(1000);
												}
											}
											});
											}else{
												ClickEnable();
												jQuery("#btss-loading").hide();
												jQuery("#btss-message").html(data.message).fadeIn().fadeOut(1000);
											}
										}
									});
								  }
								}
							
						 }
						 else{
						  if(yt_url.toLowerCase().indexOf("dailymotion")>0){
							if(yt_url !=""){	
								var video_data = new BT.Base64().base64Encode(yt_url);
									if(video_data !=" "){
									jQuery.ajax({
										url: location.href,
										data: "action=get_video&from=dailymotion&method=playlist_video&data="+video_data,
										type: "post",
										beforeSend: function(){
											jQuery("#btss-loading").show();
										},
										success: function(response){
										var data = jQuery.parseJSON(response);	
										if(data.success ==true){	
										jQuery.ajax({
										url: location.href,
										data: "action=get_video&from=dailymotion&method=getvideos&data="+data.data,
										type: "post",
										beforeSend: function(){
											jQuery("#btss-loading").show();
										},
										success: function(response){
										var dailymotionvideo = jQuery.parseJSON(response);
										if(dailymotionvideo.success ==true){	
										if(typeof(dailymotionvideo.data)=="object" &&(dailymotionvideo.data instanceof Array)){
											 Getcountvideo("dailymotion", dailymotionvideo.data,0,false,false);									
														
										}else{																	
											   jQuery("#btss-loading").hide();
												  jQuery.ajax({
													url: location.href,
													data: "action=get_video&from=dailymotion&method=getvideo&videoid="+dailymotionvideo.data,
													type: "post",
													beforeSend: function(){
														jQuery("#btss-loading").show();
													},
													success: function(response){
														jQuery("#btss-loading").hide();
														var datavideo = jQuery.parseJSON(response);															
														if(datavideo.success == true){
															jQuery('#youtube_url').val('');
															jQuery("#sortable").append(datavideo.data);	
															jQuery('#sortable li:last-child ').find('a.edit').data('youid', datavideo.url);
															jQuery('#sortable li:last-child ').find('a.edit').data('youembed', datavideo.embed);
															jQuery('#sortable li:last-child ').find('a.edit').data('youdesc', datavideo.description);														
															jQuery('#sortable li:last-child ').find('a.edit').data('title', datavideo.title);
															reNewItem();
															jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
															ClickEnable();
														}else{
															ClickEnable();
															jQuery("#btss-loading").hide();															
															jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
															}
													}
												});
											}
											}else{
												ClickEnable();
												jQuery("#btss-loading").hide();
												jQuery("#btss-message").html(dailymotionvideo.message).fadeIn().fadeOut(1000);
											}
											}
										});
										}else{
										ClickEnable();
										jQuery("#btss-loading").hide();
										jQuery("#btss-message").html(data.message).fadeIn().fadeOut(1000);
									
										}
									}
										
									});
								}
							}
										
						  }
						  else{
							jQuery("#btss-message").html('Url invalid!').fadeIn().fadeOut(1000);
							 jQuery("#btnGetVideosFromYoutube .custom-button").html('Get Video');
							 ClickEnable();
							}
						 }
					 }


}
function Getcountvideo(source, arraydata,from,complete,state) {
var xhr;
 if (source === "youtube") {
 var videof = from;
 var value="";
	if (complete ==true) {	
		  jQuery("#btss-message").html("Complete!").fadeOut(200);
		  jQuery("#btnGetVideosFromYoutube .custom-button").html('Get Video');
		  jQuery("#btss-loading").hide();		  
		  jQuery('#youtube_url').val('');
		  ClickEnable();
		  return false;
		 
	}else{
		 
		if(state ==false){
		jQuery("#btnGetVideosFromYoutube .custom-button").addClass('stop');		
		jQuery("#btnGetVideosFromYoutube .custom-button").html('Stop');
		jQuery("#btnGetVideosFromYoutube .stop").click(function(){		
				state = true;
				if(xhr!=null) xhr.abort();
				jQuery("#btss-loading").hide();	
				jQuery("#btnGetVideosFromYoutube .custom-button").unbind('click');
				jQuery("#btnGetVideosFromYoutube .custom-button").removeClass('stop');
				jQuery("#btnGetVideosFromYoutube .custom-button").addClass('play');
				jQuery("#btnGetVideosFromYoutube .custom-button").html('Resume');
				jQuery("#btnGetVideosFromYoutube .play").click(function(){				
					state = false;	
					jQuery("#btnGetVideosFromYoutube .custom-button").unbind('click');
					jQuery("#btnGetVideosFromYoutube .custom-button").removeClass('play');
					Getcountvideo(source, arraydata,videof,false,state);	
				});
		});
		
		xhr = jQuery.ajax({
			url: location.href,
			data: "action=get_video&from=youtube&method=getvideo&videoid=" + arraydata[videof],
			type: "post",
			beforeSend: function(){
			jQuery("#btss-loading").show();
			},
			success: function(response){
				jQuery("#btss-loading").hide();
				var datavideo = jQuery.parseJSON(response);															
				if(datavideo.success == true){					
					jQuery("#sortable").append(datavideo.data);	
					jQuery('#sortable li:last-child ').find('a.edit').data('youid', datavideo.url);
					jQuery('#sortable li:last-child ').find('a.edit').data('youembed', datavideo.embed);
					jQuery('#sortable li:last-child ').find('a.edit').data('youdesc', datavideo.description);															
					jQuery('#sortable li:last-child ').find('a.edit').data('title', datavideo.title);
					reNewItem();
					jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
				}else{
					//console.log(datavideo.message);
				ClickEnable();
				jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
				}
				videof ++;								
				value = arraydata.length;				
                    if (videof <= (value-1)) {
                        Getcountvideo(source, arraydata,videof,false,state);
                    } else {
                        Getcountvideo(source, arraydata,videof,true,state);						
                    }
			}
		});
		}
	}
 }
 if(source =="vimeo"){
	var videof = from;
	var value="";
	if (complete ==true) {	
		  jQuery("#btss-message").html("Complete!").fadeOut(200);
		  jQuery("#btnGetVideosFromYoutube .custom-button").html('Get Video');
		  jQuery("#btss-loading").hide();		  
		  jQuery('#youtube_url').val('');
		  ClickEnable();
		  return false;
		 
	}else{
		if(state ==false){
		jQuery("#btnGetVideosFromYoutube .custom-button").addClass('stop');		
		jQuery("#btnGetVideosFromYoutube .custom-button").html('Stop');
		jQuery("#btnGetVideosFromYoutube .stop").click(function(){		
				state = true;
				if(xhr!=null) xhr.abort();
				 jQuery("#btss-loading").hide();
				jQuery("#btnGetVideosFromYoutube .custom-button").unbind('click');
				jQuery("#btnGetVideosFromYoutube .custom-button").removeClass('stop');
				jQuery("#btnGetVideosFromYoutube .custom-button").addClass('play');
				jQuery("#btnGetVideosFromYoutube .custom-button").html('Resume');
				jQuery("#btnGetVideosFromYoutube .play").click(function(){				
					state = false;	
					jQuery("#btnGetVideosFromYoutube .custom-button").unbind('click');
					jQuery("#btnGetVideosFromYoutube .custom-button").removeClass('play');
					Getcountvideo(source, arraydata,videof,false,state);	
				});
		});
		xhr = jQuery.ajax({
			url: location.href,
			data: "action=get_video&from=vimeo&method=getvideo&videoid=" + arraydata[videof],
			type: "post",
			beforeSend: function(){
			jQuery("#btss-loading").show();
			},
			success: function(response){
				jQuery("#btss-loading").hide();
				var datavideo = jQuery.parseJSON(response);															
				if(datavideo.success == true){					
					jQuery("#sortable").append(datavideo.data);	
					jQuery('#sortable li:last-child ').find('a.edit').data('youid', datavideo.url);
					jQuery('#sortable li:last-child ').find('a.edit').data('youembed', datavideo.embed);
					jQuery('#sortable li:last-child ').find('a.edit').data('youdesc', datavideo.description);															
					jQuery('#sortable li:last-child ').find('a.edit').data('title', datavideo.title);
					reNewItem();
					jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
				}else{
					//console.log(datavideo.message);
				ClickEnable();
				jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
				}
				videof ++;					
				value = arraydata.length;
					
                    if (videof <= (value-1)) {
                        Getcountvideo(source, arraydata,videof,false,state);
                    } else {
                        Getcountvideo(source, arraydata,videof,true,state);						
                    }
			}
		});
	}
	}
 
 }
  if (source === "dailymotion") {
 var videof = from;
 var value="";
	if (complete ==true) {	
		  jQuery("#btss-message").html("Complete!").fadeOut(200);
		  jQuery("#btnGetVideosFromYoutube .custom-button").html('Get Video');
		  jQuery("#btss-loading").hide();		  
		  jQuery('#youtube_url').val('');
		 ClickEnable();
		  return false;
		 
	}else{
		if(state ==false){
		jQuery("#btnGetVideosFromYoutube .custom-button").addClass('stop');		
		jQuery("#btnGetVideosFromYoutube .custom-button").html('Stop');
		jQuery("#btnGetVideosFromYoutube .stop").click(function(){		
				state = true;
				if(xhr!=null) xhr.abort();
				jQuery("#btss-loading").hide();
				jQuery("#btnGetVideosFromYoutube .custom-button").unbind('click');
				jQuery("#btnGetVideosFromYoutube .custom-button").removeClass('stop');
				jQuery("#btnGetVideosFromYoutube .custom-button").addClass('play');
				jQuery("#btnGetVideosFromYoutube .custom-button").html('Resume');
				jQuery("#btnGetVideosFromYoutube .play").click(function(){				
					state = false;	
					jQuery("#btnGetVideosFromYoutube .custom-button").unbind('click');
					jQuery("#btnGetVideosFromYoutube .custom-button").removeClass('play');
					Getcountvideo(source, arraydata,videof,false,state);	
				});
		});
		xhr = jQuery.ajax({
			url: location.href,
			data: "action=get_video&from=dailymotion&method=getvideo&videoid=" + arraydata[videof],
			type: "post",
			beforeSend: function(){
			jQuery("#btss-loading").show();
			},
			success: function(response){
				jQuery("#btss-loading").hide();
				var datavideo = jQuery.parseJSON(response);															
				if(datavideo.success == true){					
					jQuery("#sortable").append(datavideo.data);	
					jQuery('#sortable li:last-child ').find('a.edit').data('youid', datavideo.url);
					jQuery('#sortable li:last-child ').find('a.edit').data('youembed', datavideo.embed);
					jQuery('#sortable li:last-child ').find('a.edit').data('youdesc', datavideo.description);															
					jQuery('#sortable li:last-child ').find('a.edit').data('title', datavideo.title);
					reNewItem();
					jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
				}else{
				ClickEnable();
					//console.log(datavideo.message);
				jQuery("#btss-message").html(datavideo.message).fadeIn().fadeOut(1000);
				}
				videof ++;					
				value = arraydata.length;
					
                    if (videof <= (value-1)) {
                        Getcountvideo(source, arraydata,videof,false,state);
                    } else {
                        Getcountvideo(source, arraydata,videof,true,state);						
                    }
			}
		});
	}
	}
 }
}
function ClickEnable(){
 jQuery("#btnGetVideosFromYoutube .custom-button").bind('click',function(){
	jQuery("#btnGetVideosFromYoutube .custom-button").html('Get Video');
	jQuery("#btnGetVideosFromYoutube .custom-button").unbind('click');
	var yt_url = jQuery("#youtube_url").val();
	uploadvideo(yt_url);																	
 });
}