$(function() {
	$('#upload_photo').fileUpload({ 
	  uploader:  '/cms/swf/uploader.swf', 
	  script:    '/ideas/upload',
	  scriptData: {
		 sid: $('#sid').val(),
		 idea_id: $('#idea_id').length > 0 ? $('#idea_id').val() : ''
	  },
	  buttonImg: $('#upload_photo').attr('src'),
	  cancelImg : '/cms/img/cancel.png',
	  width: $('#upload_photo').width(),
	  height: 26,
	  multi: true,
	  auto: true,
	  queueSizeLimit: 5,
	  simUploadLimit: 1,
	  wmode: 'transparent',
	  fileExt: '*.jpg;*.jpeg;*.gif;*.png',
	  fileDesc: 'upload some photos',
	  displayData: 'percentage',
	  onComplete: function (event, queueId, fileObj, response, data, data)
		{
			if ($('#images li').length) {
				var obj = $('#images li:last').after(response);	
			} else {
				var obj = $('#images ul').html(response);
			}
			
		},
	  onError: function(event, queueID, fileObj, errorObj) {
		console.info(errorObj);
		$('#images').html(errorObj.type + ' - ' + errorObj.text);
	}
	});
	
	$('#images li input').live('focus', function() {
		if($(this).val() == 'Click here to add a caption') {
			$(this).val('');
		}
	});
	
	$('#images li a.remove').live('click', function(e) {
		e.preventDefault();
		
		var image = $(this).parent('li');
		
		$.ajax({
			url: $(this).attr('href'),
			success: function(data) {
				image.fadeOut();
			}
		});
		
		return false;
	});
	
	$('#images li .caption').live('click', function(e) {
		var imageId = $(this).attr('id').split('_')[1];
		var value = $(this).text() != 'Click here to add a caption' ? $(this).text() : '';
		
		$(this).html('<input type="text" name="captions['+imageId+']" value="'+value+'"/>').find('input').focus();
		e.stop();
		$(this).die('click', this);
	});
	
	$('#upload_video').click(function(e) {
		$('#ideaSubmit .video').slideDown();
	});
	
	var closeBtn = $('<a class="close">close</a>').click(function() {
		$('dd.video').slideUp();
	});
	
	$('dd.video label').before(closeBtn);
	
	if($('.submit #ideaSubmit .video textarea').val() != '') {
		$('#ideaSubmit .video').show();
	}
});