if(document.images){
	preloadImage = new Image();
	preloadImage.src = "/images/loading.gif";
	preloadImage = new Image();
	preloadImage.src = "/images/news_loading.gif";
}

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 postFormChangeClear = function(formid, url, selecttoclear){
	var form = $(formid).addClass('loading');
	if($(selecttoclear)){
		$(selecttoclear).selectedIndex=0;
	}
	
	form.set('send', {
		onComplete: function(response) { 
			form.removeClass('loading');
			form.set('html', response);
		}
	});

	form.send('/inc/'+url+'?change=1');
};

var ajaxSnippet = function(ajax_links, file){
	var div;
	var div_loading;
	var url = '/inc/'+file;
	
	var snippets = $$(".news h2 a");
	snippets.each(function(snippet, i) {
		snippet.addEvent("click", function(e) {
			new Event(e).stop();
			
			div = $(ajax_links+'_'+this.rel);
			
			div_loading=$('item'+this.rel).addClass('newsloading');
			
			div.fade(0, 0);
			
			var myRequest = new Request({
				url: url,
				method: 'get',
				onComplete: function(response) {
					div.set({'html': response});
					
					this.scroll = new Fx.Scroll(window);
					this.scroll.toElement(div_loading);
					
					external();
										
					div.fade(0, 1);
					div_loading.removeClass('newsloading');
				}
			}).send('ajax_id='+this.rel);
			
			var snippetsrevert = $$(".news h2 a");
			snippetsrevert.each(function(snippetrevert, i) {
				snippetrevert.className='';
			});
			
			this.className='active';
	
		});
	});
};

var ajaxGallery = function(ajax_links, file){
	var div;
	var div_loading;
	var url = '/inc/'+file;
	
	var snippets = $$("ul.gallery a");
	snippets.each(function(snippet, i) {
		snippet.addEvent("click", function(e) {
			new Event(e).stop();
			
			div_loading = $(ajax_links).addClass('loading');
			div = $(ajax_links+'inner');				
			div.fade(0, 0);
			
			var myRequest = new Request({
				url: url,
				method: 'get',
				onComplete: function(response) {
					div.set({'html': response});
					div.fade(0, 1);
					div_loading.removeClass('loading');
				}
			}).send('gallery_id='+this.rel);
			
			var snippetsrevert = $$("ul.gallery a");
			snippetsrevert.each(function(snippetrevert, i) {
				snippetrevert.className='';
			});
			
			$('gallery_a_'+this.rel).className='active';
	
		});
	});
};

function website(url){
	popupWindow = window.open(url, '_blank','');
	popupWindow.focus();
}

function popup(url){
	popupWindow = window.open(url, '_blank', 'location=no,menubar=no,width=600,height=520,toolbar=no,scrollbars=yes,resizable=no');
	popupWindow.focus();
}
function popupclose(){
	window.close();
}

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();
});
