var $j = jQuery.noConflict();

// labelify
jQuery.fn.labelify=function(a){a=jQuery.extend({text:"title",labelledClass:""},a);var b={title:function(e){return $j(e).attr("title")},label:function(e){return $j("label[for="+e.id+"]").text()}};var d;var c=$j(this);return $j(this).each(function(){if(typeof a.text==="string"){d=b[a.text]}else{d=a.text}if(typeof d!=="function"){return}var f=d(this);if(!f){return}$j(this).data("label",d(this).replace(/\n/g,""));$j(this).focus(function(){if(this.value===$j(this).data("label")){this.value=this.defaultValue;$j(this).removeClass(a.labelledClass)}}).blur(function(){if(this.value===this.defaultValue){this.value=$j(this).data("label");$j(this).addClass(a.labelledClass)}});var e=function(){c.each(function(){if(this.value===$j(this).data("label")){this.value=this.defaultValue;$j(this).removeClass(a.labelledClass)}})};$j(this).parents("form").submit(e);$j(window).unload(e);if(this.value!==this.defaultValue){return}this.value=$j(this).data("label");$j(this).addClass(a.labelledClass)})};

$j(function() {
	// navi
	$j("ul.topNav li:has(ul)").hover(function() {
		$j(this).addClass('over');
	}, function() {
		$j(this).removeClass('over');
	});
	
	// fade
	$j('.fadeLink').click(function() {
		if(disableClicks) return false;
		stopFading();
		box2fade(this);
		return false;
	});
	startFading('sicherer');

	$j('input.suche').labelify({labelledClass: "fieldlabel"});
	
	// sliders
	$j('.slideButtons a.slidePrev').click(function() {gotoStep('-'); return false;});
	$j('.slideButtons a.slideNext').click(function() {gotoStep('+'); return false;});
	//$j('.slideButtons a.overview').click(function() {gotoStep(0, true); return false;});
	if(location.hash) {
		var jumpTo = location.href.split('#')[1];
		if(!isNaN(jumpTo)) gotoStep(jumpTo);
	}
	
	$j('.frag0 .themen a').click(function() {gotoStep($j(this).attr('rel'), true); return false;});
	
	$j('.reflinks a').each(function(i, el) {
		if($j(el).attr('href') != '' && location.href.indexOf($j(el).attr('href')) != -1) $j(el).addClass('active');
	/*
		if($j.inArray($j(el).attr('href').split('/solyp3-referenzen/')[1], referenzMenu) == -1) $j(el).click(function() {return false;}).addClass('disabled');
		else if($j(el).attr('href') != '' && location.href.indexOf($j(el).attr('href')) != -1) $j(el).addClass('active');
	*/
	});
});

(function($j) { // fix ClearType IE glitch
	$j.fn.customFadeIn = function(speed, callback) {
		$j(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)	$j(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)	callback();
		});
	};
	$j.fn.customFadeOut = function(speed, callback) {
		$j(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)	$j(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)	callback();
		});
	};
})(jQuery);

var item_step = 0;

var gotoStep = function(step, jump) {
	jump = false;
	if(step=='+') step = item_step+1;
	if(step=='-') step = item_step-1;
	if(step==0) {
		$j('.slideButtons a.slidePrev').customFadeOut();
		$j('.slideButtons a.overview').customFadeOut();
	}
	else {
		$j('.slideButtons a.slidePrev').customFadeIn();
		$j('.slideButtons a.overview').customFadeIn();
	}
	if(step==item_laststep) {
		$j('.slideButtons a.slideNext').customFadeOut();
		$j('a.nextArea').customFadeIn();
	}
	else {
		$j('.slideButtons a.slideNext').customFadeIn();
		$j('a.nextArea').customFadeOut();
	}
	if(jump) $j('.sliderSlides').css('marginLeft', (step*-650)+'px');
	else $j('.sliderSlides').animate({marginLeft : (step*-650)+'px'}, 750, "easeInOutExpo");
	item_step = parseInt(step);
}

var TO_chart;
var TO_text;
var TO_image;

var fadeDelay = 500;
var fadeTime = 300;
var activeFader;
var disableClicks = false;

var stopFading = function() { // stop and hide left animations
	clearTimeout(TO_chart);
	clearTimeout(TO_text);
	clearTimeout(TO_image);
	$j('.fade').stop(true, true).hide();
	$j('#boxAnim').stop(true, true);
}

var startFading = function(fadeEl) { // start fade animation
	activeFader = fadeEl;
	$j('.'+fadeEl+' .phase1').show();
	$j('.'+fadeEl).customFadeIn(fadeTime, function() { // fade in Element
	
		TO_chart = setTimeout(function() {
			$j('.'+fadeEl+' .image').customFadeIn(fadeTime); // fade in Chart
			
			TO_text = setTimeout(function() {
				$j('.'+fadeEl+' .image').customFadeOut(fadeTime, function() { // fade out Chart
					$j('.'+fadeEl+' p').customFadeIn(fadeTime); // fade in Text
				});
				
				TO_image = setTimeout(function() {
					$j('.'+fadeEl+' .phase1').customFadeOut(fadeTime, function() { // fade out Element
						$j('.'+fadeEl+' .phase2').customFadeIn(fadeTime); // fade in Image
					});
				}, fadeDelay*14);
				
			}, fadeDelay*6);
			
		}, fadeDelay);
	});
}

var box2fade = function(clicked) { // animate box and start fading
	disableClicks = true;
	var fadeEl = $j(clicked).attr('rel');
	var thisBox = $j(clicked).closest('.box');
	var thisFader = activeFader;
	var posFrom = thisBox.offset();
	var posTo = $j('.col0').offset();
	var animBox = $j('<div id="boxAnim"></div>');
	var animTime = 500;
	
	$j('body').append(animBox);
	$j('#boxAnim').css({'left': posFrom.left+'px', 'top': posFrom.top+'px'})
	thisBox.clone().appendTo(animBox);
	thisBox.css('opacity', 0);
	
	$j('#boxAnim').animate({
		left: posTo.left+30+'px',
		top: 	'300px',
		opacity: 0
	}, 
	animTime, 
	function() {
		startFading(fadeEl);
		thisBox.slideUp('fast', function() {
			thisBox.appendTo($j('.col2')).css('opacity', 1);
			if(jQuery.browser.msie)	thisBox.appendTo($j('.col2')).get(0).style.removeAttribute('filter');			
		})
		$j('.box_'+thisFader).slideDown('fast');
		animBox.remove();
		disableClicks = false;
	});
}

var glowIn = function() { $j('.glow').fadeIn(3000, glowOut); }
var glowOut = function() { $j('.glow').fadeOut(3000, glowIn); }



