Shadowbox.loadSkin('classic', '/shadowbox/skin');
Shadowbox.loadLanguage('en', '/shadowbox/lang');
Shadowbox.loadPlayer(['flv', 'html', 'img', 'swf'], '/shadowbox/player');

$(function() {
	Shadowbox.init();
	
	if($('#adminVideos').length > 0) {
		$('a.new').removeAttr('onclick').click(function() {
			$.post('/create/video', {page:'admin',display:'ajax'}, function(form) {
				$('#adminVideos tr:first').after('<tr><td colspan="6" class="new">' + form + '</td></tr>');
				$('#adminVideos td.new form input[name=referer]').val('ajax');
				$('#adminVideos td.new form').attr('action', '/save/video').ajaxForm(function(id) {
					$.post('/read/video/' + id, {page:'admin', object:'video', display:'list'},  function(item) {
						$('#adminVideos').append(item);
						$('#adminVideos tr td.new').parent().remove();
					});
				});
			});
		});
		
		enableEditableOrders();
	}

	$('#menu li:not(.active) img').mouseenter(function() {
		var src = $(this).attr('src');
		if(src.indexOf('_over') > -1) {
		    return;
		}

		$(this).attr('src', src.replace('.png', '_over.png'));
			     }).mouseleave(function() {
				     var src = $(this).attr('src');
				     $(this).attr('src', src.replace('_over.png', '.png'));
				 });
});

function cancelEditProductDescription(id) {
	var previous = $('#project' + id + 'description').data('current');
	$('#project' + id + 'description').addClass('editable').html(previous);
}

function changeOrder(input, id, order) {
	if(isNaN(order)) {
		$(input).val($(input).data('order'));
		return;
	}
	
	$.post('/changeorder/video/' + id, {order:order}, function(table) {
		$('#adminVideos tr:gt(0)').remove();
		$('#adminVideos tbody').append(table);
		enableEditableOrders();
	});
}

function changeStatus(id, status) {
	$.post('/changestatus/video/' + id, {status:status}, function(item) {
		$('#video' + id).replaceWith(item);
	});
}

function deleteVideo(id) {
	if(confirm("Supprimer le video '" + $('#video'+id+' td:first').text() + "'?")) {
		$.post('/delete/video/' + id, {}, function() {
			$('#video'+id).remove();
		});
	}
}

function editProjectDescription(id) {
	$.post('/editprojectdescription', {id:id}, function(form) {
		var current = $('#project' + id + 'description').html();
		$('#project' + id + 'description').removeClass('editable').data('current', current).html(form);
		CMS.loadRichEdits(true);
		$('form[name="editproject' + id + 'description"]').ajaxForm(function() {
			var description = $('textarea[name="description_text"]').val();
			$('#project' + id + 'description').addClass('editable').html(description);
		});
	});
}

function editVideo(id) {
	var cache = $('#video' + id).html();
	$('#video' + id).data('cache', cache);
	$.post('/edit/video/' + id, {display:'ajax'}, function(form) {
		$('#video' + id).html('<td colspan="6">' + form + '</td>').find('.cancel').click(function () {
			$('#video' + id).html($('#video' + id).data('cache'));
		});
		
		$('#video' + id + ' form').attr('action', '/update/video/' + id).ajaxForm(function() {
			$.post('/read/video/' + id, {page:'admin', object:'video', display:'list'},  function(item) {
				$('#video' + id).replaceWith(item);
			});
		});
		
		$('#video' + id + ' form input[name=referer]').val('ajax');
	});
}

function enableEditableOrders() {
	$('#adminVideos input').blur(function () {
			var id = $(this).parent().parent().attr('id').replace('video', '');
			changeOrder(this, id, $(this).val());
		});
		
	$('#adminVideos input').each(function() {
		$(this).removeAttr('disabled').data('order', $(this).val());
	});
}

function moarVideos(offset) {
    $.post('/moarvideos', {offset:offset}, function(menu) {
	    $('#projectsMenu').replaceWith(menu);
	});
}

function playMedia(id, type, content) {
	if(type == 'image') {
		Shadowbox.open({player:'img',content:'/'+content});
	} else if($('#player').length > 0) {
		$.post('/videoplayer/' + id, {page:$('body').attr('id')}, function(media) {
			$('#player').replaceWith(media);
		});
	} else {
	    try {
		var re = new RegExp('width="([^"]*)" height="([^"]*)"');
		var match = re.exec(content);
		var width = match[1];
		var height = match[2];
	    } catch(e) {
	    }

	    Shadowbox.open({player:'html',width:width,height:height,content:content});
	}
}
