/**
 *	domready Events
 *
 */
window.addEvent('domready',function(){
	$$('.tx-marantoprojects-pi1 .images').each(function(item){ new imageSlider(item) });
	$$('address').each(function(item){new insertGoogleMaps(item)});		
	$$('p').each(function(item){ if(item.get('html') == '') item.dispose(); });		
	$$('a').setStyle('-moz-outline',0);
	
	if(navigator.platform == 'Win32' && Browser.safari != true){
		$$('body')[0].addClass('noFF');	
	}
	
	if($$('.news-amenu-container')[0]){
		var get = $get();
		var now = new Date();
		var box_value = get['tx_ttnews%5Byear%5D'] > 0 ? get['tx_ttnews%5Byear%5D'] : now.getYear()+1900;
		$$('.news-amenu-container li a')[0].set('html',box_value);
		$$('.news-amenu-container li').addEvent('click',function(e){
			this.addClass('active');
			e.stop();
		});
		$$('.news-amenu-container')[0].addEvent('mouseleave',function(e){
			$$('.news-amenu-container .active').removeClass('active');
			$$('.submenu').setStyle('display','none');
		});
		
		var submenu = new Element('div',{'class':'submenu'}).inject($$('.news-amenu-container')[0]);
		$$('.news-amenu-container a').each(function(item,index){
			if(index>0){
				item.clone().inject(submenu);	
				item.dispose();
			}
			else item.setStyle('visibility','visible');
		});
		$$('.news-amenu-container li a')[0].addEvent('click',function(){
			this.getParent('.news-amenu-container').getFirst('.submenu').setStyle('display','block');
		});
	}
});



/**
 *	imageSlider
 *	Extension-Image-Slider 
 *	[tx-marantoprojects-pi1]
 *
 */
var imageSlider = new Class({
	options:{
		duration	:	600,
		video:{
			width		:	540,
			height		:	305,
			autoplay	:	1
		},
		navigation:{
			opacity		:	0.3,
			duration	:	450
		}
	},
	initialize : function(el){
		this.el = el;
		if(el.getElements('li').length > 1){
			this.imageWidth = el.getElements('img')[0].getSize().x.toInt();
			
			this.controls = new Element('div',{
				'class'	:	'controls'
			}).inject(el);
			
			/* Item Browser */
			for(a=0; a<el.getElements('li').length; a++){
				new Element('a',{
					'html'	:	a+1
				}).inject(this.controls);
			}
			this.controls.getFirst('a').addClass('active');
			this.controls.getElements('a').addEvent('click',function(e){
				if(this.el.getParent('.tx-marantoprojects-pi1').getElements('.videoStage')[0]) this.el.getParent('.tx-marantoprojects-pi1').getElements('.videoStage')[0].dispose();
				var to = this.controls.getElements('a').indexOf(e.target);
				this.controls.getElements('a').removeClass('active').removeClass('video_active');
				e.target.addClass('active');
				this.browseItems(to);
			}.bind(this));
			
			this.el.getElements('img')
			.setStyle('cursor','pointer')
			.addEvent('click',function(e){
				var to = this.el.getElements('img').indexOf(e.target)+1;
				if(to > this.controls.getElements('a').length-1) to = 0;
				var fe = {};
				fe.target = this.controls.getElements('a')[to];
				fe.isFe = true;
				this.controls.getElements('a')[to].fireEvent('click',fe);
			}.bind(this));
			
			/* Video */
			if(this.el.getParent('.tx-marantoprojects-pi1').getElements('.video')[0]){
				new Element('a',{
					'class'	:	'video',
					'href'	:	this.el.getParent('.tx-marantoprojects-pi1').getElements('.video')[0].getAttribute('href')
				})
				.inject(this.controls)
				.addEvent('click',function(e){
					this.controls.getElements('.active').removeClass('active');
					e.target.addClass('video_active');
					var videoStage = new Element('div',{
						'class'	:	'videoStage'
					})
					.inject(this.el,'top')
					.setStyle('opacity',0)
					.set('morph',{
						duration	:	200,
						onComplete	:	function(){					
							new Swiff(this.getURL(e.target.get('href')),{
								width		:	this.options.video.width,
								height		:	this.options.video.height
							})
							.inject(videoStage);
						}.bind(this)
					}).morph({
						opacity		:	1
					});
					if(!e.isFe) e.stop();
				}.bind(this));
			}
		}
		this.setPluginNavigation();
	},
	browseItems : function(index){
		this.el.getFirst('ol').set('morph',{
			duration		:	this.options.duration
		}).morph({
			'left'	:	index * this.imageWidth * (-1)
		});
	},
	getURL	:	function(url){
		if(url.test("youtube") == true){
			var url = 'http://www.youtube.com/v/'+url.replace(/^[^v]+v.(.{11}).*/,"$1")+'&autoplay='+this.options.video.autoplay;
		}
		else if(url.test("vimeo") == true){
			vimeo_id = url.replace('http://vimeo.com/','');
			vimeo_id = vimeo_id.replace('http://www.vimeo.com/','');
			var url = 'http://www.vimeo.com/moogaloop.swf?clip_id='+vimeo_id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1&autoplay='+this.options.video.autoplay;
		}
		return url;
	},
	setPluginNavigation : function(){
		this.el.getParent('.tx-marantoprojects-pi1').getFirst('.projectNavigation').getElements('li')
		.setStyle('opacity',this.options.navigation.opacity)
		.addEvents({
			'mouseover'	:	function(e){
				if(e.target.tagName != 'LI') e.target = e.target.getParent('li');
				if(!e.target.retrieve('opacity')) e.target.store('opacity',e.target.getStyle('opacity'));
				e.target.set('morph',{
					duration	:	this.options.navigation.duration
				}).morph({
					opacity		:	1
				});
			}.bind(this),
			'mouseleave':	function(e){
				if(e.target.tagName != 'LI') e.target = e.target.getParent('li');
				e.target.set('morph',{
					duration	:	this.options.navigation.duration
				}).morph({
					opacity		:	e.target.retrieve('opacity')
				});
			}.bind(this)
		});
		this.el.getParent('.tx-marantoprojects-pi1').getFirst('.projectNavigation').getFirst('.active').setStyle('opacity',1);	
	}
});



