// diplomafoto

function dflog(text) {
//	if (typeof(console) != 'object') {
//		return;
//	}
//	if (typeof(console.log) != 'function') {
//		return;
//	}
//	console.log(text);
}



function LiteBox(id) {
	this.show = function() {
		if (this.idiot_explorer) {
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}

		background = new Element('div', {
			'id': this.id +'_bg',
			'class': 'litebox_bg'
			});
		background.setStyle('opacity: 0.8');
		background.setStyle('z-index: 90001');
		$('bodyid').insert({bottom: background});

		var box = new Element('div', {
			'id': this.id,
			'class': 'litebox'
			});
		//box.setStyle('...');
		box.setStyle('z-index: 90002');
		box.update('betöltés...');
		$('bodyid').insert({bottom: box});
		this.check_size();
		this.setposition();
	};
	this.update = function(content) {
		$(this.id).update(content);
	};
	this.observe_bg_click = function(f) {
		$(this.id +'_bg').observe('click', f);
	};
	this.remove = function() {
		if (this.idiot_explorer){
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
			this.setScroll(0, this.yPos);
		}
		$(this.id).remove();
		$(this.id +'_bg').remove();
	};
	this.check_browser = function() {
		var browser;

		browser = navigator.userAgent;
		if (/msie/i.test(browser)) {
			this.idiot_explorer = 1;
		}
	};
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	this.prepareIE = function(height, overflow) {
		var bod, htm;

		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow;
	};
	// In IE, select elements hover on top of the lightbox
	this.hideSelects = function(visibility) {
		var selects, i;

		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	};
	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	this.getScroll = function() {
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	};
	this.setScroll = function(x, y) {
		window.scrollTo(x, y);
	};
	this.check_size = function() {
		var root = document.viewport.getDimensions();
		var box = $(this.id).getDimensions();
		if (root.height < box.height) {
			$(this.id).setStyle('height:'+root.height+'px');
		}
		if (root.width < box.width) {
			$(this.id).setStyle('width:'+root.width+'px');
		}
	};
	this.setposition = function() {
		var root = document.viewport.getDimensions();
		var box = $(this.id).getDimensions();
		var left, top;
		if (box.width >= root.width)
			left = 0;
		else
			left = (root.width - box.width) / 2;
		left = left.toFixed(0);
		if (box.height >= root.height)
			top = 0;
		else
			top = (root.height - box.height) / 2;
		top = top.toFixed(0);
		$(this.id).setStyle('left: '+left+'px; top: '+top+'px');
	};

	this.id = id;
	this.yPos = 0;
	this.idiot_explorer = 0;
	this.check_browser();
}



function FotoBox() {
	var me = this; // for callbacks
	this.close = function(ev) {
		me.litebox.remove();
		if (me.basket_changed) {
			if ($('basket_table')) {
				location.reload();
			} else {
				new Ajax.Updater('basket_info',
					'/ajax/basket/info.html', {
					method: 'get'
					});
			}
		}
	};
	this.on_foto_load = function(transport) {
		me.basket_loaded = 0;
		me.basket_changed = 0;
		//$('fotobox').update(transport.responseText);
		me.litebox.update(transport.responseText);
		$('fotobox_close').observe('click', me.close);
		$('fotobox_order').observe('click', me.show_order);
		$$('#fotobox .fotobox_nav').each(function(el) {
			dflog('nav button'+ el.identify());
			el.observe('click', me.on_nav_click);
			el.setStyle('cursor: pointer;');
		});
	};
	this.on_nav_click = function(ev) {
		var id, match;

		id = $(ev.element()).identify();
		if (match = /fotobox_nav_([a-z0-9_.-]+)/.exec(id)) {
			me.foto = match[1];
			new Ajax.Request('/ajax/foto', {
				method: 'get',
				parameters: { foto: me.foto },
				onSuccess: me.on_foto_load,
				onFailure: me.close
				});
		}
	};
	this.on_basket_load = function(transport) {
		var data, inp;
	
		dflog('on_basket_load');

		$$('#fotobox_orderbox .fotobox_qty').each(function(el) {
			el.enable();
			el.value = '0';
		});

		try {
			data = transport.responseText.evalJSON();
		} catch(e) {
			return;
		}
		
		for (i = 0; i < data.list.length; i++) {
			if (data.list[i].foto != me.foto)
				continue;
			inp = $('fotobox_qty_'+ data.list[i].format);
			if (!inp)
				continue;
			inp.value = data.list[i].qty;
		}
		me.basket_loaded = 1;
		$$('#fotobox_orderbox .fotobox_qty_incr').each(function(el) {
			el.observe('click', me.on_incr);
		});
	};
	this.show_order = function() {
		if (!me.basket_loaded) {
			new Ajax.Request('/ajax/basket/list', {
				method: 'get',
				onSuccess: me.on_basket_load,
				onFailure: me.close
				});
		}
		$('fotobox_orderbox').show();
		$('fotobox_orderbox_close').observe('click', me.hide_order);
		$$('#fotobox_orderbox .fotobox_qty').each(function(el) {
			el.observe('change', me.on_change);
		});
	};
	this.hide_order = function() {
		$('fotobox_orderbox').hide();
		// XXX stop observe
	}
	this.on_incr = function(ev) {
		var el, input, qty;

		el = $(ev.element());
		input = el.adjacent('.fotobox_qty')[0];
		if (!input)
			return;
		qty = parseInt(input.value) + 1;
		me.set_qty_value(input, qty);
	};
	this.on_change = function(ev) {
		var inp, format, qty;

		inp = ev.element();
		qty = inp.value;
		me.set_qty_value(inp, qty);
	}
	this.set_qty_value = function(inp, qty) {
		var format;

		qty = qty.toString();
		me.basket_changed = 1;

		format = inp.name;
		qty = qty.replace(/^0+/g, '');
		qty = qty.replace(/\D/g, '');
		if (qty == '')
			qty = '0';
		if (qty > 99) {
			qty = '99';
		}
		inp.value = qty;

		new Ajax.Request('/ajax/basket/set', {
			method: 'post',
			parameters: {
				foto: me.foto,
				format: format,
				qty: qty
				}
			});
	};
	this.show = function(foto) {
		var background, box;

		if ($('fotobox')) {
			alert('1 foto max');
			return false;
		}

		this.foto = foto;
		this.litebox = new LiteBox('fotobox');
		this.litebox.show();
		this.litebox.observe_bg_click(this.close);

		new Ajax.Request('/ajax/foto', {
			method: 'get',
			parameters: { foto: this.foto },
			onSuccess: this.on_foto_load,
			onFailure: this.close
			});
	}

	// constructor
	this.foto = '';
	this.litebox = 0;
	this.basket_loaded = 0;
	this.basket_changed = 0;
}



