/*!
 * listAttributes jQuery Plugin v1.1.0
 *
 * Copyright 2010, Michael Riddle
 * Licensed under the MIT
 * http://jquery.org/license
 *
 * Date: Sun Mar 28 05:49:39 2010 -0900
 */
if(jQuery) {
	jQuery(document).ready(function() {
		jQuery.fn.listAttributes = function(prefix) {
			var list = [];
			$(this).each(function() {
				var attributes = [];
				for(var key in this.attributes) {
					if(!isNaN(key)) {
						if(!prefix || this.attributes[key].name.substr(0,prefix.length) == prefix) {
							attributes.push(this.attributes[key].name);
						}
					}
				}
				list.push(attributes);
			});
			return list;
		}
		
		jQuery.fn.execute = function() {
			var self = this;
			var func = arguments[0];
			
			if(typeof func == 'function'){
				setTimeout(function(){ func.apply(self, arguments.slice(1) ) }, 0);
			}
			
			return this;
		}
	});
}

function getJQuery(version, alert){
	if(typeof jQuery != "function"){
		if(alert){
			alert("jQuery nicht gefunden!\n[http://www.jquery.com/]");
		}
		return undefined;
	}

	if(typeof version == "string" && version > jQuery().jquery){
		if(alert){
			alert("jQuery "+version+" benötigt!\nVersion "+jQuery().jquery+" gefunden\n[http://www.jquery.com/]");
		}
		return undefined;
	}
	
	return jQuery;
}

//function slider(selector, delay, duration, type){
//	var $J = getJQuery();
//	
//	if(!$J){
//		return;
//	}
//	
//	if(typeof delay == "string" || typeof delay == "number"){
//		delay = parseInt(delay) || 2*1000;
//	} else {
//		delay = 2*1000;
//	}
//	
//	if(typeof duration == "number" || typeof duration == "number"){
//		duration = parseInt(duration) || 500;
//	} else {
//		duration = 500;
//	}
//	
//	var slides = $J(selector);
//	var numSlides = slides.length;
//	
//	function slide(index){
//		var prev = (index-1+numSlides)%numSlides;
//		var next = (index+1)%numSlides;
//		
//		switch(type){
//			case "slideleft": // NYI
//				$J(slides[prev]).delay(delay).animate({ left: -750}, duration, "swing", function(){ $J(slides[prev]).hide(); });
//				$J(slides[index]).delay(delay).show().animate({ left: 0 }, duration, "swing", function(){ slide(next); });
//				$J(slides[next]).delay(delay).css({ left: 750 });
//				break;
//			case "slideright": // NYI
//				$J(slides[prev]).delay(delay).fadeOut(duration);
//				$J(slides[index]).delay(delay).fadeIn(duration, function(){ slide(next); });
//				break;
//			default:
//				$J(slides[prev]).delay(delay).fadeOut(duration);
//				$J(slides[index]).delay(delay).fadeIn(duration, function(){ slide(next); });
//				break;
//		}
//	}
//	
//	$J(slides[0]).show();
//	
//	if(numSlides > 1){
//		slide(1);
//	}
//}