/**
 *	insertGoogleMaps
 *	Allen address-Tags werden GoogleMaps angehängt
 *
 */
var insertGoogleMaps = new Class({
	options:{
		showRoute	:	true,
		googlemaps:{
			zoom	:	14,
			maptype	:	google.maps.MapTypeId.ROADMAP
		}
	},
	initialize : function(el){
		this.lang = $$('body')[0].getAttribute('class');
		this.mapElement = new Element('div',{ 'class' : 'map_canvas'}).inject(el,'after');
		if(el.getChildren('p')[0]) {
			var address = el.getChildren()[0];	
		}
		else var address = el;
		this.addressFormatted = el.getChildren('p')[0].clone();
		this.address = el.getChildren('p')[0].get('html').replace(/<br>/,' ').replace(/<br>/,' ').replace(/Gifted Films/,'').replace(/West/,'').replace(/GmbH/,'').replace(/<strong>/,'').replace(/<\/strong>/,'').replace(/<span>/,'').replace(/<\/span>/,'');
		console.log(this.address);
		this.geocoder = new google.maps.Geocoder();
		if(this.geocoder){
			this.geocoder.geocode({ 'address': this.address },function(results,status){
				if(status == google.maps.GeocoderStatus.OK) {
					this.createMap(results[0].geometry.location);
					if(this.options.showRoute) this.addRoute();
				}
			}.bind(this));
		}
	},
	createMap : function(location){
		this.latlng = new google.maps.LatLng(location.lat(), location.lng());
		//this.latlng = new google.maps.LatLng(50.9471532, 6.923192);

		var mapOptions = {
			zoom: this.options.googlemaps.zoom,
			center: this.latlng,
			mapTypeId: this.options.googlemaps.maptype,
			disableDefaultUI: true,
			navigationControl: true,
			scaleControl: true
		};
		this.map = new google.maps.Map(this.mapElement,mapOptions);
		this.directionsDisplay = new google.maps.DirectionsRenderer();
		this.directionsDisplay.setMap(this.map);
		
		/* Marker */
		this.marker = new google.maps.Marker({
			position: this.latlng,
			map: this.map,
			icon: '/fileadmin/templates/logo_small.png'
		});
		
		/* Infofenster */
		this.infowindow = new google.maps.InfoWindow({
			content: this.addressFormatted.get('html')
		});
		google.maps.event.addListener(this.marker, 'click', function() {
			this.infowindow.open(this.map,this.marker);
		}.bind(this));


	},
	addRoute : function(){
		var _self = this;
		this.routeDiv = new Element('div',{
			'class'	:	'route'
		}).inject(this.mapElement,'after');
		
		if(this.lang == 'lang_en'){
			var anfahrt = 'Route';	
			var ihre_adresse = 'Your address';
		}
		if(this.lang == 'lang_de'){
			var anfahrt = 'Anfahrtsbeschreibung';	
			var ihre_adresse = 'Ihre Adresse';
		}
		
		new Element('h3',{
			'html'	:	anfahrt
		}).inject(this.routeDiv);
		
		new Element('label',{
			'html'	:	ihre_adresse
		}).inject(this.routeDiv);
		
		new Element('input',{
			'class'	:	'routeAddress'
		})
		.inject(this.routeDiv)
		.addEvent('keydown',function(e){
			if(e.key == 'enter') _self.calculateRoute(this.get('value'));
		});
	},
	calculateRoute : function(start){
		if(this.geocoder){
			this.geocoder.geocode({ 'address': start },function(results,status){
				if(status == google.maps.GeocoderStatus.OK) {
					var latlng = new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng());
					this.showRoute(latlng);
				}
			}.bind(this));
		}
	},
	showRoute : function(latlng){
		this.marker.setMap(null);
		var selectedMode = 'DRIVING';
		var request = {
			origin: latlng, 
			destination: this.latlng,
			travelMode: google.maps.DirectionsTravelMode[selectedMode]
		};		
		
		this.panel = new Element('div',{
			'class'	:	'directionsPanel'
		}).inject(this.routeDiv);
		this.directionsDisplay.setPanel(this.panel);

		var directionsService = new google.maps.DirectionsService();
		directionsService.route(request,function(response,status){			
			if(status == google.maps.DirectionsStatus.OK) {
				this.directionsDisplay.setDirections(response);
			}
		}.bind(this));
	}
	
});

