/* /admin/article */

tinyMCE.init({
	mode : "exact",
	elements : 'tinymce',
	theme : "advanced",
	plugins : 'advlink,advimage',
	relative_urls : false
});

$(document).ready(function() {
	/* site wide */
	
	height = $('#body .inner').height();
	$('#body .inner .content').css('height', height);
	$('#body .inner .side_bar').css('height', height);

	email_obfuscated_href = $('.email-obfuscated').eq(0).attr('href');
	email_obfuscated_text = $('.email-obfuscated').eq(0).text();
	$('.email-obfuscated').attr('href', email_obfuscated_href.replace('--REMOVE--ME--', ''));
	$('.email-obfuscated').text(email_obfuscated_text.replace('--REMOVE--ME--', ''));

	/* /admin */
	
	$('#admin form[name=sidebar] select').bind('change', function() {
		$.ajax({
			type: "POST",
			url: 'admin/sidebar/'+$(this).attr('name').replace('article_', '')+'/'+$(this).val(),
			dataType: 'json'
		});
	});
	
	/* /admin/gallery */

	$(function(){
		var btnUpload=$('#admin_gallery #upload');
		if (typeof $(btnUpload).attr('id') != 'undefined') {
			new AjaxUpload(btnUpload, {
				action: parent.window.location+'/image_upload',
				name: 'image',
				responseType: 'json',
				data: {},
				onSubmit: function(file, ext){
					if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
						// check for valid file extension
						$('#admin_gallery #upload_status .message').text('Only JPG, PNG or GIF files are allowed.');
						$('#admin_gallery #upload_status .message').attr('class', 'message error');
						$('#admin_gallery #upload_status .message').show();
						return false;
					}
					
					$('#admin_gallery #upload_status .message').text('Uploading...');
					$('#admin_gallery #upload_status .message').attr('class', 'message information');
					$('#admin_gallery #upload_status .message').show();
				},
				onComplete: function(file, response) {
					if (response.error) {
						$('#admin_gallery #upload_status .message').text(response.error);
						$('#admin_gallery #upload_status .message').attr('class', 'message error');
						$('#admin_gallery #upload_status .message').show();
					}
					else {
						$('#admin_gallery #upload_status .message').text('Image uploaded successfully.');
						$('#admin_gallery #upload_status .message').attr('class', 'message success');
						$('#admin_gallery #upload_status .message').show();
						
						$('<li></li>').appendTo('#admin_gallery #images').html('<div class="image"><img src="'+response.file_name+'" alt="" /></div><a class="delete" target="_top" href="">Delete</a>');
					}
				}
			});
		}
	});
	
	$('#admin_gallery .delete').live('click', function() {
		if (window.confirm('Are you sure you would like to delete this image?')) {
			link = $(this);
		
			$.ajax({
				type: "POST",
				url: parent.window.location+'/image_delete',
				data: { file_name: $(this).siblings('.image').children('img').get(0).src },
				dataType: 'json',
				success: function(response) {
					if (response.error) {
						$('#admin_gallery #upload_status .message').text(response.error);
						$('#admin_gallery #upload_status .message').attr('class', 'message error');
						$('#admin_gallery #upload_status .message').show();
					}
					else {
						link.parent().slideUp('slow');
					}
				}
			});
		}
		
		return false;
	});
	
	/* /gallery */
	
	$(".cboxelement").colorbox();
	
	var gallery_view_position = 0;
	
	$('#gallery .album .left').click(function(e) {
		if (gallery_view_position == 0) return false;
	
		$("#gallery .album .images .container").animate({"left": "+=543px"}, "slow");
		
		gallery_view_position--;
		
		if (gallery_view_position == 0) {
			$('#album .left').hide();
		}
		
		if (gallery_view_position < $("#album li").size()/3-1) {
			$('#album .right').show();
		}
		
		return false;
	});
	
	$('#album .right').click(function(e) {
		$("#album .images .container").animate({"left": "-=543px"}, "slow");
		
		gallery_view_position++;
		
		$('#album .left').show();
		
		if (gallery_view_position >= Math.round($("#album li").size()/3)) {
			$('#album .right').hide();
		}
		
		return false;
	});
});