/*
 slider(selector, delay, duration, type, callback)
 slider(options)
 options = {
	selector: string,
	delay: string or number,
	duration: string or number,
	type: string,
	callback: function,
	endSlide: string or number,
	random: boolean,
	direct: boolean
 }
 
 all types need at least 3 slides, except default which needs at least 2
*/
function slider(selector, delay, duration, type, callback, endSlide, random, direct){
	var $J = getJQuery();
	
	if(!$J){
		return;
	}
	
	var options;
	if(typeof selector == "object"){
		options = selector;
		selector = options.selector;
		delay = options.delay;
		duration = options.duration;
		type = options.type;
		callback = options.callback;
		endSlide = options.endSlide;
		random = options.random;
		direct = options.direct;
	}
	
	if(typeof delay == "string" || typeof delay == "number"){
		delay = parseInt(delay) || 2000;
	} else {
		delay = 2000;
	}
	
	if(typeof duration == "string" || typeof duration == "number"){
		duration = parseInt(duration) || 500;
	} else {
		duration = 500;
	}
	
	if(typeof callback != "function"){
		callback = function(){};
	}
	
	if(!type){
		type = "fade";
	}
	
	if(direct !== true){
		direct = false;
	}
	
	if(random && type !== "fade"){
		throw("randomized slides only available with type fade");
	}
	
	var slides = $J(selector);
	var numSlides = slides.length;
	
	if(typeof endSlide == "string" || typeof endSlide == "number"){
		switch(endSlide){
			case 'next': break;
			case 'prev': break;
			default: endSlide = parseInt(endSlide) % numSlides; break;
		}
		stopSlider(selector);
	} else {
		endSlide = null;
	}
	
	var width = $J(selector).parent().width();
	var height = $J(selector).parent().height();
	
	function slide(index, prev){
		if(endSlide != null){
			delay = 0;
		}
		
		if(typeof prev != "number"){
			prev = (index-1+numSlides)%numSlides;
		}
		
		var next = (index+1)%numSlides;
		
		if(direct && endSlide !== null){
			index = endSlide;
			next = (index+1)%numSlides;
		}
		
		if(random){
			do {
				index = Math.floor(Math.random()*numSlides);
			} while(prev === index)
		}
		
		if(prev === endSlide){
			return;
		}
		
		switch(type){
			case "slideleft":
				$J(slides[prev]).delay(delay).animate({ left: -width}, duration, "swing", function(){ $J(slides[prev]).hide(); });
				$J(slides[index]).delay(delay).show().animate({ left: 0 }, duration, "swing", function(){
					callback(selector, index);
					$J(slides[next]).css({ left: width });
					slide(next);
				});
				break;
			case "slideright":
				$J(slides[prev]).delay(delay).animate({ right: -width}, duration, "swing", function(){ $J(slides[prev]).hide(); });
				$J(slides[index]).delay(delay).show().animate({ right: 0 }, duration, "swing", function(){
					callback(selector, index);
					$J(slides[next]).css({ right: width });
					slide(next);
				});
				break;
			case "slideup":
				$J(slides[prev]).delay(delay).animate({ top: -height}, duration, "swing", function(){ $J(slides[prev]).hide(); });
				$J(slides[index]).delay(delay).show().animate({ top: 0 }, duration, "swing", function(){
					callback(selector, index);
					$J(slides[next]).css({ top: height });
					slide(next);
				});
				break;
			case "slidedown":
				$J(slides[prev]).delay(delay).animate({ bottom: -height}, duration, "swing", function(){ $J(slides[prev]).hide(); });
				$J(slides[index]).delay(delay).show().animate({ bottom: 0 }, duration, "swing", function(){
					callback(selector, index);
					$J(slides[next]).css({ bottom: height });
					slide(next);
				});
				break;
			default:
				$J(slides[prev]).delay(delay).fadeOut(duration);
				$J(slides[index]).delay(delay).fadeIn(duration, function(){
					callback(selector, index);
					slide(next, index);
				});
				break;
		}
	}
	
	var curSlide = 0;
	for(var i=numSlides-1; i>=0; i--){
		if($J(slides[i]).css("display") != "none"){
			curSlide = i;
		}
	}
	
	switch(type){
		case "slideleft":
			for(var i=1; i<numSlides; i++){
				$J(slides[i]).css({ left: width });
			}
			$J(slides[curSlide]).css({ left: 0 });
			break;
		case "slideright":
			for(var i=1; i<numSlides; i++){
				$J(slides[i]).css({ right: width });
			}
			$J(slides[curSlide]).css({ right: 0 });
			break;
		case "slideup":
			for(var i=1; i<numSlides; i++){
				$J(slides[i]).css({ top: height });
			}
			$J(slides[curSlide]).css({ top: 0 });
			break;
		case "slidedown":
			for(var i=1; i<numSlides; i++){
				$J(slides[i]).css({ bottom: height });
			}
			$J(slides[curSlide]).css({ bottom: 0 });
			break;
	}
	
	if(random && endSlide === null){
		curSlide = Math.floor(Math.random()*numSlides);
	}
	
	if(typeof endSlide === 'string'){
		switch(endSlide){
			case 'next': endSlide = (curSlide+1)%numSlides; break;
			case 'prev': endSlide = (curSlide-1+numSlides)%numSlides; break;
		}
	}
	
	$J(slides[curSlide]).show();
	
	callback(selector, curSlide);
	
	if(numSlides > 1){
		slide((curSlide+1) % numSlides, curSlide);
	}
}

function stopSlider(selector){
	var $J = getJQuery();
	
	if(!$J){
		return;
	}
	
	$J(selector).clearQueue();
}

var showSlide = slider;

/******************************************************************************/
	
var sliderOptions = {
	selector : "#content-wrap #head_all .csc-textpic-image",
	delay : 5000,
	duration : 1000,
	callback : slideCalback,
	direct : true
}

