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 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('loading');
			
			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('loading');
				}
			}).send('ajax_id='+this.rel);
			
			var snippetsrevert = $$(".news h2 a");
			snippetsrevert.each(function(snippetrevert, i) {
				snippetrevert.className='';
			});
			
			this.className='active';
	
		});
	});
};

var dynamicItems = function(id, file){
	$$('#'+id+'list a').addEvent('click', function(event) { 
		url=this.rel;
		title=this.title;
		class=this.className;
		
		if(class!='no'){
			event = new Event(event).stop();
			
			new dynamicItem(url, id, file, title);
		
			$$('#'+id+'list a').removeClass('active');
		
			this.addClass('active');
		}
	});
};

var dynamicItem = function(url, id, file, title){
	var div=$(id);
	var fileurl = '/inc/'+file;
	var loading = $('loading');
	
	div.fade(0, 0);
	loading.addClass('loadingdynamic');
			
	var myRequest = new Request({
		url: fileurl,
		method: 'get',
		onComplete: function(response) {			
			div.set({'html': response});
			div.fade(0, 1);
			loading.removeClass('loadingdynamic');
			external();
			document.title=title+' - '+doctitle;
		}
	}).send('ajax_url='+url);
};

var portfolioSetup = function(page){
	var div;
	var url = '/inc/portfolio.php';
	
	var portitems = $$("ul.portfolio a");
	portitems.each(function(portitem, i) {
		portitem.addEvent("click", function(e) {
			new Event(e).stop();
			
			div = $('portfolio');
			
			div.fade(0, 0);
			
			var myRequest = new Request({
				url: url,
				method: 'get',
				onComplete: function(response) {
					div.set({'html': response});
									
					external();
										
					div.fade(0, 1);
				}
			}).send('id='+this.rel+'&page='+page);
			
			var portitemsrevert = $$("ul.portfolio a");
			portitemsrevert.each(function(portitemrevert, i) {
				portitemrevert.className='';
			});
			
			this.className='active';
	
		});
	});
};

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();
});