if(document.images){
	preloadImage = new Image();
	preloadImage.src = "/images/loading.gif";
	preloadImage = new Image();
	preloadImage.src = "/images/news_loading.gif";
}

var current=0;
var periodical;
var inspector;

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 fadeImages = function(max){
	var current=1;
	
	var timer = 9;
	var periodical;
	 
	var refresh = (function() {
		$('home'+current).setStyle('z-index', '10');
		$('home'+current).fade(1, 0);		
		
		current=(current==max ? 0 : current);
		current++;
		
		$('home'+current).setStyle('z-index', '20');
		$('home'+current).fade(0, 1);
	});

	periodical = refresh.periodical(timer * 1000, this);
};

var slideShow = function(slidesinput, max){
	var slides = slidesinput;
	var pos = 0;
	var offset = 648;
	var currentslide = 1;
	inspector = $('fullimg');
	var moveleft=$('moveleft');
	var moveright=$('moveright');
	
	var imgscroll = new Fx.Scroll('wrapper', {
		offset: {'x': 0, 'y': 0},
		transition: Fx.Transitions.Cubic.easeOut
	}).toLeft();	
	
	moveleft.addEvent('click', function(event) { event = new Event(event).stop();
		if(currentslide == 1) return;
		currentslide=currentslide-1;
		pos += -(offset);
		imgscroll.start(pos);
		
		moveright.removeClass('end');
		
		if(currentslide==1){
			moveleft.addClass('end');
		}
	});
	
	moveright.addEvent('click', function(event) { event = new Event(event).stop();
		if(currentslide >= slides) return;
		currentslide=currentslide+1;
		pos += offset;
		imgscroll.start(pos);
		
		moveleft.removeClass('end');
		
		if(slidesinput==currentslide){
			moveright.addClass('end');
		}
	});
	
	var snippets = $$(".items a");
	snippets.each(function(snippet, i) {
		snippet.addEvent("click", function(e) { 
			e = new Event(e).stop();
			
			$clear(periodical);
			new changeImage(this.rel, snippet);
		});
	});
	
	timer=5;
	var refresh = (function() {
		current=(current==(max-1) ? -1 : current);
		current++;
		
		if(current>(21*currentslide)){
			currentslide=currentslide+1;
			pos += offset;
			imgscroll.start(pos);
			
			moveleft.removeClass('end');
			
			if(slidesinput==currentslide){
				moveright.addClass('end');
			}
		}
		
		if(current==0){
			currentslide=1;
			pos=0;
			imgscroll.start(pos);
			
			moveright.removeClass('end');
			moveleft.addClass('end');
		}
		
		currentobject=$('thumb'+current);
		currentrel=currentobject.rel;
			
		new changeImage(currentrel, currentobject);
	});

	periodical = refresh.periodical(timer * 1000, this);
};

var changeImage = function(rel, snippet){
	inspector.fade(0, 0);
	var myRequest = new Request({
		url: '/inc/portfolio.php',
		method: 'get',
		onComplete: function(response) {
			inspector.set({'html': response});
			inspector.fade(0, 1);
			
			external();
		}
	}).send('ids='+rel);
	
	var snippetsrevert = $$(".items a");
	snippetsrevert.each(function(snippetrevert, i) {
		snippetrevert.removeClass('active');
	});
	snippet.addClass('active');
};

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.removeClass('active');
			});
			
			snippet.addClass('active');
		});
	});
};

var ajaxServices = function(ajax_links, file){
	var div;
	var div_loading;
	var url = '/inc/'+file;
	
	var snippets = $$("#services_list a");
	snippets.each(function(snippet, i) {
		snippet.addEvent("click", function(e) {
			new Event(e).stop();
			
			div = $(ajax_links);
			
			div_loading=$('services_list').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 = $$("#services_list a");
			snippetsrevert.each(function(snippetrevert, i) {
				snippetrevert.removeClass('active');
			});
			snippet.addClass('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();
	/*document.body.addEvent('contextmenu',function(e) {
		e.stop();
	});*/
});