function slideCalback(sel, num){
	$('#sliderNavigation .navButton').removeClass('active').eq(num).addClass('active');
}

function initSliderNavigation(sel, parent){
	var container = $('<div id="sliderNavigation"></div>');
	var navButton = $('<span class="navButton"><span class="inner"> </span></span>');
	
	navButton.click(function(){
		var opt = $.extend({}, sliderOptions);
		opt.endSlide = $(this).prevAll().length;
		
		stopSlider(opt.selector);
		showSlide(opt);
	});
	
	for(var i=0,len=$(sel).length; i<len; ++i){
		container.append(navButton.clone(true));
	}
	
	parent.append(container);
}

getJQuery()(document).ready(function(){
	slider("#header #head_all .csc-default");
	
	initSliderNavigation(sliderOptions.selector, $('#head_all'));
	
	slider(sliderOptions);
	
	if($().marquee){
		
		$('marquee').each(function(){
			this.width = $(this).width();
			if($(document.body).hasClass('ltie9')){
				this.scrollamount = 3;
			}
		}).marquee();
	}
	
	$('.csc-frame-slider').each(function(){
		var $this = $(this);
		var $imgs = $(this).find('.csc-textpic-imagewrap img');
		var width = $imgs.width();
		var height = $imgs.height();
		var margin = '0 auto';
		
		if($this.find('.csc-textpic-right').length > 0){
			margin = '0 0 0 auto';
		}
		
		if($this.find('.csc-textpic-left').length > 0){
			margin = '0 auto 0 0';
		}
		
		$this.css({
			'position' : 'relative',
			'overflow' : 'hidden',
			'width' : width,
			'height' : height,
			'margin' : margin
		});
		
		$imgs.css({
			'position' : 'absolute',
			'top' : '0',
			'left' : '0',
			'display' : 'none'
		});
		
		slider($imgs);
	});
	
	initRegistrationForm();
	

	$('.csc-frame-summary-detail .csc-header').each(function(index, element){
		$(element).click(summaryDetailCallback);
	});
	
	//var event092011 = $("#news_right a[href*='id=1608']");
	//var event092011Parent = event092011.parent();
	//var blinkEle = $('<span></span>');
	//var event092011TopParent = event092011.parents('.news-latest-item');
	//var event092011TopParentPrev = event092011TopParent.parent().children().eq(0);
	//
	//event092011.css({ color : '#FF0033' }).detach();
	//
	//blink(blinkEle)
	//blinkEle.append(event092011);
	//
	//event092011Parent.append(blinkEle);
	//
	//event092011TopParent.detach();
	//
	//event092011TopParentPrev.after(event092011TopParent);
});

function summaryDetailCallback(){
	if($(this).parent().hasClass('open')){
		$(this).next().slideUp();
	} else {
		$(this).next().slideDown();
	}
	
	$(this).parent().toggleClass('open');
}

function blink($ele){
	blinkOff.call($ele.get(0));
}

function blinkOn(){
	$(this).delay(371).animate({ opacity: 1 }, { complete: blinkOff })
}

function blinkOff(){
	$(this).delay(739).animate({ opacity: 0 }, { complete: blinkOn })
}

function initRegistrationForm(){
	var $sets = $('.tx_powermail_pi1_fieldwrap_html_radio.style3 fieldset');
	
	$sets.addClass('clearfix').each(function(){
		var $this = $(this);
		var data = {};
		
		$this.find('[data-id]').each(function(){
			var $this = $(this);
			var id = $this.attr('data-id');
			
			if(!data[id]){
				data[id] = {};
			}
			
			var list = $this.listAttributes('data')[0];
			
			for(var i=0, len=list.length; i<len; ++i){
				data[id][list[i]] = $this.attr(list[i]);
			}
			
			if(typeof data[id].elements == 'object'){
				data[id].elements.push($this.parent().parent());
			} else {
				data[id].elements = [$this.parent().parent()];
			}
		});
		
		var $div, j;
		for(var i in data){
			$div = $('<div class="clearfix optiongroup"><p>'+
			data[i]['data-id']+'<br />'+
			data[i]['data-street']+'<br />'+
			data[i]['data-city']+'<br />'+
			'</p></div>');
			
			for(j=0, len=data[i].elements.length; j<len; ++j){
				$div.append(data[i].elements[j].detach());
			}
			
			$this.append($div);
		}
		
		
	});
}
