/*
 * jQuery Ajax call to become a fan
 */
function becomeFan(nUserId, nDestUserId)
{	
	// are we logged in?
	if (nUserId.length > 0)
	{
    	// check if already a fan?
        $.ajax({
            url: '/ajax/fanrequest/' + nDestUserId,
            type: 'GET',
            success: function(sResponse)
            {
        		if (sResponse == 'success') {
    				// set dialog content
    				$("#dialogInfoBody").html('You are now a fan of this user!');
    			
    				// pop dialog notice box
    	        	$("#dialogInfo").dialog(
    			    {
    			    	title: 'Clipstar',
    		            resizable: false,
    		            close: function() { $(this).dialog('destroy'); },
    		            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
    			    });        			
        		} else if (sResponse == 'exists') {
    				// set dialog content
    				$("#dialogInfoBody").html('You are already a fan of this user!');
    			
    				// pop dialog notice box
    	        	$("#dialogInfo").dialog(
    			    {
    			    	title: 'Clipstar',
    		            resizable: false,
    		            close: function() { $(this).dialog('destroy'); },
    		            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
    			    });        			
        		}
            },
            error: function()
            {
            	// set dialog content
				$("#dialogInfoBody").html('For some reason that did\'t work!\n Please refresh the page and try again.');
			
				// pop dialog notice box
		    	$("#dialogInfo").dialog(
			    {
			    	title: 'Clipstar',
		            resizable: false,
		            close: function() { $(this).dialog('destroy'); },
		            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
			    });		            	
            }
        });
	}
	else
	{
		// set dialog content
		$("#dialogInfoBody").html('You must be a registered user and logged in to become a fan of a user. Click <a href="/login/">here to login</a>.');
	
		// pop dialog notice box
    	$("#dialogInfo").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: { "OK": function() { $(this).dialog('destroy'); }}
	    });	
	}
};
 

/*
 * 
 */
function addToGroup(nVideoId, nUserId)
{
	// are we logged in?
	if (nUserId.length > 0)
	{	
		// pop dialog notice box
    	$("#dialogGroupVideo").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: 
            { 
            	"OK": function() 
            	{ 
            		$(this).dialog('destroy');
            		
            		// get ids
            		nGroupId = $("select[name='myGroups'] :selected").val();
            		nVideoId = $("input[name='videoId']").val();
            		
            		// check that this video is not already in the group
            	    $.ajax({
            	        url: '/ajax/addvideotogroup/'+nVideoId+'/'+nGroupId,
            	        type: 'GET',
            	        success: function (sResponse) {
            	    		if (sResponse == 'success') {
            	    			// video added
        	    				$("#dialogInfoBody").html('Video successfully added.');
            	    			
        	    				// pop dialog notice box
        			        	$("#dialogInfo").dialog(
        					    {
        					    	title: 'Clipstar',
        				            resizable: false,
        				            close: function() { $(this).dialog('destroy'); },
        				            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
        					    });             	    			
            	    		} else if (sResponse == 'exists') {
            	    			
                	        	// sorry video already in the group :)
        	    				// set dialog content
        	    				$("#dialogInfoBody").html('Sorry this video has already been added to the group.');
        	    			
        	    				// pop dialog notice box
        			        	$("#dialogInfo").dialog(
        					    {
        					    	title: 'Clipstar',
        				            resizable: false,
        				            close: function() { $(this).dialog('destroy'); },
        				            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
        					    });            	    			
            	    		}
            	    	}
            	    });
            	},
            	"Cancel": function () {$(this).dialog('destroy');}
            }  					    
	    });		
	}
}

/*
 * jQuery Ajax call to add a video to
 * the users favourites
 */
function videoFave(nVideoId, nUserId)
{	
	// are we logged in?
	if (nUserId.length > 0)
	{
	    $.ajax(
	    {
			url: '/ajax/videoadd/' + nVideoId + '/favourite',
			type: 'GET',
			success: function(sResponse)
	    	{
	    		if (sResponse == 'success') {
    				// set dialog content
    				$("#dialogInfoBody").html('This video has successfully been added to your favourites!<br/><br/>You can view your existing favourites by clicking <a href="/manage-profile/videos/favourites/">here</a>');
    			
    				// pop dialog notice box
		        	$("#dialogInfo").dialog(
				    {
				    	title: 'Clipstar',
			            resizable: false,
			            close: function() { $(this).dialog('destroy'); },
			            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
				    });	    			
	    		} else if (sResponse == 'alreadyfavourite') {
					// set dialog content
					$("#dialogInfoBody").html("This video has already been added to your favourites!");
				
					// pop dialog notice box
		        	$("#dialogInfo").dialog(
				    {
				    	title: 'Clipstar',
			            resizable: false,
			            close: function() { $(this).dialog('destroy'); },
			            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
				    });	 	    			
	    		}
	    	}
        });
    } else {
		// set dialog content
		$("#dialogInfoBody").html('You must be a registered user and logged in to add a video to your favourites. Click <a href="/login/">here to login</a>.');
	
		// pop dialog notice box
    	$("#dialogInfo").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
	    });     	
    }
};

