document.observe('dom:loaded', function() { 

	$$('.target-new').each(function(el) {
		el.writeAttribute('target', '_new');
	});
	
	$$('.target-blank').each(function(el) {
		el.writeAttribute('target', '_blank');
	});
 
	li = $$('#show-fade li');
	
	if (li.length > 1) {
		pf = new Protofade($('fader'), {
			autostart: true,
			updatefunction: function(slide) {
				li.each(function(el){
					el.removeClassName('sa');
				});
				
				li[slide].addClassName('sa');
			},
			duration: 2,
			delay: 20
		});

		li.each(function(el){
			el.observe('click', function() {
				cl = li.indexOf(el);
				pf.updateSlide(cl);
			});
		});
	}

	$$('#header .menu li').each(function(el) { 
		if (el.down('.tip-target') != undefined) {
			new Tip(el.down('.tip-target'), el.down('.tip-popup'), {
				style: 'reset',
				hook: { tip: 'topMiddle', target: 'bottomMiddle' },
				offset: { x: 0, y: -5 },
				showOn: 'mouseover',
				hideOn: false,
				hideAfter: 2,
				width: 160
			});
		}
	});


	ar = $$('#show-slide span');
	ar.each( function(el){
		el.observe('click', function(ev) {
			ar.each( function(el){
				el.removeClassName('sa');
			});
		
			el = ev.element();
			of = ar.indexOf(el);
			
			el.addClassName('sa');
			hc.scrollTo(of);
			clearInterval(tc); 
			tc = setInterval(automaticSlide, 12000);
		});
	});

	if ($$('#slider').length == 1) { 
		hc = new UI.Carousel('slider');
		tc = setInterval(automaticSlide, 12000);
	}
});

function updateControl(id) {
	xr = $$('#show-slide span');
	xr.each( function(el){
		el.removeClassName('sa');
	});

	xr[id].addClassName('sa');
}

function automaticSlide() {
	ta = $$('#show-slide span').indexOf($$('#show-slide .sa').first());

	if(($$('#show-slide span').length - 1) != ta)	{
		hc.scrollTo(ta + 1);
		updateControl(ta + 1);
		}
		
	else {
		hc.scrollTo(0);
		updateControl(0);
		}
}
