if(document.images){
	preloadImage = new Image();
	preloadImage.src = "/images/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 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);
};

/* ###### fadeImages functon ###### */

var loop=false;
var zindex=0;
var fadeImages = function(){
	var fnChain = new Chain();
	var durationval=3000;
	var delayval=0;
	var imgList = $$('.section_home img');
	var imgMax=imgList.length;
	var i=1;
	
	imgList.each(function(img){
		img.set('id', 'fade'+i);
		
		if(loop==false && i!=1){
			img.setStyle('opacity',0);

		}
		
		if(i==2){
			durationval=4000;
			delayval=1500;
		};
		
		if(i==3){
			durationval=3000;
			delayval=8000;
		};
		
		img.set('morph', {
			duration: durationval,
			onStart: function(){
				if(loop==false && img.id=='fade1'){
					img.setStyle('z-index', zindex);
				}else{
					img.setStyles({'opacity': 0, 'z-index': zindex});
				}
			},
			onComplete: (function(){
				fnChain.callChain();
				
				zindex++;
						
				if(img.id==('fade'+imgMax)){
					loop=true;
					(function(){new fadeImages();}).delay(4500);
				}
					
			}).delay(delayval)
		});
		
		fnChain.chain(
				function(){img.morph({opacity: ((img.id=='fade1' && loop==false) ? 1 : [0,1])});
			}
		);
		
		i++;
	});
	
	fnChain.callChain();
};

/* ###### fadeImages functon ###### */

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();
});