if(document.images){
	p = new Image();
	p.src = "/images/loading_form.gif";
	p = new Image();
	p.src = "/images/loading_black.gif";
	p = new Image();
	p.src = "/images/tip.gif";
	p = new Image();
	p.src = "/images/tip.png";
	p = new Image();
	p.src = "/images/tipbot.gif";
	p = new Image();
	p.src = "/images/tipbot.png";
	p = new Image();
	p.src = "/images/tiptop.gif";
	p = new Image();
	p.src = "/images/tiptop.png";
}

var current=0;
var periodical;

var aboutTip = function(){
	var tips = new Tips('div.aside_about img',{
		className: 'abouttip',
		fixed: true,
		offsets: {
			'x': -170,
			'y': 230
		},
		hideDelay: 50,
		showDelay: 50
	});
};

var popupTip = function(){
	var content=Array();
	var popups = $$('span.popup');
	popups.each(function(popup, p){
		popup.setStyle('display', 'none');
		content[p]=popup.innerHTML;
	});
	
	var links = $$('a.popup');
	links.each(function(link, p) {
		link.store('tip:text', content[p]);
		link.addEvent('click', function(){
			return false;
		});
	});

	var tips = new Tips('a.popup',{
		className: 'popuptip',
		fixed: false,
		offsets: {
			'x': 16,
			'y': 16
		},
		hideDelay: 50,
		showDelay: 50
	});
};



var sliderControl = function(links, divs, index){
	var toggles = $$(links);
	var content = $$(divs);
	var AccordionObject = new Accordion(toggles, content, { 
		onActive: function(toggler, element) {
			toggler.addClass('active');
		},
		onBackground: function(toggler, element) {
	        toggler.removeClass('active');
		},
		opacity: true,
		alwaysHide: true,
		show: index
	});
};

var postForm = function(formid, url){
	$(formid).addEvent('submit', function(e) {
		e.stop();
		var form = $(formid).addClass('loading');
		
		this.set('send', {
			onComplete: function(response) { 
				form.removeClass('loading');
				form.set('html', response);
			}
		});

		this.send('/inc/'+url);
	});
};

var postFormChange = function(formid, url){
	var form = $(formid).addClass('loading');
	
	form.set('send', {
		onComplete: function(response) { 
			form.removeClass('loading');
			form.set('html', response);
		}
	});

	form.send('/inc/'+url+'?change=1');
};

var galleryItems = function(id, file){
	$$('#'+id+'list a').addEvent('click', function(event) { event = new Event(event).stop();
		gid=this.rel;
		new galleryItem(gid, id, file);
		
		$$('#'+id+'list a').removeClass('active');
		
		this.addClass('active');
	});
};

var galleryItem = function(gid, id, file){
	var div=$(id);
	var url = '/inc/'+file;
	var loading = $('loading');
	
	div.fade(0, 0);
	loading.addClass('loading');
			
	var myRequest = new Request({
		url: url,
		method: 'get',
		onComplete: function(response) {			
			div.set({'html': response});
			div.fade(0, 1);	
			loading.removeClass('loading');
		}
	}).send('gid='+gid);
};

var loadImageItems = function(listclassid, file){
	$$('ul.'+listclassid+' a').addEvent('click', function(event) { event = new Event(event).stop();
		new loadImage(this.rel, listclassid, file);
		
		$$('ul.'+listclassid+' a').removeClass('active');
		
		this.addClass('active');
	});
};

var loadImage = function(rel, id, file){
	var div=$(id);
	var url = '/inc/'+file;
	
	div.fade(0, 0);
			
	var myRequest = new Request({
		url: url,
		method: 'get',
		onComplete: function(response) {			
			div.set({'html': response});
			div.fade(0, 1);	
		}
	}).send('gid='+rel);
};

var firstimage=0;
var photoChange = function(id){	
	if(firstimage==0){
		$('fade'+current).setStyle('z-index', '10');
		$('fade'+current).fade(1, 0);
		
		if(id==1){
			$('text').fade(1, 0);
		}
				
		current=id;
			
		$('fade'+current).setStyle('z-index', '20');
		$('fade'+current).fade(0, 1);
		
		if(id==0){
			$('text').fade(0, 1);
		}
	}	
};

var fadeImages = function(){
	var current=0;
	
	var timer = 6;
	
	var images = $$("div.images img");
	
	var max=(images.length-1);
	
	if(max>0){
		images.each(function(image, i) {
			image.setStyle('opacity', (i==0 ? 1 : 0));
			image.set('id', 'fade'+i);
		});
		
		var refresh = (function() {
			current=(current==max ? -1 : current);
			current++;
			
			if(current==1 && firstimage==0){
				firstimage=1;
				current=0;
			}else{
				firstimage=0;
			}
			
			new photoChange(current);
		});
	
		periodical = refresh.periodical(timer * 1000, this);
	}
};

function website(url){
	popupWindow = window.open(url, '_blank','');
	popupWindow.focus();
}

var external = function(){
	var anchors = $$("a");
	anchors.each(function(anchor, i) {
		var relAttribute = String(anchor.getAttribute('rel'));
		if (anchor.id=='print'){
			anchor.onclick = function() {
				print();
				return false;
			};
		}else if (anchor.getAttribute('href') && (relAttribute=='external')){
			anchor.onclick = function() {
				website(this);
				return false;
			};
		}
	});
};

window.addEvent('domready', function(){
	new external();
});