function init(view) {
	
	if(view.match("popup_") == null) {
		cssdropdown.startchrome("chromemenu");
	}

	switch(view) {
		case 'popup_category.php':
			getUrl('recipe_categories.php', 'categories');
			getUrlParent('recipe_categories.php', 'category');
			break;	
		case 'popup_link.php':
			submitForm('search', 'update');
			break;	
		case 'popup_occasion.php':
			var id = '';
			if(window.opener.document.getElementById('id')) {
				id = '?id=' + window.opener.document.getElementById('id').value;
			}
			getUrlParent('recipe_occasion.php' + id, 'occasion');
			break;	
		case 'popup_source.php':
		case 'popup_territory.php':
		case 'popup_publisher.php':
			var id = '';
			if(window.opener.document.getElementById('id')) {
				id = '?id=' + window.opener.document.getElementById('id').value;
			}
			if(window.opener.document.getElementById('source')) {
				getUrlParent('recipe_source.php' + id, 'source');
			}
			break;
		case 'popup_source_edit.php':
			var id = '';
			if(window.opener.document.getElementById('id')) {
				id = '?id=' + window.opener.document.getElementById('id').value;
			}
			getUrlParent('recipe_source.php' + id, 'source');

			id = document.getElementById('id').value;
			getUrl('popup_source_form.php?id=' + id, 'edit_form');
			break;
		case 'popup_usage.php':
			id = document.getElementById('recipe_id').value;
			getUrl('recipe_source.php?id=' + id, 'primary');
			getUrlParent('recipe_source.php?id=' + id, 'source');
			additional();
			break;
		case 'recipe_add.php':
			getUrl('recipe_categories.php', 'category');
			getUrl('recipe_secondary.php', 'secondary');
			getUrl('recipe_occasion.php', 'occasion');
			getUrl('recipe_source.php', 'source');
			break;
		case 'recipe_edit.php':
			id = document.getElementById('id').value;
			getUrl('recipe_categories.php?id=' + id, 'category');
			getUrl('recipe_secondary.php?id=' + id, 'secondary');
			getUrl('recipe_occasion.php?id=' + id, 'occasion');
			getUrl('recipe_source.php?id=' + id, 'source');
			break;
		case 'search_rec.php':
		case 'search_src.php':
		case 'index.php':
			searchInit();
			break;
		default:
	}

}

function putSelectTextIn(select, id) {
	
	$(id).value = $(select).options[$(select).selectedIndex].text;

}

function popup(url, size) {
	
	var width = 400;
	var height = 400;

	if(size == 'large') {
		var width = 820;
		var height = 600;
	}

	window.open(url,'','width=' + width + ',height=' + height + ',top=50,left=50,scrollbars=1');
}

function getUrl(url, update) {

	var output = $(update).empty().removeClass('hide');
	output.empty().addClass('ajax-loading');

	/**
	 * The simple way for an Ajax request, use onRequest/onComplete/onFailure
	 * to do add your own Ajax depended code.
	 */
	new Ajax(url, {
		method: 'get',
		update: output,
		onComplete: function() {
			output.removeClass('ajax-loading');
		}
	}).request();

}

function getUrlParent(url, update) {
	var output = $(window.opener.document.getElementById(update));
	output.empty().addClass('ajax-loading');

	new Ajax(url, {
		method: 'get',
		update: output,
		onComplete: function() {
			output.removeClass('ajax-loading');
		}
	}).request();
}

function submitForm(form, update, remove) {

	$(form).addEvent('submit', function(e) {
		/**
		 * Prevent the submit event
		 */
		new Event(e).stop();

		/**
		 * This empties the log and shows the spinning indicator
		 */
		var output = $(update).empty().removeClass('hide');
		output.empty().addClass('ajax-loading');
	 
		/**
		 * send takes care of encoding and returns the Ajax instance.
		 * onComplete removes the spinner from the log.
		 */
		this.send({
			update: output,
			onComplete: function() {
				output.removeClass('ajax-loading');
			}
		});

		if($(remove)) {
			$(remove).remove();
		}
	});

}

function conf(msg, url) {

	if(confirm(msg)) {

		window.location.href=url;

	} else {

		return false();

	}

}

function getUrlConf(url, update, conf) {

	if(confirm(conf)) {

		getUrl(url, update);

	}

}

function chgAct(form, url){
	$(form).action=url;
}

function show(id) {
	if($(id))
		$(id).removeClass('hide');
}

function hide(id) {
	if($(id))
		$(id).addClass('hide');
}

function updateWithSelect(id, sel) {
	$each($(sel).options, function(option){
		if (option.selected) { 
			$(id).value = option.text;
		}
	});
}

function appear(id, sel, pre) {
	
	var appear = pre + $(id).value;

	if($(sel).value != '') {
		hide(pre + $(sel).value);
	}

	if($(appear)) {
		$(sel).value = $(id).value;
		show(appear);
	}

}
