var is_loading = false;

function ajaxify_request(ref) {
	re = /^([^\?]*)/;
	uri = 'ajax_collection_list.php'+ref.replace(re, '');
	var aturn = new Fx.Tween('collection-content',{
		property:'opacity',
		link:'chain'
	});
	new Request({
		method: 'get', 
		url: uri,
		encoding: 'utf-8',
		onRequest:function() {
			// called when calling: fade out
			aturn.start(1,0);
			ajaxify_load();
		},
		onComplete:function() {
			// called right before success
			ajaxify_unload();
		},
		onSuccess:function(res) {
			$('collection-content').set('html',res);
			ajaxify_links();
			// fade in
			aturn.start(0,1);
		},
		onFailure:function() {
			$('collection-content').set('html','<p class="empty">Uh oh something went wrong, please reload the page</p>');
		}
	}).send();
}

function ajaxify_item(id) {
	uri = 'ajax_collection_item.php?id='+id;
	new Fx.Tween('collection-ajax', {
		property:'opacity',
		onComplete:function() {
			new Request({
				method: 'get', 
				url: uri,
				onRequest:function() {
					// called when calling: fade out
					ajaxify_load();
				},
				onComplete:function() {
					// called right before success
					ajaxify_unload();
				},
				onSuccess:function(res) {
					$('collection-ajax').set('html',res).tween('opacity',1);
					ajaxify_thumbs();
				},
				onFailure:function() {
					alert('Uh oh something went wrong, please try again');
					$('collection-ajax').destroy();
					$('collection-content').tween('opacity',1);
				}
			}).send();
		}
	}).start(0);
}

function ajaxify_categories() {
	if (lst = $$('#collection-menu a')) {
		lst.each(function(el) {
			el.addEvent('click', function(e) {
				e.stop();
				close_item(0);
				$$('#collection-menu li').each(function(em) {
					em.removeClass('current');
				});
				ajaxify_request(this.get('href'));
				em = this.getParent('li');
				em.addClass('current');
			});
		});
	}
}

function ajaxify_links() {
	/* pagination */
	if (lst = $$('#product-page a')) {
		lst.each(function(el) {
			el.addEvent('click', function(e) {
				e.stop();
				ajaxify_request(this.get('href'))
			});
		});
	}
	/* products */
	if(lst = $$('#product-list a')) {
		lst.each(function(el) {
			el.addEvent('click', function(e) {
				e.stop();
				re = /^([^\?]*)/;
				uri = 'ajax_collection_item.php'+this.get('href').replace(re, '');
				new Request({
					method: 'get', 
					url: uri,
					onRequest:function() {
						// called when calling: fade out
						$('collection-content').tween('opacity',0);
						ajaxify_load();
					},
					onComplete:function() {
						// called right before success
						ajaxify_unload();
					},
					onSuccess:function(res) {
						ct = new Element('div',{'id':'collection-ajax'});
						ct.set('opacity',0);
						ct.set('html',res);
						ct.inject('global','top');
						ct.tween('opacity',1);
						ajaxify_thumbs();
					},
					onFailure:function() {
						alert('Uh oh something went wrong, please try again');
						$('collection-content').tween('opacity',1);
					}
				}).send();
			});
		});
		
	}
}
function ajaxify_thumbs() {
	if (lst = $$('.pic-thb')) {
		lst.each(function(el,idx) {
			el.addEvent('click',function() {
				changeimg(idx+1);
			});
		});
	}
}
function close_item(def) {
	if ($('collection-ajax')) {
		if (def == 1) {
			$('collection-content').set('opacity',1);
		}
		new Fx.Tween('collection-ajax', {
			property: 'opacity',
			onComplete:function() {
				$('collection-ajax').destroy();
			}
		}).start(1,0);
	}
}
function changeimg(id) {
	el = $('pic-thb'+id);
	$('pic-big').set('src', el.get('src'));
	for(i=1;i<=3;i++) {
		if (eo = $('pic-thb'+i)) {
			if (id == i) {
				eo.addClass('current');
			} else {
				eo.removeClass('current');
			}
		}
	}
}
window.addEvent('domready',function() {
	/* if (el = $('collection')) {
		new Element('a',{
			'id':'download',
			'href':'http://www.thierrymartino.com/pdf/harley-davidson.pdf',
			'target':'_blank',
			'opacity':0
		}).set('text','download PDF catalog')
		.inject(el,'before');
	} */
	new Asset.images(['img/'+bgimg], {
		'onComplete':function() {
			window.setTimeout('showtime()',200);	
		}
	});
	if ($('accordion')) {
		new Accordion($('accordion'), 'h3.toggler', 'div.info', {
			display: false,
			opacity: true,
			alwaysHide: true,
			onActive: function(toggler, element){
				toggler.addClass('active');
				element.setStyle('margin-bottom','15px');
			},
			onBackground: function(toggler, element){
				toggler.removeClass('active');
				element.setStyle('margin-bottom','0'); 
			}
		});
	}
	ajaxify_categories();
	ajaxify_links();
	ajaxify_thumbs();
});
function showtime() {
	el = new Element('img',{'id':'photo','src':'img/'+bgimg,'opacity':0}).inject('global','before');
	new Fx.Tween(el, {
		property:'opacity',
		duration:'long',
		onComplete:function() {
			if (e2 = $('download')) {
				new Fx.Tween(e2, {
					'transition': Fx.Transitions.Bounce.easeOut,
					'duration':'long',
					'property':'opacity'
				}).start(0,1);
				/*
				e2.set('tween', {duration: 1000});
				e2.tween('opacity', 1);
				*/
			}
		}
	}).start(0,1);
}
function ajaxify_load(t) {
	is_loading = window.setTimeout('ajaxify_load_div()',1000);
}
function ajaxify_load_div() {
	new Element('div',{'id':'collection-load'}).inject('global','top');
}
function ajaxify_unload() {
	window.clearTimeout(is_loading);
	if (el = $('collection-load')) {
		el.destroy();
	}
}