// Cycle für Produktteaser ////////////////////////
function cycle() {
	
	this.interval = 15000;
	this.speed = 1000;
	this.elements = [];
	this.target = false;
	this.timeout = false;
	this.current = -1;
	
	var _self = this;
	
	this.start = function() {
		if(_self.target) {
			$(_self.target).children().each(function(index) {
				_self.elements.push(this);
				
				if(index == 0) {
					change();
				}
				
			});
			
			if(_self.elements.length >= 2) {			
				setTimer();			
			}			
			
		} else {
			$.error('cycle target missing');
		}
	}
	
	function change() {
	
		// Timout clearen
		clearTimeout(_self.timeout);
	
		// altes Element ausblenden
		$(_self.elements[_self.current]).fadeOut(_self.speed);
		
		// Index erhöhen bzw. zurücksetzen
		if(_self.current < _self.elements.length -1) {
			_self.current ++
		} else {
			_self.current = 0;
		}
		
		// neuses Element einblenden
		$(_self.elements[_self.current]).fadeIn(_self.speed);
		
		// Interval setzen für Wechsel
		setTimer();
	}
	
	function setTimer() {
		_self.timeout = setTimeout(function() {
			change();
		},_self.interval);
	}
}

// Spamschutz //////////////////////////////////////

function decryptMail(adress) {
	var m = adress;
	var dm = str_replace('*', '.', str_replace('/', '@', m));
	return dm;
}

function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}


// Formvalidation //////////////////////////////////

/*var form = {

	errors: false,
	fields: false,
	onerror: '',
	onsuccess: '',

	
	config: {
		requiredClass: 'required',
		failedClass: 'failed'
	},
	
	validate: function(formid) {
	
		if(this.errors.length > 0) {
			this.resetErrors();
		}
	
		this.fields = new Array();
		this.errors = new Array();
	
		this.findRequired(formid);
		
		for(i = 0; i< this.fields.length; i++) {
			this.checkValue(i);
		}
				
		this.action(formid);
		
	},
	
	findRequired: function(formid) {
		
		$('#'+formid+' .'+this.config.requiredClass).each(function() {
			var field = new Object();
				field._self = $(this);
				field.id = $(this).attr('id');
				field.name = $(this).attr('name');
				field.value = $(this).val();
				field.tag = $(this).context.tagName
				field.type = $(this).context.type
				
			form.fields.push(field);
		});
	},
	
	checkValue: function(index) {
	
		if(this.fields[index].value == '') {
			this.errors.push(this.fields[index]._self);
		}
	},
	
	markField: function(htmlObject) {
		htmlObject.addClass(this.config.failedClass);
	},
	
	removeMark: function(htmlObject) {
		htmlObject.removeClass(this.config.failedClass);
	},
	
	resetErrors: function() {
		for(i = 0; i<this.errors.length; i++) {
			this.removeMark(this.errors[i]);
		}
	},
	
	action: function(formid) {
		
		if(this.errors.length > 0) {
			for(i = 0; i<this.errors.length; i++) {
				 this.markField(this.errors[i]);
			}
			
			$('.fields.info').prepend('<div class="note">'+this.onerror+'</div>');
			
		} else {			
			//xajax_submitform(xajax.getFormValues(formid));
			//alert('alles erfolgreich');
			$('#'+formid).submit();
		}
	}
}*/

// Google Maps ///////////////////////////////////

var marker = '';
var map = '';
			
function drawGooglemap(id, address, popupstring) {
		
	var geocoder = new google.maps.Geocoder();

    var myOptions = {
      zoom: 15,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById(id), myOptions);

    
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
          
          var route = '<br /><a href="http://maps.google.com/maps?saddr=&daddr='+address.replace(' ','+')+'" target="_blank">»Routenplaner</a>';
          
          var infowindow = new google.maps.InfoWindow({
          	content: '<div class="maps_popup">'+popupstring+route+'</div>'
          });
    	  
          //infowindow.open(map, marker);
          
          google.maps.event.addListener(marker, 'click', function() {
          	//infowindow.open(map,marker);
          });
        }
      });
    }  
}
		

// DOM Ready /////////////////////////////////////

$(document).ready(function() {
	
	// Accordeon
	$('.acc_title').click(function() {
		if($(this).hasClass('open')) {
			$(this).removeClass('open');
			$(this).parent().children('.acc_content').slideUp();
		} else {
			$('.acc_title.open').parent().children('.acc_content').slideUp();
		  $('.acc_title.open').removeClass('open');
			$(this).parent().children('.acc_content').slideDown();
			$(this).addClass('open');
		}
	
	});
	
	// E-Mails entschlüsseln und Links generieren
	$('.m').each(function() {
		$(this).html(decryptMail($(this).attr('rel')));
		$(this).attr('href', 'mailto:'+decryptMail($(this).attr('rel')));
	});
	
	// Cycle initialisieren
	var	produkte = new cycle();
		produkte.target = '#page_produktteaser';
		produkte.interval = 5000;
		produkte.start();
	

	// Cycle fuer Header-Grafiken initialisieren
	if ( $('#page_header_slideimages') ) {
		var	header_images = new cycle();
			header_images.target = '#page_header_sliceimages';
			header_images.interval = 15000;
			header_images.start();
	}
});