/*
 * Add video to promoting
 */
function videoPromoting(nVideoId, nUserId)
{
	// are we logged in?
	if (nUserId.length > 0)
	{
	    $.ajax(
	    {
			url: '/ajax/videoadd/' + nVideoId + '/promoting',
			type: 'GET',
			success: function(sResponse)
	    	{
	    		if (sResponse == 'success') {
    				// set dialog content
    				$("#dialogInfoBody").html('You are now promoting this video!');
    			
    				// pop dialog notice box
		        	$("#dialogInfo").dialog(
				    {
				    	title: 'Clipstar',
			            resizable: false,
			            close: function() { $(this).dialog('destroy'); },
			            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
				    });	    			
	    		} else if (sResponse == 'alreadypromoting') {
					// set dialog content
					$("#dialogInfoBody").html("You are already promoting this video!");
				
					// pop dialog notice box
		        	$("#dialogInfo").dialog(
				    {
				    	title: 'Clipstar',
			            resizable: false,
			            close: function() { $(this).dialog('destroy'); },
			            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
				    });	 	    			
	    		}
	    	}
        });
    } else {
		// set dialog content
		$("#dialogInfoBody").html('You must be a registered user and logged in to promote a video. Click <a href="/login/">here to login</a>.');
	
		// pop dialog notice box
    	$("#dialogInfo").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
	    });     	
    }
};

 /*
  * Add video to my story
  */
function videoStory(nVideoId, nUserId)
{
	// are we logged in?
	if (nUserId.length > 0)
	{
	    $.ajax(
	    {
			url: '/ajax/videoadd/' + nVideoId + '/story',
			type: 'GET',
			success: function(sResponse)
	    	{
	    		if (sResponse == 'success') {
    				// set dialog content
    				$("#dialogInfoBody").html('This is now a my story video!');
    			
    				// pop dialog notice box
		        	$("#dialogInfo").dialog(
				    {
				    	title: 'Clipstar',
			            resizable: false,
			            close: function() { $(this).dialog('destroy'); },
			            buttons: { "OK": function() { $(this).dialog('destroy'); }}  					    
				    });	    			
	    		} else if (sResponse == 'alreadystory') {
					// set dialog content
					$("#dialogInfoBody").html("This is already a story video!");
				
					// pop dialog notice box
		        	$("#dialogInfo").dialog(
				    {
				    	title: 'Clipstar',
			            resizable: false,
			            close: function() { $(this).dialog('destroy'); },
			            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
				    });	 	    			
	    		}
	    	}
        });
    } else {
		// set dialog content
		$("#dialogInfoBody").html('You must be a registered user and logged in to promote a video. Click <a href="/login/">here to login</a>.');
	
		// pop dialog notice box
    	$("#dialogInfo").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
	    });     	
    }		
};


 
 /*
  *  Vote for this video
  */
function videoVote(nVideoId, nUserId, sVoteKey)
{
	// we logged in?
	if (nUserId.length > 0)
	{
    	// add your vote
		$.ajax({
            url: '/ajax/videovote/' + nVideoId + '/' + sVoteKey,
            type: 'GET',
            success: function(sResponse)
            {
				if (sResponse == 'success') {
					$("#votegraphic").attr({ src: "/images/vote-graphic.gif", alt: "Thanks for voting!" });
					// set dialog content
					$("#vote_handle").hide('slow',function(){$("#hidden").show('slow')});					
				} else if (sResponse == 'fail') {
	            	// set dialog content
					$("#dialogInfoBody").html('For some reason that did\'t work!\n Please refresh the page and try again.');
				
					// pop dialog notice box
			    	$("#dialogInfo").dialog(
				    {
				    	title: 'Clipstar',
			            resizable: false,
			            close: function() { $(this).dialog('destroy'); },
			            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
				    });					
				}
			
            }
        });
	}
	else
	{
		// set dialog content
		$("#dialogInfoBody").html('You must be a registered user and logged in to vote. Click <a href="/login/">here to login</a>.');
	
		// pop dialog notice box
    	$("#dialogInfo").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
	    });	 		
	}
};
  
/*
 * 
 */
