var _id = 0; 
$(document).ready(function() {
	$('.banner').randomize('img');
  $('.banner').cycle({
		fx: 'fade',
		timeout:  10000
	});
	$('div.beraterdetails').each(function(){
		_id++;
		$(this).attr('id','berater_'+_id);
	});
	$('div.newsletteroverlay').each(function(){
		_id++;
		$(this).attr('id','newsletter_'+_id);
	});
	$('div.beraterprofil img').each(function(){
		var details = $(this).siblings('div.beraterdetails')
		if (details.attr('id')) {
			details.find('img').attr('src',$(this).attr('src'));
			details.find('img').attr('alt',$(this).attr('alt'));
			details.find('img').attr('title',$(this).attr('title'));
			$(this).attr('rel','#'+details.attr('id'));
			$(this).siblings('p').last().children().first().attr('rel','#'+details.attr('id')).attr('href','javascript:return(false);').overlay({
				mask: {
					color: '#000',
					loadSpeed: 200,
					opacity: 0.5
				},
				top: 'center'
			});
			$(this).overlay({
				mask: {
					color: '#000',
					loadSpeed: 200,
					opacity: 0.5
				},
				top: 'center'
			});
		};
	});
	$('div.newsletterelement p a').each(function(){
		var overlay = $(this).parent().siblings('div.newsletteroverlay');
		var theframe = $('<iframe frameborder="0" scrolling="auto"></iframe>');
		if (overlay.attr('id')) {
			$(this).attr('rel','#'+overlay.attr('id'));
			$(this).overlay({
				mask: {
					color: '#000',
					loadSpeed: 200,
					opacity: 0.5
				},
				top: 'center',
				onBeforeLoad: function() {
					var wrap = this.getOverlay().find(".contentwrap");
					var link = this.getTrigger().attr("href");
					$(theframe).attr({ src: link, style: 'height:540px; width:665px; border:none;' });
					wrap.html(theframe);
				}
			});
		};
	});
	if ($('.tx-srfeuserregister-pi1-error').length>0) {
		$('.tx-srfeuserregister-pi1-error').first().next().focus();
	} else {
		if ($('#user').attr('value')) {
		  $('#tx-srfeuserregister-pi1-email').attr('value',$('#user').attr('value'));
		  $('#tx-srfeuserregister-pi1-first_name').focus();
		} else {
		  $('#user').focus();
		};
	};
	if ($('#tx-srfeuserregister-pi1-password')) {
		$('#tx-srfeuserregister-pi1-password').parent('dd').hide().prev('dt').hide();
	};
	// if ($('#tx-srfeuserregister-pi1-fe_users_form')) {
	// 	$('#tx-srfeuserregister-pi1-fe_users_form').attr('action','');
	// };
	if (!$('#user').attr('value') && $('#tx-srfeuserregister-pi1-fe_users_form').length>0 && !$('#tx-srfeuserregister-pi1-email').attr('value')) {
		$('div.tx-srfeuserregister-pi1').hide();
	};
	if ($('#tx-srfeuserregister-pi1-fe_users_form').length==0) {
		$('#login_form').hide();
	};
});
(function($) {

$.fn.randomize = function(childElem) {
  return this.each(function() {
      var $this = $(this);
      var elems = $this.children(childElem);

      elems.sort(function() { return (Math.round(Math.random())-0.5); });  

      $this.remove(childElem);  

      for(var i=0; i < elems.length; i++)
        $this.append(elems[i]);      

  });    
}
})(jQuery);
