trial = {
	total_assets: 3,
	count: 0,
	lame_retry: 0,
	
	css: [
		'/css/jamm/layout.css',
		'/css/jamm/toolbar.css',
		'/css/jamm/bin.css',
		'/css/jamm/publisher.css'
	],
	
	init: function() {
		this.bound = {
			loadManger: this.loadManager.bind(this)
		}
		
		this.new_button = $('jamm-utils').getElement('ul li.new');
		this.new_button.addEvent('click', this.bound.loadManger);
		
		if($('jamm-fav')) {
			$('jamm-fav').addEvent('click', this.toggleFavorite.bind(this));
		}
	},
	
	loadManager: function() {
		if(jamm_header.browser_supported()) {
			this.new_button.removeEvent('click', this.bound.loadManger);
			
			this.new_button.addClass('loading');
			
			this.css.each(function(url) {
				Asset.css(url);
			}, this);
			
			Asset.javascript('/js/mootools-1.2-more.js', {onload: this.loaded.bind(this)});
			Asset.javascript('/js/jamm/manager.js', {onload: this.loaded.bind(this)});
			
			new Request.JAMM({url: '/user/' + jamm.user.name + '/jamm/' + jamm.name + '?view=items', onSuccess: this.itemsSuccess.bind(this)}).GET();
		} else {
			this.new_button.dispose();
		}
	},
	
	itemsSuccess: function(data) {
		loaded_media = data;
		
		this.loaded();
	},
	
	loaded: function() {
		this.count++;
		if(this.count === this.total_assets) {
			this.runManager();
		}
	},
	
	runManager: function() {
		try {
			manager.init();
			manager.toggleModes();
			
			if(user.id === 0) {
				jamm_properties.dialog.show();
				
				tip_bubble_manager.add(new Tip_Bubble('settings-bubble', {fade: false, custom_event: 'settingsclosed'}));
				tip_bubble_manager.add(new Tip_Bubble('bin-create-bubble', {open_element: 'creator', open_event: 'mouseenter', custom_event: 'mediacreated'}));
				tip_bubble_manager.add(new Tip_Bubble('bin-media-bubble', {open_element: 'bin', open_event: 'mouseenter', custom_event: 'mediadragged'}));
				tip_bubble_manager.add(new Tip_Bubble('bin-open-bubble', {custom_event: 'binclosed'}));
				tip_bubble_manager.add(new Tip_Bubble('selected-bubble', {custom_event: 'mediaselected'}));
				
				Cookie.write('seen_tip_bubbles', true);
			}
		} catch(e) {
			if(this.lame_retry < 2) {
				this.safarAndIESucks();
			}
		}
	},
	
	safarAndIESucks: function() {
		this.count--;
		this.lame_retry++;
		Asset.javascript('/js/jamm/manager.js', {onload: this.loaded.bind(this)});
	},
	
	jammViewed: function(data) {
		if(data.increase !== null) {
			//alert('jamm increased by ' + data.increase + ' points!');
		}
	},
	
	toggleFavorite: function() {
		var action = $('jamm-fav').hasClass('selected') ? 'un-favorite' : 'favorite';
		$('jamm-fav').addClass('loading');
		
		new Request.JAMM({url: '/user/' + user.name, 'onSuccess': this.successFavorite.bind(this), 'onComplete': this.completeFavorite.bind(this)}).POST({action: action, jamm_id: jamm.id});
	},
	
	successFavorite: function() {
		$('jamm-fav').toggleClass('selected');
	},
	
	completeFavorite: function() {
		$('jamm-fav').removeClass('loading');
	}
}

window.addEvent('domready', trial.init.bind(trial));

window.addEvent('load', function() {
	if(window.mark_view === true) {
		var view_jamm = function() {
			new Request.JAMM({url: '/user/' + jamm.user.name + '/jamm/' + jamm.name, 'onSuccess': trial.jammViewed.bind(trial)}).POST({action: 'viewed'});
		}
		
		view_jamm.delay(5000);
	}
});

/*
Script: Assets.js
	Provides methods to dynamically load JavaScript, CSS, and Image files into the document.

License:
	MIT-style license.
*/

var Asset = new Hash({

	javascript: function(source, properties){
		properties = $extend({
			onload: $empty,
			document: document,
			check: $lambda(true)
		}, properties);
		
		var script = new Element('script', {'src': source, 'type': 'text/javascript'});
		
		var load = properties.onload.bind(script), check = properties.check, doc = properties.document;
		delete properties.onload; delete properties.check; delete properties.document;
		
		script.addEvents({
			load: load,
			readystatechange: function(){
				if (['loaded', 'complete'].contains(this.readyState)) load();
			}
		}).setProperties(properties);
		
		
		if (Browser.Engine.webkit419) var checker = (function(){
			if (!$try(check)) return;
			$clear(checker);
			load();
		}).periodical(50);
		
		return script.inject(doc.head);
	},

	css: function(source, properties){
		return new Element('link', $merge({
			'rel': 'stylesheet', 'media': 'screen', 'type': 'text/css', 'href': source
		}, properties)).inject(document.head);
	},

	image: function(source, properties){
		properties = $merge({
			'onload': $empty,
			'onabort': $empty,
			'onerror': $empty
		}, properties);
		var image = new Image();
		var element = $(image) || new Element('img');
		['load', 'abort', 'error'].each(function(name){
			var type = 'on' + name;
			var event = properties[type];
			delete properties[type];
			image[type] = function(){
				if (!image) return;
				if (!element.parentNode){
					element.width = image.width;
					element.height = image.height;
				}
				image = image.onload = image.onabort = image.onerror = null;
				event.delay(1, element, element);
				element.fireEvent(name, element, 1);
			};
		});
		image.src = element.src = source;
		if (image && image.complete) image.onload.delay(1);
		return element.setProperties(properties);
	},

	images: function(sources, options){
		options = $merge({
			onComplete: $empty,
			onProgress: $empty
		}, options);
		if (!sources.push) sources = [sources];
		var images = [];
		var counter = 0;
		sources.each(function(source){
			var img = new Asset.image(source, {
				'onload': function(){
					options.onProgress.call(this, counter, sources.indexOf(source));
					counter++;
					if (counter == sources.length) options.onComplete();
				}
			});
			images.push(img);
		});
		return new Elements(images);
	}

});

/* IE is retarded - have to set these global objects before loading manager.js -- soconnor*/
bin = {}
settings = {}
sections = {}
publisher = {}