function videoReport(nVideoId, nUserId)
{
	// we logged in?
	if (nUserId.length > 0)
	{		
		// pop dialog notice box
    	$("#dialogReport").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: 
            { 
            	"OK": function() 
            	{
            		sReason = $("textarea[name='thereason']").val();
            	
        			$.ajax({
        				url: '/ajax/videoreport/' + nVideoId,
        				type: 'POST',
        				data: "reason=" + sReason,
        				success: function(sResponse)
        				{
        					if (sResponse=='success') {
        						// set dialog content
        						$("#dialogInfoBody").html('You have reported this video to our moderators!');
        					
        						// pop dialog notice box
        				    	$("#dialogInfo").dialog(
        					    {
        					    	title: 'Clipstar',
        				            resizable: false,
        				            close: function() { $(this).dialog('destroy'); },
        				            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
        					    });        						
        					} else if (sResponse=='exists') {
        						// set dialog content
        						$("#dialogInfoBody").html('You have already reported this video.');
        					
        						// pop dialog notice box
        				    	$("#dialogInfo").dialog(
        					    {
        					    	title: 'Clipstar',
        				            resizable: false,
        				            close: function() { $(this).dialog('destroy'); },
        				            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
        					    });        						
        					} else if (sResponse=='noreason') {
        						// set dialog content
        						$("#dialogInfoBody").html('You must enter a reason for reporting this video.');
        					
        						// pop dialog notice box
        				    	$("#dialogInfo").dialog(
        					    {
        					    	title: 'Clipstar',
        				            resizable: false,
        				            close: function() { $(this).dialog('destroy'); },
        				            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
        					    });           						
        					}
        				},
        				error: function()
        				{
    						// set dialog content
    						$("#dialogInfoBody").html('We are sorry, there seems to have been a problem. Please try again.');
    					
    						// pop dialog notice box
    				    	$("#dialogInfo").dialog(
    					    {
    					    	title: 'Clipstar',
    				            resizable: false,
    				            close: function() { $(this).dialog('destroy'); },
    				            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
    					    });         					
        				}
        			});
            		$(this).dialog('destroy'); 
            	}
            } 
	    });
	}
	else
	{
		// set dialog content
		$("#dialogInfoBody").html('You must be a registered user and logged in to report a video. Click <a href="/login/">here to login</a>.');
	
		// pop dialog notice box
    	$("#dialogInfo").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
	    });	 		
	}
};
 
/*
 * Share video
 */
function shareVideo(nUserId)
{	
	// are we logged in?
	if (nUserId.length > 0)
	{		
		// pop dialog notice box
	 	$("#dialogShare").dialog(
		{
			title: 'Clipstar',
			resizable: false,
			close: function() { $(this).dialog('destroy'); },
			buttons: 
			{ 
				"Share": function()
				{
				    nVideoId   = $("input[name='videoid']").val();
					sShareBody = escape($("textarea[name='sharebody']").val());
					sShareTo   = $("input[name='shareto']").val();
					sUsername  = $("input[name='username']").val();
					
					$.ajax({
				        url: '/ajax/sharevideo/'+nVideoId,
				        type: 'POST',
				        data: "sharebody=" + sShareBody + "&username=" + sUsername + "&shareto=" + sShareTo,  
				        error: function()
				        {
							$(this).dialog('destroy');
				        },
				        success: function()
				        {
				        	$(this).dialog('destroy');
						}
					});					
					$(this).dialog('destroy');	
				}
			} 
	    });
	}
	else
	{
		// set dialog content
		$("#dialogInfoBody").html('You must be a registered user and logged in to share a video. Click <a href="/login/">here to login</a>.');
	
		// pop dialog notice box
    	$("#dialogInfo").dialog(
	    {
	    	title: 'Clipstar',
            resizable: false,
            close: function() { $(this).dialog('destroy'); },
            buttons: { "OK": function() { $(this).dialog('destroy'); }} 
	    });	 
	}	
};
 

/*
 * Let user set a video as their profile video
 */
function setProfileVideo(nVideoId)
{
	// first get all this users videos
	$.ajax({
        url: '/ajax/setprofilevideo/'+nVideoId,
        type: 'GET',
        success: function(sResponse) {
			if (sResponse == 'success') {
 				// set dialog content
 				$("#dialogInfoBody").html('You have set your profile video!');
 			
 				// pop dialog notice box
 	        	$("#dialogInfo").dialog(
 			    {
 			    	title: 'Clipstar',
 		            resizable: false,
 		            close: function() { $(this).dialog('destroy'); },
 		            buttons: 
 		            { 
 		            	"OK": function() 
 		            	{
 		            		$(this).dialog('destroy');
 		            		window.location.reload();
 		            	}
 		            }  					    
 			    });				
			}
			window.location.reload();
		}
	});
};
 
//videoCloak
function videoCloak(){
	if (document.getElementById('cloakLayer')){
		var cloakContainer = $('#cloakLayer');
		cloakContainer.fadeOut(1000, function() {$(this).remove();});
	}
	else{
		var height = $('body').height();
		var cloakContainer = $('<div id="cloakLayer" style="height:'+height+'px;"></div>');
	    $('body').append(cloakContainer);
	    $('#cloakLayer').css({ opacity: '0' }).animate({ opacity: '0.9' }, 1000);

	}
};

function deleteVideo(nVideoId) {
	$("#dialogInfoBody").html('Are you sure you want to remove this video?');
	
	// pop dialog notice box
	$("#dialogInfo").dialog(
    {
    	title: 'Clipstar',
        resizable: false,
        close: function() { $(this).dialog('destroy'); },
        buttons: 
        { 
        	"OK": function() 
        	{
        		$(this).dialog('destroy');
        		window.location.replace('/manage-profile/videos/delete/'+nVideoId + '/');
        	},
        	"Cancel": function()
        	{
        		$(this).dialog('destroy');
        	}
        }  					    
    });
}