/* ===================================================
   ===================================================
   Hybrid Garden
   
   Rhys Burnie 2009
   
   DONT RUN SCRIPTS FROM HERE
   Run from head, intance classes to HG registry

*/

/* ===================================================
	Class: HG_tools
	
	Various enhancement for my own use.
	
*/
var HG_tools = Class({
	initialize: function() {
			if(typeof(Shadowbox)!='undefined') {
				
				// TODO: 	Look for local links
				//			Make 'em open in Shadowbox but make sure if
				//			a local link present in an open shadowbox
				//			it doesnt open new shadowbox just link within iframe
				
				// Look for edit links and make them open in Shadowbox
				var edit_links = $$('span.edit-post a');
				$each(edit_links, function(a,i) {
						a.store('index',i);
						a.addEvent('click', function(e) {
							new Event(e).stop();
							Shadowbox.open({
								player: 'iframe',
								content: this.get('href')
							});
						});
				});
			}
	}
});

/* ===================================================
	Class: Background
	
	Base class for future background work.
	
*/
var Background = Class({
		Implements: Options,
		options: {
			init: [
				'background-color',
				'background-image',
				'background-position',
				'background-repeat',
				'background-attachment'
			],
			onComplete: function() {
				arguments[0].showImage();
			}
		},
		
		initialize: function(el) {
				if(typeof(arguments[1])!='undefined') {
					this.setOptions(arguments[1]);
				}
				this.el = $(el);
				this.storeInitInfo();
				if(this.options.init.get('background-image')!='none') {
					this.el.setStyle('background-image','none');
					this.img = new Element('img').set('src',this.getSrc()).addEvent('load', function() {
						this.img_size = this.img.getSize();
						this.options.onComplete(this);
					}.bind(this)).setStyles({
						'position': 'absolute',
						'top': -9999,
						'left': -9999,
						'visibility': 'hidden'
					}).inject(document.body);
				}else{
					this.onComplete();
				}
		},
		
		storeInitInfo: function() {
			var h = {};
			$each(this.options.init,function(k) {
				h[k] = this.el.getStyle(k);
			},this);
			this.options.init = $H(h);
		},
		
		getSrc: function() {
			return $chk(this.src) ? this.src : this.src = this.options.init.get('background-image').replace('url(','').replace(')','');	
		},
		
		getBGpos: function() {
			var p = this.options.init.get('background-position').split(' ');
			return {x: p[0], y: p[1]};
		},
		
		showImage: function() {
			this.el.setStyle('background-image','url('+this.getSrc()+')');	
		}
});