// Javascript document

(function() {
var CONF = window.CONF = window.CONF || {};
function dt(t){return document.createTextNode(t)};
CONF.ROOT = ((jQuery('head > script[src$=/script.js]').attr('src') || '').match(/^(.*)js[0-9]*\/script\.js$/)||[])[1] ||
			((jQuery('script[src$=/script.js]').attr('src') || '').match(/^(.*)js[0-9]*\/script\.js$/)||[])[1] || '/';
CONF.STYLES = ((jQuery('head > link[href$=/style.css]').attr('href') || '').match(/^\/(.*style[0-9]*\/)style\.css$/)||[])[1] ||
				((jQuery('link[href$=/style.css]').attr('href') || '').match(/^\/(.*style[0-9]*\/)style\.css$/)||[])[1];
console.info('detected ROOT: '+ CONF.ROOT);

function	addButton(id, text, func) {
	var buttons = jQuery('#Buttons');
	if (!buttons.length)
		buttons = jQuery('<span id="Buttons"></span>').appendTo('#Head');
	else
		buttons.append(dt(' | '));
	return jQuery('<a id="'+id+'" href="/'+text+'" title="'+text+'">'+text+'</a>').click(func).appendTo(buttons);
}

jQuery(function() {
	var list = jQuery('#List');
	if (list.length) {
		var a = addButton('Diaporama', 'Diaporama', function() {
			jQuery.loadComponent([CONF.ROOT+'js/library/jquery.slideshow.js'], function() {
				list.slideshow({button: a});
			});
			return false;
		});
		jQuery.loadComponent(CONF.ROOT+'js/jquery.itemview.js', function() {
			list.itemview();
		});
	}
	
	var item = jQuery('#Item');
	if (item.length) {
		jQuery.loadComponent(CONF.ROOT+'js/item.js', function() {
			item.editable();
		});
	}
	if (jQuery.cookie('admin')=='yes')
		admin();

	var debug = jQuery('<span class="debug">debug</span>').appendTo();
	var foot = jQuery('#Foot');
	var body = jQuery('body:first');
	foot.append(debug);
	var jwin = jQuery(window);
	var prev = '';
	var orientation = '';
	jwin.resize(function() {
		var h = jwin.height();
		var w = jwin.width();
		if (h < 450) {
			if (prev != 'tiny')
				body.removeClass('small large').addClass((prev = 'tiny'));
		}
		else if (h < 700) {
			if (prev != 'small')
				body.removeClass('tiny large').addClass((prev = 'small'));
		}
		else {
			if (prev != 'large')
				body.removeClass('tiny small').addClass((prev = 'large'));
		}
		if (w > h) {
			if (orientation != 'landscape')
				body.removeClass('portrait').addClass((orientation = 'landscape'));
		}
		else {
			if (orientation != 'portrait')
				body.removeClass('landscape').addClass((orientation = 'portrait'));
		}
		debug.html(jwin.width()+' x '+jwin.height()+' => '+prev+' / '+orientation);
//		jQuery.cookie('ss', jwin.width()+'x'+jwin.height());
//		console.info('ss: '+jwin.width()+'x'+jwin.height());
	}).resize();
});

})();