function OrderPage() {
	me = this;
	this.on_load = function() {
		$$('.order_input').each(function(inp) {
			inp.observe('change', me.on_change);
			//inp.enable();
		});
	}
	this.on_change = function(ev) {
		var inp, name, value;

		inp = ev.element();
		name = inp.name;
		value = inp.value;
		//inp.setStyle('background: yellow;');
		//inp.disable();
		new Ajax.Request('/ajax/set', {
			method: 'post',
			parameters: {
				name: name,
				value: value
				},
			onSuccess: function() {
				//inp.enable();
				inp.setStyle('background: #e6ffe6;');
			},
			onFailure: function() {
				//inp.enable();
				inp.setStyle('background: #ffe8e8;');
			}
			});
	}

	// constructor
	var match;
	if (!(match = /\/order\/(\d+)$/.exec(document.URL))) {
		return;
	}
	this.page = match[1];
	this.on_load();
}



function AlbumPage() {
	me = this;
	this.on_load = function() {
		var fl, id;

		fotobox = new FotoBox;

		fl = $$('.album_foto');
		fl.each(function(el) {
			el.observe('click', me.on_foto_click);
			el.setStyle('cursor: pointer;');
		});

//		me.fotolist = new Array;
//		fl.each(function(el) {
//			id = el.identify();
//			if (match = /album_foto_([a-z0-9_.-]+)/.exec(id)) {
//				me.fotolist.push(match[1]);
//			}
//		});
	};
//	this.prev_foto = function(cur) {
//		var f, prev;
//
//		prev = '_first';
//		for (f = 0; f < me.fotolist.length; f++) {
//			if (cur == f) {
//				return prev;
//			}
//			prev = f;
//		}
//		return '_notfound';
//	}
	this.on_foto_click = function(ev) {
		var el, id, dom_tree, a, match;

		el = $(ev.element());
		dom_tree = el.ancestors();
		for (a = 0; a < dom_tree.length; a++) {
			if (dom_tree[a].hasClassName('album_foto')) {
				el = dom_tree[a];
				break;
			}
		}
		id = el.identify();
		dflog('click: id='+ id);
		if (match = /album_foto_([a-z0-9_.-]+)/.exec(id)) {
			dflog('fotobox: foto='+ match[1]);
			//new FotoBox(match[1]).show();
			fotobox.show(match[1]);
		}
	};
}



function BasketClear() {
	me = this;
	this.on_load = function(transport) {
		me.litebox.update(transport.responseText);
		$('basketclear_yes').observe('click', me.on_yes);
		$('basketclear_no').observe('click', me.close);
	};
	this.close = function() {
		me.litebox.remove();
	};
	this.on_yes = function() {
		new Ajax.Request('/ajax/basket/clear', {
			method: 'get',
			onSuccess: function() {
				location.reload();
			},
			onFailure: function() {
				me.box.update('error');
				//location.reload();
			}
			});
	};

	this.litebox = new LiteBox('basketclear_box');
	this.litebox.show();
	this.litebox.observe_bg_click(this.close);
	
	new Ajax.Request('/ajax/basket/clear.html', {
		method: 'get',
		onSuccess: this.on_load,
		onFailure: this.close
		});
}



var albumpage;
var fotobox;

Event.observe(window, 'load', function() {
	if (/\/order\/(\d+)$/.test(document.URL)) {
		dflog('order page!');
		new OrderPage();
	}
	//if (/\/album\//.test(document.URL)) {
	//	//dflog('album page!');
	//	albumpage = new AlbumPage();
	//	albumpage.on_load();
	//}
});

function album_init() {
	albumpage = new AlbumPage();
	albumpage.on_load();
}

