if(document.images){
	preloadImage = new Image();
	preloadImage.src = "/images/loading.gif";
}

var disabled=0;

var postForm = function(formid, url){
	if(!disabled){
		disabled=1;
		
		$(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);
					disabled=0;
				}
			});
	
			this.send('/inc/'+url);
		});
	}
};

var postFormChange = function(formid, url){
	disabled=1;
	
	var form = $(formid).addClass('loading');
	
	form.set('send', {
		onComplete: function(response) { 
			form.removeClass('loading');
			form.set('html', response);
			disabled=0;
		}
	});

	form.send('/inc/'+url+'?change=1');
};

var paymentType = function(active){
	optionArray= Array('optiontransfer', 'optionpaypal', 'optioncc');
	
	for(x=0; x<optionArray.length; x++){
		if(optionArray[x]==active){
			$(optionArray[x]+'div').addClass('optionactive');
			$(optionArray[x]+'div').removeClass('option');
		}else{
			$(optionArray[x]+'div').addClass('option');
			$(optionArray[x]+'div').removeClass('optionactive');
		}
	}
};

var imageSwap = function(div, id){
	var link = $(id);
	var destdiv = $(div);
			
	link.onclick = function() {
		destdiv.fade(0, 0).empty();
		var im = new Element('img', {src: link.href});
		this.inject.delay(200, im, (destdiv));
		this.fade.delay(500, destdiv, (0, 1));
		
		return false;
	};
};

var sliderControl = function(links, divs, index){
	var toggles = $$(links);
	var content = $$(divs);
	
	content.each(function(contents) {
		contents.removeAttribute('id');
	});
	
	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
	});
	
	if(index!=0){
		this.scroll = new Fx.Scroll(window);
		this.scroll.toElement($('faq'+index));
	}
};

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 photoChange = function(p, currentimg, nextimg){
	$('fade'+p+'-'+currentimg).setStyle('z-index', '10');
	$('fade'+p+'-'+currentimg).fade(1, 0);
		
	$('fade'+p+'-'+nextimg).setStyle('z-index', '20');
	$('fade'+p+'-'+nextimg).fade(0, 1);
};

var fadeImages = function(){
	var timer = 5;
	var currentimg;
	var nextimg;
	var max=new Array();
	var current=new Array();
	var paras = $$("div.images p");
	
	paras.each(function(para, p) {
		var images = para.getChildren();
		
		current[p]=0;
		
		max[p]=(images.length-1);
		
		if(max[p]>0){
			images.each(function(image, i) {
				image.set('id', 'fade'+p+'-'+i);
			});
			
			var refresh = function() {
				currentimg=current[p];
				current[p]=(current[p]==max[p] ? -1 : current[p]);
				current[p]++;
				
				nextimg=current[p];
				
				new photoChange(p, currentimg, nextimg);
			};
			
			var delayRefresh = refresh.create({ //When called, this function will attempt.
			    delay: (1000*p)
			});

		
			periodical = delayRefresh.periodical(timer * 1000, this);
		}
	});	
};

function website(url){
	popupWindow = window.open(url, '_blank','');
	popupWindow.focus();
}

function popup(url){
	popupWindow = window.open(url, '_blank','width=498,height=539,resizable=no,scrollbars=yes,toolbar=no');
	popupWindow.focus();
}

var linksScan = function(){
	var anchors = $$("a");
	anchors.each(function(anchor, i) {
		var relAttribute = anchor.get('rel');
		var classAttribute = anchor.get('class');
		
		if (classAttribute=='popup' || relAttribute=='popup'){
			anchor.addEvent('click', function() {
				popup(this);
				return false;
			});
		}else if (relAttribute=='external'){
			anchor.addEvent('click', function() {
				website(this);
				return false;
			});
		}
	});
};

window.addEvent('domready', function(){
	new linksScan();
	Shadowbox.init();
});