

/********** tell a friend *************/
window.form_friend_options = {
	duration:		700,
	transition_types:	['morph', 'fade'], // morph, fade
	overlay:		{ color: '#000000', opacity: 0.5, close_on_click: false }
};


/********** optin *************/
window.form_subscribe_options = {
	duration:		700,
	transition:		Fx.Transitions.Cubic.easeOut,
	transition_types:	['morph', 'fade'], // morph, fade
	overlay:		{ color: '#000000', opacity: 0.5, close_on_click: true }
};

/******** gallery **********/
window.gallery_options = {
	overlay: 	{ color: '#000', opacity: 0.6, close_on_click: false },
	background: 	'#fff',
	fontcolor:	'#444',
	border:		'solid gray 1px',
	padding:	'10px',
	image_dir:	'gallery-light',
	fade_duration:	500,
	morph_duration:	400,
	enable_slideshow: true,
	slideshow_delay: 7000
};


window.addEvent('domready', function() {
	// load menus
/*
	Asset.javascript('assets/js/class/Menus.js', {
		onload: function() {
			window.MainMenu = new Menus('mainmenu');
		}
	});
*/

	Asset.javascript('assets/js/class/CJMenu.js', {
		onload: function() {
			window.MainMenu = new CJMenu('mainmenu');
		}
	});

	// load cart widget
	Asset.javascript('assets/js/class/ShopCart.js', {
		onload: function() {
			window.Cart = new ShopCart({
				anchor: 'btnCart',
				align: 'center',
				width: 320
			});
		}
	});

	// methods for db forms
	Asset.javascript('assets/js/forms.js');

	// tree view
	Asset.css('assets/css/TreeView.css');
	Asset.javascript('assets/js/class/TreeView.js', {
		onload: function() {
			new TreeView('tradedocs', {
				onOverFunction: function() { }
			});
		}
	});


	// buttons
	try { new Buttons(); } catch(e) {};
});


window.addEvent('domready', function() {

	// fade in rollovers
	$$('img.fade').each(function(el) {
		var cfg;
		try { cfg = JSON.decode(el.getAttribute('fade'));  } catch(e) { cfg = {}; };
		if(!cfg) cfg = {};

		if(!cfg.duration) cfg.duration = 300;
		if(!cfg.from) cfg.from = 0.5;
		if(!cfg.to) cfg.to = 1;

		el.setStyle('opacity', cfg.from);
		el.store('fadeConfig', cfg);

		el.addEvents({
			'mouseover': function() {
				var cfg = this.retrieve('fadeConfig');
				new Fx.Tween(this, { duration: cfg.duration }).start('opacity', cfg.to);
			},
			'mouseout': function() {
				var cfg = this.retrieve('fadeConfig');
				new Fx.Tween(this, { duration: cfg.duration }).start('opacity', cfg.from);
			}
		});
	});



	// popImage
	$$('a.popImage').addEvent('click', function() {
		popImage(this.href);
		return false;
	});

});


window.addEvent('load', function() {
	// handle rollover images
	$$('img.rollover,input.rollover').each(function(el) {
		var pimg = new Image();
		pimg.src = el.src.replace(/\.([^\.]+)$/, '-over.$1');

		el.addEvents({
			mouseover: function() {
				this.src = this.src.replace(/\.([^\.]+)$/, '-over.$1');
			},
			mouseout: function() {
				this.src = this.src.replace(/-over/, '');
			}
		});
	});

});



function popImage(imgHref) {
	div = $('_divPop');
	if(!div) div = new Element('div', {
		id: '_divPop',
		'class': 'popupForm',
		styles: {
			display: 'none',
			top: 0,
			left: 0
		}
	}).inject($(document.body));

	div.empty();
	div.setStyles({
		width: 200,
		height: 150,
		left: ($(document.body).getWidth() / 2) - 100 + $(document.body).getScroll().x,
		top: ($(document.body).getHeight() / 2) - 75 + $(document.body).getScroll().y,
		display: 'block',
		opacity: 0,
		overflow: 'hidden'
	});

	new Element('img', {
		src: '/assets/images/loading-small.gif',
		styles: {
			width: 24,
			height: 24,
			display: 'block',
			margin: 'auto',
			marginTop: div.getHeight() / 2 - 12
		}
	}).inject(div);


	var c = new Chain();
	c.chain(function() {
		new Fx.Tween(div, {
			duration: 200,
			onComplete: function() {
				c.callChain();
			}
		}).start('opacity', 1);
	}).chain(function() {
		new Element('img', {
			src: imgHref,
			events: {
				load: function() {
					c.callChain(this);
				}
			}
		});

	}).chain(function(img) {
		div.empty();
		img.setStyle('opacity', 0);
		img.inject(div);

		var to = {
			width: div.getScrollWidth(),
			height: div.getScrollHeight()
		};
		to.top = ($(document.body).getHeight() / 2) - (to.height/2) + $(document.body).getScroll().y;
		if(to.top < 0) to.top = 0;

		to.left = ($(document.body).getWidth() / 2) - (to.width/2) + $(document.body).getScroll().x;
		if(to.left < 0) to.left = 0;

		new Fx.Morph(div, {
			onComplete: function() {
				new Fx.Tween(img, {
					duration: 100
				}).start('opacity', 1);


				var _d = new Element('div', { styles: { position: 'relative' } }).inject(div);

				new Element('img', {
					src: '/assets/images/slideshow/closebox.png',
					styles: {
						position: 'absolute',
						zIndex: 100,
						top: (to.height * -1) + 2,
						left: to.width - 32,
						cursor: 'pointer'
					},
					events: {
						click: function() {
							if( $('_divPop') ) {
								new Fx.Tween('_divPop', {
									duration: 150,
									onComplete: function() {
										$('_divPop').destroy();
									}
								}).start('opacity', 0);
							}
						}
					}
				}).inject(_d);
			}
		}).start({
			width: to.width,
			height: to.height,
			top: to.top,
			left: to.left
		});

	}).chain(function() {

	}).chain(function() {

	}).callChain();


}


