
/*
window.addEvent('domready', function() {
	var myAccordion = new Accordion($('upload_options'), 'h2.toggler', 'div.upload_option', {
	  display: 2,
		opacity: false,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#0099ff');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#e2e2e2');
		}
	});
});
*/
window.addEvent('domready', function() {
  if ($chk($('uploader_browse'))) {
  	var swiffy = new FancyUpload2($('uploader_status'), $('uploader_list'), {
  		url: $('uploader_form').action + "?upload_video_id=" + $('upload_video_id').value,
  		fieldName: 'video_upload',
  		path: '/images/uploader/uploader.swf',
  		limitSize: 2000 * 1024 * 1024,
  		typeFilter: {'Videos (*.aac, *.aif, *.aiff, *.asf, *.avi, *.dv, *.mov, *.m4v, *.mp1, *.mp2, *.mp3, *.mp4, *.mpg, *.wav, *.wma, *.wmv)': '*.aac; *.aif; *.aiff; *.asf; *.avi; *.dv; *.mov; *.m4v; *.mp1; *.mp2; *.mp3; *.mp4; *.mpg; *.wav; *.wma; *.wmv;'},
  		debug: true,
  		target: 'uploader_browse',
  		onLoad: function() {
  		  this.target.addEvents({
  				click: function() {
  					return false;
  				},
  				mouseenter: function() {
  				  this.src = '/images/uploader/browse_hover.png';
  				},
  				mouseleave: function() {
  					this.src = '/images/uploader/browse.png';
  					this.blur();
  				},
  				mousedown: function() {
  					this.focus();
  				}
  			});
  		},
  		onSelectSuccess: function() {
  		  //alert('about to start');
  		  $('select_video').style.display = 'none';
  		  $('technical_details').style.display = 'none';
  		  $('uploader_status').style.display = 'block';
    		$('uploader_progress').style.display = 'block';
    		//alert('really about to start');
  		  this.start();
  		  //alert('started');
  		},
  		onFileRemove: function() {
  		  $('uploader_progress').style.display = 'none';
				$('metadata').style.display = 'none';
				$('uploader_list').style.display = 'none';
				$('current-text').innerHTML = '<p>Upload cancelled. <a href="/video/new?upload=true">Reload</a> the page to upload another video.</p>';
				$('current-text').style.cssFloat = 'left';
  		},
  		onFileSuccess: function(file, response) {
  		  $('uploader_progress').style.display = 'none';
  		  $('uploader_list').style.display = 'none';
  		  $('current-text').style.display = 'none';
  		  var json = $H(JSON.decode(response, true));
  		  var uploadStatus = json.get('uploadStatus');
  		  switch(uploadStatus) {
		    case "1":
  		    message = "<p><strong>Upload complete! Your video is now being converted.</strong></p><p>You will receive an email when it is live.</p><p>Make sure to press the Save button at the bottom of the page before leaving or closing this window.</p><p>Go to the <a href='/videos/main' style='display: inline'>Video Main Page</a>.</p>";
  		    break;
		    case "2":
		      message = "There was a problem saving your video. Please try again.";
		      break;
		    case "3":
		      message = "You have uplaoded an invalid file type. Please review the list of acceptable formats and try again.";
		      break;
  		  }
  		  $('upload_response').innerHTML = message;
  		  $('upload_response').style.display = 'block';
  		}
  	});
  }
  
  if ($chk($('thumbnail_browse'))) {
    var swiffy2 = new FancyUpload2($('uploader_status2'), $('uploader_list2'), {
  		url: '/video/add_thumbnail' + "?upload_video_id=" + $('upload_video_id').value,
  		fieldName: 'thumbnail_upload',
  		path: '/images/uploader/uploader.swf',
  		limitSize: 2 * 1024 * 1024,
  		typeFilter: {'Images (*.jpg, *.gif, *.png, *.bmp)': '*.jpg; *.gif; *.png; *.bmp;'},
  		debug: true,
  		target: 'thumbnail_browse',
  		onLoad: function() {
  		  this.target.addEvents({
  				click: function() {
  					return false;
  				},
  				mouseenter: function() {
  				  this.src = '/images/uploader/browse_hover.png';
  				},
  				mouseleave: function() {
  					this.src = '/images/uploader/browse.png';
  					this.blur();
  				},
  				mousedown: function() {
  					this.focus();
  				}
  			});
  		},
  		onSelectSuccess: function() {
  		  this.start();
  		},
  		onFileSuccess: function(file, response) {
  		  var json = $H(JSON.decode(response, true));
  		  var thumbnail = json.get('thumbnail');
  		  $('custom_thumbnail').src = thumbnail;
  		  $('video_thumbnail').value = thumbnail;
  		}
  	});
  }
  if ($chk($('metadata_form'))) {
  	$('metadata_form').addEvent('submit', function(e) {
  		new Event(e).stop();
  		var myRequest = new Request({method: 'post', url: this.action});
  		var loading = $('metadata_spinner').empty().addClass('ajax-loading');
  		myRequest.send({
  			data: "video[title]=" + this.video_title.value + "&video[description]=" + this.video_description.value + "&video[credit]=" + this.video_credit.value + "&video[id]=" + this.upload_video_id.value + "&video[type]=" + this.video_type.value + "&video[comments_allowed]=" + this.video_comments_allowed.value + "&video[allow_embed]=" + this.video_allow_embed.value + "&is_upload=1&tags=" + this.tags.value,
  			update: loading,
  			evalResponse: true,
  			onComplete: function() {
  				loading.removeClass('ajax-loading');
  			}
  		});
  	});
  }
  if ($chk($('video_embed_code'))) {
    $('video_embed_code').addEvent('change', function(e) {
      var myRequest = new Request({method: 'post', url: '/video/pull_thumbnail'});
      var data = "id=" + $('upload_video_id').value + "&video[embed_code]=" + escape(this.value);
  		myRequest.send({
  			data: data,
  			evalResponse: true
  		});
  	});
  }
  if ($chk($('video_submit'))) {
    $('video_submit').addEvent('mouseover', function(e) {
      $('video_submit').src = '/images/uploader/save_hover.png';
    });
    $('video_submit').addEvent('mouseout', function(e) {
      $('video_submit').src = '/images/uploader/save.png';
    });
  }
  if ($chk($('thumbnail_info'))) {
    var myTips = new Tips('.thumbnail_info', {fixed: true, className: 'thumbnail_hover'});
  }
  if ($chk($('feature_info'))) {
    var myTips2 = new Tips('.feature_info', {fixed: true, className: 'thumbnail_hover'});
  }
  if ($chk($('comments_allowed_radio'))) {
    $('video_upload_forms').getElements('input[name=comments_allowed_radio]').each(function(el){
    	el.addEvent('click', function(e) {
    	  $('video_comments_allowed').value = this.value;
    	});
    });
  }
  if ($chk($('allow_embed_radio'))) {
    $('video_upload_forms').getElements('input[name=allow_embed_radio]').each(function(el){
    	el.addEvent('click', function(e) {
    	  $('video_allow_embed').value = this.value;
    	});
    });
  }
  if ($chk($('type_radio'))) {
    $('video_upload_forms').getElements('input[name=type_radio]').each(function(el){
    	el.addEvent('click', function(e) {
    	  $('video_type').value = this.value;
    	});
    });
  }
});

/*
window.addEvent('domready', function(){
	$('embed_form').addEvent('submit', function(e) {
	  $('uploader').style.display = 'none';
		new Event(e).stop();
		var myRequest = new Request({method: 'post', url: this.action});
		var loading = $('embed_spinner').empty().addClass('ajax-loading');
		myRequest.send({
			data: "video[embed_code]=" + encodeURIComponent(this.embed_code.value) + "&video[title]=" + encodeURIComponent(this.embed_title.value) + "&video[description]=" + encodeURIComponent(this.embed_caption.value) + "&video[credit]=" + encodeURIComponent(this.embed_credit.value) + "&is_feature=" + this.embed_is_feature.value,
			update: loading,
			evalResponse: true,
			onComplete: function() {
				loading.removeClass('ajax-loading');
			}
		});
	});
});
*/