Pelster = {};

Pelster.Navigation = Class.create();
Pelster.Navigation.prototype = {
	initialize: function(klass) {
		this.klass = klass;
		this.items = $$('.' + this.klass);
		
		this.observe();
	},
	
	observe: function() {
		this.items.each(function(item) {
			Event.observe(item, 'click', function() { 
				elem = item.getAttribute('sid');
				
				Element.toggle(elem);
			});
		});
	}
}

Pelster.Search = Class.create();
Pelster.Search.prototype = {
	initialize: function(input, submit, klass) {
		this.elem = document.getElementsByName('search')[0];
		this.subm = $(submit);
		this.klass = klass;
		this.def = this.elem.value;
		
		this.observe();
	},
	
	observe: function() {
		if(this.elem.value == this.def || this.elem.value == '') {
			//this.subm.disabled = true;
		}
		
		Event.observe(this.elem, 'focus', function() {
			if(this.elem.value == this.def) {
				this.elem.value = '';
				this.elem.className = '';
				
				//this.subm.disabled = false;
			}
		}.bind(this));
		
		Event.observe(this.elem, 'keypress', function() {
			if(!this.elem.value == this.def || !this.elem.value == '') {
				this.subm.disabled = false;
			} else {
				//this.subm.disabled = true;
			}
		}.bind(this));
		
		Event.observe(this.elem, 'blur', function() { 
			if(this.elem.value == '') {
				this.elem.value = this.def;
				this.elem.className = this.klass;
				//this.subm.disabled = true;
			}
		}.bind(this));
	}
}

Pelster.Thumbs = Class.create({
	initialize: function(klass, target) {
		this.thumbs = $$(klass);
		this.target = $(target);
		this.observe();
		this.makelink();
	},
	
	observe: function() {
		this.thumbs.each(function(t) {
			Event.observe(t, 'click', function() { 
				src = t.src.split('&src=')[1];
				this.target.src = '/photowh.php?w=345&h=345&src=' + src;
			}.bind(this));
		}.bind(this));
	},
	
	makelink: function() {
		this.thumbs.each(function(t) {
			Event.observe(t, 'click', function() {
				src = t.src.split('&src=')[1];
				this.parent = document.getElementById('prod_image_wrapper');
				this.parent.innerHTML = '<a href="/photowh.php?w=800&h=600&src=' + src + '" target="_self" rel="lightbox"><img src="/photowh.php?w=345&h=345&src=' + src + '" style="border:0;"></a>';
			}.bind(this));
		}.bind(this));
	}
});

Pelster.Rental = Class.create({
	initialize: function(target, attr, submit) {
		this.target = $A(document.getElementsByTagName(target));
		this.submit = $(submit);
		this.attr = attr;
		this.targ = [];
		
		this.parse();
	},
	
	parse: function() {
		this.observeHandler = this.observe.bindAsEventListener(this);
		this.parseHandler = this.dateParse.bindAsEventListener(this);
		
		this.err = false;
		
		this.i = 0;
		this.target.each(function(e) {
			if(e.getAttribute(this.attr)) {
				this.targ[this.i] = e;
				
				Event.observe(e, 'click', this.observeHandler);
				Event.observe(e, 'blur', this.parseHandler);
				
				this.i++;
			}
		}.bind(this));
		
		/*Event.observe(this.submit, 'click', function() { 	
			this.target.each(function(e) {
				if(e.getAttribute(this.attr)) {
					alert(e.value);
				}
			}.bind(this));
			
		}.bind(this));*/
		
		
		/*Event.observe('formDate', 'submit', function() { alert('submit'); return false; });
		
		$('formDate').onsubmit = function() { 	
			this.target.each(function(e) {
				if(e.getAttribute(this.attr)) {
					if(e.value.match(/--/gi) || e.value.match(/bijv/gi) || e.value.match(/nan/gi)) {
						this.err = true;
						
						e.style.cssText = 'border:1px solid red; padding:2px';
					}
				}
			}.bind(this));
			
			if(this.err) {
				return false;
			}
		}.bind(this);*/
	},
	
	observe: function(evnt) {
		this.elem = Event.element(evnt);
		this.val = this.elem.value;
		
		if(this.val.match(/bijv/gi)) {
			this.elem.value = '';
		}
	},
	
	dateParse: function(evnt) {
		this.elem = Event.element(evnt);
		this.val = this.elem.value;
		
		if(this.val != '' && this.elem.getAttribute('cid') != 'e_date') {
			this.elem.value = this.val.replace(/^(0[1-9]|[1-2][0-9]|3[01])(-|)(01|02|03|04|05|06|07|08|09|10|11|12)(-|)(20\d\d)$/, '$1-$3-$5');
			
			this.date = this.elem.value.split('-');
			this.d = parseInt(this.date[0]);	this.m = this.date[1];	this.y = parseInt(this.date[2]);
			
			this.elem.value = this.d + '-' + ((this.m < 10 && !this.m.match(/^0/)) ? '0' + this.m : this.m) + '-' + this.y;
			
			if((parseInt(this.m) + 1) > 12) {
				this.m = '01';
				this.y = 2011;
			} else {
				this.m = ((this.m == '09') ? 9 + 1 : parseInt(this.m) + 1);
				this.m = ((this.m < 9) ? '0' + this.m : this.m);
			}
	
			this.date = this.d + '-' + this.m + '-' + this.y;
			this.targ[1].value = this.date;
		} else if(this.elem.getAttribute('cid') == 'e_date') {
			this.date = this.val.split('-');
			this.odate = this.targ[0].value.split('-');
			
			if(this.date[2] == this.odate[2] && this.date[1] <= this.odate[1]) {
				this.date[1] = parseInt(this.odate[1]) + 1;
			}
			
			this.date = this.date[0] + '-' + this.date[1] + '-' + this.date[2];
			this.elem.value = this.date;
		}
	}
});

document.observe('dom:loaded', function() {
	//new Madeko.Navigation('sub_nav');
	new Pelster.Search('search', 'submit', 'italic');
	new Pelster.Thumbs('.thumb', 'image');
	//new Madeko.Rental('input', 'cid', 'setDate');
});
