jQuery.fn.bindAll = function(options) {
	var $this = this;
	jQuery.each(options, function(key, val){
		$this.bind(key, val);
	});
	return this;
}

$(function(){
	
	// Remove unused Months
	$('#homeScroll h2').each(function(n, e) {
		if (!$(e).next().is('li')) $(e).hide();
		//console.log('Found H3');
	});
	
	$('#mainBody div.scroll').jScrollPane({
		showArrows: true,
		scrollbarWidth: 7
	});
	
	// Parents Gallery
	$('#parentsGalleryThumbs a').click(function() {
		// Select the current item
		$('#parentsGalleryThumbs a').removeClass('selected');
		$(this).addClass('selected');
		
		var params = $(this).attr('rel');
		params = params.split('|');
		// Check if we already loaded it
		if (params[0] == $('#parentsGalleryLarge img').attr('src')) return;
		// Load new img src
		$('#parentsGalleryLarge img').attr('src', params[0]);
		
		// Update title
		$('#currentImageTitle').text(params[1]);
		
		return false;
	});
	
	// Add horiz. scroll
	$('#parentsGalleryThumbs').jcarousel({
		scroll: 5,
		buttonNextHTML: '<span></span>',
		buttonPrevHTML: '<span></span>'
	});
	
	// About us tabs
	$('ul#aboutUsNav li').click(function() {
		$('ul#aboutUsNav li').removeClass('selected');
		$(this).addClass('selected');
		$('div.aboutUsTab').hide();
		var id = $(this).attr('id');
		if (id == 'tab1') {
			$('#aboutUs1').show();
		} else if (id == 'tab2') {
			$('#aboutUs2').show();
		}
		return false;
	});//end click
	
	
	// Admin Delete Images
	$('#deleteImageListing .parentsDelete a').live('click', function() {
		var id = clean_id($(this).attr('id'));
		var obj = $(this);
		
		// Make sure the js api is avalable
		if (typeof trashAsset == 'function') {
			var conf = confirm('Are you sure you want to delete the image from the Parents Gallery?');
			if (conf) {
				// Show blanket
				$('#blanket').show();
				
				trashAsset(id, function(e) {
					// Remove from view
					obj.parent('.parentsDelete').remove();
					//alert(e);
					$('#blanket').hide();
				});
			}//end if
			
		} else {
			alert('Something is not right, and the image cannot be deleted. Please contact webmaster@puc.edu.');
		}//end else
		
		return false;
		
	});
	
	
	// Multiple File upload
	var listeners = {
		swfuploadLoaded: function(event) {
			$('.barHold', this).remove();
			$(this).append('<div class="barHold"><div class="progress"><span class="fileSize"></span></div></div>');
			$(this).append('<span class="currentSize"></span>');
			
		},
		fileQueued: function(event, file){
			$('.log', this).append('<li>File queued - '+file.name+'</li>');
			// start the upload once it is queued
			// but only if this queue is not disabled
			$(this).swfupload('startUpload');
		},
		fileQueueError: function(event, file, errorCode, message){
			$('.log', this).append('<li>File queue error - '+message+'</li>');
		},
		fileDialogStart: function(event){
			$('.log', this).append('<li>File dialog start</li>');
		},
		fileDialogComplete: function(event, numFilesSelected, numFilesQueued){
			$('.log', this).append('<li>File dialog complete</li>');
		},
		uploadStart: function(event, file){
			$('.log', this).append('<li>Upload start - '+file.name+'</li>');
			// don't start the upload if this queue is disabled
			if ($('input[name=disabled]:checked', this).length) {
				event.preventDefault();
			}
		},
		uploadProgress: function(event, file, bytesLoaded){
			var percent = parseInt(100 / file.size * bytesLoaded);
			$('.progress', this).css('width', percent + '%');
			$('.fileSize', this).text(percent+'%');
			$('.currentSize', this).text(bytesToSize(bytesLoaded) + ' of ' + bytesToSize(file.size));
		},
		uploadSuccess: function(event, file, serverData){
			$('.log', this).append('<li>Upload success - '+file.name+'</li>');
		},
		uploadComplete: function(event, file){
			$('.log', this).append('<li>Upload complete - '+file.name+'</li>');
			var obj = this;

			// Refresh
			refreshImageListing(obj);
			
		},
		uploadError: function(event, file, errorCode, message){
			$('.log', this).append('<li>Upload error - '+message+'</li>');
		}
	};

	$('.swfupload-control').bindAll(listeners);
	
});


function bytesToSize(bytes) {	
	var precision = 2;
	var kilobyte = 1024;
	var megabyte = kilobyte * 1024;
	var gigabyte = megabyte * 1024;
	var terabyte = gigabyte * 1024;
	
	if ((bytes >= 0) && (bytes < kilobyte)) {
		return bytes + ' B';

	} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
		return roundNumber(bytes / kilobyte, 2) + ' KB';

	} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
		return roundNumber(bytes / megabyte, 2) + ' MB';

	} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
		return roundNumber(bytes / gigabyte, 2) + ' GB';

	} else if (bytes >= terabyte) {
		return roundNumber(bytes / gigabyte, 2) + ' TB';
	} else {
		return bytes + ' B';
	}
}

function roundNumber(num, dec) {
	return num.toFixed(2);
}


/**
* Function that cleans the input leaving just the asset id
*
* @param string		item_id				The id of the asset we are getting locks for
*
* @access public
*/
function clean_id(item_id) {
	return item_id.replace(/[^0-9]/g, '');
}// End clean_id
