// When the DOM has loaded, init the form link.
jQuery(document).ready(
	function(){
		// Get the add new upload link.
		var jAddNewSubmission = jQuery( "#add-video-submission" );
 
		// Hook up the click event.
		jAddNewSubmission
			.attr( "href", "javascript:void( 0 )" )
			.click(
				function( objEvent ){
					AddNewSubmission();
 
					// Prevent the default action.
					objEvent.preventDefault();
					return( false );
				}
				)
		;
 
	}
	);
 
 
// This adds a new file upload to the form.
function AddNewSubmission(){	
	// Get a reference to the upload container.
	var jFieldsContainer = jQuery( "#video_submissions_container" );
 //alert("jFieldsContainer");
	// Get the file upload template.
	var jFieldsTemplate = jQuery( "#element-templates div.row" );
 
	// Duplicate the upload template. This will give us a copy
	// of the templated element, not attached to any DOM.
	var jFields = jFieldsTemplate.clone();
 
	// At this point, we have an exact copy. This gives us two
	// problems; on one hand, the values are not correct. On
	// the other hand, some browsers cannot dynamically rename
	// form inputs. To get around the FORM input name issue, we
	// have to strip out the inner HTML and dynamically generate
	// it with the new values.
	var strNewHTML = jFields.html();
 
	// Now, we have the HTML as a string. Let's replace the
	// template values with the correct ones. To do this, we need
	// to see how many upload elements we have so far.
	var intNewFileCount = (jFieldsContainer.find( "div.row" ).length + 1);
 
	// Set the proper ID.
	jFields.attr( "id", ("video-data-" + intNewFileCount) );
 
	// Replace the values.
	strNewHTML = strNewHTML
		.replace(
			new RegExp( "::FIELD2::", "i" ),
			intNewFileCount
			)
		.replace(
			new RegExp( "::ID::", "i" ),
			(intNewFileCount)
			)
		.replace(
			new RegExp( "::VID::", "i" ),
			(intNewFileCount)
			)
		.replace(
			new RegExp( "::CONTAINERID::", "i" ),
			("video-data-" + intNewFileCount)
			)
	;
 
	// Now that we have the new HTML, we can replace the
	// HTML of our new upload element.
	jFields.html( strNewHTML );
 
	// At this point, we have a totally intialized file upload
	// node. Let's attach it to the DOM.
	jFieldsContainer.append( jFields );
}

function removeSubmission(container)
{
	var confirm_delete= confirm("Do you really want to delete this submission?");
	//Remove Submission container from DOM
	if(confirm_delete == true){
		jQuery('#'+container).remove();
	}
}

function toggelVideoData(el)
{
	var hideShowLink = jQuery(el);
	var videoFieldset = hideShowLink.parent().siblings('.video-fieldset');
	var videoTitle = videoFieldset.find('.video-title');	
	
	//if its expanded then close it, else open it.
	if(videoFieldset.hasClass('expanded')){		
		videoFieldset.removeClass('expanded');
		videoFieldset.addClass('collapsed');
		//Change Text
		
		var titleText = videoTitle.val();
		if(titleText.length > 15){
			titleText = titleText.substring(0, 15)+ "...";
		}
		
		var videoTitleText = "";
		if(videoTitle.val() != ""){
			videoTitleText = " for "+titleText;
		}
		
		hideShowLink.text("Show Details"+videoTitleText);
	} else {
		videoFieldset.removeClass('collapsed');		
		videoFieldset.addClass('expanded');
		//Change Text
		hideShowLink.text("Hide Details");
	}
}