function $get(key,url){
	if(arguments.length < 2) url =location.href;
	if(arguments.length > 0 && key != ""){
		if(key == "#"){
			var regex = new RegExp("[#]([^$]*)");
		} else if(key == "?"){
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]"+key+"=([^&#]*)");
		}
		var results = regex.exec(url);
		return (results == null )? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
			if(url.length > 1){
				url = url[1].split("#");
				if(url.length > 1) results["hash"] = url[1];
				url[0].split("&").each(function(item,index){
					item = item.split("=");
					results[item[0]] = item[1];
				});
			}
		return results;
	}
}


/* TYPO3 AntiSpam */
function decryptString(enc,offset){var dec="";var len=enc.length;for(var i=0;i<len;i++){var n=enc.charCodeAt(i);if(n>=0x2B&&n<=0x3A){dec+=decryptCharcode(n,0x2B,0x3A,offset);}else if(n>=0x40&&n<=0x5A){dec+=decryptCharcode(n,0x40,0x5A,offset);}else if(n>=0x61&&n<=0x7A){dec+=decryptCharcode(n,0x61,0x7A,offset);}else{dec+=enc.charAt(i);}}return dec;}
function linkTo_UnCryptMailto(s){location.href=decryptString(s,-1);}
function decryptCharcode(n,start,end,offset){n=n+offset;if(offset>0&&n>end){n=start+(n-end-1);}else if(offset<0&&n<start){n=end-(start-n-1);}return String.fromCharCode(n);}
