// JavaScript Core Document
function external(){
	var l = $$('a');
	var host = top.location.host;
	l.each(function(el){
		if ((el.getProperty('rel')=='external') || (!el.href.test(host)) || (el.href.test('.pdf'))){
			el.setProperty('target', '_blank');
		}
	});
}
function slideshows(){
	/* settings */
	var logged = $$('.editor');
	// if (logged.length > 0) return false;
	var slideshows = $$('.slideshow');
	slideshows.each(function(container){
		var images = container.getElements('img');
		if (images.length > 1){
			if (container.getProperty('rel')){
				var showDuration = container.getProperty('rel') * 1;
			}else{
				var showDuration = 4000;
			}
			var currentIndex = 0;
			var interval;
			/* opacity and fade */
			images.each(function(img,i){ 
				myFx = new Fx.Morph(img, {});
				myFx.set({'opacity': 0, 'position': 'absolute', 'top': 0, 'left': 0});
			});
			var myFx = new Fx.Morph(images[0], {'duration': 1000});
			myFx.start({'opacity': 1});
			/* worker */
			var show = function() {
				myFx1 = new Fx.Morph(images[currentIndex], {'duration': 1000});
				myFx1.start({'opacity': 0});
				myFx2 = new Fx.Morph(images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0], {'duration': 1000});
				myFx2.start({'opacity': 1});
			};
			/* start once the page is finished loading */
			interval = show.periodical(showDuration);
		}
	});
}
function flash(){
	var movies = $$('.flash');
    movies.each(function(el){
		var container = el.id;
		var id = el.id;
		var target = el.get('rel');
		var dims = el.getSize();
		var width = dims.x;
		var height = dims.y;
		var obj = new Swiff(target, {id: id, width: width, height: height, container: container});
	});
}
function _getmap(){
	var map_exists = $('gmap');
	if (map_exists){
		window.addEvent('load',function(){
			load('gmap');
		});	
		window.addEvent('unload',function(){
			GUnload();
		});	
	}
}
var datePickers = function (){
	var d = new Date();
	da = d.getDate() + 1;
	mo = d.getMonth();
	ye = d.getFullYear();
		
	fullDate = ye + '-' + (mo < 10 ? '0' + mo : mo) + '-' + (da < 10 ? '0' + da : da);
	new DatePicker('.datePicker', {inputOutputFormat: 'Y-m-d', allowEmpty: true, positionOffset: {x: 140, y: -25}, minDate: {date: fullDate, format: 'Y-m-d'}});
}
var popups = function(){
	$$('a').each(function(el){
		if (el.hasClass('popup')){
			el.addEvent('click', function(e){
				var href = el.rel;
				href += '&aj=1';
				var vars = href.split("?");
				if (vars.length){
					data = vars[1].split('&');
				}
				if (!$('eventPopup')){
					var popup = new Element('div', {'id': 'eventPopup'});
					popup.inject($('location'), 'after');
				}else{
					var popup = $('eventPopup');
				}
				var myFx = new Fx.Morph(popup, {'duration': 1000});
				myFx.set({'opacity': 0});
				var req = new Request.HTML({
					method: 'get',
					url: href,
					data: data,
					update: popup,
					onComplete: function(response) {
						myFx.start({'opacity': 1});
						slideshows();
						$('close').addEvent('click', function(e){
							if ($('eventPopup')){
								var myFx = new Fx.Morph(popup, {'duration': 500, 'onComplete': function(e){
										$('eventPopup').destroy();
									}});
								myFx.start({'opacity': 0});
							};
						});
						var myDragInstance = new Drag($('eventPopup'));
					}
				}).send();
				var e = new Event(e).stop();
			});
		}
	});
}
window.addEvent('domready', function(){
	external();
	slideshows();
	flash();
	_getmap();
	datePickers();
	popups();
});
